163 lines
3.6 KiB
Go
163 lines
3.6 KiB
Go
package service
|
|
|
|
import (
|
|
"container/list"
|
|
"sync"
|
|
|
|
"apigo.cc/gojs"
|
|
"apigo.cc/gojs/goja"
|
|
)
|
|
|
|
var taskData = map[string]map[string]any{}
|
|
var taskDataLock = sync.RWMutex{}
|
|
|
|
var taskList = map[string]*list.List{}
|
|
var taskListLock = sync.RWMutex{}
|
|
|
|
func DataSet(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 DataGet(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 DataKeys(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
|
|
}
|
|
|
|
func DataCount(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 {
|
|
return vm.ToValue(len(taskData[scope]))
|
|
}
|
|
return vm.ToValue(0)
|
|
}
|
|
|
|
func DataFetch(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 {
|
|
all := make(map[string]any)
|
|
for k, v := range taskData[scope] {
|
|
all[k] = v
|
|
}
|
|
return vm.ToValue(all)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DataRemove(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
scope := args.Str(0)
|
|
key := args.Str(1)
|
|
taskDataLock.Lock()
|
|
defer taskDataLock.Unlock()
|
|
if taskData[scope] != nil {
|
|
if key != "" {
|
|
delete(taskData[scope], key)
|
|
} else {
|
|
delete(taskData, scope)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ListPush(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(2)
|
|
scope := args.Str(0)
|
|
value := args.Any(1)
|
|
fromHead := args.Bool(2)
|
|
taskListLock.Lock()
|
|
defer taskListLock.Unlock()
|
|
list1 := taskList[scope]
|
|
if list1 == nil {
|
|
list1 = list.New()
|
|
taskList[scope] = list1
|
|
}
|
|
if fromHead {
|
|
list1.PushFront(value)
|
|
} else {
|
|
list1.PushBack(value)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ListPop(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
scope := args.Str(0)
|
|
fromEnd := args.Bool(1)
|
|
taskListLock.Lock()
|
|
var item *list.Element
|
|
defer taskListLock.Unlock()
|
|
list1 := taskList[scope]
|
|
if list1 != nil {
|
|
if fromEnd {
|
|
item = list1.Front()
|
|
} else {
|
|
item = list1.Back()
|
|
}
|
|
if item != nil {
|
|
list1.Remove(item)
|
|
return vm.ToValue(item.Value)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ListCount(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
scope := args.Str(0)
|
|
taskListLock.RLock()
|
|
defer taskListLock.RUnlock()
|
|
if taskList[scope] != nil {
|
|
return vm.ToValue(taskList[scope].Len())
|
|
}
|
|
return vm.ToValue(0)
|
|
}
|
|
|
|
func ListRemove(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := gojs.MakeArgs(&argsIn, vm).Check(1)
|
|
scope := args.Str(0)
|
|
taskListLock.Lock()
|
|
defer taskListLock.Unlock()
|
|
delete(taskList, scope)
|
|
return nil
|
|
}
|