2023-02-09 12:16:36 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/dop251/goja"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PromiseProxy struct {
|
|
|
|
*goja.Promise
|
|
|
|
wg sync.WaitGroup
|
|
|
|
resolve func(result interface{})
|
|
|
|
reject func(reason interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PromiseProxy) Resolve(result interface{}) {
|
|
|
|
defer p.wg.Done()
|
|
|
|
p.resolve(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PromiseProxy) Reject(reason interface{}) {
|
|
|
|
defer p.wg.Done()
|
|
|
|
p.resolve(reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PromiseProxy) Wait() {
|
|
|
|
p.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPromiseProxy(promise *goja.Promise, resolve func(result interface{}), reject func(reason interface{})) *PromiseProxy {
|
|
|
|
proxy := &PromiseProxy{
|
|
|
|
Promise: promise,
|
|
|
|
wg: sync.WaitGroup{},
|
|
|
|
resolve: resolve,
|
|
|
|
reject: reject,
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy.wg.Add(1)
|
|
|
|
|
|
|
|
return proxy
|
|
|
|
}
|
2023-04-24 12:16:30 +02:00
|
|
|
|
|
|
|
func NewPromiseProxyFrom(rt *goja.Runtime) *PromiseProxy {
|
|
|
|
promise, resolve, reject := rt.NewPromise()
|
|
|
|
|
|
|
|
return NewPromiseProxy(promise, resolve, reject)
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:27:43 +01:00
|
|
|
func isPromise(v any) (*goja.Promise, bool) {
|
2023-11-28 16:35:49 +01:00
|
|
|
if v == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:27:43 +01:00
|
|
|
promise, ok := v.(*goja.Promise)
|
2023-04-24 12:16:30 +02:00
|
|
|
return promise, ok
|
|
|
|
}
|