Skip to content
This repository has been archived by the owner on Dec 12, 2017. It is now read-only.

Commit

Permalink
性能提升:Object.freeze 是不必要的,因为每个 listener 得到的对象都是一个新对象,改动了也不会影响其他 listener
Browse files Browse the repository at this point in the history
  • Loading branch information
lmk123 committed Sep 6, 2015
1 parent 02eca02 commit a3c76c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "chrome-storage-wrapper" ,
"version" : "0.1.1" ,
"version" : "0.1.2" ,
"description" : "A tiny wrapper for chrome.storage that using Promise." ,
"author" : {
"name" : "Milk Lee" ,
Expand Down
9 changes: 4 additions & 5 deletions release/chrome-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@
* 删除一个监听函数
* @param {Function} listener
*/
removeChangeListener: function removeChangeListener(listener) {
var index = changeCallbacks.indexOf(listener);
removeChangeListener: function removeChangeListener(newListener) {
var index = changeCallbacks.indexOf(newListener);
if (index >= 0) {
changeCallbacks.splice(index, 1);
}
Expand Down Expand Up @@ -214,9 +214,8 @@
customChanges[key] = changes[key].newValue;
}

// 防止对象在回调里被修改,因为这会导致其它回调也收到修改后的对象
changeCallbacks.forEach(function (listener) {
listener(Object.freeze(customChanges), area);
changeCallbacks.forEach(function (newListener) {
newListener(customChanges, area);
});
});

Expand Down
9 changes: 4 additions & 5 deletions src/chrome-storage.es6
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@
* 删除一个监听函数
* @param {Function} listener
*/
removeChangeListener ( listener ) {
const index = changeCallbacks.indexOf( listener );
removeChangeListener ( newListener ) {
const index = changeCallbacks.indexOf( newListener );
if ( index >= 0 ) {
changeCallbacks.splice( index , 1 );
}
Expand All @@ -202,9 +202,8 @@
customChanges[ key ] = changes[ key ].newValue;
}

// 防止对象在回调里被修改,因为这会导致其它回调也收到修改后的对象
changeCallbacks.forEach( ( listener ) => {
listener( Object.freeze( customChanges ) , area );
changeCallbacks.forEach( ( newListener ) => {
newListener( customChanges , area );
} );
} );

Expand Down

0 comments on commit a3c76c5

Please sign in to comment.