deep-state-observer
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -411,2 +411,3 @@ 'use strict'; | ||
this.proxyPath = []; | ||
//public subscribe: typeof sub; | ||
this.handler = { | ||
@@ -778,13 +779,13 @@ get: (obj, prop) => { | ||
const paths = this.scan.get(cleanPath); | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) | ||
continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (options.bulk) { | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) | ||
continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (!this.isMuted(fn)) { | ||
@@ -1422,3 +1423,3 @@ fn(bulkValue, { | ||
} | ||
get(userPath = undefined) { | ||
get(userPath) { | ||
if (this.destroyed) | ||
@@ -1425,0 +1426,0 @@ return; |
@@ -6,6 +6,6 @@ export interface PathInfo { | ||
} | ||
export interface ListenerFunctionEventInfo { | ||
export interface ListenerFunctionEventInfo<T> { | ||
type: string; | ||
listener: Listener; | ||
listenersCollection: ListenersCollection; | ||
listener: Listener<T>; | ||
listenersCollection: ListenersCollection<T>; | ||
path: PathInfo; | ||
@@ -15,3 +15,3 @@ params: Params; | ||
} | ||
export declare type ListenerFunction = (value: any, eventInfo: ListenerFunctionEventInfo) => void; | ||
export declare type ListenerFunction<T> = (value: T, eventInfo: ListenerFunctionEventInfo<T>) => void; | ||
export declare type Match = (path: string, debug?: boolean) => boolean; | ||
@@ -48,4 +48,4 @@ export interface Options { | ||
} | ||
export interface Listener { | ||
fn: ListenerFunction; | ||
export interface Listener<T> { | ||
fn: ListenerFunction<T>; | ||
options: ListenerOptions; | ||
@@ -55,3 +55,3 @@ id?: number; | ||
} | ||
export interface Queue { | ||
export interface Queue<T> { | ||
id: number; | ||
@@ -61,25 +61,25 @@ resolvedPath: string; | ||
fn: () => void; | ||
originalFn: ListenerFunction; | ||
originalFn: ListenerFunction<T>; | ||
options: ListenerOptions; | ||
groupId: number; | ||
} | ||
export interface GroupedListener { | ||
listener: Listener; | ||
listenersCollection: ListenersCollection; | ||
eventInfo: ListenerFunctionEventInfo; | ||
export interface GroupedListener<T> { | ||
listener: Listener<PathValue<T, PossiblePath<T>>>; | ||
listenersCollection: ListenersCollection<T>; | ||
eventInfo: ListenerFunctionEventInfo<T>; | ||
value: any; | ||
} | ||
export interface GroupedListenerContainer { | ||
single: GroupedListener[]; | ||
bulk: GroupedListener[]; | ||
export interface GroupedListenerContainer<T> { | ||
single: GroupedListener<T>[]; | ||
bulk: GroupedListener<T>[]; | ||
} | ||
export interface GroupedListeners { | ||
[path: string]: GroupedListenerContainer; | ||
export interface GroupedListeners<T> { | ||
[path: string]: GroupedListenerContainer<T>; | ||
} | ||
export declare type Updater = (value: any) => any; | ||
export declare type ListenersObject = Map<string | number, Listener>; | ||
export interface ListenersCollection { | ||
export declare type ListenersObject<T> = Map<string | number, Listener<T>>; | ||
export interface ListenersCollection<T> { | ||
path: string; | ||
originalPath: string; | ||
listeners: ListenersObject; | ||
listeners: ListenersObject<PathValue<T, PossiblePath<T>>>; | ||
isWildcard: boolean; | ||
@@ -92,3 +92,3 @@ isRecursive: boolean; | ||
} | ||
export declare type Listeners = Map<string, ListenersCollection>; | ||
export declare type Listeners<T> = Map<string, ListenersCollection<T>>; | ||
export interface WaitingPath { | ||
@@ -147,2 +147,5 @@ dirty: boolean; | ||
} | ||
declare type PathImpl<T, K extends keyof T> = K extends string ? T[K] extends Record<string, any> ? T[K] extends ArrayLike<any> ? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}` : K | `${K}.${PathImpl<T[K], keyof T[K]>}` : K : any; | ||
declare type PossiblePath<T> = PathImpl<T, keyof T> | keyof T | string; | ||
declare type PathValue<T, P extends PossiblePath<T>> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? Rest extends PossiblePath<T[K]> ? PathValue<T[K], Rest> : any : any : P extends keyof T ? T[P] : any; | ||
export interface UnknownObject { | ||
@@ -184,3 +187,3 @@ [key: string]: unknown; | ||
private same; | ||
getListeners(): Listeners; | ||
getListeners(): Listeners<T>; | ||
destroy(): void; | ||
@@ -199,3 +202,3 @@ private match; | ||
private getParams; | ||
subscribeAll(userPaths: string[], fn: ListenerFunction | WaitingListenerFunction, options?: ListenerOptions): () => void; | ||
subscribeAll(userPaths: string[], fn: ListenerFunction<PathValue<T, PossiblePath<T>>>, options?: ListenerOptions): () => void; | ||
private getCleanListenersCollection; | ||
@@ -205,3 +208,3 @@ private getCleanListener; | ||
private getListenersCollection; | ||
subscribe(listenerPath: string, fn: ListenerFunction, options?: ListenerOptions, subscribeAllOptions?: SubscribeAllOptions): () => void; | ||
subscribe<P extends PossiblePath<T>>(listenerPath: P, fn: ListenerFunction<PathValue<T, P>>, options?: ListenerOptions, subscribeAllOptions?: SubscribeAllOptions): () => void; | ||
private unsubscribe; | ||
@@ -232,9 +235,9 @@ private runQueuedListeners; | ||
getCollectedStack(): UpdateStack[]; | ||
get(userPath?: string | undefined): any; | ||
get<P extends PossiblePath<T>>(userPath: P | string): PathValue<T, P>; | ||
private lastExecs; | ||
last(callback: () => void): void; | ||
isMuted(pathOrListenerFunction: string | ListenerFunction): boolean; | ||
isMutedListener(listenerFunc: ListenerFunction): boolean; | ||
mute(pathOrListenerFunction: string | ListenerFunction): Set<ListenerFunction>; | ||
unmute(pathOrListenerFunction: string | ListenerFunction): boolean; | ||
isMuted(pathOrListenerFunction: string | ListenerFunction<PathValue<T, PossiblePath<T>>>): boolean; | ||
isMutedListener(listenerFunc: ListenerFunction<PathValue<T, PossiblePath<T>>>): boolean; | ||
mute(pathOrListenerFunction: string | ListenerFunction<PathValue<T, PossiblePath<T>>>): Set<ListenerFunction<(string extends keyof T ? T[keyof T & string] : any) | PathValue<T, keyof T> | PathValue<T, PathImpl<T, keyof T>>>>; | ||
unmute(pathOrListenerFunction: string | ListenerFunction<PathValue<T, PossiblePath<T>>>): boolean; | ||
private debugSubscribe; | ||
@@ -241,0 +244,0 @@ private debugListener; |
@@ -409,2 +409,3 @@ /*! ***************************************************************************** | ||
this.proxyPath = []; | ||
//public subscribe: typeof sub; | ||
this.handler = { | ||
@@ -776,13 +777,13 @@ get: (obj, prop) => { | ||
const paths = this.scan.get(cleanPath); | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) | ||
continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (options.bulk) { | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) | ||
continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (!this.isMuted(fn)) { | ||
@@ -1420,3 +1421,3 @@ fn(bulkValue, { | ||
} | ||
get(userPath = undefined) { | ||
get(userPath) { | ||
if (this.destroyed) | ||
@@ -1423,0 +1424,0 @@ return; |
177
index.ts
@@ -11,6 +11,6 @@ import WildcardObject from "./wildcard-object-scan"; | ||
export interface ListenerFunctionEventInfo { | ||
export interface ListenerFunctionEventInfo<T> { | ||
type: string; | ||
listener: Listener; | ||
listenersCollection: ListenersCollection; | ||
listener: Listener<T>; | ||
listenersCollection: ListenersCollection<T>; | ||
path: PathInfo; | ||
@@ -21,3 +21,3 @@ params: Params; | ||
export type ListenerFunction = (value: any, eventInfo: ListenerFunctionEventInfo) => void; | ||
export type ListenerFunction<T> = (value: T, eventInfo: ListenerFunctionEventInfo<T>) => void; | ||
export type Match = (path: string, debug?: boolean) => boolean; | ||
@@ -58,4 +58,4 @@ | ||
export interface Listener { | ||
fn: ListenerFunction; | ||
export interface Listener<T> { | ||
fn: ListenerFunction<T>; | ||
options: ListenerOptions; | ||
@@ -66,3 +66,3 @@ id?: number; | ||
export interface Queue { | ||
export interface Queue<T> { | ||
id: number; | ||
@@ -72,3 +72,3 @@ resolvedPath: string; | ||
fn: () => void; | ||
originalFn: ListenerFunction; | ||
originalFn: ListenerFunction<T>; | ||
options: ListenerOptions; | ||
@@ -78,16 +78,16 @@ groupId: number; | ||
export interface GroupedListener { | ||
listener: Listener; | ||
listenersCollection: ListenersCollection; | ||
eventInfo: ListenerFunctionEventInfo; | ||
export interface GroupedListener<T> { | ||
listener: Listener<PathValue<T, PossiblePath<T>>>; | ||
listenersCollection: ListenersCollection<T>; | ||
eventInfo: ListenerFunctionEventInfo<T>; | ||
value: any; | ||
} | ||
export interface GroupedListenerContainer { | ||
single: GroupedListener[]; | ||
bulk: GroupedListener[]; | ||
export interface GroupedListenerContainer<T> { | ||
single: GroupedListener<T>[]; | ||
bulk: GroupedListener<T>[]; | ||
} | ||
export interface GroupedListeners { | ||
[path: string]: GroupedListenerContainer; | ||
export interface GroupedListeners<T> { | ||
[path: string]: GroupedListenerContainer<T>; | ||
} | ||
@@ -97,8 +97,8 @@ | ||
export type ListenersObject = Map<string | number, Listener>; | ||
export type ListenersObject<T> = Map<string | number, Listener<T>>; | ||
export interface ListenersCollection { | ||
export interface ListenersCollection<T> { | ||
path: string; | ||
originalPath: string; | ||
listeners: ListenersObject; | ||
listeners: ListenersObject<PathValue<T, PossiblePath<T>>>; | ||
isWildcard: boolean; | ||
@@ -112,3 +112,3 @@ isRecursive: boolean; | ||
export type Listeners = Map<string, ListenersCollection>; | ||
export type Listeners<T> = Map<string, ListenersCollection<T>>; | ||
@@ -189,2 +189,22 @@ export interface WaitingPath { | ||
type PathImpl<T, K extends keyof T> = K extends string | ||
? T[K] extends Record<string, any> | ||
? T[K] extends ArrayLike<any> | ||
? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}` | ||
: K | `${K}.${PathImpl<T[K], keyof T[K]>}` | ||
: K | ||
: any; | ||
type PossiblePath<T> = PathImpl<T, keyof T> | keyof T | string; | ||
type PathValue<T, P extends PossiblePath<T>> = P extends `${infer K}.${infer Rest}` | ||
? K extends keyof T | ||
? Rest extends PossiblePath<T[K]> | ||
? PathValue<T[K], Rest> | ||
: any | ||
: any | ||
: P extends keyof T | ||
? T[P] | ||
: any; | ||
function log(message: string, info: any) { | ||
@@ -234,3 +254,3 @@ console.debug(message, info); | ||
class DeepState<T> { | ||
private listeners: Listeners; | ||
private listeners: Listeners<PathValue<T, PossiblePath<T>> | string>; | ||
private data: T | object; | ||
@@ -243,3 +263,6 @@ private options: Options; | ||
private subscribeQueue = []; | ||
private listenersIgnoreCache: WeakMap<Listener, { truthy: string[]; falsy: string[] }> = new WeakMap(); | ||
private listenersIgnoreCache: WeakMap< | ||
Listener<PathValue<T, PossiblePath<T>>>, | ||
{ truthy: string[]; falsy: string[] } | ||
> = new WeakMap(); | ||
private is_match: any; | ||
@@ -249,3 +272,3 @@ private destroyed = false; | ||
private muted: Set<string>; | ||
private mutedListeners: Set<ListenerFunction>; | ||
private mutedListeners: Set<ListenerFunction<PathValue<T, PossiblePath<T>>>>; | ||
private groupId: number = 0; | ||
@@ -259,2 +282,3 @@ private traceId: number = 0; | ||
private proxyPath = []; | ||
//public subscribe: typeof sub; | ||
private handler = { | ||
@@ -382,3 +406,3 @@ get: (obj, prop) => { | ||
public getListeners(): Listeners { | ||
public getListeners(): Listeners<T> { | ||
return this.listeners; | ||
@@ -518,3 +542,3 @@ } | ||
userPaths: string[], | ||
fn: ListenerFunction | WaitingListenerFunction, | ||
fn: ListenerFunction<PathValue<T, PossiblePath<T>>>, | ||
options: ListenerOptions = defaultListenerOptions | ||
@@ -531,3 +555,3 @@ ) { | ||
unsubscribers.push( | ||
this.subscribe(userPath, fn, options, { | ||
this.subscribe(userPath as never, fn, options, { | ||
all: userPaths, | ||
@@ -547,3 +571,3 @@ index, | ||
private getCleanListenersCollection(values = {}): ListenersCollection { | ||
private getCleanListenersCollection(values = {}): ListenersCollection<T> { | ||
return { | ||
@@ -563,3 +587,6 @@ listeners: new Map(), | ||
private getCleanListener(fn: ListenerFunction, options: ListenerOptions = defaultListenerOptions): Listener { | ||
private getCleanListener( | ||
fn: ListenerFunction<PathValue<T, PossiblePath<T>>>, | ||
options: ListenerOptions = defaultListenerOptions | ||
): Listener<PathValue<T, PossiblePath<T>>> { | ||
return { | ||
@@ -596,3 +623,6 @@ fn, | ||
private getListenersCollection(listenerPath: string, listener: Listener): ListenersCollection { | ||
private getListenersCollection( | ||
listenerPath: string, | ||
listener: Listener<PathValue<T, PossiblePath<T>>> | ||
): ListenersCollection<PathValue<T, PossiblePath<T>>> { | ||
if (this.listeners.has(listenerPath)) { | ||
@@ -631,8 +661,8 @@ let listenersCollection = this.listeners.get(listenerPath); | ||
public subscribe( | ||
listenerPath: string, | ||
fn: ListenerFunction, | ||
public subscribe<P extends PossiblePath<T>>( | ||
listenerPath: P, | ||
fn: ListenerFunction<PathValue<T, P>>, | ||
options: ListenerOptions = defaultListenerOptions, | ||
subscribeAllOptions: SubscribeAllOptions = { | ||
all: [listenerPath], | ||
all: [listenerPath as string], | ||
index: 0, | ||
@@ -647,3 +677,3 @@ groupId: this.groupId, | ||
this.listenersIgnoreCache.set(listener, { truthy: [], falsy: [] }); | ||
const listenersCollection = this.getListenersCollection(listenerPath, listener); | ||
const listenersCollection = this.getListenersCollection(listenerPath as string, listener); | ||
if (options.debug) { | ||
@@ -662,5 +692,5 @@ console.log(); | ||
path: { | ||
listener: listenerPath, | ||
listener: listenerPath as string, | ||
update: undefined, | ||
resolved: this.cleanNotRecursivePath(listenerPath), | ||
resolved: this.cleanNotRecursivePath(listenerPath as string), | ||
}, | ||
@@ -673,12 +703,12 @@ params: this.getParams(listenersCollection.paramsInfo, cleanPath), | ||
const paths = this.scan.get(cleanPath); | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (options.bulk) { | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (!this.isMuted(fn)) { | ||
@@ -690,3 +720,3 @@ fn(bulkValue, { | ||
path: { | ||
listener: listenerPath, | ||
listener: listenerPath as string, | ||
update: undefined, | ||
@@ -707,3 +737,3 @@ resolved: undefined, | ||
path: { | ||
listener: listenerPath, | ||
listener: listenerPath as string, | ||
update: undefined, | ||
@@ -720,4 +750,4 @@ resolved: this.cleanNotRecursivePath(path), | ||
} | ||
this.debugSubscribe(listener, listenersCollection, listenerPath); | ||
return this.unsubscribe(listenerPath, this.id); | ||
this.debugSubscribe(listener, listenersCollection, listenerPath as string); | ||
return this.unsubscribe(listenerPath as string, this.id); | ||
} | ||
@@ -747,3 +777,6 @@ | ||
private getQueueNotifyListeners(groupedListeners: GroupedListeners, queue: Queue[] = []): Queue[] { | ||
private getQueueNotifyListeners( | ||
groupedListeners: GroupedListeners<PathValue<T, PossiblePath<T>>>, | ||
queue: Queue<PathValue<T, PossiblePath<T>>>[] = [] | ||
): Queue<PathValue<T, PossiblePath<T>>>[] { | ||
for (const path in groupedListeners) { | ||
@@ -826,3 +859,3 @@ if (this.isMuted(path)) continue; | ||
private shouldIgnore(listener: Listener, updatePath: string): boolean { | ||
private shouldIgnore(listener: Listener<PathValue<T, PossiblePath<T>>>, updatePath: string): boolean { | ||
if (!listener.options.ignore) return false; | ||
@@ -851,3 +884,3 @@ for (const ignorePath of listener.options.ignore) { | ||
originalPath: string = null | ||
): GroupedListeners { | ||
): GroupedListeners<PathValue<T, PossiblePath<T>>> { | ||
options = { ...defaultUpdateOptions, ...options }; | ||
@@ -938,3 +971,3 @@ const listeners = {}; | ||
originalPath: string = null | ||
): Queue[] { | ||
): Queue<PathValue<T, PossiblePath<T>>>[] { | ||
return this.getQueueNotifyListeners(this.getSubscribedListeners(updatePath, newValue, options, type, originalPath)); | ||
@@ -949,4 +982,4 @@ } | ||
originalPath: string = null | ||
): GroupedListeners { | ||
const listeners: GroupedListeners = {}; | ||
): GroupedListeners<PathValue<T, PossiblePath<T>>> { | ||
const listeners: GroupedListeners<PathValue<T, PossiblePath<T>>> = {}; | ||
for (let [listenerPath, listenersCollection] of this.listeners) { | ||
@@ -1039,5 +1072,5 @@ if (!listenersCollection.isRecursive) continue; | ||
type: string = "update", | ||
queue: Queue[], | ||
queue: Queue<PathValue<T, PossiblePath<T>>>[], | ||
originalPath: string = null | ||
): Queue[] { | ||
): Queue<PathValue<T, PossiblePath<T>>>[] { | ||
return this.getQueueNotifyListeners( | ||
@@ -1055,3 +1088,3 @@ this.getNestedListeners(updatePath, newValue, options, type, originalPath), | ||
originalPath: string = null | ||
): GroupedListeners { | ||
): GroupedListeners<PathValue<T, PossiblePath<T>>> { | ||
const listeners = {}; | ||
@@ -1119,3 +1152,3 @@ if ( | ||
private runQueue(queue: Queue[]) { | ||
private runQueue(queue: Queue<PathValue<T, PossiblePath<T>>>[]) { | ||
const firedGroups = []; | ||
@@ -1134,3 +1167,3 @@ for (const q of queue) { | ||
private sortAndRunQueue(queue: Queue[], path: string) { | ||
private sortAndRunQueue(queue: Queue<PathValue<T, PossiblePath<T>>>[], path: string) { | ||
queue.sort(function (a, b) { | ||
@@ -1392,3 +1425,3 @@ return a.id - b.id; | ||
public get(userPath: string | undefined = undefined) { | ||
public get<P extends PossiblePath<T>>(userPath: P | string): PathValue<T, P> { | ||
if (this.destroyed) return; | ||
@@ -1398,3 +1431,3 @@ if (typeof userPath === "undefined" || userPath === "") { | ||
} | ||
return this.pathGet(this.split(userPath), this.data); | ||
return this.pathGet(this.split(userPath as string), this.data); | ||
} | ||
@@ -1418,3 +1451,3 @@ | ||
public isMuted(pathOrListenerFunction: string | ListenerFunction): boolean { | ||
public isMuted(pathOrListenerFunction: string | ListenerFunction<PathValue<T, PossiblePath<T>>>): boolean { | ||
if (!this.options.useMute) return false; | ||
@@ -1438,7 +1471,7 @@ if (typeof pathOrListenerFunction === "function") { | ||
public isMutedListener(listenerFunc: ListenerFunction): boolean { | ||
public isMutedListener(listenerFunc: ListenerFunction<PathValue<T, PossiblePath<T>>>): boolean { | ||
return this.mutedListeners.has(listenerFunc); | ||
} | ||
public mute(pathOrListenerFunction: string | ListenerFunction) { | ||
public mute(pathOrListenerFunction: string | ListenerFunction<PathValue<T, PossiblePath<T>>>) { | ||
if (typeof pathOrListenerFunction === "function") { | ||
@@ -1450,3 +1483,3 @@ return this.mutedListeners.add(pathOrListenerFunction); | ||
public unmute(pathOrListenerFunction: string | ListenerFunction) { | ||
public unmute(pathOrListenerFunction: string | ListenerFunction<PathValue<T, PossiblePath<T>>>) { | ||
if (typeof pathOrListenerFunction === "function") { | ||
@@ -1458,3 +1491,7 @@ return this.mutedListeners.delete(pathOrListenerFunction); | ||
private debugSubscribe(listener: Listener, listenersCollection: ListenersCollection, listenerPath: string) { | ||
private debugSubscribe( | ||
listener: Listener<PathValue<T, PossiblePath<T>>>, | ||
listenersCollection: ListenersCollection<T>, | ||
listenerPath: string | ||
) { | ||
if (listener.options.debug) { | ||
@@ -1469,3 +1506,3 @@ this.options.log("listener subscribed", { | ||
private debugListener(time: number, groupedListener: GroupedListener) { | ||
private debugListener(time: number, groupedListener: GroupedListener<PathValue<T, PossiblePath<T>>>) { | ||
if (groupedListener.eventInfo.options.debug || groupedListener.listener.options.debug) { | ||
@@ -1479,3 +1516,3 @@ this.options.log("Listener fired", { | ||
private debugTime(groupedListener: GroupedListener): number { | ||
private debugTime(groupedListener: GroupedListener<PathValue<T, PossiblePath<T>>>): number { | ||
return groupedListener.listener.options.debug || groupedListener.eventInfo.options.debug ? Date.now() : 0; | ||
@@ -1482,0 +1519,0 @@ } |
{ | ||
"name": "deep-state-observer", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"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.", | ||
@@ -48,3 +48,2 @@ "main": "index.cjs.js", | ||
"homepage": "https://github.com/neuronetio/deep-state-observer#readme", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -60,4 +59,4 @@ "@rollup/plugin-commonjs": "^11.0.2", | ||
"tslib": "^1.10.0", | ||
"typescript": "^3.5.3" | ||
"typescript": "^4.4.3" | ||
} | ||
} |
@@ -9,2 +9,3 @@ export interface Zobj { | ||
c: Zobj; | ||
d: Zobj; | ||
} | ||
@@ -11,0 +12,0 @@ export interface SomeState { |
@@ -409,2 +409,3 @@ /*! ***************************************************************************** | ||
this.proxyPath = []; | ||
//public subscribe: typeof sub; | ||
this.handler = { | ||
@@ -776,13 +777,13 @@ get: (obj, prop) => { | ||
const paths = this.scan.get(cleanPath); | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) | ||
continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (options.bulk) { | ||
const bulkValue = []; | ||
for (const path in paths) { | ||
if (this.isMuted(path)) | ||
continue; | ||
bulkValue.push({ | ||
path, | ||
params: this.getParams(listenersCollection.paramsInfo, path), | ||
value: paths[path], | ||
}); | ||
} | ||
if (!this.isMuted(fn)) { | ||
@@ -1420,3 +1421,3 @@ fn(bulkValue, { | ||
} | ||
get(userPath = undefined) { | ||
get(userPath) { | ||
if (this.destroyed) | ||
@@ -1546,5 +1547,10 @@ return; | ||
}, | ||
d: { | ||
d: 4, | ||
e: "55", | ||
}, | ||
}, | ||
}; | ||
const state = new DeepState(someState); | ||
state.subscribe("y.c", (v) => { }); | ||
state.$$$.y.c.d = 33; | ||
@@ -1557,1 +1563,3 @@ if (state.get("y.c.d") !== 33) { | ||
} | ||
sub("y.c.d", (val) => { }); | ||
get("y.c.e"); |
35
test.ts
@@ -0,1 +1,2 @@ | ||
import * as path from "path"; | ||
import DeepStateObserver from "./index"; | ||
@@ -12,2 +13,3 @@ | ||
c: Zobj; | ||
d: Zobj; | ||
} | ||
@@ -29,2 +31,6 @@ | ||
}, | ||
d: { | ||
d: 4, | ||
e: "55", | ||
}, | ||
}, | ||
@@ -35,2 +41,4 @@ }; | ||
state.subscribe("y.c", (v) => {}); | ||
state.$$$.y.c.d = 33; | ||
@@ -43,1 +51,28 @@ | ||
} | ||
type PathImpl<T, K extends keyof T> = K extends string | ||
? T[K] extends Record<string, any> | ||
? T[K] extends ArrayLike<any> | ||
? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}` | ||
: K | `${K}.${PathImpl<T[K], keyof T[K]>}` | ||
: K | ||
: never; | ||
type Path<T> = PathImpl<T, keyof T> | keyof T; | ||
type PathValue<T, P extends Path<T>> = P extends `${infer K}.${infer Rest}` | ||
? K extends keyof T | ||
? Rest extends Path<T[K]> | ||
? PathValue<T[K], Rest> | ||
: never | ||
: never | ||
: P extends keyof T | ||
? T[P] | ||
: never; | ||
declare function get<P extends Path<SomeState>>(path: P): PathValue<SomeState, P>; | ||
declare function sub<P extends Path<SomeState>>(path: P, callback: (val: PathValue<SomeState, P>) => void): void; | ||
sub("y.c.d", (val) => {}); | ||
const v = get("y.c.e"); |
@@ -50,3 +50,3 @@ "use strict"; | ||
this.simpleMatch(first, second) || | ||
stringMatcher_1.Match(first, second, this.wildcard)); | ||
(0, stringMatcher_1.Match)(first, second, this.wildcard)); | ||
}; | ||
@@ -53,0 +53,0 @@ WildcardObject.prototype.handleArray = function handleArray(wildcard, currentArr, partIndex, path, result) { |
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
12705
510486