diff --git a/src/ObservableObject.ts b/src/ObservableObject.ts index cbb9a1312..d76baf905 100644 --- a/src/ObservableObject.ts +++ b/src/ObservableObject.ts @@ -835,9 +835,11 @@ function activateNodeFunction( node: NodeValue & ((value: ComputedProxyParams, childKey?: string) => any), childNode?: NodeValue, ) { - let dispose: any; + let dispose: () => void; let subscribed = false; let isSetting = false; + // The onSet function handles the observable being set + // and forwards the set elsewhere const onSet: ComputedParams['onSet'] = (setter) => { const doSet = (params: ListenerParams) => { if (params.changes.length > 1 || !isFunction(params.changes[0].prevAtPath)) { @@ -852,6 +854,8 @@ function activateNodeFunction( dispose?.(); dispose = onChange(node, doSet as any, { immediate: true }); }; + // The subscribe function runs a function that listens to + // a data source and sends updates into the observable const subscribe: ComputedParams['subscribe'] = (fn) => { if (!subscribed) { subscribed = true; @@ -862,6 +866,9 @@ function activateNodeFunction( }); } }; + // The proxy function simply marks the node as a proxy with this function + // so that child nodes will be created with this function, and then simply + // activated as a function const proxy: ComputedProxyParams['proxy'] = (fn) => { node.proxyFn2 = fn; }; @@ -869,6 +876,7 @@ function activateNodeFunction( let curTarget$: ObservableObject; observe( () => { + // Run the function at this node let value = node({ onSet, subscribe, proxy }, childNode?.key); // If target is an observable, get() it to make sure we listen to its changes // and set up an onSet to write changes back to it diff --git a/src/observableInterfaces.ts b/src/observableInterfaces.ts index 47cdf0fb4..634b33a25 100644 --- a/src/observableInterfaces.ts +++ b/src/observableInterfaces.ts @@ -519,8 +519,6 @@ export interface ComputedParams { subscribe: (fn: (params: { update: (props: ObservableOnChangeParams) => void }) => void) => void; } -export interface ComputedProxyParams { - onSet: (fn: (params: ListenerParams) => void) => void; - subscribe: (fn: (params: { update: (props: ObservableOnChangeParams) => void }) => void) => void; +export interface ComputedProxyParams extends ComputedParams { proxy: (fn: (key: string, params: ComputedParams) => T) => void; }