Skip to content

Commit

Permalink
wip(okam-core): optimize observable method naming for private method …
Browse files Browse the repository at this point in the history
…using double underline and up $nextTick init
  • Loading branch information
wuhy committed Jan 4, 2019
1 parent 4e17767 commit dfecb09
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default class ComputedObserver {
result[k] = this.initDeps(k);
});

this.ctx.$setData(result);
this.ctx.__setViewData(result);
}

/**
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class ComputedObserver {
: (old !== value || (typeof old === 'object'));
if (neeUpdate) {
ctx.data[p] = value;
ctx.$setData({[p]: value});
ctx.__setViewData({[p]: value});
this.notifyWatcher(value, old, [p]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/okam-core/src/extend/data/observable/Observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export default class Observer {
this.setContextData(this.rawData);
}

this.ctx.$setData({[selector]: val});
this.ctx.__setViewData({[selector]: val});
this.notifyWatcher(val, oldVal, paths);
}

Expand Down
71 changes: 43 additions & 28 deletions packages/okam-core/src/extend/data/observable/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ export default {
* @private
*/
created() {
this.$waitingDataUpQueues = [];
this.__waitingSetDataQueue = [];
this.__dataUpTaskNum = 0;

// init nextTick callback
this.__nextTickCallback = this.$notifySetDataDone.bind(this);
this.__executeDataUpdate = this.$executeDataUpdate.bind(this);
this.__nextTickCallback = this.__notifySetDataDone.bind(this);
this.__executeDataUpdate = this.__doDataUpdate.bind(this);

this.$dataListener = new EventListener();
this.__propsObserver = makePropsObservable(this);
Expand All @@ -182,8 +182,8 @@ export default {
* @private
*/
detached() {
this.$upQueues = null;
this.$waitingDataUpQueues = null;
this.__setDataQueue = null;
this.__upDoneCallbackQueue = null;

this.$dataListener.dispose();
this.$dataListener = null;
Expand All @@ -201,18 +201,20 @@ export default {
* @param {Function} callback the callback to execute
*/
$nextTick(callback) {
let queues = this.$waitingDataUpQueues;
if (queues) {
queues.push(callback);
let queues = this.__upDoneCallbackQueue;
if (!queues) {
queues = this.__upDoneCallbackQueue = [];
}

queues.push(callback);
},

/**
* Notify setData done
*
* @private
*/
$notifySetDataDone() {
__notifySetDataDone() {
if (this.$isDestroyed || this.__dataUpTaskNum === 0) {
return;
}
Expand All @@ -223,7 +225,7 @@ export default {
}

this.__dataUpTaskNum = 0;
let queues = this.$waitingDataUpQueues;
let queues = this.__upDoneCallbackQueue;
/* istanbul ignore next */
if (queues) {
let num = queues.length;
Expand All @@ -243,44 +245,57 @@ export default {
*
* @private
*/
$executeDataUpdate() {
__doDataUpdate() {
if (this.$isDestroyed) {
return;
}

let queues = this.$upQueues;
/* istanbul ignore next */
if (queues) {
// TODO optimize value update: merge operations
// call lifecycle beforeUpdate hook
this.beforeUpdate && this.beforeUpdate();
this.setData(getSetDataPaths(queues), this.__nextTickCallback);
this.$upQueues = null;
let queues = this.__setDataQueue;
this.__setDataQueue = null;
if (!queues || !queues.length) {
return;
}

// call lifecycle beforeUpdate hook
this.beforeUpdate && this.beforeUpdate();
this.setData(getSetDataPaths(queues), this.__nextTickCallback);
},

/**
* Set the view data. It'll not update the view immediately, it's deferred
* to execute when enter the next event loop.
*
* @private
* @param {string|Object} obj the data to set or the path to set
* @param {*=} value the new value to set, optional
* @param {Object} obj the data to set
*/
$setData(obj, value) {
if (typeof obj === 'string') {
obj = {[obj]: value};
}

let queues = this.$upQueues;
__setViewData(obj) {
let queues = this.__setDataQueue;
let isUpdating = !!queues;
queues || (queues = this.$upQueues = []);
queues || (queues = this.__setDataQueue = []);
queues.push(obj);

if (!isUpdating) {
this.__dataUpTaskNum++;
nextTick(this.__executeDataUpdate);
}
},

/**
* Set the view data. It'll not update the view immediately, it's deferred
* to execute when enter the next event loop.
*
* @private
* @param {string|Object} obj the data to set or the path to set
* @param {*=} value the new value to set, optional
*/
$setData(obj, value) {
console.warn('cannot call this API directly, it is private and will be deprecated in future');

if (typeof obj === 'string') {
obj = {[obj]: value};
}

this.__setViewData(obj);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ describe('observable', function () {
instance.setData = spySetData;

instance.created();
instance.$notifySetDataDone();
instance.__notifySetDataDone();
expect(spyUpdated).toNotHaveBeenCalled();
});

Expand Down Expand Up @@ -844,12 +844,12 @@ describe('observable', function () {
instance.setData = spySetData;

instance.created();
instance.$waitingDataUpQueues = null;
instance.__upDoneCallbackQueue = null;

let spyFunc1 = createSpy(() => {});
instance.$nextTick(spyFunc1);

instance.$waitingDataUpQueues = [];
instance.__upDoneCallbackQueue = [];
instance.a = 6;

let spyFunc2 = createSpy(() => {});
Expand Down

0 comments on commit dfecb09

Please sign in to comment.