Files
directus/src/hydrate.ts
Rijk van Zanten 33062b9dd9 Add latency store / indicator (#392)
* Add latency store and track latency based on user tracking

* Add latency store to dehydration logic

* Add signal icons

* Add latency-indicator component

* Set correct size of latency spinner
2020-04-13 10:08:27 -04:00

68 lines
1.7 KiB
TypeScript

import { useAppStore } from '@/stores/app/';
import { useCollectionsStore } from '@/stores/collections/';
import { useFieldsStore } from '@/stores/fields/';
import { useUserStore } from '@/stores/user/';
import { useRequestsStore } from '@/stores/requests/';
import { useCollectionPresetsStore } from '@/stores/collection-presets/';
import { useSettingsStore } from '@/stores/settings/';
import { useProjectsStore } from '@/stores/projects/';
import { useLatencyStore } from '@/stores/latency';
type GenericStore = {
id: string;
hydrate?: () => Promise<void>;
dehydrate?: () => Promise<void>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
};
export function useStores(
stores = [
useCollectionsStore,
useFieldsStore,
useUserStore,
useRequestsStore,
useCollectionPresetsStore,
useSettingsStore,
useProjectsStore,
useLatencyStore,
]
) {
return stores.map((useStore) => useStore()) as GenericStore[];
}
/* istanbul ignore next: useStores has a test already */
export async function hydrate(stores = useStores()) {
const appStore = useAppStore();
if (appStore.state.hydrated) return;
if (appStore.state.hydrating) return;
appStore.state.hydrating = true;
try {
for (const store of stores) {
await store.hydrate?.();
}
} catch (error) {
appStore.state.error = error;
} finally {
appStore.state.hydrating = false;
}
appStore.state.hydrated = true;
}
/* istanbul ignore next: useStores has a test already */
export async function dehydrate(stores = useStores()) {
const appStore = useAppStore();
if (appStore.state.hydrated === false) return;
for (const store of stores) {
await store.dehydrate?.();
}
appStore.state.hydrated = false;
}