@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
70 lines (69 loc) • 2.46 kB
TypeScript
declare global {
interface Object {
__brainId: number;
__brainIsProxy?: boolean;
__brainTarget: object;
__brainParents: TProxy[];
__brainChildren: Map<string, TProxy>;
__brainFullPaths: string[];
}
}
export type TProxy = InstanceType<typeof Proxy>;
type TTarget = any;
type Callback = (path: string, oldValue: TTarget, newValue: TTarget | TProxy, parents: TProxy[]) => void;
export default class Brain<T extends Record<string | symbol, any>> {
private proxyCount;
private readonly initialState;
private readonly stateProxy;
private readonly externalCallback;
private readonly proxyIds;
private readonly targetToProxy;
private readonly proxyToTarget;
private readonly proxyChildren;
private readonly proxyParents;
private readonly proxyToFullPaths;
private readonly mergeMinimalPathsCache;
private delayed;
private readonly delayedCallbacks;
constructor(initialState: TTarget, callback: Callback);
private callback;
/**
*
* Creates a proxy for the given target object
* @param target The target object to create a proxy for
* @param prop The property name of this target in its parent
* @param parent The parent proxy (optional for root element)
* @returns The created proxy
*/
private createProxy;
/**
* Ensure we keep only minimal unique paths for a proxy.
* And ensure that the selectd paths have the same prefix than the parents path
* Example:
* existing: ["group"]
* candidates: ["group.children.0.parent"]
* => keep only ["group"]
* Other example if parentsPaths is defined:
* existing: ["group.inner"]
* candidates: ["group.inner", "group2.inner"]
* parrents: ["group2"]
* => keep only ["group2.inner"] (Because it is the only one still linked to the right parent)
*/
private mergeMinimalPaths;
private isRightParent;
private getOrCreateProxyForValue;
private cleanProxyForValue;
private recalculateChildrenForArray;
private updateChildPathsRecursively;
private handleGetVirtualProperties;
private handleGetIgnored;
private handleGetFunctions;
private getHandler;
private setHandler;
private setArrayHandler;
private getFullPath;
private readonly objectHandler;
delay(statechanges: (state: T) => void): void;
getState(): T;
}
export {};