https://github.com/s3rj1k/go-ontick
Golang module that provides the ability to execute function(s) on timer tick.
https://github.com/s3rj1k/go-ontick
ticker timer
Last synced: 6 months ago
JSON representation
Golang module that provides the ability to execute function(s) on timer tick.
- Host: GitHub
- URL: https://github.com/s3rj1k/go-ontick
- Owner: s3rj1k
- License: mit
- Created: 2024-03-14T15:43:07.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-15T14:39:47.000Z (over 1 year ago)
- Last Synced: 2025-02-12T06:57:02.885Z (8 months ago)
- Topics: ticker, timer
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OnTick
`OnTick` is a Go module that provides the ability to execute function(s) on timer tick.
## Getting Started
### Prerequisites
- Go 1.22.1 or later.
### Installation
To install `OnTick`, use the `go get` command:
```bash
go get github.com/s3rj1k/go-ontick
```### Example
Using `ontick.New`:
```
package mainimport (
"context"
"fmt"
"sync/atomic"
"time""github.com/s3rj1k/go-ontick"
)func main() {
var (
c1 atomic.Int64
c2 atomic.Int64
)ticker := ontick.New(context.TODO(), 10*time.Millisecond, 2, "key42")
go func() {
<-time.After(42 * 10 * time.Millisecond)
ticker.Stop()
}()ticker.Do(
func(ctx context.Context) {
c1.Add(1)
},
func(ctx context.Context) {
c2.Add(1)
},
)ticker.Do(func(ctx context.Context) {
panic("This will not be executed.")
})ticker.Wait()
fmt.Printf("%d =? %d\n", c1.Load(), c2.Load())
}
```or using `ontick.DoFunc`:
```
package mainimport (
"context"
"fmt"
"sync"
"time""github.com/s3rj1k/go-ontick"
)func main() {
var wg sync.WaitGroupctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()key := "It's_Ticking_Time"
ontick.DoFunc(ctx, &wg, 2*time.Second, key, func(ctx context.Context) {
if tt, ok := ctx.Value(key).(time.Time); ok {
fmt.Println("Tick at:", tt)
}
})wg.Wait()
}```
## Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss potential improvements or features.
## License
`OnTick` is available under the MIT license. See the LICENSE file for more info.