Skip to content

Commit 6c5c4fe

Browse files
committed
🐛 修复 BatchUpdates 更新不及时的问题
1 parent 519a3c9 commit 6c5c4fe

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

dist/redux-miniprogram-bindings.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,14 @@ class BatchUpdates {
325325
}
326326
var batchUpdates = new BatchUpdates();
327327

328+
let subscriptionCount = 0;
329+
let emitSubscriptionCount = 0;
328330
function subscription(thisArg, mapState, updateDeps) {
331+
subscriptionCount += 1;
329332
const { store } = getProvider();
330333
let prevState = store.getState();
331334
const listener = () => {
335+
emitSubscriptionCount += 1;
332336
const currState = store.getState();
333337
let ownStateChanges = null;
334338
if (isArray(mapState)) {
@@ -355,8 +359,16 @@ function subscription(thisArg, mapState, updateDeps) {
355359
batchUpdates.push(thisArg, ownStateChanges);
356360
}
357361
prevState = currState;
362+
if (emitSubscriptionCount === subscriptionCount) {
363+
emitSubscriptionCount = 0;
364+
batchUpdates.exec();
365+
}
366+
};
367+
const unsubscribe = store.subscribe(listener);
368+
return () => {
369+
subscriptionCount -= 1;
370+
unsubscribe();
358371
};
359-
return store.subscribe(listener);
360372
}
361373

362374
function connect({ type = 'page', mapState, mapDispatch, manual, } = {}) {

dist/redux-miniprogram-bindings.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/redux-miniprogram-bindings.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,14 @@ class BatchUpdates {
325325
}
326326
var batchUpdates = new BatchUpdates();
327327

328+
let subscriptionCount = 0;
329+
let emitSubscriptionCount = 0;
328330
function subscription(thisArg, mapState, updateDeps) {
331+
subscriptionCount += 1;
329332
const { store } = getProvider();
330333
let prevState = store.getState();
331334
const listener = () => {
335+
emitSubscriptionCount += 1;
332336
const currState = store.getState();
333337
let ownStateChanges = null;
334338
if (isArray(mapState)) {
@@ -355,8 +359,16 @@ function subscription(thisArg, mapState, updateDeps) {
355359
batchUpdates.push(thisArg, ownStateChanges);
356360
}
357361
prevState = currState;
362+
if (emitSubscriptionCount === subscriptionCount) {
363+
emitSubscriptionCount = 0;
364+
batchUpdates.exec();
365+
}
366+
};
367+
const unsubscribe = store.subscribe(listener);
368+
return () => {
369+
subscriptionCount -= 1;
370+
unsubscribe();
358371
};
359-
return store.subscribe(listener);
360372
}
361373

362374
function connect({ type = 'page', mapState, mapDispatch, manual, } = {}) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-miniprogram-bindings",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Redux binding utils for miniprogram",
55
"main": "./dist/redux-miniprogram-bindings.js",
66
"types": "./types/index.d.ts",

src/extend/subscription.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ import getProvider from '../provider'
33
import batchUpdates from './batchUpdates'
44
import { isArray } from '../utils'
55

6+
// 记录订阅和响应订阅的数量
7+
let subscriptionCount = 0
8+
let emitSubscriptionCount = 0
9+
610
export default function subscription(
711
thisArg: PageComponentOption,
812
mapState: MapState,
913
updateDeps: string[]
1014
) {
15+
subscriptionCount += 1
1116
const { store } = getProvider()
1217

1318
let prevState = store.getState()
1419
const listener = () => {
20+
emitSubscriptionCount += 1
1521
const currState = store.getState()
1622
let ownStateChanges: IAnyObject | null = null
1723

@@ -43,7 +49,18 @@ export default function subscription(
4349
}
4450

4551
prevState = currState
52+
53+
if (emitSubscriptionCount === subscriptionCount) {
54+
// 订阅已全部响应,此时可以执行更新
55+
emitSubscriptionCount = 0
56+
batchUpdates.exec()
57+
}
4658
}
4759

48-
return store.subscribe(listener)
60+
const unsubscribe = store.subscribe(listener)
61+
62+
return () => {
63+
subscriptionCount -= 1
64+
unsubscribe()
65+
}
4966
}

types/extend/subscription.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export default function subscription(
33
thisArg: PageComponentOption,
44
mapState: MapState,
55
updateDeps: string[]
6-
): import('redux').Unsubscribe
6+
): () => void

0 commit comments

Comments
 (0)