@goldfishjs/reactive
Advanced tools
Comparing version 1.1.3 to 1.1.4
import _typeof from "@babel/runtime/helpers/typeof"; | ||
import { call, getCurrent, Dep } from './dep'; | ||
import { isRaw } from './raw'; | ||
import { definePropertySilently } from './observable'; | ||
var FLAG_KEY = '__reactive-cpt__'; | ||
@@ -59,3 +60,3 @@ | ||
var removeListenersGroup = []; | ||
Object.defineProperty(obj, key, { | ||
definePropertySilently(obj, key, { | ||
configurable: false, | ||
@@ -118,3 +119,3 @@ enumerable: true, | ||
Object.defineProperty(obj, FLAG_KEY, { | ||
definePropertySilently(obj, FLAG_KEY, { | ||
value: depMap, | ||
@@ -121,0 +122,0 @@ enumerable: false, |
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
import { getCurrent, Dep } from './dep'; | ||
import { isArray, genId } from './utils'; | ||
import { genId, isArray } from './utils'; | ||
import silentValue, { isSilentValue } from './silentValue'; | ||
import { isRaw } from './raw'; | ||
import { isObject } from '@goldfishjs/utils'; | ||
import { isObject, silent, deepVisit, DeepVisitBreak } from '@goldfishjs/utils'; | ||
var OBSERVE_FLAG = {}; | ||
@@ -13,5 +13,14 @@ var OBSERVE_KEY = '__reactive-ob__'; | ||
} | ||
export function definePropertySilently() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
silent(function () { | ||
Object.defineProperty.apply(Object, args); | ||
})(); | ||
} | ||
export function markObservable(obj) { | ||
if (isObject(obj) && obj[OBSERVE_KEY] !== OBSERVE_FLAG) { | ||
Object.defineProperty(obj, OBSERVE_KEY, { | ||
definePropertySilently(obj, OBSERVE_KEY, { | ||
value: OBSERVE_FLAG, | ||
@@ -32,4 +41,4 @@ configurable: false, | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
@@ -43,2 +52,3 @@ | ||
defineProperty(this, i); | ||
isObject(this[i]) && createObserver(this[i]); | ||
} | ||
@@ -63,3 +73,3 @@ } | ||
if (!value[NOTIFY_KEY]) { | ||
Object.defineProperty(value, NOTIFY_KEY, { | ||
definePropertySilently(value, NOTIFY_KEY, { | ||
configurable: true, | ||
@@ -74,9 +84,11 @@ enumerable: false, | ||
var notifyFn = function notifyFn(n, o, options) { | ||
dep.notifyChange(n, o, Object.assign({ | ||
type: 'notify' | ||
}, options || {})); | ||
}; | ||
if (notifyFns) { | ||
var notifyFn = function notifyFn(n, o, options) { | ||
dep.notifyChange(n, o, Object.assign({ | ||
type: 'notify' | ||
}, options || {})); | ||
}; | ||
notifyFns[notifyId] = notifyFn; | ||
notifyFns[notifyId] = notifyFn; | ||
} | ||
} | ||
@@ -100,4 +112,4 @@ } | ||
if (notifyFns) { | ||
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
args[_key3 - 1] = arguments[_key3]; | ||
} | ||
@@ -125,3 +137,3 @@ | ||
defineNotify(value, dep, notifyId); | ||
Object.defineProperty(obj, key, { | ||
definePropertySilently(obj, key, { | ||
configurable: typeof key === 'number', | ||
@@ -158,6 +170,2 @@ enumerable: true, | ||
}); | ||
if (isObject(value)) { | ||
createObserver(value); | ||
} | ||
} | ||
@@ -170,17 +178,27 @@ | ||
if (isArray(obj)) { | ||
for (var i = 0, il = obj.length; i < il; i += 1) { | ||
defineProperty(obj, i); | ||
} | ||
} else { | ||
for (var key in obj) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) { | ||
continue; | ||
var visitKeyPathLength = 0; | ||
deepVisit(obj, function (value, key, po, keyPathList) { | ||
if (visitKeyPathLength !== keyPathList.length) { | ||
visitKeyPathLength = keyPathList.length; | ||
if (isObservable(po) || isRaw(po)) { | ||
return DeepVisitBreak.CHILDREN; | ||
} | ||
defineProperty(obj, key); | ||
markObservable(po); | ||
} | ||
defineProperty(po, key); // We should mark the empty array or object to be observable in children. | ||
if (isArray(value) && value.length === 0 || isObject(value) && Object.keys(value).length === 0) { | ||
markObservable(value); | ||
} | ||
return DeepVisitBreak.NO; | ||
}); // If there is no elements in an object, | ||
// we should also change it to an observable object. | ||
if (visitKeyPathLength === 0) { | ||
markObservable(obj); | ||
} | ||
markObservable(obj); | ||
} | ||
@@ -191,2 +209,3 @@ | ||
obj[name] = value; | ||
return; | ||
} | ||
@@ -198,2 +217,3 @@ | ||
defineProperty(obj, name); | ||
isObject(obj[name]) && createObserver(obj[name]); | ||
obj[name] = silent ? silentValue(value) : value; | ||
@@ -200,0 +220,0 @@ callNotifyFns(obj, obj, obj); |
{ | ||
"name": "@goldfishjs/reactive", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Reactive.", | ||
@@ -18,3 +18,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@goldfishjs/utils": "^1.1.3" | ||
"@goldfishjs/utils": "^1.1.4" | ||
}, | ||
@@ -21,0 +21,0 @@ "devDependencies": { |
import { call, getCurrent, Dep, DepList } from './dep'; | ||
import { isRaw } from './raw'; | ||
import { definePropertySilently } from './observable'; | ||
@@ -77,3 +78,3 @@ type Getter = () => any; | ||
let removeListenersGroup: Function[][] = []; | ||
Object.defineProperty(obj, key, { | ||
definePropertySilently(obj, key, { | ||
configurable: false, | ||
@@ -131,3 +132,3 @@ enumerable: true, | ||
Object.defineProperty(obj, FLAG_KEY, { | ||
definePropertySilently(obj, FLAG_KEY, { | ||
value: depMap, | ||
@@ -134,0 +135,0 @@ enumerable: false, |
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
import { getCurrent, Dep, ChangeOptions } from './dep'; | ||
import { isArray, genId } from './utils'; | ||
import { genId, isArray } from './utils'; | ||
import silentValue, { isSilentValue } from './silentValue'; | ||
import { isRaw } from './raw'; | ||
import { isObject } from '@goldfishjs/utils'; | ||
import { isObject, silent, deepVisit, DeepVisitBreak } from '@goldfishjs/utils'; | ||
@@ -26,5 +26,13 @@ type ObservableBaseTypes = null | undefined | string | number | boolean; | ||
export function definePropertySilently(...args: Parameters<typeof Object['defineProperty']>) { | ||
silent( | ||
() => { | ||
Object.defineProperty(...args); | ||
}, | ||
)(); | ||
} | ||
export function markObservable(obj: any) { | ||
if (isObject(obj) && obj[OBSERVE_KEY] !== OBSERVE_FLAG) { | ||
Object.defineProperty(obj, OBSERVE_KEY, { | ||
definePropertySilently(obj, OBSERVE_KEY, { | ||
value: OBSERVE_FLAG, | ||
@@ -52,2 +60,3 @@ configurable: false, | ||
defineProperty(this, i); | ||
isObject(this[i]) && createObserver(this[i]); | ||
} | ||
@@ -72,3 +81,3 @@ } | ||
if (!(value as any)[NOTIFY_KEY]) { | ||
Object.defineProperty(value, NOTIFY_KEY, { | ||
definePropertySilently(value, NOTIFY_KEY, { | ||
configurable: true, | ||
@@ -80,10 +89,13 @@ enumerable: false, | ||
} | ||
const notifyFns = (value as any)[NOTIFY_KEY]; | ||
const notifyFn = (n: any, o: any, options?: Partial<ChangeOptions>) => { | ||
dep.notifyChange(n, o, { | ||
type: 'notify', | ||
...(options || {}), | ||
}); | ||
}; | ||
notifyFns[notifyId] = notifyFn; | ||
if (notifyFns) { | ||
const notifyFn = (n: any, o: any, options?: Partial<ChangeOptions>) => { | ||
dep.notifyChange(n, o, { | ||
type: 'notify', | ||
...(options || {}), | ||
}); | ||
}; | ||
notifyFns[notifyId] = notifyFn; | ||
} | ||
} | ||
@@ -127,3 +139,3 @@ } | ||
defineNotify(value, dep, notifyId); | ||
Object.defineProperty(obj, key, { | ||
definePropertySilently(obj, key, { | ||
configurable: typeof key === 'number', | ||
@@ -157,6 +169,2 @@ enumerable: true, | ||
}); | ||
if (isObject(value)) { | ||
createObserver(value); | ||
} | ||
} | ||
@@ -169,17 +177,30 @@ | ||
if (isArray(obj)) { | ||
for (let i = 0, il = obj.length; i < il; i += 1) { | ||
defineProperty(obj, i); | ||
} | ||
} else { | ||
for (const key in obj) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) { | ||
continue; | ||
let visitKeyPathLength = 0; | ||
deepVisit(obj, (value, key, po, keyPathList) => { | ||
if (visitKeyPathLength !== keyPathList.length) { | ||
visitKeyPathLength = keyPathList.length; | ||
if (isObservable(po) || isRaw(po)) { | ||
return DeepVisitBreak.CHILDREN; | ||
} | ||
defineProperty(obj, key); | ||
markObservable(po); | ||
} | ||
defineProperty(po as any, key as any); | ||
// We should mark the empty array or object to be observable in children. | ||
if ( | ||
(isArray(value) && value.length === 0) | ||
|| (isObject(value) && Object.keys(value).length === 0) | ||
) { | ||
markObservable(value); | ||
} | ||
return DeepVisitBreak.NO; | ||
}); | ||
// If there is no elements in an object, | ||
// we should also change it to an observable object. | ||
if (visitKeyPathLength === 0) { | ||
markObservable(obj); | ||
} | ||
markObservable(obj); | ||
} | ||
@@ -207,2 +228,3 @@ | ||
obj[name] = value; | ||
return; | ||
} | ||
@@ -214,2 +236,3 @@ | ||
defineProperty(obj, name); | ||
isObject(obj[name]) && createObserver(obj[name]); | ||
obj[name] = silent ? silentValue(value) : value; | ||
@@ -216,0 +239,0 @@ callNotifyFns(obj, obj, obj); |
@@ -8,2 +8,3 @@ declare type ObservableBaseTypes = null | undefined | string | number | boolean; | ||
export declare function isObservable(obj: any): boolean; | ||
export declare function definePropertySilently(...args: Parameters<typeof Object['defineProperty']>): void; | ||
export declare function markObservable(obj: any): void; | ||
@@ -10,0 +11,0 @@ export declare type Methods = 'push' | 'splice' | 'unshift' | 'pop' | 'sort' | 'reverse' | 'shift'; |
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
61684
1976
Updated@goldfishjs/utils@^1.1.4