@goldfishjs/reactive
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.7.0](https://github.com/alipay/goldfish/compare/v1.6.1...v1.7.0) (2021-09-27) | ||
### Features | ||
* optimize the listeners updating. ([1c6d03f](https://github.com/alipay/goldfish/commit/1c6d03ff65fc277ecaa15a318c318cf3d17dfdb4)) | ||
## [1.6.1](https://github.com/alipay/goldfish/compare/v1.6.0...v1.6.1) (2021-09-09) | ||
@@ -8,0 +19,0 @@ |
@@ -32,2 +32,8 @@ import _isObject from "@goldfishjs/utils/lib/isObject"; | ||
} | ||
/** | ||
* Remove current layer and all the children's listeners. | ||
* | ||
* @param keyPathList | ||
*/ | ||
}, { | ||
@@ -49,2 +55,32 @@ key: "remove", | ||
}, { | ||
key: "removeChildren", | ||
value: function removeChildren(keyPathList) { | ||
var keyPathString = generateKeyPathString(keyPathList); | ||
for (var kps in this.fns) { | ||
if (kps !== keyPathString && kps.indexOf(keyPathString) === 0) { | ||
for (var k in this.fns[kps]) { | ||
this.fns[kps][k](); | ||
} | ||
this.fns[kps] = {}; | ||
} | ||
} | ||
} | ||
}, { | ||
key: "removeSingleLayer", | ||
value: function removeSingleLayer(keyPathList) { | ||
var keyPathString = generateKeyPathString(keyPathList); | ||
for (var kps in this.fns) { | ||
if (kps === keyPathString) { | ||
for (var k in this.fns[kps]) { | ||
this.fns[kps][k](); | ||
} | ||
this.fns[kps] = {}; | ||
} | ||
} | ||
} | ||
}, { | ||
key: "callAll", | ||
@@ -88,3 +124,3 @@ value: function callAll() { | ||
key: "watchObj", | ||
value: function watchObj(obj, keyPathList) { | ||
value: function watchObj(obj, keyPathList, options) { | ||
var _this = this; | ||
@@ -96,9 +132,67 @@ | ||
this.stopFns.remove(keyPathList); | ||
if (Array.isArray(obj)) { | ||
// Optimization for arrays. | ||
if (options !== null && options !== void 0 && options.isArray) { | ||
var method = options.method; | ||
var oldV = options.oldV; | ||
var args = options.args; | ||
if (Array.isArray(obj)) { | ||
obj.forEach(function (_, index) { | ||
_this.watchSingleKey(obj, index, keyPathList); | ||
}); | ||
if (method === 'sort' || method === 'reverse') { | ||
for (var i = 0, il = obj.length; i < il; i++) { | ||
this.stopFns.removeChildren([].concat(_toConsumableArray(keyPathList), [i])); | ||
this.watchSingleKey(obj, i, keyPathList); | ||
} | ||
} else { | ||
var start = { | ||
push: oldV.length, | ||
splice: args[0], | ||
unshift: 0, | ||
pop: oldV.length - 1, | ||
shift: 0 | ||
}[method]; | ||
var deletedCount = { | ||
push: 0, | ||
splice: args[1], | ||
unshift: 0, | ||
pop: 1, | ||
shift: 1 | ||
}[method]; | ||
var values = { | ||
push: args, | ||
splice: args.slice(2), | ||
unshift: args, | ||
pop: [], | ||
shift: [] | ||
}[method]; | ||
if (deletedCount === values.length) { | ||
for (var _i = start, _il = deletedCount; _i < _il; _i++) { | ||
this.stopFns.removeChildren([].concat(_toConsumableArray(keyPathList), [_i])); | ||
this.watchSingleKey(obj, _i, keyPathList); | ||
} | ||
} else { | ||
for (var _i2 = start, _il2 = oldV.length; _i2 < _il2; _i2++) { | ||
this.stopFns.removeChildren([].concat(_toConsumableArray(keyPathList), [_i2])); | ||
this.watchObj(obj[_i2], [].concat(_toConsumableArray(keyPathList), [_i2])); | ||
} | ||
for (var _i3 = oldV.length, _il3 = obj.length; _i3 < _il3; _i3++) { | ||
this.watchSingleKey(obj, _i3, keyPathList); | ||
} | ||
for (var _i4 = obj.length, _il4 = oldV.length; _i4 < _il4; _i4++) { | ||
this.stopFns.removeSingleLayer([].concat(_toConsumableArray(keyPathList), [_i4])); | ||
this.stopFns.removeChildren([].concat(_toConsumableArray(keyPathList), [_i4])); | ||
} | ||
} | ||
} | ||
} else { | ||
this.stopFns.remove(keyPathList); | ||
obj.forEach(function (_, index) { | ||
_this.watchSingleKey(obj, index, keyPathList); | ||
}); | ||
} | ||
} else if (_isObject(obj)) { | ||
this.stopFns.remove(keyPathList); | ||
for (var key in obj) { | ||
@@ -110,4 +204,4 @@ this.watchSingleKey(obj, key, keyPathList); | ||
}, { | ||
key: "watchSingleKey", | ||
value: function watchSingleKey(obj, key, keyPathList) { | ||
key: "watchCurrentKeyOnly", | ||
value: function watchCurrentKeyOnly(obj, key, keyPathList) { | ||
var _this2 = this; | ||
@@ -122,3 +216,3 @@ | ||
var stopList = getCurrent().addChangeListener(function (newV, oldV, options) { | ||
_this2.watchObj(newV, nextKeyPathList); | ||
_this2.watchObj(newV, nextKeyPathList, options); | ||
@@ -142,2 +236,8 @@ _this2.callback(_this2.obj, nextKeyPathList, newV, oldV, options); | ||
}); | ||
return nextKeyPathList; | ||
} | ||
}, { | ||
key: "watchSingleKey", | ||
value: function watchSingleKey(obj, key, keyPathList) { | ||
var nextKeyPathList = this.watchCurrentKeyOnly(obj, key, keyPathList); | ||
this.watchObj(obj[key], nextKeyPathList); | ||
@@ -144,0 +244,0 @@ } |
{ | ||
"name": "@goldfishjs/reactive", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Reactive.", | ||
@@ -17,4 +17,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@goldfishjs/module-usage": "^1.6.1", | ||
"@goldfishjs/utils": "^1.6.1" | ||
"@goldfishjs/module-usage": "^1.7.0", | ||
"@goldfishjs/utils": "^1.7.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "devDependencies": { |
43868
1141
Updated@goldfishjs/utils@^1.7.0