Skip to content

Commit

Permalink
perf(core): 去掉不需要的初始 setData 和数据字段 (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
allen-zh committed Jul 4, 2019
1 parent 54c2bed commit 8a19371
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
8 changes: 7 additions & 1 deletion packages/mars-core/__test__/unit/swan/swan.test.js
Expand Up @@ -157,7 +157,13 @@ describe('[swan]createComponent', () => {
// mock call methods
component.lifetimes.created.call(component);
expect(component.$vue).not.toBe(undefined);
expect(setData).toHaveBeenCalledTimes(2);
// component intial setData omited
expect(setData).toHaveBeenCalledTimes(1);
component.$vue.a = 2;
component.$vue.$nextTick(() => {
expect(setData).toHaveBeenCalledTimes(2);
});

component.lifetimes.attached.call(component);
component.lifetimes.ready.call(component);
});
Expand Down
47 changes: 42 additions & 5 deletions packages/mars-core/src/base/data.js
Expand Up @@ -37,14 +37,15 @@ export function setData(vm, $mp, isRoot = false) {

if (!vm.__swan_inited__) {
vm.__swan_inited__ = true;
data.__inited__ = true;
isRoot && (data.rootUID = $mp.__uid__);
data = getData(vm, data);
// compare initial data with $mp data
// 从 swan properties 和 data 取到的初始数据都是 plain Object 不是 Vue 数据的引用
data = compareInitialData($mp, data);

if (isRoot) {
const rootComputed = getAllComputed(vm);
data = Object.assign(data, {
rootComputed
});
data.rootComputed = rootComputed;
data.rootUID = $mp.__uid__;
}
}
else {
Expand All @@ -67,6 +68,17 @@ export function setData(vm, $mp, isRoot = false) {
}
}

// if vm has _computedWatchers and has new data
// set __inited__ true to use VM _computedWatchers data
if (
vm._computedWatchers
&& !$mp.data.__inited__
&& Object.keys(vm._computedWatchers).length > 0
&& Object.keys(data).length > 0
) {
data.__inited__ = true;
}

if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
const prefix = `${vm.$root.$mp.scope.__uid__}$${vm._name}`;
const perfTagStart = `${prefix}-data-start`;
Expand Down Expand Up @@ -100,13 +112,38 @@ export function setData(vm, $mp, isRoot = false) {
}
else {
const flushMpUpdatedCallbacks = getMpUpdatedCallbacks(vm);
// console.log('[perf setData]:', data);
$mp.setData(data, () => {
flushMpUpdatedCallbacks();
});
}
}
}

/**
* compare Initial data with $mp.data and omit the same data
*
* @param {Object} $mp swan instance
* @param {Object} data collected data from vm
* @return {Object} data omited
*/
function compareInitialData($mp, data) {
const mpData = $mp.data;
const {rootComputed, compId} = mpData;
let computedProps = {};
if (rootComputed && compId) {
computedProps = rootComputed[compId] || {};
}
Object.keys(data).forEach(key => {
let mpVal = mpData[key] !== undefined ? mpData[key] : computedProps[key];
if (mpVal !== undefined && deepEqual(data[key], mpVal)) {
delete data[key];
}
});

return data;
}

function compareAndSetData(k, val, old, key, data) {
if (!deepEqual(val, old)) {
data[`_f_.${k}.` + key] = val;
Expand Down

0 comments on commit 8a19371

Please sign in to comment.