Refactor DI for simplicity and type safety

This commit improves the dependency injection mechanism by introducing a
custom `injectKey` function.

Key improvements are:

- Enforced type consistency during dependency registration and
  instantiation.
- Simplified injection process, abstracting away the complexity with a
  uniform API, regardless of the dependency's lifetime.
- Eliminated the possibility of `undefined` returns during dependency
  injection, promoting fail-fast behavior.
- Removed the necessity for type casting to `symbol` for injection keys
  in unit tests by using existing types.
- Consalidated imports, combining keys and injection functions in one
  `import` statement.
This commit is contained in:
undergroundwires
2023-11-09 13:17:38 +01:00
parent aab0f7ea46
commit 7770a9b521
51 changed files with 398 additions and 177 deletions

View File

@@ -35,9 +35,8 @@
<script lang="ts">
import {
defineComponent, ref, onMounted, onUnmounted, computed,
inject,
} from 'vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import SizeObserver from '@/presentation/components/Shared/SizeObserver.vue';
import { hasDirective } from './NonCollapsingDirective';
import CardListItem from './CardListItem.vue';
@@ -48,7 +47,7 @@ export default defineComponent({
SizeObserver,
},
setup() {
const { currentState, onStateChange } = inject(InjectionKeys.useCollectionState)();
const { currentState, onStateChange } = injectKey((keys) => keys.useCollectionState);
const width = ref<number>(0);
const categoryIds = computed<ReadonlyArray<number>>(() => currentState

View File

@@ -49,11 +49,10 @@
<script lang="ts">
import {
defineComponent, ref, watch, computed,
inject, shallowRef,
defineComponent, ref, watch, computed, shallowRef,
} from 'vue';
import AppIcon from '@/presentation/components/Shared/Icon/AppIcon.vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import ScriptsTree from '@/presentation/components/Scripts/View/Tree/ScriptsTree.vue';
import { sleep } from '@/infrastructure/Threading/AsyncSleep';
@@ -78,8 +77,8 @@ export default defineComponent({
/* eslint-enable @typescript-eslint/no-unused-vars */
},
setup(props, { emit }) {
const { onStateChange, currentState } = inject(InjectionKeys.useCollectionState)();
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
const { onStateChange, currentState } = injectKey((keys) => keys.useCollectionState);
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const isExpanded = computed({
get: () => {

View File

@@ -39,10 +39,9 @@
<script lang="ts">
import {
defineComponent, PropType, ref, computed,
inject,
} from 'vue';
import AppIcon from '@/presentation/components/Shared/Icon/AppIcon.vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import ScriptsTree from '@/presentation/components/Scripts/View/Tree/ScriptsTree.vue';
import CardList from '@/presentation/components/Scripts/View/Cards/CardList.vue';
import { ViewType } from '@/presentation/components/Scripts/Menu/View/ViewType';
@@ -62,9 +61,9 @@ export default defineComponent({
},
},
setup() {
const { modifyCurrentState, onStateChange } = inject(InjectionKeys.useCollectionState)();
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
const { info } = inject(InjectionKeys.useApplication);
const { modifyCurrentState, onStateChange } = injectKey((keys) => keys.useCollectionState);
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const { info } = injectKey((keys) => keys.useApplication);
const repositoryUrl = computed<string>(() => info.repositoryWebUrl);
const searchQuery = ref<string | undefined>();

View File

@@ -8,11 +8,10 @@
<script lang="ts">
import {
PropType, defineComponent, ref, watch,
computed, inject,
PropType, defineComponent, ref, watch, computed,
} from 'vue';
import { SelectedScript } from '@/application/Context/State/Selection/SelectedScript';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { NodeMetadata } from '@/presentation/components/Scripts/View/Tree/NodeContent/NodeMetadata';
import { IReverter } from './Reverter/IReverter';
import { getReverter } from './Reverter/ReverterFactory';
@@ -31,8 +30,8 @@ export default defineComponent({
setup(props) {
const {
currentState, modifyCurrentState, onStateChange,
} = inject(InjectionKeys.useCollectionState)();
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
} = injectKey((keys) => keys.useCollectionState);
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const isReverted = ref(false);

View File

@@ -1,14 +1,14 @@
import {
WatchSource, inject, shallowRef, watch,
WatchSource, shallowRef, watch,
} from 'vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { ReadOnlyTreeNode } from './TreeNode';
import { TreeNodeStateDescriptor } from './State/StateDescriptor';
export function useNodeState(
nodeWatcher: WatchSource<ReadOnlyTreeNode | undefined>,
) {
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const state = shallowRef<TreeNodeStateDescriptor>();

View File

@@ -1,12 +1,12 @@
import {
WatchSource, watch, inject, shallowReadonly, shallowRef,
WatchSource, watch, shallowReadonly, shallowRef,
} from 'vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { TreeRoot } from './TreeRoot/TreeRoot';
import { QueryableNodes } from './TreeRoot/NodeCollection/Query/QueryableNodes';
export function useCurrentTreeNodes(treeWatcher: WatchSource<TreeRoot>) {
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const tree = shallowRef<TreeRoot | undefined>();
const nodes = shallowRef<QueryableNodes | undefined>();

View File

@@ -1,7 +1,7 @@
import {
WatchSource, inject, watch, shallowRef,
WatchSource, watch, shallowRef,
} from 'vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { IEventSubscription } from '@/infrastructure/Events/IEventSource';
import { TreeRoot } from './TreeRoot/TreeRoot';
import { TreeNode } from './Node/TreeNode';
@@ -15,7 +15,7 @@ export function useNodeStateChangeAggregator(
useTreeNodes = useCurrentTreeNodes,
) {
const { nodes } = useTreeNodes(treeWatcher);
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const onNodeChangeCallback = shallowRef<NodeStateChangeEventCallback>();

View File

@@ -1,10 +1,9 @@
import { inject } from 'vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { TreeNodeCheckState } from '../TreeView/Node/State/CheckState';
import { TreeNodeStateChangedEmittedEvent } from '../TreeView/Bindings/TreeNodeStateChangedEmittedEvent';
export function useCollectionSelectionStateUpdater() {
const { modifyCurrentState, currentState } = inject(InjectionKeys.useCollectionState)();
const { modifyCurrentState, currentState } = injectKey((keys) => keys.useCollectionState);
function updateNodeSelection(change: TreeNodeStateChangedEmittedEvent) {
const { node } = change;

View File

@@ -1,7 +1,7 @@
import {
computed, inject, shallowReadonly, shallowRef, triggerRef,
computed, shallowReadonly, shallowRef, triggerRef,
} from 'vue';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { SelectedScript } from '@/application/Context/State/Selection/SelectedScript';
import { getScriptNodeId } from './CategoryNodeMetadataConverter';
@@ -20,8 +20,8 @@ export function useSelectedScriptNodeIds(scriptNodeIdParser = getScriptNodeId) {
}
function useSelectedScripts() {
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
const { onStateChange } = inject(InjectionKeys.useCollectionState)();
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const { onStateChange } = injectKey((keys) => keys.useCollectionState);
const selectedScripts = shallowRef<readonly SelectedScript[]>([]);

View File

@@ -1,9 +1,9 @@
import {
Ref, inject, shallowReadonly, shallowRef,
Ref, shallowReadonly, shallowRef,
} from 'vue';
import { IScript } from '@/domain/IScript';
import { ICategory } from '@/domain/ICategory';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { IReadOnlyUserFilter } from '@/application/Context/State/Filter/IUserFilter';
import { IFilterResult } from '@/application/Context/State/Filter/IFilterResult';
import { TreeViewFilterEvent, createFilterRemovedEvent, createFilterTriggeredEvent } from '../TreeView/Bindings/TreeInputFilterEvent';
@@ -18,8 +18,8 @@ type TreeNodeFilterResultPredicate = (
) => boolean;
export function useTreeViewFilterEvent() {
const { onStateChange } = inject(InjectionKeys.useCollectionState)();
const { events } = inject(InjectionKeys.useAutoUnsubscribedEvents)();
const { onStateChange } = injectKey((keys) => keys.useCollectionState);
const { events } = injectKey((keys) => keys.useAutoUnsubscribedEvents);
const latestFilterEvent = shallowRef<TreeViewFilterEvent | undefined>(undefined);

View File

@@ -1,9 +1,9 @@
import {
WatchSource, computed, inject,
WatchSource, computed,
ref, watch,
} from 'vue';
import { ICategoryCollection } from '@/domain/ICategoryCollection';
import { InjectionKeys } from '@/presentation/injectionSymbols';
import { injectKey } from '@/presentation/injectionSymbols';
import { TreeInputNodeData } from '../TreeView/Bindings/TreeInputNodeData';
import { NodeMetadata } from '../NodeContent/NodeMetadata';
import { convertToNodeInput } from './TreeNodeMetadataConverter';
@@ -17,7 +17,7 @@ export function useTreeViewNodeInput(
},
nodeConverter = convertToNodeInput,
) {
const { currentState } = inject(InjectionKeys.useCollectionState)();
const { currentState } = injectKey((keys) => keys.useCollectionState);
const categoryId = ref<number | undefined>();