New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@goldfishjs/reactive

Package Overview
Dependencies
Maintainers
2
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@goldfishjs/reactive - npm Package Compare versions

Comparing version 2.21.1 to 2.22.0-alpha.0

8

CHANGELOG.md

@@ -6,2 +6,10 @@ # Change Log

# [2.22.0-alpha.0](https://github.com/alipay/goldfish/compare/v2.21.1...v2.22.0-alpha.0) (2022-11-08)
**Note:** Version bump only for package @goldfishjs/reactive
## [2.21.1](https://github.com/alipay/goldfish/compare/v2.21.1-alpha.0...v2.21.1) (2022-10-03)

@@ -8,0 +16,0 @@

13

lib/autorun.js

@@ -5,16 +5,10 @@ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";

import { call, getCurrent } from './dep';
var Runner = /*#__PURE__*/function () {
function Runner(fn, errorCb) {
_classCallCheck(this, Runner);
_defineProperty(this, "depList", void 0);
_defineProperty(this, "isStopped", false);
_defineProperty(this, "removeListenersGroup", []);
this.autorun(fn, errorCb);
}
_createClass(Runner, [{

@@ -24,11 +18,8 @@ key: "autorun",

var _this = this;
call(function () {
fn();
_this.depList = getCurrent();
var removeFns = _this.depList.addChangeListener(function () {
!_this.isStopped && _this.autorun(fn);
});
_this.removeListenersGroup.push(removeFns);

@@ -49,15 +40,11 @@ }, errorCb);

}]);
return Runner;
}();
export default function autorun(fn, errorCb) {
var runner = new Runner(fn, errorCb);
var stopFn = function stopFn() {
return runner.stop();
};
stopFn.depList = runner.depList;
return stopFn;
}

@@ -6,15 +6,11 @@ import _typeof from "@babel/runtime/helpers/typeof";

