This commit changes `WatchSource` signatures into `Readonly<Ref>`s.
It provides two important benefits:
1. Eliminates the possibility of `undefined` states, that's result of
using `WatchSource`s. This previously required additional null checks.
By using `Readonly<Ref>`, the state handling becomes simpler and less
susceptible to null errors.
2. Optimizes performance by using references:
- Avoids the reactive layer of `computed` references when not needed.
- The `watch` syntax, such as `watch(() => ref.value)`, can introduce
side effects. For example, it does not account for `triggerRef` in
scenarios where the value remains unchanged, preventing the watcher
from running (vuejs/core#9579).
34 lines
1013 B
TypeScript
34 lines
1013 B
TypeScript
import { TreeRoot } from '@/presentation/components/Scripts/View/Tree/TreeView/TreeRoot/TreeRoot';
|
|
import {
|
|
NodeStateChangeEventArgs,
|
|
NodeStateChangeEventCallback,
|
|
useNodeStateChangeAggregator,
|
|
} from '@/presentation/components/Scripts/View/Tree/TreeView/UseNodeStateChangeAggregator';
|
|
import type { Ref } from 'vue';
|
|
|
|
export class UseNodeStateChangeAggregatorStub {
|
|
public callback: NodeStateChangeEventCallback | undefined;
|
|
|
|
public treeRootRef: Readonly<Ref<TreeRoot>> | undefined;
|
|
|
|
public onNodeStateChange(callback: NodeStateChangeEventCallback) {
|
|
this.callback = callback;
|
|
}
|
|
|
|
public notifyChange(change: NodeStateChangeEventArgs) {
|
|
if (!this.callback) {
|
|
throw new Error('callback is not set');
|
|
}
|
|
this.callback(change);
|
|
}
|
|
|
|
public get(): typeof useNodeStateChangeAggregator {
|
|
return (treeRootRef: Readonly<Ref<TreeRoot>>) => {
|
|
this.treeRootRef = treeRootRef;
|
|
return {
|
|
onNodeStateChange: this.onNodeStateChange.bind(this),
|
|
};
|
|
};
|
|
}
|
|
}
|