Skip to content

Commit b1c6f98

Browse files
committed
♻️ 缩减传递的上下文内容
1 parent 59f73cc commit b1c6f98

13 files changed

+105
-74
lines changed

dist/redux-miniprogram-bindings.alipay.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,18 @@ class BatchUpdates {
243243
constructor() {
244244
this.queue = [];
245245
}
246-
push(thisArg, data) {
246+
push(context, data) {
247247
const queue = this.queue;
248248
let queueItem;
249+
const contextId = context.id;
249250
for (let i = 0, len = queue.length; i < len; i++) {
250-
if (queue[i].thisArg === thisArg) {
251+
if (queue[i].context.id === contextId) {
251252
queueItem = queue[i];
252253
break;
253254
}
254255
}
255256
if (!queueItem) {
256-
queueItem = { thisArg, data: {} };
257+
queueItem = { context, data: {} };
257258
queue.push(queueItem);
258259
}
259260
Object.assign(queueItem.data, data);
@@ -267,17 +268,16 @@ class BatchUpdates {
267268
const { namespace } = getProvider();
268269
for (let i = 0, len = queue.length; i < len; i++) {
269270
const queueItem = queue[i];
270-
const diffData = diff(queueItem.data, ((namespace
271-
? queueItem.thisArg.data[namespace]
272-
: queueItem.thisArg.data)), namespace);
271+
const { data: contextData } = queueItem.context;
272+
const diffData = diff(queueItem.data, namespace ? contextData[namespace] : contextData, namespace);
273273
if (!isEmptyObject(diffData)) {
274274
queueItem.diffData = diffData;
275275
}
276276
}
277277
let queueItem;
278278
while ((queueItem = queue.shift())) {
279279
if (queueItem.diffData) {
280-
queueItem.thisArg.setData(queueItem.diffData);
280+
queueItem.context.setData(queueItem.diffData);
281281
}
282282
}
283283
}
@@ -286,7 +286,7 @@ var batchUpdates = new BatchUpdates();
286286

287287
let trackCount = 0;
288288
let triggerCount = 0;
289-
function subscription(thisArg, mapState) {
289+
function subscription(context, mapState) {
290290
trackCount += 1;
291291
const unsubscribe = useSubscribe((currState, prevState) => {
292292
let ownStateChanges;
@@ -315,7 +315,7 @@ function subscription(thisArg, mapState) {
315315
}
316316
}
317317
if (ownStateChanges) {
318-
batchUpdates.push(thisArg, ownStateChanges);
318+
batchUpdates.push(context, ownStateChanges);
319319
}
320320
triggerCount += 1;
321321
if (triggerCount === trackCount) {
@@ -339,7 +339,6 @@ function connect({ type = 'page', mapState, mapDispatch, manual, } = {}) {
339339
manual = manualDefaults;
340340
}
341341
return function processOption(options) {
342-
options.$$type = type;
343342
if (isArray(mapState) && mapState.length > 0) {
344343
const ownState = handleMapState(mapState);
345344
const [onLoadKey, onUnloadKey] = lifetimes[type];
@@ -350,19 +349,23 @@ function connect({ type = 'page', mapState, mapDispatch, manual, } = {}) {
350349
options[onLoadKey] = function (...args) {
351350
const ownState = handleMapState(mapState);
352351
if (!isEmptyObject(ownState)) {
353-
const diffData = diff(ownState, (namespace ? this.data[namespace] : this.data), namespace);
352+
const diffData = diff(ownState, namespace ? this.data[namespace] : this.data, namespace);
354353
if (!isEmptyObject(diffData)) {
355354
this.setData(diffData);
356355
}
357356
}
358-
unsubscribe = subscription(this, mapState);
357+
unsubscribe = subscription({
358+
id: Symbol('contextId'),
359+
data: this.data,
360+
setData: this.setData.bind(this),
361+
}, mapState);
359362
if (oldOnLoad) {
360363
oldOnLoad.apply(this, args);
361364
}
362365
};
363366
options[onUnloadKey] = function (...args) {
364367
if (oldOnUnload) {
365-
oldOnUnload.call(this, args);
368+
oldOnUnload.apply(this, args);
366369
}
367370
if (unsubscribe) {
368371
unsubscribe();

dist/redux-miniprogram-bindings.alipay.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.

dist/redux-miniprogram-bindings.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,18 @@ class BatchUpdates {
244244
constructor() {
245245
this.queue = [];
246246
}
247-
push(thisArg, data) {
247+
push(context, data) {
248248
const queue = this.queue;
249249
let queueItem;
250+
const contextId = context.id;
250251
for (let i = 0, len = queue.length; i < len; i++) {
251-
if (queue[i].thisArg === thisArg) {
252+
if (queue[i].context.id === contextId) {
252253
queueItem = queue[i];
253254
break;
254255
}
255256
}
256257
if (!queueItem) {
257-
queueItem = { thisArg, data: {} };
258+
queueItem = { context, data: {} };
258259
queue.push(queueItem);
259260
}
260261
Object.assign(queueItem.data, data);
@@ -268,17 +269,16 @@ class BatchUpdates {
268269
const { namespace } = getProvider();
269270
for (let i = 0, len = queue.length; i < len; i++) {
270271
const queueItem = queue[i];
271-
const diffData = diff(queueItem.data, ((namespace
272-
? queueItem.thisArg.data[namespace]
273-
: queueItem.thisArg.data)), namespace);
272+
const { data: contextData } = queueItem.context;
273+
const diffData = diff(queueItem.data, namespace ? contextData[namespace] : contextData, namespace);
274274
if (!isEmptyObject(diffData)) {
275275
queueItem.diffData = diffData;
276276
}
277277
}
278278
let queueItem;
279279
while ((queueItem = queue.shift())) {
280280
if (queueItem.diffData) {
281-
queueItem.thisArg.setData(queueItem.diffData);
281+
queueItem.context.setData(queueItem.diffData);
282282
}
283283
}
284284
}
@@ -287,7 +287,7 @@ var batchUpdates = new BatchUpdates();
287287

288288
let trackCount = 0;
289289
let triggerCount = 0;
290-
function subscription(thisArg, mapState) {
290+
function subscription(context, mapState) {
291291
trackCount += 1;
292292
const unsubscribe = useSubscribe((currState, prevState) => {
293293
let ownStateChanges;
@@ -316,7 +316,7 @@ function subscription(thisArg, mapState) {
316316
}
317317
}
318318
if (ownStateChanges) {
319-
batchUpdates.push(thisArg, ownStateChanges);
319+
batchUpdates.push(context, ownStateChanges);
320320
}
321321
triggerCount += 1;
322322
if (triggerCount === trackCount) {
@@ -340,7 +340,6 @@ function connect({ type = 'page', mapState, mapDispatch, manual, } = {}) {
340340
manual = manualDefaults;
341341
}
342342
return function processOption(options) {
343-
options.$$type = type;
344343
if (isArray(mapState) && mapState.length > 0) {
345344
const ownState = handleMapState(mapState);
346345
const [onLoadKey, onUnloadKey] = lifetimes[type];
@@ -351,19 +350,23 @@ function connect({ type = 'page', mapState, mapDispatch, manual, } = {}) {
351350
options[onLoadKey] = function (...args) {
352351
const ownState = handleMapState(mapState);
353352
if (!isEmptyObject(ownState)) {
354-
const diffData = diff(ownState, (namespace ? this.data[namespace] : this.data), namespace);
353+
const diffData = diff(ownState, namespace ? this.data[namespace] : this.data, namespace);
355354
if (!isEmptyObject(diffData)) {
356355
this.setData(diffData);
357356
}
358357
}
359-
unsubscribe = subscription(this, mapState);
358+
unsubscribe = subscription({
359+
id: Symbol('contextId'),
360+
data: this.data,
361+
setData: this.setData.bind(this),
362+
}, mapState);
360363
if (oldOnLoad) {
361364
oldOnLoad.apply(this, args);
362365
}
363366
};
364367
options[onUnloadKey] = function (...args) {
365368
if (oldOnUnload) {
366-
oldOnUnload.call(this, args);
369+
oldOnUnload.apply(this, args);
367370
}
368371
if (unsubscribe) {
369372
unsubscribe();

0 commit comments

Comments
 (0)