var FLAG_KEY = '__reactive-cpt__';
function isGetter(x) {
return typeof x === 'function';
}
function isSetter(x) {
return typeof x === 'function';
}
function isFullComputedValue(x) {
return x !== null && _typeof(x) === 'object' && (isGetter(x.get) || isSetter(x.set));
}
export function isComputed(obj) {

@@ -27,5 +23,3 @@ return obj && hasOwnProperty(obj, FLAG_KEY);

}
var depMap = {};
var _loop = function _loop(key) {

@@ -35,7 +29,5 @@ if (!hasOwnProperty(obj, key)) {

}
var computedValue = obj[key];
var setter = void 0;
var getter = void 0;
if (isGetter(computedValue)) {

@@ -47,3 +39,2 @@ getter = computedValue;

}
if (computedValue.set) {

@@ -53,7 +44,5 @@ setter = computedValue.set;

}
if (!getter) {
return "continue";
}
var realGetter = getter;

@@ -70,7 +59,5 @@ var isDirty = true;

var outerDepList = getCurrent();
if (outerDepList) {
outerDepList.add(dep);
}
if (isDirty) {

@@ -102,3 +89,2 @@ call(function () {

}
return cachedValue;

@@ -110,3 +96,2 @@ },

}
setter(v);

@@ -119,9 +104,6 @@ dep.notifyChange(undefined, cachedValue, {

};
for (var key in obj) {
var _ret = _loop(key);
if (_ret === "continue") continue;
}
definePropertySilently(obj, FLAG_KEY, {

@@ -128,0 +110,0 @@ value: depMap,

@@ -6,7 +6,5 @@ import { default as reactive } from './reactive';

var prevEnterFunction = target[enterKey];
target[enterKey] = function () {
this.store = createStore(this);
this.stopWatchList = reactive.call(this, setData.bind(this), onError);
if (prevEnterFunction) {

@@ -17,3 +15,2 @@ try {

}
prevEnterFunction.call.apply(prevEnterFunction, [this].concat(args));

@@ -25,5 +22,3 @@ } catch (e) {

};
var prevLeaveFunction = target[leaveKey];
target[leaveKey] = function () {

@@ -35,3 +30,2 @@ if (this.stopWatchList) {

}
if (this.store && this.store.destroy) {

@@ -44,3 +38,2 @@ try {

}
if (prevLeaveFunction) {

@@ -51,3 +44,2 @@ try {

}
prevLeaveFunction.call.apply(prevLeaveFunction, [this].concat(args));

@@ -54,0 +46,0 @@ } catch (e) {

import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function defaultIsChanged(n, o) {
return n !== o;
}
export var Dep = /*#__PURE__*/function () {
function Dep(obj, key) {
_classCallCheck(this, Dep);
_defineProperty(this, "listenerList", []);
_defineProperty(this, "obj", void 0);
_defineProperty(this, "key", void 0);
this.obj = obj;
this.key = key;
}
_createClass(Dep, [{

@@ -31,3 +22,2 @@ key: "addListener",

var _this = this;
this.listenerList.push(listener);

@@ -47,3 +37,2 @@ return function () {

}, options || {});
this.listenerList.forEach(function (listener) {

@@ -54,3 +43,2 @@ listener(newValue, oldValue, realOptions);

}]);
return Dep;

@@ -61,6 +49,4 @@ }();

_classCallCheck(this, DepList);
_defineProperty(this, "list", []);
}
_createClass(DepList, [{

@@ -77,3 +63,2 @@ key: "add",

var isDone = false;
var checker = function checker(n, o, options) {

@@ -83,3 +68,2 @@ if (options.type !== 'notify' && !options.isChanged(n, o)) {

}
if (!shouldBatch || options.isArray) {

@@ -91,3 +75,2 @@ cb(n, o, options);

}
isDone = true;

@@ -99,3 +82,2 @@ Promise.resolve().then(function () {

};
this.list.forEach(function (dep) {

@@ -112,3 +94,2 @@ removeListeners.push(dep.addListener(checker));

}]);
return DepList;

@@ -126,6 +107,4 @@ }();

*/
export function call(fn, errorCb) {
stack.push(new DepList());
try {

@@ -132,0 +111,0 @@ fn();

2

lib/generateKeyPathString.js

@@ -6,3 +6,2 @@ export default function generateKeyPathString(keyPathList, prefix) {

}
if (/[.[\]]/.test(cur)) {

@@ -12,5 +11,4 @@ var property = cur.replace(/"/, '\\"');

}
return prev ? "".concat(prev, ".").concat(cur) : cur;
}, prefix !== null && prefix !== void 0 ? prefix : '');
}

@@ -6,7 +6,4 @@ import _DeepVisitBreak from "@goldfishjs/utils/lib/DeepVisitBreak";

import _silent from "@goldfishjs/utils/lib/silent";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { getCurrent, Dep } from './dep';

@@ -27,3 +24,2 @@ import { genId, isArray } from './utils';

}
_silent(function () {

@@ -42,3 +38,2 @@ Object.defineProperty.apply(Object, args);

}
return data;

@@ -57,3 +52,2 @@ }

}
return data;

@@ -65,3 +59,2 @@ }

}
return data;

@@ -76,3 +69,2 @@ }

/* eslint-disable no-extend-native */
Array.prototype[methodName] = function () {

@@ -82,3 +74,2 @@ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {

}
if (isObservable(this)) {

@@ -88,3 +79,2 @@ var originLength = this.length;

var result = oldMethod.apply(this, args);
if (originLength < this.length) {

@@ -96,3 +86,2 @@ for (var i = originLength; i < this.length; i += 1) {

}
callNotifyFns(this, this, this, {

@@ -107,7 +96,5 @@ args: args,

}
return oldMethod.apply(this, args);
};
/* eslint-enable no-extend-native */
});

@@ -125,5 +112,3 @@

}
var notifyFns = value[NOTIFY_KEY];
if (notifyFns) {

@@ -135,3 +120,2 @@ var notifyFn = function notifyFn(n, o, options) {

};
notifyFns[notifyId] = notifyFn;

@@ -141,7 +125,5 @@ }

}
function removeNotify(value, notifyId) {
if (_isObject(value)) {
var notifyFns = value[NOTIFY_KEY];
if (notifyFns) {

@@ -152,7 +134,5 @@ notifyFns[notifyId] = null;

}
function callNotifyFns(value) {
if (_isObject(value)) {
var notifyFns = value[NOTIFY_KEY];
if (notifyFns) {

@@ -162,3 +142,2 @@ for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {

}
for (var key in notifyFns) {

@@ -172,10 +151,8 @@ if (notifyFns[key]) {

}
function defineProperty(obj, key) {
var desc = Object.getOwnPropertyDescriptor(obj, key); // Do not modify the un-configurable properties and `getter/setter` properties.
var desc = Object.getOwnPropertyDescriptor(obj, key);
// Do not modify the un-configurable properties and `getter/setter` properties.
if (desc && (!desc.configurable || desc.get)) {
return;
}
var value = obj[key];

@@ -190,7 +167,5 @@ var dep = new Dep(obj, key);

var currentDepList = getCurrent();
if (currentDepList) {
currentDepList.add(dep);
}
return isSilentValue(value) ? value.value : value;

@@ -202,13 +177,9 @@ },

}
var newValue = v;
var oldValue = value;
value = v;
if (oldValue !== value && _isObject(oldValue)) {
removeNotify(oldValue, notifyId);
}
defineNotify(value, dep, notifyId);
if (!isSilentValue(v)) {

@@ -220,3 +191,2 @@ dep.notifyChange(newValue, oldValue);

}
function createObserver(obj) {

@@ -226,27 +196,20 @@ if (isObservable(obj) || isMarkedUnobservable(obj)) {

}
var visitKeyPathDeepRecords = [];
_deepVisit(obj, function (value, key, po, keyPathList) {
if (!visitKeyPathDeepRecords[keyPathList.length]) {
visitKeyPathDeepRecords[keyPathList.length] = true;
if (isObservable(po) || isMarkedUnobservable(po)) {
return _DeepVisitBreak.CHILDREN;
}
markObservable(po);
}
defineProperty(po, key); // We should mark the empty array or object to be observable in children.
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,
});
// If there is no elements in an object,
// we should also change it to an observable object.
if (!visitKeyPathDeepRecords[0]) {

@@ -256,3 +219,2 @@ markObservable(obj);

}
export function set(obj, name, value, options) {

@@ -263,5 +225,3 @@ if (!isObservable(obj)) {

}
var silent = options && options.silent || false;
if (!hasOwnProperty(obj, name)) {

@@ -268,0 +228,0 @@ defineProperty(obj, name);

@@ -7,5 +7,3 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";

}
this.store.init && this.store.init();
var watchKeys = function watchKeys(data) {

@@ -18,5 +16,4 @@ var stop = watchDeep(data, setData, {

};
var stopWatchList = [].concat(_toConsumableArray(watchKeys(this.store.getState())), _toConsumableArray(watchKeys(this.store.getComputed())));
return stopWatchList;
}

@@ -6,5 +6,5 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty";

return v && v[SILENT_KEY] === SILENT_FLAG;
} // Convert the value to `silent value`.
}
// Convert the value to `silent value`.
// Setting `silent value` to the observable object will not trigger the `change callback`.
export default function silentValue() {

@@ -14,18 +14,13 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {

}
if (!args.length) {
return;
}
if (args.length === 1) {
var _ref;
return isSilentValue(args[0]) ? args[0] : (_ref = {}, _defineProperty(_ref, SILENT_KEY, SILENT_FLAG), _defineProperty(_ref, "value", args[0]), _ref);
}
return args.map(function (v) {
var _ref2;
return isSilentValue(v) ? v : (_ref2 = {}, _defineProperty(_ref2, SILENT_KEY, SILENT_FLAG), _defineProperty(_ref2, "value", v), _ref2);
});
}

@@ -9,19 +9,11 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";

import { isArray, hasOwnProperty } from './utils';
var Watcher = /*#__PURE__*/function () {
function Watcher(fn, cb, options) {
_classCallCheck(this, Watcher);
_defineProperty(this, "isStopped", false);
_defineProperty(this, "cb", void 0);
_defineProperty(this, "options", void 0);
_defineProperty(this, "fn", void 0);
_defineProperty(this, "isFirstTime", true);
_defineProperty(this, "removeListeners", []);
this.fn = fn;

@@ -32,3 +24,2 @@ this.cb = cb;

}
_createClass(Watcher, [{

@@ -47,4 +38,4 @@ key: "stop",

this.removeListeners.splice(0, this.removeListeners.length);
} // Deep visite the object to collect the dependencies.
}
// Deep visite the object to collect the dependencies.
}, {

@@ -56,3 +47,2 @@ key: "deepVisit",

}
if (isArray(obj)) {

@@ -67,3 +57,2 @@ for (var i = 0, il = obj.length; i < il; i += 1) {

}
this.deepVisit(obj[key]);

@@ -77,22 +66,15 @@ }

var _this = this;
var result;
call(function () {
var _this$removeListeners;
result = _this.fn();
if (_this.options.deep) {
_this.deepVisit(result);
}
(_this$removeListeners = _this.removeListeners).push.apply(_this$removeListeners, _toConsumableArray(getCurrent().addChangeListener(function (n, o, options) {
_this.callRemoveListeners();
if (_this.isStopped) {
return;
}
var currentResult = _this.watch();
if (_this.options.deep || options.type === 'notify' || currentResult !== result) {

@@ -107,3 +89,2 @@ try {

}, this.options.onError);
if (this.isFirstTime && this.options.immediate) {

@@ -117,10 +98,7 @@ this.isFirstTime = false;

}
return result;
}
}]);
return Watcher;
}();
export default function watch(fn, cb) {

@@ -127,0 +105,0 @@ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {

@@ -12,12 +12,8 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";

import { isArray, hasOwnProperty } from './utils';
var StopFns = /*#__PURE__*/function () {
function StopFns() {
_classCallCheck(this, StopFns);
_defineProperty(this, "fns", {});
_defineProperty(this, "stopFnKey", '__stop_fn_key__');
}
_createClass(StopFns, [{

@@ -27,3 +23,2 @@ key: "add",

var currentFns = this.fns;
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) {

@@ -33,14 +28,10 @@ if (!hasOwnProperty(currentFns, keyPathList.raw[i])) {

}
currentFns = currentFns[keyPathList.raw[i]];
}
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)));
}
currentFns[this.stopFnKey][curKey] = fn;

@@ -52,3 +43,2 @@ }

var _this = this;
var call = function call(obj) {

@@ -61,5 +51,3 @@ if (obj[_this.stopFnKey]) {

};
call(currentFns);
_deepVisit(currentFns, function () {

@@ -75,3 +63,2 @@ call(arguments.length <= 2 ? undefined : arguments[2]);

*/
}, {

@@ -81,3 +68,2 @@ key: "remove",

var currentFns = this.fns;
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) {

@@ -87,14 +73,11 @@ if (!currentFns[keyPathList.raw[i]]) {

}
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.
this.callStopFns(currentFns);
// Remove current layer stop functions.
currentFns[this.stopFnKey] = {};
// Remove the children's stop functions.
for (var k in currentFns) {

@@ -104,3 +87,2 @@ if (k === this.stopFnKey) {

}
currentFns[k] = isArray(currentFns[k]) ? [] : {};

@@ -113,3 +95,2 @@ }

var currentFns = this.fns;
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) {

@@ -119,7 +100,5 @@ if (!currentFns[keyPathList.raw[i]]) {

}
currentFns = currentFns[keyPathList.raw[i]];
} // Remove the children's stop functions.
}
// Remove the children's stop functions.
for (var k in currentFns) {

@@ -129,3 +108,2 @@ if (k === this.stopFnKey) {

}
this.callStopFns(currentFns[k]);

@@ -139,3 +117,2 @@ currentFns[k] = isArray(currentFns[k]) ? [] : {};

var currentFns = this.fns;
for (var i = 0, il = keyPathList.raw.length; i < il; i += 1) {

@@ -145,7 +122,5 @@ if (!currentFns[keyPathList.raw[i]]) {

}
currentFns = currentFns[keyPathList.raw[i]];
} // Remove current layer stop functions.
}
// Remove current layer stop functions.
if (currentFns && currentFns[this.stopFnKey]) {

@@ -155,3 +130,2 @@ for (var k in currentFns[this.stopFnKey]) {

}
currentFns[this.stopFnKey] = {};

@@ -172,18 +146,11 @@ }

}]);
return StopFns;
}();
var Watcher = /*#__PURE__*/function () {
function Watcher(obj, callback, options) {
_classCallCheck(this, Watcher);
_defineProperty(this, "obj", void 0);
_defineProperty(this, "options", void 0);
_defineProperty(this, "callback", void 0);
_defineProperty(this, "stopFns", new StopFns());
this.obj = obj;

@@ -193,3 +160,2 @@ this.callback = callback;

}
_createClass(Watcher, [{

@@ -199,7 +165,5 @@ key: "watchObj",

var _this2 = this;
if (_isObject(obj) && isMarkedUnobservable(obj)) {
return;
}
if (isArray(obj)) {

@@ -211,3 +175,2 @@ // Optimization for arrays.

var args = options.args;
if (method === 'sort' || method === 'reverse') {

@@ -243,3 +206,2 @@ for (var i = 0, il = obj.length; i < il; i++) {

}[method];
if (deletedCount === values.length) {

@@ -262,7 +224,5 @@ for (var _i = start, _il = deletedCount; _i < _il; _i++) {

}
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++) {

@@ -286,3 +246,2 @@ var _newKeyPathList = {

this.stopFns.remove(keyPathList);
for (var key in obj) {

@@ -297,3 +256,2 @@ this.watchSingleKey(obj, key, keyPathList);

var _this3 = this;
var nextKeyPathList = {

@@ -307,9 +265,6 @@ raw: [].concat(_toConsumableArray(keyPathList.raw), [key]),

/* eslint-enable @typescript-eslint/no-unused-expressions */
var stopList = getCurrent().addChangeListener(function (newV, oldV, options) {
_this3.watchObj(newV, nextKeyPathList, options);
_this3.callback(_this3.obj, nextKeyPathList.raw, newV, oldV, options);
}, false);
_this3.stopFns.add(keyPathList, key, function () {

@@ -322,3 +277,2 @@ return stopList.forEach(function (s) {

var _this3$options;
if ((_this3$options = _this3.options) !== null && _this3$options !== void 0 && _this3$options.onError) {

@@ -342,7 +296,5 @@ _this3.options.onError(e);

var _this$options;
if ((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.immediate) {
this.callback(this.obj, [], this.obj, undefined);
}
return this.watchObj(this.obj, {

@@ -359,6 +311,4 @@ raw: [],

}]);
return Watcher;
}();
export default function watchDeep(obj, callback, options) {

@@ -365,0 +315,0 @@ var watcher = new Watcher(obj, callback, options);

{
"name": "@goldfishjs/reactive",
"version": "2.21.1",
"version": "2.22.0-alpha.0",
"description": "Reactive.",

@@ -17,4 +17,4 @@ "main": "lib/index.js",

"dependencies": {
"@goldfishjs/module-usage": "^2.21.1",
"@goldfishjs/utils": "^2.21.1"
"@goldfishjs/module-usage": "^2.22.0-alpha.0",
"@goldfishjs/utils": "^2.22.0-alpha.0"
},

@@ -21,0 +21,0 @@ "devDependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc