deep-state-observer
Advanced tools
Comparing version 5.5.10 to 5.5.11
@@ -445,2 +445,3 @@ import WildcardObject from "./wildcard-object-scan"; | ||
private cutPath(longer: string, shorter: string): string { | ||
if (shorter === "") return ""; | ||
longer = this.cleanNotRecursivePath(longer); | ||
@@ -451,3 +452,3 @@ shorter = this.cleanNotRecursivePath(shorter); | ||
const longerParts = this.getIndicesOf(this.options.delimiter, longer); | ||
return longer.substr(0, longerParts[shorterPartsLen]); | ||
return longer.substring(0, longerParts[shorterPartsLen]); | ||
} | ||
@@ -1079,7 +1080,6 @@ | ||
if (!listenersCollection.isRecursive) continue; | ||
// listenerPath is longer and is shortened - because we want to get listeners underneath change | ||
// listenerPath may be longer and is shortened - because we want to get listeners underneath change | ||
const currentAbovePathCut = this.cutPath(listenerPath, updatePath); | ||
if (this.match(currentAbovePathCut, updatePath)) { | ||
listeners[listenerPath] = { single: [], bulk: [] }; | ||
// listener is listening below updated node | ||
@@ -1086,0 +1086,0 @@ const restBelowPathCut = this.trimPath(listenerPath.substr(currentAbovePathCut.length)); |
{ | ||
"name": "deep-state-observer", | ||
"version": "5.5.10", | ||
"version": "5.5.11", | ||
"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.esm.js", |
604
test.esm.js
// forked from https://github.com/joonhocho/superwild | ||
const segments = []; | ||
function Match(pattern, match, wchar = "*") { | ||
@@ -6,3 +7,3 @@ if (pattern === wchar) { | ||
} | ||
let segments = []; | ||
segments.length = 0; | ||
let starCount = 0; | ||
@@ -205,2 +206,5 @@ let minLength = 0; | ||
if (path.length === 0) { | ||
for (const key in obj) { | ||
delete obj[key]; | ||
} | ||
for (const key in value) { | ||
@@ -348,3 +352,3 @@ obj[key] = value[key]; | ||
only: [], | ||
source: '', | ||
source: "", | ||
debug: false, | ||
@@ -382,9 +386,2 @@ data: undefined, | ||
function log(message, info) { | ||
@@ -409,3 +406,6 @@ console.debug(message, info); | ||
queue: false, | ||
useCache: true, | ||
defaultBulkValue: true, | ||
useCache: false, | ||
useSplitCache: false, | ||
useIndicesCache: false, | ||
maxSimultaneousJobs: 1000, | ||
@@ -418,11 +418,2 @@ maxQueueRuns: 1000, | ||
const defaultListenerOptions = { | ||
bulk: false, | ||
debug: false, | ||
source: '', | ||
data: undefined, | ||
queue: false, | ||
group: false, | ||
}; | ||
/** | ||
@@ -436,5 +427,5 @@ * Is object - helper function to determine if specified variable is an object | ||
if (item && item.constructor) { | ||
return item.constructor.name === 'Object'; | ||
return item.constructor.name === "Object"; | ||
} | ||
return typeof item === 'object' && item !== null; | ||
return typeof item === "object" && item !== null; | ||
} | ||
@@ -449,9 +440,7 @@ | ||
__init() {this.jobsRunning = 0;} | ||
__init2() {this.updateQueue = [];} | ||
__init3() {this.subscribeQueue = [];} | ||
__init4() {this.listenersIgnoreCache | ||
= new WeakMap();} | ||
__init4() {this.listenersIgnoreCache = new WeakMap();} | ||
__init5() {this.is_match = null;} | ||
@@ -464,13 +453,16 @@ __init6() {this.destroyed = false;} | ||
__init8() {this.groupId = 0;} | ||
__init9() {this.traceId = 0;} | ||
__init9() {this.namedGroups = [];} | ||
__init10() {this.numberGroups = [];} | ||
__init11() {this.traceId = 0;} | ||
__init10() {this.traceMap = new Map();} | ||
__init11() {this.tracing = [];} | ||
__init12() {this.savedTrace = [];} | ||
__init13() {this.collection = null;} | ||
__init14() {this.collections = 0;} | ||
__init15() {this.cache = new Map();} | ||
__init12() {this.traceMap = new Map();} | ||
__init13() {this.tracing = [];} | ||
__init14() {this.savedTrace = [];} | ||
__init15() {this.collection = null;} | ||
__init16() {this.collections = 0;} | ||
__init17() {this.cache = new Map();} | ||
__init18() {this.splitCache = new Map();} | ||
constructor(data = {}, options = {}) {DeepState.prototype.__init.call(this);DeepState.prototype.__init2.call(this);DeepState.prototype.__init3.call(this);DeepState.prototype.__init4.call(this);DeepState.prototype.__init5.call(this);DeepState.prototype.__init6.call(this);DeepState.prototype.__init7.call(this);DeepState.prototype.__init8.call(this);DeepState.prototype.__init9.call(this);DeepState.prototype.__init10.call(this);DeepState.prototype.__init11.call(this);DeepState.prototype.__init12.call(this);DeepState.prototype.__init13.call(this);DeepState.prototype.__init14.call(this);DeepState.prototype.__init15.call(this);DeepState.prototype.__init16.call(this); | ||
constructor(data = {}, options = {}) {DeepState.prototype.__init.call(this);DeepState.prototype.__init2.call(this);DeepState.prototype.__init3.call(this);DeepState.prototype.__init4.call(this);DeepState.prototype.__init5.call(this);DeepState.prototype.__init6.call(this);DeepState.prototype.__init7.call(this);DeepState.prototype.__init8.call(this);DeepState.prototype.__init9.call(this);DeepState.prototype.__init10.call(this);DeepState.prototype.__init11.call(this);DeepState.prototype.__init12.call(this);DeepState.prototype.__init13.call(this);DeepState.prototype.__init14.call(this);DeepState.prototype.__init15.call(this);DeepState.prototype.__init16.call(this);DeepState.prototype.__init17.call(this);DeepState.prototype.__init18.call(this);DeepState.prototype.__init19.call(this);DeepState.prototype.__init20.call(this);DeepState.prototype.__init21.call(this); | ||
this.listeners = new Map(); | ||
@@ -496,15 +488,19 @@ this.waitingListeners = new Map(); | ||
this.mutedListeners = new Set(); | ||
this.scan = new WildcardObject( | ||
this.data, | ||
this.options.delimiter, | ||
this.options.wildcard | ||
); | ||
this.scan = new WildcardObject(this.data, this.options.delimiter, this.options.wildcard); | ||
this.destroyed = false; | ||
} | ||
cacheGet( | ||
pathChunks, | ||
data = this.data, | ||
create = false | ||
) { | ||
getDefaultListenerOptions() { | ||
return { | ||
bulk: false, | ||
bulkValue: this.options.defaultBulkValue, | ||
debug: false, | ||
source: "", | ||
data: undefined, | ||
queue: false, | ||
group: false, | ||
}; | ||
} | ||
cacheGet(pathChunks, data = this.data, create = false) { | ||
const path = pathChunks.join(this.options.delimiter); | ||
@@ -540,11 +536,16 @@ const weakRefValue = this.cache.get(path); | ||
/** | ||
* Silently update data | ||
* @param path string | ||
* @param value any | ||
* @returns | ||
*/ | ||
silentSet(path, value) { | ||
return this.pathSet(this.split(path), value, this.data); | ||
} | ||
async loadWasmMatcher(pathToWasmFile) { | ||
await init(pathToWasmFile); | ||
this.is_match = is_match; | ||
this.scan = new WildcardObject( | ||
this.data, | ||
this.options.delimiter, | ||
this.options.wildcard, | ||
this.is_match | ||
); | ||
this.scan = new WildcardObject(this.data, this.options.delimiter, this.options.wildcard, this.is_match); | ||
} | ||
@@ -554,4 +555,3 @@ | ||
return ( | ||
(['number', 'string', 'undefined', 'boolean'].includes(typeof newValue) || | ||
newValue === null) && | ||
(["number", "string", "undefined", "boolean"].includes(typeof newValue) || newValue === null) && | ||
oldValue === newValue | ||
@@ -574,15 +574,9 @@ ); | ||
match( | ||
first, | ||
second, | ||
nested = true | ||
) { | ||
match(first, second, nested = true) { | ||
if (this.is_match) return this.is_match(first, second); | ||
if (first === second) return true; | ||
if (first === this.options.wildcard || second === this.options.wildcard) | ||
return true; | ||
if (first === this.options.wildcard || second === this.options.wildcard) return true; | ||
if ( | ||
!nested && | ||
this.getIndicesCount(this.options.delimiter, first) < | ||
this.getIndicesCount(this.options.delimiter, second) | ||
this.getIndicesCount(this.options.delimiter, first) < this.getIndicesCount(this.options.delimiter, second) | ||
) { | ||
@@ -595,3 +589,5 @@ // first < second because first is a listener path and may be longer but not shorter | ||
__init19() {this.indices = new Map();} | ||
getIndicesOf(searchStr, str) { | ||
if (this.options.useIndicesCache && this.indices.has(str)) return this.indices.get(str); | ||
const searchStrLen = searchStr.length; | ||
@@ -608,6 +604,9 @@ if (searchStrLen == 0) { | ||
} | ||
if (this.options.useIndicesCache) this.indices.set(str, indices); | ||
return indices; | ||
} | ||
__init20() {this.indicesCount = new Map();} | ||
getIndicesCount(searchStr, str) { | ||
if (this.options.useIndicesCache && this.indicesCount.has(str)) return this.indicesCount.get(str); | ||
const searchStrLen = searchStr.length; | ||
@@ -624,2 +623,3 @@ if (searchStrLen == 0) { | ||
} | ||
if (this.options.useIndicesCache) this.indicesCount.set(str, indices); | ||
return indices; | ||
@@ -629,11 +629,9 @@ } | ||
cutPath(longer, shorter) { | ||
if (shorter === "") return ""; | ||
longer = this.cleanNotRecursivePath(longer); | ||
shorter = this.cleanNotRecursivePath(shorter); | ||
if (longer === shorter) return longer; | ||
const shorterPartsLen = this.getIndicesCount( | ||
this.options.delimiter, | ||
shorter | ||
); | ||
const shorterPartsLen = this.getIndicesCount(this.options.delimiter, shorter); | ||
const longerParts = this.getIndicesOf(this.options.delimiter, longer); | ||
return longer.substr(0, longerParts[shorterPartsLen]); | ||
return longer.substring(0, longerParts[shorterPartsLen]); | ||
} | ||
@@ -650,3 +648,13 @@ | ||
split(path) { | ||
return path === '' ? [] : path.split(this.options.delimiter); | ||
if (path === "") return []; | ||
if (!this.options.useSplitCache) { | ||
return path.split(this.options.delimiter); | ||
} | ||
const fromCache = this.splitCache.get(path); | ||
if (fromCache) { | ||
return fromCache.slice(); | ||
} | ||
const value = path.split(this.options.delimiter); | ||
this.splitCache.set(path, value.slice()); | ||
return value; | ||
} | ||
@@ -663,5 +671,3 @@ | ||
cleanNotRecursivePath(path) { | ||
return this.isNotRecursive(path) | ||
? path.substring(0, path.length - 1) | ||
: path; | ||
return this.isNotRecursive(path) ? path.substring(0, path.length - 1) : path; | ||
} | ||
@@ -674,3 +680,3 @@ | ||
getParamsInfo(path) { | ||
let paramsInfo = { replaced: '', original: path, params: {} }; | ||
let paramsInfo = { replaced: "", original: path, params: {} }; | ||
let partIndex = 0; | ||
@@ -681,9 +687,6 @@ let fullReplaced = []; | ||
original: part, | ||
replaced: '', | ||
name: '', | ||
replaced: "", | ||
name: "", | ||
}; | ||
const reg = new RegExp( | ||
`\\${this.options.param}([^\\${this.options.delimiter}\\${this.options.param}]+)`, | ||
'g' | ||
); | ||
const reg = new RegExp(`\\${this.options.param}([^\\${this.options.delimiter}\\${this.options.param}]+)`, "g"); | ||
let param = reg.exec(part); | ||
@@ -699,6 +702,3 @@ if (param) { | ||
reg.lastIndex = 0; | ||
paramsInfo.params[partIndex].replaced = part.replace( | ||
reg, | ||
this.options.wildcard | ||
); | ||
paramsInfo.params[partIndex].replaced = part.replace(reg, this.options.wildcard); | ||
fullReplaced.push(paramsInfo.params[partIndex].replaced); | ||
@@ -770,3 +770,3 @@ partIndex++; | ||
fn, | ||
options = defaultListenerOptions | ||
options = this.getDefaultListenerOptions() | ||
) { | ||
@@ -776,5 +776,10 @@ if (this.destroyed) return () => {}; | ||
let index = 0; | ||
if (options.group) { | ||
let groupId = null; | ||
if (typeof options.group === "boolean" && options.group) { | ||
this.groupId++; | ||
groupId = this.groupId; | ||
options.bulk = true; | ||
} else if (typeof options.group === "string") { | ||
options.bulk = true; | ||
groupId = options.group; | ||
} | ||
@@ -786,3 +791,3 @@ for (const userPath of userPaths) { | ||
index, | ||
groupId: options.group ? this.groupId : null, | ||
groupId, | ||
}) | ||
@@ -816,7 +821,7 @@ ); | ||
fn, | ||
options = defaultListenerOptions | ||
options = this.getDefaultListenerOptions() | ||
) { | ||
return { | ||
fn, | ||
options: { ...defaultListenerOptions, ...options }, | ||
options: { ...this.getDefaultListenerOptions(), ...options }, | ||
groupId: null, | ||
@@ -826,7 +831,3 @@ }; | ||
getListenerCollectionMatch( | ||
listenerPath, | ||
isRecursive, | ||
isWildcard | ||
) { | ||
getListenerCollectionMatch(listenerPath, isRecursive, isWildcard) { | ||
listenerPath = this.cleanNotRecursivePath(listenerPath); | ||
@@ -839,9 +840,6 @@ const self = this; | ||
} else { | ||
scopedListenerPath = self.cutPath( | ||
self.cleanNotRecursivePath(listenerPath), | ||
path | ||
); | ||
scopedListenerPath = self.cutPath(self.cleanNotRecursivePath(listenerPath), path); | ||
} | ||
if (debug) { | ||
console.log('[getListenerCollectionMatch]', { | ||
console.log("[getListenerCollectionMatch]", { | ||
listenerPath, | ||
@@ -854,4 +852,3 @@ scopedListenerPath, | ||
} | ||
if (isWildcard && self.match(scopedListenerPath, path, isRecursive)) | ||
return true; | ||
if (isWildcard && self.match(scopedListenerPath, path, isRecursive)) return true; | ||
return scopedListenerPath === path; | ||
@@ -861,6 +858,3 @@ }; | ||
getListenersCollection( | ||
listenerPath, | ||
listener | ||
) { | ||
getListenersCollection(listenerPath, listener) { | ||
if (this.listeners.has(listenerPath)) { | ||
@@ -890,7 +884,3 @@ let listenersCollection = this.listeners.get(listenerPath); | ||
...collCfg, | ||
match: this.getListenerCollectionMatch( | ||
collCfg.path, | ||
collCfg.isRecursive, | ||
collCfg.isWildcard | ||
), | ||
match: this.getListenerCollectionMatch(collCfg.path, collCfg.isRecursive, collCfg.isWildcard), | ||
}); | ||
@@ -907,7 +897,7 @@ this.id++; | ||
fn, | ||
options = defaultListenerOptions, | ||
options = this.getDefaultListenerOptions(), | ||
subscribeAllOptions = { | ||
all: [listenerPath ], | ||
index: 0, | ||
groupId: this.groupId, | ||
groupId: null, | ||
} | ||
@@ -917,19 +907,35 @@ ) { | ||
this.jobsRunning++; | ||
const type = 'subscribe'; | ||
const type = "subscribe"; | ||
let listener = this.getCleanListener(fn, options); | ||
if (options.group) listener.groupId = subscribeAllOptions.groupId; | ||
if (options.group) { | ||
options.bulk = true; | ||
if (typeof options.group === "string") { | ||
listener.groupId = options.group; | ||
} else if (subscribeAllOptions.groupId) { | ||
listener.groupId = subscribeAllOptions.groupId; | ||
} | ||
} | ||
this.listenersIgnoreCache.set(listener, { truthy: [], falsy: [] }); | ||
const listenersCollection = this.getListenersCollection( | ||
listenerPath , | ||
listener | ||
); | ||
const listenersCollection = this.getListenersCollection(listenerPath , listener); | ||
if (options.debug) { | ||
console.log('[subscribe]', { listenerPath, options }); | ||
console.log("[subscribe]", { listenerPath, options }); | ||
} | ||
listenersCollection.count++; | ||
if ( | ||
!options.group || | ||
(options.group && | ||
subscribeAllOptions.all.length - 1 === subscribeAllOptions.index) | ||
) { | ||
let shouldFire = true; | ||
if (listener.groupId) { | ||
if (typeof listener.groupId === "string") { | ||
if (this.namedGroups.includes(listener.groupId)) { | ||
shouldFire = false; | ||
} else { | ||
this.namedGroups.push(listener.groupId); | ||
} | ||
} else if (typeof listener.groupId === "number") { | ||
if (this.numberGroups.includes(listener.groupId)) { | ||
shouldFire = false; | ||
} else { | ||
this.numberGroups.push(listener.groupId); | ||
} | ||
} | ||
} | ||
if (shouldFire) { | ||
const cleanPath = this.cleanNotRecursivePath(listenersCollection.path); | ||
@@ -1029,3 +1035,3 @@ const cleanPathChunks = this.split(cleanPath); | ||
this.queueRuns = 0; | ||
throw new Error('Maximal number of queue runs exhausted.'); | ||
throw new Error("Maximal number of queue runs exhausted."); | ||
} else { | ||
@@ -1041,6 +1047,3 @@ Promise.resolve() | ||
getQueueNotifyListeners( | ||
groupedListeners, | ||
queue = [] | ||
) { | ||
getQueueNotifyListeners(groupedListeners, queue = []) { | ||
for (const path in groupedListeners) { | ||
@@ -1051,11 +1054,5 @@ if (this.isMuted(path)) continue; | ||
let alreadyInQueue = false; | ||
let resolvedIdPath = | ||
singleListener.listener.id + | ||
':' + | ||
singleListener.eventInfo.path.resolved; | ||
let resolvedIdPath = singleListener.listener.id + ":" + singleListener.eventInfo.path.resolved; | ||
if (!singleListener.eventInfo.path.resolved) { | ||
resolvedIdPath = | ||
singleListener.listener.id + | ||
':' + | ||
singleListener.eventInfo.path.listener; | ||
resolvedIdPath = singleListener.listener.id + ":" + singleListener.eventInfo.path.listener; | ||
} | ||
@@ -1076,3 +1073,3 @@ for (const excludedListener of queue) { | ||
singleListener.listener.fn( | ||
singleListener.value(), | ||
singleListener.value ? singleListener.value() : undefined, | ||
singleListener.eventInfo | ||
@@ -1082,11 +1079,5 @@ ); | ||
} else { | ||
let resolvedIdPath = | ||
singleListener.listener.id + | ||
':' + | ||
singleListener.eventInfo.path.resolved; | ||
let resolvedIdPath = singleListener.listener.id + ":" + singleListener.eventInfo.path.resolved; | ||
if (!singleListener.eventInfo.path.resolved) { | ||
resolvedIdPath = | ||
singleListener.listener.id + | ||
':' + | ||
singleListener.eventInfo.path.listener; | ||
resolvedIdPath = singleListener.listener.id + ":" + singleListener.eventInfo.path.listener; | ||
} | ||
@@ -1100,3 +1091,3 @@ queue.push({ | ||
singleListener.listener.fn( | ||
singleListener.value(), | ||
singleListener.value ? singleListener.value() : undefined, | ||
singleListener.eventInfo | ||
@@ -1125,3 +1116,3 @@ ); | ||
for (const bulk of bulkListener.value) { | ||
bulkValue.push({ ...bulk, value: bulk.value() }); | ||
bulkValue.push({ ...bulk, value: bulk.value ? bulk.value() : undefined }); | ||
} | ||
@@ -1138,11 +1129,5 @@ if (!this.isMuted(bulkListener.listener.fn)) { | ||
} else { | ||
let resolvedIdPath = | ||
bulkListener.listener.id + | ||
':' + | ||
bulkListener.eventInfo.path.resolved; | ||
let resolvedIdPath = bulkListener.listener.id + ":" + bulkListener.eventInfo.path.resolved; | ||
if (!bulkListener.eventInfo.path.resolved) { | ||
resolvedIdPath = | ||
bulkListener.listener.id + | ||
':' + | ||
bulkListener.eventInfo.path.listener; | ||
resolvedIdPath = bulkListener.listener.id + ":" + bulkListener.eventInfo.path.listener; | ||
} | ||
@@ -1191,3 +1176,3 @@ queue.push({ | ||
options, | ||
type = 'update', | ||
type = "update", | ||
originalPath = null | ||
@@ -1198,4 +1183,4 @@ ) { | ||
for (let [listenerPath, listenersCollection] of this.listeners) { | ||
listeners[listenerPath] = { single: [], bulk: [], bulkData: [] }; | ||
if (listenersCollection.match(updatePath)) { | ||
listeners[listenerPath] = { single: [], bulk: [], bulkData: [] }; | ||
const params = listenersCollection.paramsInfo | ||
@@ -1205,4 +1190,3 @@ ? this.getParams(listenersCollection.paramsInfo, updatePath) | ||
const cutPath = this.cutPath(updatePath, listenerPath); | ||
const traverse = | ||
listenersCollection.isRecursive || listenersCollection.isWildcard; | ||
const traverse = listenersCollection.isRecursive || listenersCollection.isWildcard; | ||
const value = traverse ? () => this.get(cutPath) : () => newValue; | ||
@@ -1213,9 +1197,6 @@ const bulkValue = [{ value, path: updatePath, params }]; | ||
if (listener.options.debug) { | ||
console.log( | ||
`[getSubscribedListeners] Listener was not fired because it was ignored.`, | ||
{ | ||
listener, | ||
listenersCollection, | ||
} | ||
); | ||
console.log(`[getSubscribedListeners] Listener was not fired because it was ignored.`, { | ||
listener, | ||
listenersCollection, | ||
}); | ||
} | ||
@@ -1266,10 +1247,7 @@ continue; | ||
showMatch = true; | ||
console.log( | ||
`[getSubscribedListeners] Listener was not fired because there was no match.`, | ||
{ | ||
listener, | ||
listenersCollection, | ||
updatePath, | ||
} | ||
); | ||
console.log(`[getSubscribedListeners] Listener was not fired because there was no match.`, { | ||
listener, | ||
listenersCollection, | ||
updatePath, | ||
}); | ||
} | ||
@@ -1289,16 +1267,16 @@ } | ||
options, | ||
type = 'update', | ||
type = "update", | ||
originalPath = null | ||
) { | ||
return this.getQueueNotifyListeners( | ||
this.getSubscribedListeners( | ||
updatePath, | ||
newValue, | ||
options, | ||
type, | ||
originalPath | ||
) | ||
); | ||
return this.getQueueNotifyListeners(this.getSubscribedListeners(updatePath, newValue, options, type, originalPath)); | ||
} | ||
useBulkValue(listenersCollection) { | ||
for (const [listenerId, listener] of listenersCollection.listeners) { | ||
if (listener.options.bulk && listener.options.bulkValue) return true; | ||
if (!listener.options.bulk) return true; | ||
} | ||
return false; | ||
} | ||
getNestedListeners( | ||
@@ -1308,3 +1286,3 @@ updatePath, | ||
options, | ||
type = 'update', | ||
type = "update", | ||
originalPath = null | ||
@@ -1316,30 +1294,54 @@ ) { | ||
if (!listenersCollection.isRecursive) continue; | ||
listeners[listenerPath] = { single: [], bulk: [] }; | ||
// listenerPath is longer and is shortened - because we want to get listeners underneath change | ||
// listenerPath may be longer and is shortened - because we want to get listeners underneath change | ||
const currentAbovePathCut = this.cutPath(listenerPath, updatePath); | ||
if (this.match(currentAbovePathCut, updatePath)) { | ||
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 restBelowPathCut = this.trimPath(listenerPath.substr(currentAbovePathCut.length)); | ||
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 = []; | ||
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 = { | ||
@@ -1352,3 +1354,3 @@ type, | ||
update: originalPath ? originalPath : updatePath, | ||
resolved: this.cleanNotRecursivePath(fullPath), | ||
resolved: undefined, | ||
}, | ||
@@ -1360,3 +1362,3 @@ params, | ||
if (listener.options.bulk) { | ||
bulk.push({ value, path: fullPath, params }); | ||
bulk.push({ value: undefined, path: undefined, params }); | ||
bulkListeners[listenerId] = listener; | ||
@@ -1368,3 +1370,3 @@ } else { | ||
eventInfo, | ||
value, | ||
value: undefined, | ||
}); | ||
@@ -1399,11 +1401,8 @@ } | ||
if (listener.options.debug) { | ||
console.log( | ||
'[getNestedListeners] Listener was not fired because there was no match.', | ||
{ | ||
listener, | ||
listenersCollection, | ||
currentCutPath: currentAbovePathCut, | ||
updatePath, | ||
} | ||
); | ||
console.log("[getNestedListeners] Listener was not fired because there was no match.", { | ||
listener, | ||
listenersCollection, | ||
currentCutPath: currentAbovePathCut, | ||
updatePath, | ||
}); | ||
} | ||
@@ -1420,3 +1419,3 @@ } | ||
options, | ||
type = 'update', | ||
type = "update", | ||
queue, | ||
@@ -1426,9 +1425,3 @@ originalPath = null | ||
return this.getQueueNotifyListeners( | ||
this.getNestedListeners( | ||
updatePath, | ||
newValue, | ||
options, | ||
type, | ||
originalPath | ||
), | ||
this.getNestedListeners(updatePath, newValue, options, type, originalPath), | ||
queue | ||
@@ -1442,3 +1435,3 @@ ); | ||
options, | ||
type = 'update', | ||
type = "update", | ||
originalPath = null | ||
@@ -1448,5 +1441,5 @@ ) { | ||
if ( | ||
typeof options.only !== 'object' || | ||
typeof options.only !== "object" || | ||
!Array.isArray(options.only) || | ||
typeof options.only[0] === 'undefined' || | ||
typeof options.only[0] === "undefined" || | ||
!this.canBeNested(newValue) | ||
@@ -1457,7 +1450,5 @@ ) { | ||
for (const notifyPath of options.only) { | ||
const wildcardScanNewValue = new WildcardObject( | ||
newValue, | ||
this.options.delimiter, | ||
this.options.wildcard | ||
).get(notifyPath); | ||
const wildcardScanNewValue = new WildcardObject(newValue, this.options.delimiter, this.options.wildcard).get( | ||
notifyPath | ||
); | ||
listeners[notifyPath] = { bulk: [], single: [] }; | ||
@@ -1488,7 +1479,3 @@ for (const wildcardPath in wildcardScanNewValue) { | ||
if (listener.options.bulk) { | ||
if ( | ||
!listeners[notifyPath].bulk.some( | ||
(bulkListener) => bulkListener.listener === listener | ||
) | ||
) { | ||
if (!listeners[notifyPath].bulk.some((bulkListener) => bulkListener.listener === listener)) { | ||
listeners[notifyPath].bulk.push({ | ||
@@ -1545,13 +1532,7 @@ listener, | ||
options, | ||
type = 'update', | ||
originalPath = '' | ||
type = "update", | ||
originalPath = "" | ||
) { | ||
const queue = this.getQueueNotifyListeners( | ||
this.getNotifyOnlyListeners( | ||
updatePath, | ||
newValue, | ||
options, | ||
type, | ||
originalPath | ||
) | ||
this.getNotifyOnlyListeners(updatePath, newValue, options, type, originalPath) | ||
); | ||
@@ -1562,3 +1543,3 @@ this.sortAndRunQueue(queue, updatePath); | ||
canBeNested(newValue) { | ||
return typeof newValue === 'object' && newValue !== null; | ||
return typeof newValue === "object" && newValue !== null; | ||
} | ||
@@ -1568,3 +1549,3 @@ | ||
let newValue = fn; | ||
if (typeof fn === 'function') { | ||
if (typeof fn === "function") { | ||
newValue = fn(oldValue); | ||
@@ -1611,34 +1592,10 @@ } | ||
if (options.only.length) { | ||
groupedListenersPack.push( | ||
this.getNotifyOnlyListeners( | ||
path, | ||
newValue, | ||
options, | ||
'update', | ||
updatePath | ||
) | ||
); | ||
groupedListenersPack.push(this.getNotifyOnlyListeners(path, newValue, options, "update", updatePath)); | ||
} else { | ||
groupedListenersPack.push( | ||
this.getSubscribedListeners( | ||
path, | ||
newValue, | ||
options, | ||
'update', | ||
updatePath | ||
) | ||
); | ||
groupedListenersPack.push(this.getSubscribedListeners(path, newValue, options, "update", updatePath)); | ||
if (this.canBeNested(newValue)) { | ||
groupedListenersPack.push( | ||
this.getNestedListeners( | ||
path, | ||
newValue, | ||
options, | ||
'update', | ||
updatePath | ||
) | ||
); | ||
groupedListenersPack.push(this.getNestedListeners(path, newValue, options, "update", updatePath)); | ||
} | ||
} | ||
options.debug && this.options.log('Wildcard update', { path, newValue }); | ||
options.debug && this.options.log("Wildcard update", { path, newValue }); | ||
waitingPaths.push(path); | ||
@@ -1659,31 +1616,13 @@ } | ||
if (this.destroyed) return; | ||
while ( | ||
this.updateQueue.length && | ||
this.updateQueue.length < this.options.maxSimultaneousJobs | ||
) { | ||
while (this.updateQueue.length && this.updateQueue.length < this.options.maxSimultaneousJobs) { | ||
const params = this.updateQueue.shift(); | ||
params.options.queue = false; // prevent infinite loop | ||
this.update( | ||
params.updatePath, | ||
params.fnOrValue, | ||
params.options, | ||
params.multi | ||
); | ||
this.update(params.updatePath, params.fnOrValue, params.options, params.multi); | ||
} | ||
} | ||
updateNotify( | ||
updatePath, | ||
newValue, | ||
options | ||
) { | ||
updateNotify(updatePath, newValue, options) { | ||
const queue = this.notifySubscribedListeners(updatePath, newValue, options); | ||
if (this.canBeNested(newValue)) { | ||
this.notifyNestedListeners( | ||
updatePath, | ||
newValue, | ||
options, | ||
'update', | ||
queue | ||
); | ||
this.notifyNestedListeners(updatePath, newValue, options, "update", queue); | ||
} | ||
@@ -1709,17 +1648,5 @@ this.sortAndRunQueue(queue, updatePath); | ||
} | ||
queue = queue.concat( | ||
this.notifySubscribedListeners( | ||
current.updatePath, | ||
value, | ||
current.options | ||
) | ||
); | ||
queue = queue.concat(this.notifySubscribedListeners(current.updatePath, value, current.options)); | ||
if (this.canBeNested(current.newValue)) { | ||
this.notifyNestedListeners( | ||
current.updatePath, | ||
value, | ||
current.options, | ||
'update', | ||
queue | ||
); | ||
this.notifyNestedListeners(current.updatePath, value, current.options, "update", queue); | ||
} | ||
@@ -1754,3 +1681,3 @@ } | ||
if (jobsRunning > this.options.maxSimultaneousJobs) { | ||
throw new Error('Maximal simultaneous jobs limit reached.'); | ||
throw new Error("Maximal simultaneous jobs limit reached."); | ||
} | ||
@@ -1776,11 +1703,6 @@ this.updateQueue.push({ updatePath, fnOrValue, options, multi }); | ||
if (options.debug) { | ||
this.options.log( | ||
`Updating ${updatePath} ${ | ||
options.source ? `from ${options.source}` : '' | ||
}`, | ||
{ | ||
oldValue, | ||
newValue, | ||
} | ||
); | ||
this.options.log(`Updating ${updatePath} ${options.source ? `from ${options.source}` : ""}`, { | ||
oldValue, | ||
newValue, | ||
}); | ||
} | ||
@@ -1846,7 +1768,3 @@ | ||
const multiObject = { | ||
update( | ||
updatePath, | ||
fnOrValue, | ||
options = defaultUpdateOptions | ||
) { | ||
update(updatePath, fnOrValue, options = defaultUpdateOptions) { | ||
if (grouped) { | ||
@@ -1856,3 +1774,3 @@ const split = self.split(updatePath); | ||
const currentValue = self.pathGet(split, self.data); | ||
if (typeof value === 'function') { | ||
if (typeof value === "function") { | ||
value = value(currentValue); | ||
@@ -1913,5 +1831,5 @@ } | ||
get(userPath) { | ||
get(userPath = undefined) { | ||
if (this.destroyed) return; | ||
if (typeof userPath === 'undefined' || userPath === '') { | ||
if (userPath === undefined || userPath === "") { | ||
return this.data; | ||
@@ -1925,3 +1843,3 @@ } | ||
__init16() {this.lastExecs = new WeakMap();} | ||
__init21() {this.lastExecs = new WeakMap();} | ||
last(callback) { | ||
@@ -1944,3 +1862,3 @@ let last = this.lastExecs.get(callback); | ||
if (!this.options.useMute) return false; | ||
if (typeof pathOrListenerFunction === 'function') { | ||
if (typeof pathOrListenerFunction === "function") { | ||
return this.isMutedListener(pathOrListenerFunction); | ||
@@ -1967,3 +1885,3 @@ } | ||
mute(pathOrListenerFunction) { | ||
if (typeof pathOrListenerFunction === 'function') { | ||
if (typeof pathOrListenerFunction === "function") { | ||
return this.mutedListeners.add(pathOrListenerFunction); | ||
@@ -1975,3 +1893,3 @@ } | ||
unmute(pathOrListenerFunction) { | ||
if (typeof pathOrListenerFunction === 'function') { | ||
if (typeof pathOrListenerFunction === "function") { | ||
return this.mutedListeners.delete(pathOrListenerFunction); | ||
@@ -1982,9 +1900,5 @@ } | ||
debugSubscribe( | ||
listener, | ||
listenersCollection, | ||
listenerPath | ||
) { | ||
debugSubscribe(listener, listenersCollection, listenerPath) { | ||
if (listener.options.debug) { | ||
this.options.log('listener subscribed', { | ||
this.options.log("listener subscribed", { | ||
listenerPath, | ||
@@ -1998,7 +1912,4 @@ listener, | ||
debugListener(time, groupedListener) { | ||
if ( | ||
groupedListener.eventInfo.options.debug || | ||
groupedListener.listener.options.debug | ||
) { | ||
this.options.log('Listener fired', { | ||
if (groupedListener.eventInfo.options.debug || groupedListener.listener.options.debug) { | ||
this.options.log("Listener fired", { | ||
time: Date.now() - time, | ||
@@ -2011,6 +1922,3 @@ info: groupedListener, | ||
debugTime(groupedListener) { | ||
return groupedListener.listener.options.debug || | ||
groupedListener.eventInfo.options.debug | ||
? Date.now() | ||
: 0; | ||
return groupedListener.listener.options.debug || groupedListener.eventInfo.options.debug ? Date.now() : 0; | ||
} | ||
@@ -2020,3 +1928,3 @@ | ||
this.traceId++; | ||
const id = this.traceId + ':' + name; | ||
const id = this.traceId + ":" + name; | ||
this.traceMap.set(id, { | ||
@@ -2023,0 +1931,0 @@ id, |
@@ -1479,3 +1479,3 @@ const State = require("../index.cjs.js"); | ||
const values = []; | ||
state.subscribe("", (val) => { | ||
const unsub = state.subscribe("", (val) => { | ||
values.push(JSON.stringify(val)); | ||
@@ -1488,3 +1488,7 @@ }); | ||
expect(values[1]).toEqual(JSON.stringify({ a: { b: { c: "c" } } })); | ||
values.length = 0; | ||
}); | ||
it("should notify listeners when full state was replaced", () => { | ||
const state = new State({ a: { b: { c: "c" } } }); | ||
const values = []; | ||
state.subscribe("a.b", (val) => { | ||
@@ -1498,3 +1502,6 @@ values.push(JSON.stringify(val)); | ||
expect(values[1]).toEqual(JSON.stringify({ c: "cc" })); | ||
state.update("", { a: { b: { c: "ccc" } } }); | ||
expect(values.length).toEqual(3); | ||
expect(values[2]).toEqual(JSON.stringify({ c: "ccc" })); | ||
}); | ||
}); |
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
43
524528
14388