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).
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
import {
|
|
type Ref, shallowReadonly, shallowRef, triggerRef,
|
|
} from 'vue';
|
|
import { TreeRoot } from '@/presentation/components/Scripts/View/Tree/TreeView/TreeRoot/TreeRoot';
|
|
import { useCurrentTreeNodes } from '@/presentation/components/Scripts/View/Tree/TreeView/UseCurrentTreeNodes';
|
|
import { QueryableNodes } from '@/presentation/components/Scripts/View/Tree/TreeView/TreeRoot/NodeCollection/Query/QueryableNodes';
|
|
import { QueryableNodesStub } from './QueryableNodesStub';
|
|
|
|
export class UseCurrentTreeNodesStub {
|
|
public treeRootRef: Readonly<Ref<TreeRoot>> | undefined;
|
|
|
|
private nodes = shallowRef<QueryableNodes>(new QueryableNodesStub());
|
|
|
|
public withQueryableNodes(nodes: QueryableNodes): this {
|
|
this.nodes.value = nodes;
|
|
return this;
|
|
}
|
|
|
|
public triggerNewNodes(nodes: QueryableNodes) {
|
|
this.nodes.value = nodes;
|
|
triggerRef(this.nodes);
|
|
}
|
|
|
|
public get(): typeof useCurrentTreeNodes {
|
|
return (treeRootRef: Readonly<Ref<TreeRoot>>) => {
|
|
this.treeRootRef = treeRootRef;
|
|
return {
|
|
nodes: shallowReadonly(this.nodes),
|
|
};
|
|
};
|
|
}
|
|
}
|