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 21, 2023
1 parent 2315fa3 commit 0b2216b
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 @@ -836,9 +836,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 @@ -853,6 +855,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 @@ -863,13 +867,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 @@ -523,8 +523,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 0b2216b

Please sign in to comment.