deep-state-observer
Advanced tools
Comparing version 4.1.9 to 4.1.10
17
bench.js
@@ -6,2 +6,3 @@ const Benchmark = require("benchmark"); | ||
const height = 1000; | ||
const subs = 100; | ||
@@ -20,9 +21,21 @@ function getObj() { | ||
const item = `h${Math.round(height / 2)}.w${Math.round(width / 2)}`; | ||
function generateSubs(state) { | ||
for (let i = 0; i < subs; i++) { | ||
state.subscribe(item, () => { | ||
const x = 1 + Math.random(); | ||
}); | ||
} | ||
} | ||
const noProxyNoMaps = new State(getObj(), { useProxy: false, useObjectMaps: false }); | ||
generateSubs(noProxyNoMaps); | ||
const ProxyNoMaps = new State(getObj(), { useProxy: true, useObjectMaps: false }); | ||
generateSubs(ProxyNoMaps); | ||
const noProxyMaps = new State(getObj(), { useProxy: false, useObjectMaps: true }); | ||
generateSubs(noProxyMaps); | ||
const ProxyMaps = new State(getObj(), { useProxy: true, useObjectMaps: true }); | ||
generateSubs(ProxyMaps); | ||
const item = `h${Math.round(height / 2)}.w${Math.round(width / 2)}`; | ||
console.log("update & get"); | ||
@@ -29,0 +42,0 @@ new Benchmark.Suite("update get") |
@@ -185,2 +185,3 @@ export interface PathInfo { | ||
private handler; | ||
private objectMapOnlyHandler; | ||
proxy: object; | ||
@@ -204,4 +205,6 @@ /** | ||
private setProxy; | ||
private setProxyForMapOnly; | ||
private isProxy; | ||
private makeObservable; | ||
private makeProxyForMapOnly; | ||
loadWasmMatcher(pathToWasmFile: string): Promise<void>; | ||
@@ -208,0 +211,0 @@ private same; |
74
index.ts
@@ -331,2 +331,15 @@ import WildcardObject from "./wildcard-object-scan"; | ||
}; | ||
private objectMapOnlyHandler = { | ||
set(obj: any, prop, value) { | ||
if (prop === this.proxyProperty) return true; | ||
if (prop in obj && (this.same(obj[prop], value) || (this.isProxy(value) && obj[prop] === value))) { | ||
return true; | ||
} | ||
const path = obj[this.proxyProperty].path ? obj[this.proxyProperty].path + this.options.delimiter + prop : prop; | ||
this.updateMapDown(path, value, obj); | ||
obj[prop] = value; | ||
return true; | ||
}, | ||
}; | ||
public proxy: object; | ||
@@ -342,2 +355,3 @@ /** | ||
this.handler.set = this.handler.set.bind(this); | ||
this.objectMapOnlyHandler.set = this.objectMapOnlyHandler.set.bind(this); | ||
this.options = { ...getDefaultOptions(), ...options }; | ||
@@ -581,2 +595,20 @@ | ||
private setProxyForMapOnly(target: any, data: ProxyData) { | ||
if (!this.options.useObjectMaps) return target; | ||
if (typeof target[this.proxyProperty] === "undefined") { | ||
Object.defineProperty(target, this.proxyProperty, { | ||
enumerable: false, | ||
writable: false, | ||
configurable: false, | ||
value: data, | ||
}); | ||
return new Proxy(target, this.objectMapOnlyHandler); | ||
} else { | ||
for (const key in data) { | ||
target[this.proxyProperty][key] = data[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
private isProxy(target: any) { | ||
@@ -587,2 +619,3 @@ return typeof target[this.proxyProperty] !== "undefined"; | ||
private makeObservable(target: any, path: string, parent: ProxyNode) { | ||
if (!this.options.useProxy && this.options.useObjectMaps) return this.makeProxyForMapOnly(target, path, parent); | ||
if (!this.options.useProxy) return target; | ||
@@ -632,2 +665,43 @@ if (isObject(target) || Array.isArray(target)) { | ||
private makeProxyForMapOnly(target: any, path: string, parent: ProxyNode) { | ||
if (!this.options.useObjectMaps) return target; | ||
if (isObject(target) || Array.isArray(target)) { | ||
if (typeof target[this.proxyProperty] !== "undefined") { | ||
const pp = target[this.proxyProperty]; | ||
if (pp.path === path && pp.parent === parent) return target; | ||
} | ||
if (isObject(target)) { | ||
for (const key in target) { | ||
if (key === this.proxyProperty) continue; | ||
if ((isObject(target[key]) || Array.isArray(target[key])) && !this.isProxy(target[key])) { | ||
target[key] = this.makeProxyForMapOnly( | ||
target[key], | ||
`${path ? path + this.options.delimiter : ""}${key}`, | ||
target | ||
); | ||
} | ||
} | ||
} else { | ||
for (let key = 0, len = target.length; key < len; key++) { | ||
if ((isObject(target[key]) || Array.isArray(target[key])) && !this.isProxy(target[key])) { | ||
target[key] = this.makeProxyForMapOnly( | ||
target[key], | ||
`${path ? path + this.options.delimiter : ""}${key}`, | ||
target | ||
); | ||
} | ||
} | ||
} | ||
if (!this.isProxy(target)) { | ||
const proxyObj: ProxyData = Object.create(null); | ||
proxyObj.path = path; | ||
proxyObj.pathChunks = this.split(path); | ||
proxyObj.saving = []; | ||
proxyObj.parent = parent; | ||
target = this.setProxyForMapOnly(target, proxyObj); | ||
} | ||
} | ||
return target; | ||
} | ||
public async loadWasmMatcher(pathToWasmFile: string) { | ||
@@ -634,0 +708,0 @@ await init(pathToWasmFile); |
{ | ||
"name": "deep-state-observer", | ||
"version": "4.1.9", | ||
"version": "4.1.10", | ||
"description": "Deep state observer is an state management library that will fire listeners only when specified object node (which also can be a wildcard) was changed.", | ||
@@ -5,0 +5,0 @@ "main": "index.cjs.js", |
@@ -332,2 +332,21 @@ const State = require("../index.cjs.js"); | ||
it("should update map when we are changing update property (only maps)", () => { | ||
const state = new State({ x: { y: { z: 1 } } }, { useObjectMaps: true, useProxy: false }); | ||
const values = []; | ||
state.subscribe("x.y.z", (val) => { | ||
values.push(val); | ||
}); | ||
expect(values[0]).toEqual(1); | ||
state.update("", (oldValue) => { | ||
oldValue.xx = { yy: 10 }; | ||
return oldValue; | ||
}); | ||
expect(state.data.xx.yy).toEqual(10); | ||
expect(state.get("xx.yy")).toEqual(10); | ||
const root = state.get(""); | ||
root.oo = { ww: 10 }; | ||
expect(state.data.oo.ww).toEqual(10); | ||
expect(state.get("oo.ww")).toEqual(10); | ||
}); | ||
it("should save function", () => { | ||
@@ -334,0 +353,0 @@ const values = []; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
627558
15321