Refactor watch sources for reliability

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).
This commit is contained in:
undergroundwires
2023-11-11 13:55:21 +01:00
parent 58cd551a30
commit 7ab16ecccb
25 changed files with 190 additions and 217 deletions

View File

@@ -1,5 +1,5 @@
import {
WatchSource, shallowReadonly, shallowRef, triggerRef,
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';
@@ -7,7 +7,7 @@ import { QueryableNodes } from '@/presentation/components/Scripts/View/Tree/Tree
import { QueryableNodesStub } from './QueryableNodesStub';
export class UseCurrentTreeNodesStub {
public treeWatcher: WatchSource<TreeRoot> | undefined;
public treeRootRef: Readonly<Ref<TreeRoot>> | undefined;
private nodes = shallowRef<QueryableNodes>(new QueryableNodesStub());
@@ -22,8 +22,8 @@ export class UseCurrentTreeNodesStub {
}
public get(): typeof useCurrentTreeNodes {
return (treeWatcher: WatchSource<TreeRoot>) => {
this.treeWatcher = treeWatcher;
return (treeRootRef: Readonly<Ref<TreeRoot>>) => {
this.treeRootRef = treeRootRef;
return {
nodes: shallowReadonly(this.nodes),
};