-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
62 lines (60 loc) · 1.35 KB
/
tools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package async
import (
"sync"
)
func catchError(p *GoPromise) {
err := recover()
if p.settled() {
err = nil
} else if err != nil {
p.err = true
p.reject(err)
}
}
func meaninglessError() {
if env == prod {
return
}
panic("this func call is meaningless")
}
func (p *GoPromise) execPromise(task promiseTask) {
defer catchError(p)
task(p.resolve, p.reject)
}
func collectTask() {
wg.Add(1)
}
func clearTask() {
wg.Done()
}
func gPromise() *GoPromise {
return &GoPromise{&lock{make(chan int), nil, Pending, false, &sync.Once{}}, nil}
}
func isPromise(v interface{}) (*GoPromise, bool) {
obj, ok := v.(*GoPromise)
return obj, ok
}
func isThenable(v interface{}) (Thenable, bool) {
obj, ok := v.(Thenable)
return obj, ok
}
func deepAwait(promise, ret, err interface{}) (interface{}, interface{}) {
newRet, newErr := ret, err
if v, ok := isPromise(promise); ok {
newRet, newErr = v.Await()
} else if thenableStruct, isThenable := isThenable(promise); isThenable {
promise := Promise(thenableStruct.Then)
newRet, newErr = promise.Await()
}
return newRet, newErr
}
func funcCallWithErrorIntercept(tasks *Tasks, funcName string) *GoPromise {
if len(*tasks) == 0 {
meaninglessError()
return nil
}
return tools[funcName](tasks)
}
func safeAct() *counter {
return &counter{0, sync.Mutex{}}
}