-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
166 lines (150 loc) · 3.96 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import omit from "nanoutils/lib/omit"
import Eventbus from "delightful-bus"
import { apicase, mergeOptions } from "@apicase/core"
const stripAdapter = omit(["adapter"])
const modes = {
debounce({ state, options, reqOptions, placeholder, createRequest }) {
const last = state.queue.slice(-1)[0]
if (last) last.cancel()
clearTimeout(state.blockerTicker)
state.isBlocked = true
state.blockerTicker = setTimeout(() => {
state.isBlocked = false
createRequest()
}, options.delay)
state.queue.push(placeholder)
},
throttle({ state, options, placeholder, createRequest }) {
if (state.isBlocked) return
state.isBlocked = true
state.blockerTicker = setTimeout(() => {
state.isBlocked = false
createRequest()
}, options.delay)
state.queue.push(placeholder)
},
queue({ state, options, placeholder, createRequest }) {
const last = state.queue.slice(-1)[0]
if (last) {
last.once(
"finish",
options.delay
? createRequest
: () => {
setTimeout(createRequest, options.delay)
}
)
} else {
createRequest()
}
state.queue.push(placeholder)
},
interval({
state,
spawner,
options,
reqOptions,
placeholder,
createRequest
}) {
const interval = setInterval(() => {
createRequest()
}, options.delay)
placeholder.once("cancel", () => {
clearInterval(interval)
})
state.queue.push(placeholder)
},
delay({ state, options, reqOptions, placeholder, createRequest }) {
const timer = setTimeout(createRequest, options.delay)
placeholder.once("cancel", () => {
clearTimeout(timer)
})
state.queue.push(placeholder)
},
default({ state, placeholder, createRequest }) {
createRequest()
state.queue.push(placeholder)
}
}
export function IncomingRequest() {
const bus = new Eventbus()
bus.injectObserverTo(this)
let request
this.promise = new Promise(resolve => {
bus.once("receive", resolve)
bus.once("cancel", () => {
resolve(null)
})
})
this.setRequest = req => {
bus.emit("receive", req)
request = bus.sendTo(req)
this.on = req.on
this.off = req.off
this.once = req.once
this.then = req.then
this.catch = req.catch
}
this.cancel = () => {
return Promise.resolve(request ? request.cancel() : null).then(() => {
bus.emit("cancel")
})
}
this.then = cb => this.promise.then(cb)
this.catch = cb => this.promise.catch(cb)
}
export function ApiSpawner(options) {
const spawnerOptions = {
base: options.base || {},
delay: options.time || 0,
mode: options.mode || "default"
}
const bus = new Eventbus()
bus.injectObserverTo(this)
const spawnerState = {
isCalled: false,
isBlocked: false,
blockerTicker: null,
queue: []
}
const createReq = reqOptions => {
if (spawnerOptions.base.constructor.name === "ApiService") {
return spawnerOptions.base.doRequest(reqOptions || {})
} else {
const mergedOptions = mergeOptions([
spawnerOptions.base,
reqOptions || {}
])
return bus.sendTo(
apicase(mergedOptions.adapter)(stripAdapter(mergedOptions))
)
}
}
this.spawn = reqOptions => {
const placeholder = new IncomingRequest()
const remove = () => {
const idx = spawnerState.queue.indexOf(placeholder)
spawnerState.queue.splice(idx, 1)
}
placeholder.once("finish", remove).once("cancel", remove)
const callback =
typeof spawnerOptions.mode === "function"
? spawnerOptions.mode
: modes[spawnerOptions.mode]
callback({
state: spawnerState,
options: spawnerOptions,
spawner: this,
reqOptions: reqOptions,
placeholder: placeholder,
createRequest: () => placeholder.setRequest(createReq(reqOptions))
})
return placeholder
}
this.stop = () => {
spawnerState.queue.forEach(req => {
req.cancel()
})
}
}