|
|
@@ -9,6 +9,12 @@ import (
|
|
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/syncext"
|
|
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/syncext"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DelayedCombiningInvoker is a utility to combine multiple consecutive requests into a single execution
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Requests are made with Request(), and consecutive requests are combined during the `delay` period.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Can be used, e.g., for search-controls, where we want to init the search when teh user stops typing
|
|
|
|
|
|
|
|
// Or generally to queue an execution once a burst of requests is over.
|
|
|
|
type DelayedCombiningInvoker struct {
|
|
|
|
type DelayedCombiningInvoker struct {
|
|
|
|
syncLock sync.Mutex
|
|
|
|
syncLock sync.Mutex
|
|
|
|
triggerChan chan bool
|
|
|
|
triggerChan chan bool
|
|
|
@@ -79,10 +85,14 @@ func (d *DelayedCombiningInvoker) Request() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *DelayedCombiningInvoker) run() {
|
|
|
|
func (d *DelayedCombiningInvoker) run() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
needsExecutorRunningCleanup := true
|
|
|
|
defer func() {
|
|
|
|
defer func() {
|
|
|
|
d.syncLock.Lock()
|
|
|
|
if needsExecutorRunningCleanup {
|
|
|
|
d.executorRunning.Set(false)
|
|
|
|
d.syncLock.Lock()
|
|
|
|
d.syncLock.Unlock()
|
|
|
|
d.executorRunning.Set(false)
|
|
|
|
|
|
|
|
d.syncLock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
@@ -129,6 +139,9 @@ func (d *DelayedCombiningInvoker) run() {
|
|
|
|
_ = langext.RunPanicSafe(d.action)
|
|
|
|
_ = langext.RunPanicSafe(d.action)
|
|
|
|
// =================================================
|
|
|
|
// =================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
d.executorRunning.Set(false) // ensure HasPendingRequests returns fals ein onExecutionDone listener
|
|
|
|
|
|
|
|
needsExecutorRunningCleanup = false
|
|
|
|
|
|
|
|
|
|
|
|
for _, fn := range d.onExecutionDone {
|
|
|
|
for _, fn := range d.onExecutionDone {
|
|
|
|
_ = langext.RunPanicSafe(fn)
|
|
|
|
_ = langext.RunPanicSafe(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|