service/task.go

55 lines
1.2 KiB
Go

package service
import (
"sync"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
)
var taskData = map[string]map[string]any{}
var taskDataLock = sync.RWMutex{}
func SetTaskData(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(3)
scope := args.Str(0)
key := args.Str(1)
value := args.Any(2)
taskDataLock.Lock()
defer taskDataLock.Unlock()
if taskData[scope] == nil {
taskData[scope] = map[string]any{}
}
taskData[scope][key] = value
return nil
}
func GetTaskData(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(2)
scope := args.Str(0)
key := args.Str(1)
taskDataLock.RLock()
defer taskDataLock.RUnlock()
if taskData[scope] != nil {
return vm.ToValue(taskData[scope][key])
}
return nil
}
func GetTaskDataKeys(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1)
scope := args.Str(0)
taskDataLock.RLock()
defer taskDataLock.RUnlock()
if taskData[scope] != nil {
keys := make([]string, len(taskData[scope]))
i := 0
for key := range taskData[scope] {
keys[i] = key
i++
}
return vm.ToValue(keys)
}
return nil
}