This commit is contained in:
cdle
2023-06-02 16:52:49 +08:00
parent 985e012f01
commit 8eb0db0dc1
4 changed files with 109 additions and 37 deletions
+40 -2
View File
@@ -23,15 +23,42 @@ type HttpListen struct {
}
var httpListens sync.Map
var httpListensAny sync.Map
var listenCounter2 int64
func AddHttpListen(api, method string, vm *goja.Runtime, uuid string, resolve func(result interface{}), reject func(reason interface{})) {
key := atomic.AddInt64(&listenCounter2, 1)
if method == "ANY" {
var hl *HttpListen
v, ok := httpListensAny.Load(uuid)
if ok {
// logs.Debug("load", uuid)
hl = v.(*HttpListen)
} else {
hl = &HttpListen{
Path: api,
Method: method,
Chan: make(chan *RR, 1000),
}
// logs.Debug("crate", uuid)
httpListensAny.Store(uuid, hl)
}
f, ok := <-hl.Chan
if ok {
// logs.Debug("http resolved")
resolve(f)
} else {
// logs.Debug("script is stopped")
reject(Error(vm, "script is stopped"))
}
return
}
hl := &HttpListen{
Path: api,
Method: method,
Chan: make(chan *RR, 1),
UUID: uuid,
}
key := atomic.AddInt64(&listenCounter2, 1)
hl.Chan = make(chan *RR, 1)
httpListens.Store(key, hl)
f, ok := <-hl.Chan
if ok {
@@ -53,4 +80,15 @@ func CancelHttpListen(uuid string) {
}
return true
})
httpListensAny.Range(func(key, value any) bool {
hl := value.(*HttpListen)
if hl.UUID == uuid {
httpListens.Delete(key)
if !hl.Closed {
hl.Closed = true
close(hl.Chan)
}
}
return true
})
}