@goldfishjs/reactive
Advanced tools
Comparing version 2.17.1 to 2.18.0
@@ -6,2 +6,18 @@ # Change Log | ||
# [2.18.0](https://github.com/alipay/goldfish/compare/v2.17.1...v2.18.0) (2022-07-25) | ||
### Bug Fixes | ||
* **reactive:** markObservable no return ([f64e791](https://github.com/alipay/goldfish/commit/f64e79185ad5c3f61c0a72d61fdec87f04ffc57b)) | ||
### Features | ||
* optimize the watchDeep performance. ([affdf6b](https://github.com/alipay/goldfish/commit/affdf6b7dd1669ea0e9e3771bdc9835a27a52fa5)) | ||
## [2.17.1](https://github.com/alipay/goldfish/compare/v2.17.0...v2.17.1) (2022-07-22) | ||
@@ -8,0 +24,0 @@ |
@@ -9,5 +9,5 @@ declare type ObservableBaseTypes = null | undefined | string | number | boolean; | ||
export declare function definePropertySilently(...args: Parameters<typeof Object['defineProperty']>): void; | ||
export declare function markObservable(data: any): void; | ||
export declare function markUnobservable(data: Array<any> | Record<string, any>): void; | ||
export declare function unmarkUnobservable(data: Array<any> | Record<string, any>): void; | ||
export declare function markObservable(data: any): any; | ||
export declare function markUnobservable(data: Array<any> | Record<string, any>): any[] | Record<string, any>; | ||
export declare function unmarkUnobservable(data: Array<any> | Record<string, any>): any[] | Record<string, any>; | ||
export declare function isMarkedUnobservable(data: Array<any> | Record<string, any>): boolean; | ||
@@ -14,0 +14,0 @@ export declare type Methods = 'push' | 'splice' | 'unshift' | 'pop' | 'sort' | 'reverse' | 'shift'; |
@@ -39,2 +39,4 @@ import _DeepVisitBreak from "@goldfishjs/utils/lib/DeepVisitBreak"; | ||
} | ||
return data; | ||
} | ||
@@ -52,2 +54,4 @@ export function markUnobservable(data) { | ||
} | ||
return data; | ||
} | ||
@@ -58,2 +62,4 @@ export function unmarkUnobservable(data) { | ||
} | ||
return data; | ||
} | ||
@@ -60,0 +66,0 @@ export function isMarkedUnobservable(data) { |
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; | ||
import _isObject from "@goldfishjs/utils/lib/isObject"; | ||
import _DeepVisitBreak from "@goldfishjs/utils/lib/DeepVisitBreak"; | ||
import _deepVisit from "@goldfishjs/utils/lib/deepVisit"; | ||
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
@@ -10,2 +12,6 @@ import _createClass from "@babel/runtime/helpers/createClass"; | ||
function hasOwnProperty(obj, property) { | ||
return Object.prototype.hasOwnProperty.call(obj, property); | ||
} | ||
var StopFns = /*#__PURE__*/function () { | ||
@@ -16,2 +22,4 @@ function StopFns() { | ||
_defineProperty(this, "fns", {}); | ||
_defineProperty(this, "stopFnKey", '__stop_fn_key__'); | ||
} | ||
@@ -22,18 +30,41 @@ | ||
value: function add(keyPathList, curKey, fn) { | ||
var keyPathString = keyPathList.str; | ||
var currentFns = this.fns; | ||
if (!this.fns[keyPathString]) { | ||
this.fns[keyPathString] = {}; | ||
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) { | ||
if (!hasOwnProperty(currentFns, keyPathList.raw[i])) { | ||
currentFns[keyPathList.raw[i]] = typeof keyPathList.raw[i] === 'number' ? [] : {}; | ||
} | ||
currentFns = currentFns[keyPathList.raw[i]]; | ||
} | ||
if (Object.prototype.hasOwnProperty.call(this.fns[keyPathString], curKey)) { | ||
if (!currentFns[this.stopFnKey]) { | ||
currentFns[this.stopFnKey] = {}; | ||
} | ||
if (hasOwnProperty(currentFns[this.stopFnKey], curKey) && typeof currentFns[this.stopFnKey][curKey] === 'function') { | ||
throw new Error("Duplicate stop function for key: ".concat(generateKeyPathString([curKey], keyPathList.str))); | ||
} | ||
this.fns[keyPathString][curKey] = fn; | ||
currentFns[this.stopFnKey][curKey] = fn; | ||
} | ||
}, { | ||
key: "isStartWithKeyPathString", | ||
value: function isStartWithKeyPathString(target, keyPathString) { | ||
return target === keyPathString || target.indexOf(keyPathString) === 0 && '.['.indexOf(target.replace(keyPathString, '')[0]) !== -1; | ||
key: "callStopFns", | ||
value: function callStopFns(currentFns) { | ||
var _this = this; | ||
var call = function call(obj) { | ||
if (obj[_this.stopFnKey]) { | ||
for (var key in obj[_this.stopFnKey]) { | ||
obj[_this.stopFnKey][key] && obj[_this.stopFnKey][key](); | ||
} | ||
} | ||
}; | ||
call(currentFns); | ||
_deepVisit(currentFns, function () { | ||
call(arguments.length <= 2 ? undefined : arguments[2]); | ||
return _DeepVisitBreak.NO; | ||
}); | ||
} | ||
@@ -49,12 +80,26 @@ /** | ||
value: function remove(keyPathList) { | ||
var keyPathString = keyPathList.str; | ||
var currentFns = this.fns; | ||
for (var kps in this.fns) { | ||
if (this.isStartWithKeyPathString(kps, keyPathString)) { | ||
for (var k in this.fns[kps]) { | ||
this.fns[kps][k](); | ||
} | ||
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) { | ||
if (!currentFns[keyPathList.raw[i]]) { | ||
return; | ||
} | ||
this.fns[kps] = {}; | ||
currentFns = currentFns[keyPathList.raw[i]]; | ||
} | ||
if (!currentFns || !currentFns[this.stopFnKey]) { | ||
return; | ||
} | ||
this.callStopFns(currentFns); // Remove current layer stop functions. | ||
currentFns[this.stopFnKey] = {}; // Remove the children's stop functions. | ||
for (var k in currentFns) { | ||
if (k === this.stopFnKey) { | ||
continue; | ||
} | ||
currentFns[k] = Array.isArray(currentFns[k]) ? [] : {}; | ||
} | ||
@@ -65,12 +110,20 @@ } | ||
value: function removeChildren(keyPathList) { | ||
var keyPathString = keyPathList.str; | ||
var currentFns = this.fns; | ||
for (var kps in this.fns) { | ||
if (kps !== keyPathString && this.isStartWithKeyPathString(kps, keyPathString)) { | ||
for (var k in this.fns[kps]) { | ||
this.fns[kps][k](); | ||
} | ||
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) { | ||
if (!currentFns[keyPathList.raw[i]]) { | ||
return; | ||
} | ||
this.fns[kps] = {}; | ||
currentFns = currentFns[keyPathList.raw[i]]; | ||
} // Remove the children's stop functions. | ||
for (var k in currentFns) { | ||
if (k === this.stopFnKey) { | ||
continue; | ||
} | ||
this.callStopFns(currentFns[k]); | ||
currentFns[k] = Array.isArray(currentFns[k]) ? [] : {}; | ||
} | ||
@@ -81,12 +134,19 @@ } | ||
value: function removeSingleLayer(keyPathList) { | ||
var keyPathString = keyPathList.str; | ||
var currentFns = this.fns; | ||
for (var kps in this.fns) { | ||
if (kps === keyPathString) { | ||
for (var k in this.fns[kps]) { | ||
this.fns[kps][k](); | ||
} | ||
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) { | ||
if (!currentFns[keyPathList.raw[i]]) { | ||
return; | ||
} | ||
this.fns[kps] = {}; | ||
currentFns = currentFns[keyPathList.raw[i]]; | ||
} // Remove current layer stop functions. | ||
if (currentFns && currentFns[this.stopFnKey]) { | ||
for (var k in currentFns[this.stopFnKey]) { | ||
currentFns[this.stopFnKey][k] && currentFns[this.stopFnKey][k](); | ||
} | ||
currentFns[this.stopFnKey] = {}; | ||
} | ||
@@ -97,7 +157,3 @@ } | ||
value: function callAll() { | ||
for (var k1 in this.fns) { | ||
for (var k2 in this.fns[k1]) { | ||
this.fns[k1][k2](); | ||
} | ||
} | ||
this.callStopFns(this.fns); | ||
} | ||
@@ -135,3 +191,3 @@ }, { | ||
value: function watchObj(obj, keyPathList, options) { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -215,3 +271,3 @@ if (_isObject(obj) && isMarkedUnobservable(obj)) { | ||
obj.forEach(function (_, index) { | ||
_this.watchSingleKey(obj, index, keyPathList); | ||
_this2.watchSingleKey(obj, index, keyPathList); | ||
}); | ||
@@ -230,3 +286,3 @@ } | ||
value: function watchCurrentKeyOnly(obj, key, keyPathList) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -243,8 +299,8 @@ var nextKeyPathList = { | ||
var stopList = getCurrent().addChangeListener(function (newV, oldV, options) { | ||
_this2.watchObj(newV, nextKeyPathList, options); | ||
_this3.watchObj(newV, nextKeyPathList, options); | ||
_this2.callback(_this2.obj, nextKeyPathList.raw, newV, oldV, options); | ||
_this3.callback(_this3.obj, nextKeyPathList.raw, newV, oldV, options); | ||
}, false); | ||
_this2.stopFns.add(keyPathList, key, function () { | ||
_this3.stopFns.add(keyPathList, key, function () { | ||
return stopList.forEach(function (s) { | ||
@@ -255,6 +311,6 @@ return s(); | ||
}, function (e) { | ||
var _this2$options; | ||
var _this3$options; | ||
if ((_this2$options = _this2.options) !== null && _this2$options !== void 0 && _this2$options.onError) { | ||
_this2.options.onError(e); | ||
if ((_this3$options = _this3.options) !== null && _this3$options !== void 0 && _this3$options.onError) { | ||
_this3.options.onError(e); | ||
} else { | ||
@@ -261,0 +317,0 @@ throw e; |
{ | ||
"name": "@goldfishjs/reactive", | ||
"version": "2.17.1", | ||
"version": "2.18.0", | ||
"description": "Reactive.", | ||
@@ -17,4 +17,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@goldfishjs/module-usage": "^2.17.1", | ||
"@goldfishjs/utils": "^2.17.1" | ||
"@goldfishjs/module-usage": "^2.18.0", | ||
"@goldfishjs/utils": "^2.18.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53353
1194