2022-09-05 23:49:23 +02:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/scripthaus-dev/mshell/pkg/base"
|
|
|
|
)
|
|
|
|
|
2022-10-28 02:10:36 +02:00
|
|
|
func startCmdWait(ck base.CommandKey) {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
GlobalStore.CmdWaitMap[ck] = nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
func pushCmdWaitIfRequired(ck base.CommandKey, fn func()) bool {
|
2022-09-05 23:49:23 +02:00
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
2022-09-06 01:31:22 +02:00
|
|
|
fns, ok := GlobalStore.CmdWaitMap[ck]
|
2022-09-05 23:49:23 +02:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
fns = append(fns, fn)
|
|
|
|
GlobalStore.CmdWaitMap[ck] = fns
|
2022-09-05 23:49:23 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
func runCmdUpdateFn(ck base.CommandKey, fn func()) {
|
|
|
|
pushed := pushCmdWaitIfRequired(ck, fn)
|
2022-09-05 23:49:23 +02:00
|
|
|
if pushed {
|
|
|
|
return
|
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
fn()
|
2022-09-05 23:49:23 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
func runCmdWaitFns(ck base.CommandKey) {
|
2022-09-05 23:49:23 +02:00
|
|
|
for {
|
2022-09-06 01:31:22 +02:00
|
|
|
fn := removeFirstCmdWaitFn(ck)
|
|
|
|
if fn == nil {
|
2022-09-05 23:49:23 +02:00
|
|
|
break
|
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
fn()
|
2022-09-05 23:49:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
func removeFirstCmdWaitFn(ck base.CommandKey) func() {
|
2022-09-05 23:49:23 +02:00
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
fns := GlobalStore.CmdWaitMap[ck]
|
|
|
|
if len(fns) == 0 {
|
2022-09-05 23:49:23 +02:00
|
|
|
delete(GlobalStore.CmdWaitMap, ck)
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
fn := fns[0]
|
|
|
|
GlobalStore.CmdWaitMap[ck] = fns[1:]
|
|
|
|
return fn
|
2022-09-05 23:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeCmdWait(ck base.CommandKey) {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
fns := GlobalStore.CmdWaitMap[ck]
|
|
|
|
if len(fns) == 0 {
|
2022-09-05 23:49:23 +02:00
|
|
|
delete(GlobalStore.CmdWaitMap, ck)
|
|
|
|
return
|
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
go runCmdWaitFns(ck)
|
2022-09-05 23:49:23 +02:00
|
|
|
}
|