core.Delayed(): more generally usable version

This commit is contained in:
Helmut Merz 2023-08-11 20:57:50 +02:00
parent a67c31e0b5
commit eef9fba161
2 changed files with 9 additions and 12 deletions

View file

@ -10,6 +10,14 @@ import (
"git.sr.ht/~cco/go-scopes/logging" "git.sr.ht/~cco/go-scopes/logging"
) )
func Delayed(proc lib.Proc, delay time.Duration) lib.Proc {
return func(ctx lib.Context) {
logging.Debug(ctx).Int("delay", int(delay)).Msg("lib.Delayed")
time.Sleep(delay * time.Millisecond)
proc(ctx)
}
}
func None(_ lib.Context) {} func None(_ lib.Context) {}
func Start(ctx lib.Context) { func Start(ctx lib.Context) {
@ -17,17 +25,6 @@ func Start(ctx lib.Context) {
lib.RunCtx(ctx, Listen) lib.RunCtx(ctx, Listen)
} }
func DelayedStart(starter lib.StartProc, delay time.Duration) lib.StartProc {
if starter == nil {
starter = Start
}
return func(ctx lib.Context) {
logging.Debug(ctx).Int("delay", int(delay)).Msg("core.DelayedStart")
time.Sleep(delay * time.Millisecond)
starter(ctx)
}
}
func Listen(ctx lib.Context) { func Listen(ctx lib.Context) {
step := ctx.Config().Step() step := ctx.Config().Step()
for step(ctx) { for step(ctx) {

View file

@ -35,7 +35,7 @@ func Config() lib.Config {
server_c.AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver")) server_c.AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver"))
test_client := &client.Cfg{ test_client := &client.Cfg{
BaseCfg: b("test-client", core.DelayedStart(core.Start, 50)), BaseCfg: b("test-client", core.Delayed(core.Start, 50)),
Url: ovr("http://localhost:8123/api", SERVER_URL), Url: ovr("http://localhost:8123/api", SERVER_URL),
} }
test_client.AddAction("demo", action.Base(client.Send)) test_client.AddAction("demo", action.Base(client.Send))