Files
directus/app/src/hydrate.ts
Nicola Krumschmidt 7bf90efa62 Add support for a package extension bundle type (#15672)
* Add bundle type to constants and types

* Add support for API bundle extensions

* Rename generateExtensionsEntry to generateExtensionsEntrypoint

* Add support for App bundle extensions

* Refactor App extension registration

* Replace extensions inject with useExtensions()

* Replace getInterfaces() with useExtensions()

* Replace getDisplays() with useExtensions()

* Replace getLayouts() with useExtensions()

* Replace getModules() with useExtensions()

* Replace getPanels() with useExtensions()

* Replace getOperations() with useExtensions()

* Add useExtension() composable

* Replace useExtensions() with useExtension() where applicable

* Remove interface getters

* Remove display getters

* Remove layout getters

* Remove module getter

* Remove panel getters

* Remove operation getters

* Rename extension register.ts files to index.ts

* Perform module pre register check in parallel

* Remove Refs from AppExtensionConfigs type

* Remove old extension shims

* Ensure registration of modules is awaited when hydrating

* Add support for scaffolding package extensions

* Add support for building bundle extensions

* Add JsonValue type

* Use json for complex command line flags

* Load internal extensions if custom ones are not available

* Fix extension manifest validation for pack extensions

* Fix tests in shared

* Add SplitEntrypoint type

* Move command specific utils to helpers

* Add SDK version getter

* Move extension dev deps generation to helpers

* Move template path to getter util

* Move template copying to a helper

* Only rename copied template files

* Add directus-extension add command

* Convert provided extension source path to url

* Replace deprecated import.meta.globEager

* Mock URL.createObjectURL to make App unit tests pass

* Update rollup-plugin-typescript2

* indentation

* sort vite glob imported modules

* fix unintentional wrong commit

* Simplify app extension import logic

* reinstall @rollup/plugin-virtual

* add test for getInterfaces() expected sort order

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>
2022-11-16 11:28:52 -05:00

110 lines
3.1 KiB
TypeScript

import { setLanguage } from '@/lang/set-language';
import { getBasemapSources } from '@/utils/geometry/basemap';
import { useAppStore } from '@/stores/app';
import { useCollectionsStore } from '@/stores/collections';
import { useFieldsStore } from '@/stores/fields';
import { useLatencyStore } from '@/stores/latency';
import { useInsightsStore } from '@/stores/insights';
import { useFlowsStore } from '@/stores/flows';
import { usePermissionsStore } from '@/stores/permissions';
import { usePresetsStore } from '@/stores/presets';
import { useRelationsStore } from '@/stores/relations';
import { useRequestsStore } from '@/stores/requests';
import { useServerStore } from '@/stores/server';
import { useSettingsStore } from '@/stores/settings';
import { useUserStore } from '@/stores/user';
import { useNotificationsStore } from '@/stores/notifications';
import { useTranslationStrings } from '@/composables/use-translation-strings';
import { onDehydrateExtensions, onHydrateExtensions } from './extensions';
type GenericStore = {
$id: string;
hydrate?: () => Promise<void>;
dehydrate?: () => Promise<void>;
[key: string]: any;
};
export function useStores(
stores = [
useCollectionsStore,
useFieldsStore,
useUserStore,
useRequestsStore,
usePresetsStore,
useSettingsStore,
useServerStore,
useLatencyStore,
useRelationsStore,
usePermissionsStore,
useInsightsStore,
useFlowsStore,
useNotificationsStore,
]
): GenericStore[] {
return stores.map((useStore) => useStore()) as GenericStore[];
}
export async function hydrate(): Promise<void> {
const stores = useStores();
const appStore = useAppStore();
const userStore = useUserStore();
const serverStore = useServerStore();
const permissionsStore = usePermissionsStore();
const { refresh: hydrateTranslationStrings } = useTranslationStrings();
if (appStore.hydrated) return;
if (appStore.hydrating) return;
appStore.hydrating = true;
try {
/**
* @NOTE
* Multiple stores rely on the userStore to be set, so they can fetch user specific data. The
* following makes sure that the user store is always fetched first, before we hydrate anything
* else.
*/
await userStore.hydrate();
let lang = 'en-US';
if (serverStore.info?.project?.default_language) lang = serverStore.info.project.default_language;
if (userStore.currentUser?.role) {
await permissionsStore.hydrate();
const hydratedStores = ['userStore', 'permissionsStore'];
await Promise.all(stores.filter(({ $id }) => !hydratedStores.includes($id)).map((store) => store.hydrate?.()));
await onHydrateExtensions();
await hydrateTranslationStrings();
if (userStore.currentUser?.language) lang = userStore.currentUser?.language;
}
await setLanguage(lang);
appStore.basemap = getBasemapSources()[0].name;
} catch (error: any) {
appStore.error = error;
} finally {
appStore.hydrating = false;
}
appStore.hydrated = true;
}
export async function dehydrate(stores = useStores()): Promise<void> {
const appStore = useAppStore();
if (appStore.hydrated === false) return;
for (const store of stores) {
await store.dehydrate?.();
}
await onDehydrateExtensions();
appStore.hydrated = false;
}