-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathscheduler.js
More file actions
194 lines (170 loc) · 4.87 KB
/
scheduler.js
File metadata and controls
194 lines (170 loc) · 4.87 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { warn, isArray, callWithErrorHandling, isDev, isReact } from '@mpxjs/utils'
import Mpx from '../index'
let isFlushing = false
let isFlushPending = false
const queue = []
let flushIndex = 0
const pendingPostFlushCbs = []
let activePostFlushCbs = null
let postFlushIndex = 0
const resolvedPromise = Promise.resolve()
let currentFlushPromise = null
const RECURSION_LIMIT = 100
const getId = (job) => job.id == null ? Infinity : job.id
const comparator = (a, b) => {
const diff = getId(a) - getId(b)
if (diff === 0) {
if (a.pre && !b.pre) return -1
if (b.pre && !a.pre) return 1
}
return diff
}
function findInsertionIndex (id) {
// the start index should be `flushIndex + 1`
let start = flushIndex + 1
let end = queue.length
while (start < end) {
const middle = (start + end) >>> 1
const middleJob = queue[middle]
const middleJobId = getId(middleJob)
if (middleJobId < id || (middleJobId === id && middleJob.pre)) {
start = middle + 1
} else {
end = middle
}
}
return start
}
export function nextTick (fn) {
if (isReact) {
const p = new Promise((resolve) => {
setTimeout(() => {
resolve(fn && fn.call(this))
})
})
return p
}
const p = currentFlushPromise || resolvedPromise
return fn ? p.then(this ? fn.bind(this) : fn) : p
}
export function queuePostFlushCb (cb) {
if (isArray(cb)) {
pendingPostFlushCbs.push(...cb)
} else if (
!activePostFlushCbs ||
!activePostFlushCbs.includes(cb, cb.allowRecurse ? postFlushIndex + 1 : postFlushIndex)
) {
pendingPostFlushCbs.push(cb)
}
queueFlush()
}
export function queueJob (job) {
// the dedupe search uses the startIndex argument of Array.includes()
// by default the search index includes the current job that is being run
// so it cannot recursively trigger itself again.
// if the job is a watch() callback, the search will start with a +1 index to
// allow it recursively trigger itself - it is the user's responsibility to
// ensure it doesn't end up in an infinite loop.
if (
!queue.length ||
!queue.includes(job, isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex)
) {
if (job.id == null) {
queue.push(job)
} else {
queue.splice(findInsertionIndex(job.id), 0, job)
}
queueFlush()
}
}
export function hasPendingJob (job) {
return queue.length && queue.includes(job, isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex)
}
function queueFlush () {
if (!isFlushing && !isFlushPending) {
isFlushPending = true
if (Mpx.config.forceFlushSync) {
flushJobs()
} else {
currentFlushPromise = resolvedPromise.then(flushJobs)
}
}
}
export function flushPreFlushCbs (instance, seen) {
if (isDev) seen = seen || new Map()
for (let i = isFlushing ? flushIndex + 1 : 0; i < queue.length; i++) {
const cb = queue[i]
if (cb && cb.pre) {
if (instance && cb.id !== instance.uid) continue
if (isDev && checkRecursiveUpdates(seen, cb)) continue
queue.splice(i, 1)
i--
cb()
}
}
}
export function flushPostFlushCbs (seen) {
if (pendingPostFlushCbs.length) {
const deduped = [...new Set(pendingPostFlushCbs)]
pendingPostFlushCbs.length = 0
if (activePostFlushCbs) {
activePostFlushCbs.push(...deduped)
return
}
activePostFlushCbs = deduped
if (isDev) seen = seen || new Map()
// activePostFlushCbs.sort((a, b) => getId(a) - getId(b))
for (
postFlushIndex = 0;
postFlushIndex < activePostFlushCbs.length;
postFlushIndex++
) {
if (isDev && checkRecursiveUpdates(seen, activePostFlushCbs[postFlushIndex])) continue
activePostFlushCbs[postFlushIndex]()
}
activePostFlushCbs = null
postFlushIndex = 0
}
}
function flushJobs (seen) {
isFlushPending = false
isFlushing = true
if (isDev) seen = seen || new Map()
queue.sort(comparator)
try {
for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
const job = queue[flushIndex]
if (job && job.active !== false) {
if (isDev && checkRecursiveUpdates(seen, job)) continue
callWithErrorHandling(job, null, 'scheduler')
}
}
} finally {
flushIndex = 0
queue.length = 0
flushPostFlushCbs(seen)
isFlushing = false
currentFlushPromise = null
// some postFlushCb queued jobs!
// keep flushing until it drains.
if (queue.length || pendingPostFlushCbs.length) {
flushJobs(seen)
}
}
}
function checkRecursiveUpdates (seen, fn) {
if (!seen.has(fn)) {
seen.set(fn, 1)
} else {
const count = seen.get(fn)
if (count > RECURSION_LIMIT) {
warn(
'Maximum recursive updates exceeded.\n' +
'This means you have a reactive effect that is mutating its own dependencies and thus recursively triggering itself'
)
return true
} else {
seen.set(fn, count + 1)
}
}
}