Skip to content

Commit 1191f77

Browse files
committed
psuedo onFinishRendering (#15)
1 parent 5fde761 commit 1191f77

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

dist/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,33 @@ var patchReduxStore = function patchReduxStore(origStore) {
5353
listeners.push(listener);
5454

5555
if (listeners.length === 1) {
56-
unsubscribe = origStore.subscribe(function () {
56+
var pending = false;
57+
58+
var runBatchedUpdates = function runBatchedUpdates() {
59+
if (stateForUpdates) {
60+
pending = true;
61+
return;
62+
}
63+
5764
stateForUpdates = origStore.getState();
65+
66+
var onFinishRendering = function onFinishRendering() {
67+
stateForUpdates = null;
68+
69+
if (pending) {
70+
pending = false;
71+
runBatchedUpdates();
72+
}
73+
};
74+
5875
(0, _batchedUpdates.batchedUpdates)(function () {
5976
listeners.forEach(function (l) {
6077
return l();
6178
});
62-
});
63-
stateForUpdates = null;
64-
});
79+
}, onFinishRendering);
80+
};
81+
82+
unsubscribe = origStore.subscribe(runBatchedUpdates);
6583
}
6684

6785
return function () {

src/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,25 @@ const patchReduxStore = (origStore) => {
4545
const subscribe = (listener) => {
4646
listeners.push(listener);
4747
if (listeners.length === 1) {
48-
unsubscribe = origStore.subscribe(() => {
48+
let pending = false;
49+
const runBatchedUpdates = () => {
50+
if (stateForUpdates) {
51+
pending = true;
52+
return;
53+
}
4954
stateForUpdates = origStore.getState();
55+
const onFinishRendering = () => {
56+
stateForUpdates = null;
57+
if (pending) {
58+
pending = false;
59+
runBatchedUpdates();
60+
}
61+
};
5062
batchedUpdates(() => {
5163
listeners.forEach(l => l());
52-
});
53-
stateForUpdates = null;
54-
});
64+
}, onFinishRendering);
65+
};
66+
unsubscribe = origStore.subscribe(runBatchedUpdates);
5567
}
5668
return () => {
5769
const index = listeners.indexOf(listener);

0 commit comments

Comments
 (0)