Skip to content

Commit

Permalink
Add some more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Oct 16, 2023
1 parent 30ed30e commit db903e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/ObservableObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -862,13 +866,17 @@ 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;
};
let prevTarget$: ObservableObject<any>;
let curTarget$: ObservableObject<any>;
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
Expand Down
4 changes: 1 addition & 3 deletions src/observableInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,6 @@ export interface ComputedParams<T = any> {
subscribe: (fn: (params: { update: (props: ObservableOnChangeParams) => void }) => void) => void;
}

export interface ComputedProxyParams<T = any> {
onSet: (fn: (params: ListenerParams<T>) => void) => void;
subscribe: (fn: (params: { update: (props: ObservableOnChangeParams) => void }) => void) => void;
export interface ComputedProxyParams<T = any> extends ComputedParams {
proxy: (fn: (key: string, params: ComputedParams<T>) => T) => void;
}

0 comments on commit db903e7

Please sign in to comment.