Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@goldfishjs/reactive-connect

Package Overview
Dependencies
Maintainers
2
Versions
146
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@goldfishjs/reactive-connect - npm Package Compare versions

Comparing version 0.0.13 to 0.0.14

322

lib/MiniDataSetter.js

@@ -0,4 +1,5 @@

import _set from "@goldfishjs/reactive/lib/set";
import _isObservable from "@goldfishjs/reactive/lib/isObservable";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _generateKeyPathString from "@goldfishjs/reactive/lib/generateKeyPathString";
import _set from "@goldfishjs/reactive/lib/set";
import _regeneratorRuntime from "@babel/runtime/regenerator";

@@ -40,7 +41,8 @@ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";

export var Count =
import { isObject } from '@goldfishjs/utils';
export var Batch =
/*#__PURE__*/
function () {
function Count(cb) {
_classCallCheck(this, Count);
function Batch(cb) {
_classCallCheck(this, Batch);

@@ -52,3 +54,3 @@ this.segTotalList = [];

_createClass(Count, [{
_createClass(Batch, [{
key: "set",

@@ -104,5 +106,213 @@ value: function set() {

return Count;
return Batch;
}();
var Ancestor = function Ancestor() {
_classCallCheck(this, Ancestor);
this.parent = undefined;
this.children = undefined;
};
var Leaf = function Leaf() {
_classCallCheck(this, Leaf);
this.parent = undefined;
};
var LimitLeafCounter =
/*#__PURE__*/
function () {
function LimitLeafCounter() {
_classCallCheck(this, LimitLeafCounter);
this.limitLeafTotalCount = 100;
this.leafTotalCount = 0;
}
_createClass(LimitLeafCounter, [{
key: "addLeaf",
value: function addLeaf() {
this.leafTotalCount += 1;
}
}, {
key: "getRemainCount",
value: function getRemainCount() {
return this.limitLeafTotalCount - this.leafTotalCount;
}
}]);
return LimitLeafCounter;
}();
var UpdateTree =
/*#__PURE__*/
function () {
function UpdateTree(view, limitLeafTotalCount) {
_classCallCheck(this, UpdateTree);
this.root = new Ancestor();
this.view = view;
this.limitLeafTotalCount = limitLeafTotalCount;
}
_createClass(UpdateTree, [{
key: "addNode",
value: function addNode(keyPathList, value) {
var curNode = this.root;
var len = keyPathList.length;
keyPathList.forEach(function (keyPath, index) {
if (curNode.children === undefined) {
if (typeof keyPath === 'number') {
curNode.children = [];
} else {
curNode.children = {};
}
}
if (index < len - 1) {
var child = curNode.children[keyPath];
if (!child || child instanceof Leaf) {
var node = new Ancestor();
node.parent = curNode;
curNode.children[keyPath] = node;
curNode = node;
} else {
curNode = child;
}
} else {
var lastLeafNode = new Leaf();
lastLeafNode.parent = curNode;
lastLeafNode.value = value;
curNode.children[keyPath] = lastLeafNode;
}
});
}
}, {
key: "getViewData",
value: function getViewData(viewData, k) {
return isObject(viewData) ? viewData[k] : null;
}
}, {
key: "combine",
value: function combine(curNode, viewData) {
var _this = this;
if (curNode instanceof Leaf) {
return curNode.value;
}
if (!curNode.children) {
return undefined;
}
if (Array.isArray(curNode.children)) {
return curNode.children.map(function (child, index) {
return _this.combine(child, _this.getViewData(viewData, index));
});
}
var result = isObject(viewData) ? viewData : {};
for (var k in curNode.children) {
result[k] = this.combine(curNode.children[k], this.getViewData(viewData, k));
}
return result;
}
}, {
key: "iterate",
value: function iterate(curNode, keyPathList, updateObj, viewData, availableLeafCount) {
var _this2 = this;
if (curNode instanceof Leaf) {
updateObj[_generateKeyPathString(keyPathList)] = curNode.value;
this.limitLeafTotalCount.addLeaf();
} else {
var children = curNode.children;
var len = Array.isArray(children) ? children.length : Object.keys(children || {}).length;
if (len > availableLeafCount) {
updateObj[_generateKeyPathString(keyPathList)] = this.combine(curNode, viewData);
this.limitLeafTotalCount.addLeaf();
} else if (Array.isArray(children)) {
children.forEach(function (child, index) {
_this2.iterate(child, [].concat(_toConsumableArray(keyPathList), [index]), updateObj, _this2.getViewData(viewData, index), _this2.limitLeafTotalCount.getRemainCount() - len);
});
} else {
for (var k in children) {
this.iterate(children[k], [].concat(_toConsumableArray(keyPathList), [k]), updateObj, this.getViewData(viewData, k), this.limitLeafTotalCount.getRemainCount() - len);
}
}
}
}
}, {
key: "generate",
value: function generate() {
var updateObj = {};
this.iterate(this.root, [], updateObj, this.view.data, this.limitLeafTotalCount.getRemainCount());
return updateObj;
}
}, {
key: "clear",
value: function clear() {
this.root = new Ancestor();
}
}]);
return UpdateTree;
}();
var Updater =
/*#__PURE__*/
function () {
function Updater() {
_classCallCheck(this, Updater);
this.list = [];
this.limitLeafCounter = new LimitLeafCounter();
}
_createClass(Updater, [{
key: "setSetObjectValue",
value: function setSetObjectValue(view, keyPathList, value) {
var last = this.list[this.list.length - 1];
if (!last || !(last instanceof UpdateTree)) {
last = new UpdateTree(view, this.limitLeafCounter);
this.list.push(last);
}
last.addNode(keyPathList, value);
}
}, {
key: "setSpliceObjectValue",
value: function setSpliceObjectValue(keyPathList, args) {
var last = this.list[this.list.length - 1];
if (!last || last instanceof UpdateTree) {
last = {};
this.list.push(last);
}
var key = typeof keyPathList === 'string' ? keyPathList : _generateKeyPathString(keyPathList);
last[key] = args;
}
}, {
key: "iterate",
value: function iterate(fn) {
this.list.forEach(function (item) {
if (item instanceof UpdateTree) {
fn('set', item.generate());
} else {
fn('splice', item);
}
});
}
}]);
return Updater;
}();
var MiniDataSetter =

@@ -112,12 +322,11 @@ /*#__PURE__*/

function MiniDataSetter() {
var _this = this;
var _this3 = this;
_classCallCheck(this, MiniDataSetter);
this.count = new Count(function () {
return _this.flush();
this.count = new Batch(function () {
return _this3.flush();
});
this.setDataObjectMap = {};
this.spliceDataObjectMap = {};
this.viewMap = {};
this.updaterMap = {};
}

@@ -133,8 +342,7 @@

value: function flush() {
var _this2 = this;
var _this4 = this;
var _loop = function _loop(id) {
var setDataObject = _this2.setDataObjectMap[id];
var spliceDataObject = _this2.spliceDataObjectMap[id];
var view = _this2.viewMap[id];
var updater = _this4.updaterMap[id];
var view = _this4.viewMap[id];
var store = view.store;

@@ -147,5 +355,10 @@ var isSyncDataSafe = store && store.isSyncDataSafe === false ? false : true;

_this2.getBatchUpdates(view)(function () {
view.setData(setDataObject);
view.$spliceData(spliceDataObject);
_this4.getBatchUpdates(view)(function () {
updater.iterate(function (type, obj) {
if (type === 'set') {
view.setData(obj);
} else {
view.$spliceData(obj);
}
});
});

@@ -161,6 +374,16 @@ };

this.viewMap = {};
this.setDataObjectMap = {};
this.spliceDataObjectMap = {};
this.updaterMap = {};
}
}, {
key: "setValue",
value: function setValue(obj, key, value) {
if (_isObservable(obj)) {
_set(obj, key, value, {
silent: true
});
} else {
obj[key] = value;
}
}
}, {
key: "setByKeyPathList",

@@ -173,9 +396,6 @@ value: function setByKeyPathList(obj, keyPathList, value) {

for (var i = 0, il = keyPathList.length, curObj = obj; true; i += 1) {
var key = keyPathList[i];
var key = keyPathList[i]; // The last one.
if (i === il - 1) {
_set(curObj, String(key), value, {
silent: true
});
this.setValue(curObj, key, value);
break;

@@ -185,7 +405,3 @@ }

if (!curObj[key] && i < il) {
_set(curObj, String(key), {}, {
silent: true
});
curObj[key] = {};
this.setValue(curObj, key, typeof key === 'number' ? [] : {});
}

@@ -199,4 +415,3 @@

value: function set(view, keyPathList, newV, oldV, options) {
this.spliceDataObjectMap[view.$id] = this.spliceDataObjectMap[view.$id] || {};
this.setDataObjectMap[view.$id] = this.setDataObjectMap[view.$id] || {};
this.updaterMap[view.$id] = this.updaterMap[view.$id] || new Updater();
this.viewMap[view.$id] = view;

@@ -207,26 +422,31 @@

if (Array.isArray(newV) && Array.isArray(oldV) && options && options.method) {
var methodName = options && options.method;
var args = options && options.args;
var optionsOldV = options && options.oldV;
this.setByKeyPathList(view.data, keyPathList, options.oldV);
var map = {
push: [optionsOldV.length, 0].concat(_toConsumableArray(args)),
splice: args,
unshift: [0, 0].concat(_toConsumableArray(args)),
pop: [optionsOldV.length - 1, 1],
shift: [0, 1]
};
var spliceDataArgs = map[methodName];
if (Array.isArray(newV) && Array.isArray(oldV)) {
if (!options || !options.method) {
// Use `splice` to update the whole array when there is an new array set.
this.updaterMap[view.$id].setSpliceObjectValue(keyPathString, [0, oldV.length].concat(_toConsumableArray(newV)));
} else {
var methodName = options && options.method;
var args = options && options.args;
var optionsOldV = options && options.oldV;
this.setByKeyPathList(view.data, keyPathList, options.oldV);
var map = {
push: [optionsOldV.length, 0].concat(_toConsumableArray(args)),
splice: args,
unshift: [0, 0].concat(_toConsumableArray(args)),
pop: [optionsOldV.length - 1, 1],
shift: [0, 1]
};
var spliceDataArgs = map[methodName];
if (spliceDataArgs) {
this.spliceDataObjectMap[view.$id][keyPathString] = spliceDataArgs;
} else {
this.setDataObjectMap[view.$id][keyPathString] = newV;
if (spliceDataArgs) {
this.updaterMap[view.$id].setSpliceObjectValue(keyPathString, spliceDataArgs);
} else {
this.updaterMap[view.$id].setSetObjectValue(view, keyPathList, newV);
}
}
} else {
this.setDataObjectMap[view.$id][keyPathString] = newV;
this.updaterMap[view.$id].setSetObjectValue(view, keyPathList, newV);
}
} catch (e) {
this.setDataObjectMap[view.$id][keyPathList[0]] = view.data[keyPathList[0]];
this.updaterMap[view.$id].setSetObjectValue(view, [keyPathList[0]], view.data[keyPathList[0]]);
console.warn(e);

@@ -233,0 +453,0 @@ }

5

package.json
{
"name": "@goldfishjs/reactive-connect",
"version": "0.0.13",
"version": "0.0.14",
"description": "goldfish-reactive-connect",

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

"dependencies": {
"@goldfishjs/reactive": "^0.0.13",
"@goldfishjs/reactive": "^0.0.14",
"@goldfishjs/utils": "^0.0.14",
"mini-types": "^0.0.4"

@@ -22,0 +23,0 @@ },

@@ -6,5 +6,7 @@ import {

Methods,
isObservable,
} from '@goldfishjs/reactive';
import { isObject } from '@goldfishjs/utils';
export class Count {
export class Batch {
private segTotalList: number[] = [];

@@ -49,13 +51,210 @@

type AncestorChildren = Record<string, (Ancestor | Leaf)> | (Ancestor | Leaf)[];
class Ancestor {
public parent: Ancestor | undefined = undefined;
public children: AncestorChildren | undefined = undefined;
}
class Leaf {
public parent: Ancestor | undefined = undefined;
public value: any;
}
class LimitLeafCounter {
private limitLeafTotalCount = 100;
private leafTotalCount = 0;
public addLeaf() {
this.leafTotalCount += 1;
}
public getRemainCount() {
return this.limitLeafTotalCount - this.leafTotalCount;
}
}
class UpdateTree {
private root = new Ancestor();
private view: View;
private limitLeafTotalCount: LimitLeafCounter;
public constructor(view: View, limitLeafTotalCount: LimitLeafCounter) {
this.view = view;
this.limitLeafTotalCount = limitLeafTotalCount;
}
public addNode(keyPathList: (string | number)[], value: any) {
let curNode = this.root;
const len = keyPathList.length;
keyPathList.forEach((keyPath, index) => {
if (curNode.children === undefined) {
if (typeof keyPath === 'number') {
curNode.children = [];
} else {
curNode.children = {};
}
}
if (index < len - 1) {
const child = (curNode.children as any)[keyPath];
if (!child || child instanceof Leaf) {
const node = new Ancestor();
node.parent = curNode;
(curNode.children as any)[keyPath] = node;
curNode = node;
} else {
curNode = child;
}
} else {
const lastLeafNode: Leaf = new Leaf();
lastLeafNode.parent = curNode;
lastLeafNode.value = value;
(curNode.children as any)[keyPath] = lastLeafNode;
}
});
}
private getViewData(viewData: any, k: string | number) {
return isObject(viewData) ? viewData[k] : null;
}
private combine(curNode: Ancestor | Leaf, viewData: any): any {
if (curNode instanceof Leaf) {
return curNode.value;
}
if (!curNode.children) {
return undefined;
}
if (Array.isArray(curNode.children)) {
return curNode.children.map((child, index) => {
return this.combine(child, this.getViewData(viewData, index));
});
}
const result: Record<string, any> = isObject(viewData) ? viewData : {};
for (const k in curNode.children) {
result[k] = this.combine(curNode.children[k], this.getViewData(viewData, k));
}
return result;
}
private iterate(
curNode: Ancestor | Leaf,
keyPathList: (string | number)[],
updateObj: Record<string, any>,
viewData: any,
availableLeafCount: number,
) {
if (curNode instanceof Leaf) {
updateObj[generateKeyPathString(keyPathList)] = curNode.value;
this.limitLeafTotalCount.addLeaf();
} else {
const children = curNode.children;
const len = Array.isArray(children)
? children.length
: Object.keys(children || {}).length;
if (len > availableLeafCount) {
updateObj[generateKeyPathString(keyPathList)] = this.combine(curNode, viewData);
this.limitLeafTotalCount.addLeaf();
} else if (Array.isArray(children)) {
children.forEach((child, index) => {
this.iterate(
child,
[
...keyPathList,
index,
],
updateObj,
this.getViewData(viewData, index),
this.limitLeafTotalCount.getRemainCount() - len,
);
});
} else {
for (const k in children) {
this.iterate(
children[k],
[
...keyPathList,
k,
],
updateObj,
this.getViewData(viewData, k),
this.limitLeafTotalCount.getRemainCount() - len,
);
}
}
}
}
public generate() {
const updateObj: Record<string, any> = {};
this.iterate(
this.root,
[],
updateObj,
this.view.data,
this.limitLeafTotalCount.getRemainCount(),
);
return updateObj;
}
public clear() {
this.root = new Ancestor();
}
}
class Updater {
private list: (UpdateTree | Record<string, any>)[] = [];
private limitLeafCounter = new LimitLeafCounter();
public setSetObjectValue(view: View, keyPathList: (string | number)[], value: any) {
let last = this.list[this.list.length - 1];
if (!last || !(last instanceof UpdateTree)) {
last = new UpdateTree(view, this.limitLeafCounter);
this.list.push(last);
}
last.addNode(keyPathList, value);
}
public setSpliceObjectValue(keyPathList: (string | number)[] | string, args: any[]) {
let last = this.list[this.list.length - 1];
if (!last || last instanceof UpdateTree) {
last = {};
this.list.push(last);
}
const key = typeof keyPathList === 'string' ? keyPathList : generateKeyPathString(keyPathList);
last[key] = args;
}
public iterate(fn: (type: 'set' | 'splice', obj: Record<string, any>) => void) {
this.list.forEach((item) => {
if (item instanceof UpdateTree) {
fn('set', item.generate());
} else {
fn('splice', item);
}
});
}
}
type View = tinyapp.IPageInstance<any> | tinyapp.IComponentInstance<any, any>;
export default class MiniDataSetter {
private count = new Count(() => this.flush());
private count = new Batch(() => this.flush());
private setDataObjectMap: Record<string, Record<string, any>> = {};
private viewMap: Record<string, View> = {};
private spliceDataObjectMap: Record<string, Record<string, any>> = {};
private updaterMap: Record<string, Updater> = {};
private viewMap: Record<string, View> = {};
private getBatchUpdates(view: View) {

@@ -69,4 +268,3 @@ return view.$batchedUpdates ?

for (const id in this.viewMap) {
const setDataObject = this.setDataObjectMap[id];
const spliceDataObject = this.spliceDataObjectMap[id];
const updater = this.updaterMap[id];
const view = this.viewMap[id];

@@ -81,11 +279,23 @@

this.getBatchUpdates(view)(() => {
view.setData(setDataObject);
view.$spliceData(spliceDataObject);
updater.iterate((type, obj) => {
if (type === 'set') {
view.setData(obj);
} else {
view.$spliceData(obj);
}
});
});
}
this.viewMap = {};
this.setDataObjectMap = {};
this.spliceDataObjectMap = {};
this.updaterMap = {};
}
private setValue(obj: any, key: string | number, value: any) {
if (isObservable(obj)) {
reactiveSet(obj, key as any, value, { silent: true });
} else {
obj[key] = value;
}
}
private setByKeyPathList(

@@ -106,4 +316,5 @@ obj: any,

const key = keyPathList[i];
// The last one.
if (i === il - 1) {
reactiveSet(curObj, String(key), value, { silent: true });
this.setValue(curObj, key, value);
break;

@@ -113,4 +324,3 @@ }

if (!curObj[key] && i < il) {
reactiveSet(curObj, String(key), {}, { silent: true });
curObj[key] = {};
this.setValue(curObj, key, typeof key === 'number' ? [] : {});
}

@@ -129,32 +339,43 @@

) {
this.spliceDataObjectMap[view.$id] = this.spliceDataObjectMap[view.$id] || {};
this.setDataObjectMap[view.$id] = this.setDataObjectMap[view.$id] || {};
this.updaterMap[view.$id] = this.updaterMap[view.$id] || new Updater();
this.viewMap[view.$id] = view;
try {
const keyPathString = generateKeyPathString(keyPathList);
if (Array.isArray(newV) && Array.isArray(oldV) && options && options.method) {
const methodName: Methods = options && options.method;
const args = options && options.args!;
const optionsOldV = options && options.oldV!;
this.setByKeyPathList(view.data, keyPathList, options.oldV);
if (Array.isArray(newV) && Array.isArray(oldV)) {
if (!options || !options.method) {
// Use `splice` to update the whole array when there is an new array set.
this.updaterMap[view.$id].setSpliceObjectValue(
keyPathString,
[0, oldV.length, ...newV],
);
} else {
const methodName: Methods = options && options.method;
const args = options && options.args!;
const optionsOldV = options && options.oldV!;
this.setByKeyPathList(view.data, keyPathList, options.oldV);
const map: Record<string, any> = {
push: [optionsOldV.length, 0, ...args],
splice: args,
unshift: [0, 0, ...args],
pop: [optionsOldV.length - 1, 1],
shift: [0, 1],
};
const spliceDataArgs = map[methodName];
const map: Record<string, any> = {
push: [optionsOldV.length, 0, ...args],
splice: args,
unshift: [0, 0, ...args],
pop: [optionsOldV.length - 1, 1],
shift: [0, 1],
};
const spliceDataArgs = map[methodName];
if (spliceDataArgs) {
this.spliceDataObjectMap[view.$id][keyPathString] = spliceDataArgs;
} else {
this.setDataObjectMap[view.$id][keyPathString] = newV;
if (spliceDataArgs) {
this.updaterMap[view.$id].setSpliceObjectValue(keyPathString, spliceDataArgs);
} else {
this.updaterMap[view.$id].setSetObjectValue(view, keyPathList, newV);
}
}
} else {
this.setDataObjectMap[view.$id][keyPathString] = newV;
this.updaterMap[view.$id].setSetObjectValue(view, keyPathList, newV);
}
} catch (e) {
this.setDataObjectMap[view.$id][keyPathList[0]] = view.data[keyPathList[0]];
this.updaterMap[view.$id].setSetObjectValue(
view,
[keyPathList[0]],
view.data[keyPathList[0]],
);
console.warn(e);

@@ -161,0 +382,0 @@ }

/// <reference types="mini-types" />
import { ChangeOptions } from '@goldfishjs/reactive';
export declare class Count {
export declare class Batch {
private segTotalList;

@@ -13,7 +13,7 @@ private counter;

private count;
private setDataObjectMap;
private spliceDataObjectMap;
private viewMap;
private updaterMap;
private getBatchUpdates;
private flush;
private setValue;
private setByKeyPathList;

@@ -20,0 +20,0 @@ set(view: View, keyPathList: (string | number)[], newV: any, oldV: any, options?: ChangeOptions): void;

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