deep-state-observer
Advanced tools
Comparing version 5.5.3 to 5.5.4
@@ -36,2 +36,3 @@ export interface PathInfo { | ||
bulk?: boolean; | ||
bulkValue?: boolean; | ||
debug?: boolean; | ||
@@ -228,2 +229,3 @@ source?: string; | ||
private notifySubscribedListeners; | ||
private useBulkValue; | ||
private getNestedListeners; | ||
@@ -230,0 +232,0 @@ private notifyNestedListeners; |
66
index.ts
@@ -44,2 +44,3 @@ import WildcardObject from "./wildcard-object-scan"; | ||
bulk?: boolean; | ||
bulkValue?: boolean; | ||
debug?: boolean; | ||
@@ -245,2 +246,3 @@ source?: string; | ||
bulk: false, | ||
bulkValue: true, | ||
debug: false, | ||
@@ -1045,2 +1047,9 @@ source: "", | ||
private useBulkValue(listenersCollection: ListenersCollection) { | ||
for (const [listenerId, listener] of listenersCollection.listeners) { | ||
if (listener.options.bulkValue) return true; | ||
} | ||
return false; | ||
} | ||
private getNestedListeners( | ||
@@ -1061,18 +1070,51 @@ updatePath: string, | ||
listeners[listenerPath] = { single: [], bulk: [] }; | ||
// listener is listening below updated node | ||
const restBelowPathCut = this.trimPath(listenerPath.substr(currentAbovePathCut.length)); | ||
const wildcardNewValues = restBelowValues[restBelowPathCut] | ||
? restBelowValues[restBelowPathCut] // if those values are already calculated use it | ||
: new WildcardObject(newValue, this.options.delimiter, this.options.wildcard).get(restBelowPathCut); | ||
restBelowValues[restBelowPathCut] = wildcardNewValues; | ||
const useBulkValue = this.useBulkValue(listenersCollection); | ||
let wildcardNewValues; | ||
if (useBulkValue) { | ||
wildcardNewValues = restBelowValues[restBelowPathCut] | ||
? restBelowValues[restBelowPathCut] // if those values are already calculated use it | ||
: new WildcardObject(newValue, this.options.delimiter, this.options.wildcard).get(restBelowPathCut); | ||
restBelowValues[restBelowPathCut] = wildcardNewValues; | ||
} | ||
const params = listenersCollection.paramsInfo | ||
? this.getParams(listenersCollection.paramsInfo, updatePath) | ||
: undefined; | ||
const bulk: Bulk[] = []; | ||
const bulkListeners = {}; | ||
for (const currentRestPath in wildcardNewValues) { | ||
const value = () => wildcardNewValues[currentRestPath]; | ||
const fullPath = [updatePath, currentRestPath].join(this.options.delimiter); | ||
for (const [listenerId, listener] of listenersCollection.listeners) { | ||
for (const [listenerId, listener] of listenersCollection.listeners) { | ||
if (useBulkValue) { | ||
for (const currentRestPath in wildcardNewValues) { | ||
const value = () => wildcardNewValues[currentRestPath]; | ||
const fullPath = [updatePath, currentRestPath].join(this.options.delimiter); | ||
const eventInfo = { | ||
type, | ||
listener, | ||
listenersCollection, | ||
path: { | ||
listener: listenerPath, | ||
update: originalPath ? originalPath : updatePath, | ||
resolved: this.cleanNotRecursivePath(fullPath), | ||
}, | ||
params, | ||
options, | ||
}; | ||
if (this.shouldIgnore(listener, updatePath)) continue; | ||
if (listener.options.bulk) { | ||
bulk.push({ value, path: fullPath, params }); | ||
bulkListeners[listenerId] = listener; | ||
} else { | ||
listeners[listenerPath].single.push({ | ||
listener, | ||
listenersCollection, | ||
eventInfo, | ||
value, | ||
}); | ||
} | ||
} | ||
} else { | ||
const eventInfo = { | ||
@@ -1085,3 +1127,3 @@ type, | ||
update: originalPath ? originalPath : updatePath, | ||
resolved: this.cleanNotRecursivePath(fullPath), | ||
resolved: undefined, | ||
}, | ||
@@ -1093,3 +1135,3 @@ params, | ||
if (listener.options.bulk) { | ||
bulk.push({ value, path: fullPath, params }); | ||
bulk.push({ value: undefined, path: undefined, params }); | ||
bulkListeners[listenerId] = listener; | ||
@@ -1101,3 +1143,3 @@ } else { | ||
eventInfo, | ||
value, | ||
value: undefined, | ||
}); | ||
@@ -1104,0 +1146,0 @@ } |
{ | ||
"name": "deep-state-observer", | ||
"version": "5.5.3", | ||
"version": "5.5.4", | ||
"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", |
@@ -258,2 +258,71 @@ [![GitHub license](https://img.shields.io/github/license/neuronetio/deep-state-observer?style=flat-square)](https://github.com/neuronetio/deep-state-observer/blob/master/LICENSE) | ||
## Wildcard bulk without bulk values (even better performance) | ||
```javascript | ||
import { onDestroy } from "svelte"; | ||
import State from "deep-state-observer"; // const State = require('deep-state-observer'); | ||
// first parameter is an object that hold the state, and the second one is just options (optional - for now it hold just delimiter :P ) | ||
const state = new State({ | ||
byId: { | ||
1: { val: 1 }, | ||
2: { val: 2 }, | ||
3: { val: 3 }, | ||
}, | ||
}); | ||
// store some unsubscribe methods | ||
let subscribers = []; | ||
subscribers.push( | ||
state.subscribe( | ||
"byId.:id.val", | ||
(bulk, eventInfo) => { | ||
// fired only once where bulk = [ | ||
// { | ||
// value: undefined, | ||
// eventInfo.params: { id: 1 } | ||
// }, | ||
// { | ||
// value: undefined, | ||
// eventInfo.params: { id: 2 } | ||
// }, | ||
// { | ||
// value: 3, | ||
// eventInfo.params: { id: 3 } | ||
// }, | ||
// ] | ||
}, | ||
{ bulk: true, bulkValue: false } | ||
) | ||
); | ||
subscribers.push( | ||
state.subscribe( | ||
"byId.*.val", | ||
(bulk, eventInfo) => { | ||
// fired only once where bulk = [ | ||
// { | ||
// value: undefined, | ||
// eventInfo.params: undefined | ||
// }, | ||
// { | ||
// value: undefined, | ||
// eventInfo.params: undefined | ||
// }, | ||
// { | ||
// value: undefined, | ||
// eventInfo.params: undefined | ||
// }, | ||
// ] | ||
}, | ||
{ bulk: true, bulkValue: false } | ||
) | ||
); | ||
onDestroy(() => { | ||
subscribers.forEach((unsubscribe) => unsubscribe()); | ||
}); | ||
``` | ||
## Observe only chosen node changes (not recursive, not nested, for immutable data) | ||
@@ -260,0 +329,0 @@ |
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
558809
42
14344
661
3