@mpxjs/core 2.9.10 → 2.9.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/core/proxy.js +1 -1
- package/src/observer/scheduler.js +37 -59
- package/src/observer/watch.js +4 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/core",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.11",
|
|
4
4
|
"description": "mpx runtime core",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"miniprogram",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"url": "https://github.com/didi/mpx/issues"
|
|
48
48
|
},
|
|
49
49
|
"sideEffects": false,
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "5077a6d6d3960fe63118d8151dcee91be797ebfe"
|
|
51
51
|
}
|
package/src/core/proxy.js
CHANGED
|
@@ -7,22 +7,26 @@ let isFlushPending = false
|
|
|
7
7
|
const queue = []
|
|
8
8
|
let flushIndex = 0
|
|
9
9
|
|
|
10
|
-
const pendingPreFlushCbs = []
|
|
11
|
-
let activePreFlushCbs = null
|
|
12
|
-
let preFlushIndex = 0
|
|
13
|
-
|
|
14
10
|
const pendingPostFlushCbs = []
|
|
15
11
|
let activePostFlushCbs = null
|
|
16
12
|
let postFlushIndex = 0
|
|
17
13
|
|
|
18
14
|
const resolvedPromise = Promise.resolve()
|
|
19
15
|
let currentFlushPromise = null
|
|
20
|
-
let currentPreFlushParentJob = null
|
|
21
16
|
|
|
22
17
|
const RECURSION_LIMIT = 100
|
|
23
18
|
|
|
24
19
|
const getId = (job) => job.id == null ? Infinity : job.id
|
|
25
20
|
|
|
21
|
+
const comparator = (a, b) => {
|
|
22
|
+
const diff = getId(a) - getId(b)
|
|
23
|
+
if (diff === 0) {
|
|
24
|
+
if (a.pre && !b.pre) return -1
|
|
25
|
+
if (b.pre && !a.pre) return 1
|
|
26
|
+
}
|
|
27
|
+
return diff
|
|
28
|
+
}
|
|
29
|
+
|
|
26
30
|
function findInsertionIndex (id) {
|
|
27
31
|
// the start index should be `flushIndex + 1`
|
|
28
32
|
let start = flushIndex + 1
|
|
@@ -30,8 +34,13 @@ function findInsertionIndex (id) {
|
|
|
30
34
|
|
|
31
35
|
while (start < end) {
|
|
32
36
|
const middle = (start + end) >>> 1
|
|
33
|
-
const
|
|
34
|
-
middleJobId
|
|
37
|
+
const middleJob = queue[middle]
|
|
38
|
+
const middleJobId = getId(middleJob)
|
|
39
|
+
if (middleJobId < id || (middleJobId === id && middleJob.pre)) {
|
|
40
|
+
start = middle + 1
|
|
41
|
+
} else {
|
|
42
|
+
end = middle
|
|
43
|
+
}
|
|
35
44
|
}
|
|
36
45
|
|
|
37
46
|
return start
|
|
@@ -42,22 +51,14 @@ export function nextTick (fn) {
|
|
|
42
51
|
return fn ? p.then(this ? fn.bind(this) : fn) : p
|
|
43
52
|
}
|
|
44
53
|
|
|
45
|
-
export function queuePreFlushCb (cb) {
|
|
46
|
-
queueCb(cb, activePreFlushCbs, pendingPreFlushCbs, preFlushIndex)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
54
|
export function queuePostFlushCb (cb) {
|
|
50
|
-
queueCb(cb, activePostFlushCbs, pendingPostFlushCbs, postFlushIndex)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function queueCb (cb, activeQueue, pendingQueue, index) {
|
|
54
55
|
if (isArray(cb)) {
|
|
55
|
-
|
|
56
|
+
pendingPostFlushCbs.push(...cb)
|
|
56
57
|
} else if (
|
|
57
|
-
!
|
|
58
|
-
!
|
|
58
|
+
!activePostFlushCbs ||
|
|
59
|
+
!activePostFlushCbs.includes(cb, cb.allowRecurse ? postFlushIndex + 1 : postFlushIndex)
|
|
59
60
|
) {
|
|
60
|
-
|
|
61
|
+
pendingPostFlushCbs.push(cb)
|
|
61
62
|
}
|
|
62
63
|
queueFlush()
|
|
63
64
|
}
|
|
@@ -69,11 +70,10 @@ export function queueJob (job) {
|
|
|
69
70
|
// if the job is a watch() callback, the search will start with a +1 index to
|
|
70
71
|
// allow it recursively trigger itself - it is the user's responsibility to
|
|
71
72
|
// ensure it doesn't end up in an infinite loop.
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
job !== currentPreFlushParentJob) {
|
|
73
|
+
if (
|
|
74
|
+
!queue.length ||
|
|
75
|
+
!queue.includes(job, isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex)
|
|
76
|
+
) {
|
|
77
77
|
if (job.id == null) {
|
|
78
78
|
queue.push(job)
|
|
79
79
|
} else {
|
|
@@ -94,31 +94,17 @@ function queueFlush () {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
activePreFlushCbs = deduped
|
|
108
|
-
if (isDev) seen = seen || new Map()
|
|
109
|
-
for (
|
|
110
|
-
preFlushIndex = 0;
|
|
111
|
-
preFlushIndex < activePreFlushCbs.length;
|
|
112
|
-
preFlushIndex++
|
|
113
|
-
) {
|
|
114
|
-
if (isDev && checkRecursiveUpdates(seen, activePreFlushCbs[preFlushIndex])) continue
|
|
115
|
-
activePreFlushCbs[preFlushIndex]()
|
|
97
|
+
export function flushPreFlushCbs (instance, seen) {
|
|
98
|
+
if (isDev) seen = seen || new Map()
|
|
99
|
+
for (let i = isFlushing ? flushIndex + 1 : 0; i < queue.length; i++) {
|
|
100
|
+
const cb = queue[i]
|
|
101
|
+
if (cb && cb.pre) {
|
|
102
|
+
if (instance && cb.id !== instance.uid) continue
|
|
103
|
+
if (isDev && checkRecursiveUpdates(seen, cb)) continue
|
|
104
|
+
queue.splice(i, 1)
|
|
105
|
+
i--
|
|
106
|
+
cb()
|
|
116
107
|
}
|
|
117
|
-
activePreFlushCbs = null
|
|
118
|
-
preFlushIndex = 0
|
|
119
|
-
currentPreFlushParentJob = null
|
|
120
|
-
// recursively flush until it drains
|
|
121
|
-
flushPreFlushCbs(seen, parentJob)
|
|
122
108
|
}
|
|
123
109
|
}
|
|
124
110
|
|
|
@@ -143,8 +129,6 @@ export function flushPostFlushCbs (seen) {
|
|
|
143
129
|
}
|
|
144
130
|
activePostFlushCbs = null
|
|
145
131
|
postFlushIndex = 0
|
|
146
|
-
// recursively flush until it drains
|
|
147
|
-
flushPostFlushCbs(seen)
|
|
148
132
|
}
|
|
149
133
|
}
|
|
150
134
|
|
|
@@ -154,16 +138,14 @@ function flushJobs (seen) {
|
|
|
154
138
|
|
|
155
139
|
if (isDev) seen = seen || new Map()
|
|
156
140
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
queue.sort((a, b) => getId(a) - getId(b))
|
|
141
|
+
queue.sort(comparator)
|
|
160
142
|
|
|
161
143
|
try {
|
|
162
144
|
for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
|
|
163
145
|
const job = queue[flushIndex]
|
|
164
146
|
if (job && job.active !== false) {
|
|
165
147
|
if (isDev && checkRecursiveUpdates(seen, job)) continue
|
|
166
|
-
callWithErrorHandling(job, null, '
|
|
148
|
+
callWithErrorHandling(job, null, 'scheduler')
|
|
167
149
|
}
|
|
168
150
|
}
|
|
169
151
|
} finally {
|
|
@@ -176,11 +158,7 @@ function flushJobs (seen) {
|
|
|
176
158
|
currentFlushPromise = null
|
|
177
159
|
// some postFlushCb queued jobs!
|
|
178
160
|
// keep flushing until it drains.
|
|
179
|
-
if (
|
|
180
|
-
queue.length ||
|
|
181
|
-
pendingPreFlushCbs.length ||
|
|
182
|
-
pendingPostFlushCbs.length
|
|
183
|
-
) {
|
|
161
|
+
if (queue.length || pendingPostFlushCbs.length) {
|
|
184
162
|
flushJobs(seen)
|
|
185
163
|
}
|
|
186
164
|
}
|
package/src/observer/watch.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ReactiveEffect } from './effect'
|
|
2
2
|
import { isRef } from './ref'
|
|
3
3
|
import { isReactive } from './reactive'
|
|
4
|
-
import {
|
|
4
|
+
import { queueJob, queuePostFlushCb } from './scheduler'
|
|
5
5
|
import { currentInstance } from '../core/proxy'
|
|
6
6
|
import {
|
|
7
7
|
noop,
|
|
@@ -129,7 +129,9 @@ export function watch (source, cb, options = {}) {
|
|
|
129
129
|
scheduler = () => queuePostFlushCb(job)
|
|
130
130
|
} else {
|
|
131
131
|
// default: 'pre'
|
|
132
|
-
|
|
132
|
+
job.pre = true
|
|
133
|
+
if (instance) job.id = instance.uid
|
|
134
|
+
scheduler = () => queueJob(job)
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
job.allowRecurse = !!cb
|