@aerogel/core 0.0.0-next.88c59e62f64db70aedfbc4c31b5bbc287be44483 → 0.0.0-next.8ae083000611b11799d37033e9a5250d0d07c324
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aerogel-core.css +1 -0
- package/dist/aerogel-core.d.ts +2185 -992
- package/dist/aerogel-core.js +3273 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +37 -34
- package/src/bootstrap/bootstrap.test.ts +7 -10
- package/src/bootstrap/index.ts +41 -15
- package/src/bootstrap/options.ts +4 -1
- package/src/components/AppLayout.vue +14 -0
- package/src/components/{AGAppModals.vue → AppModals.vue} +3 -4
- package/src/components/AppOverlays.vue +9 -0
- package/src/components/AppToasts.vue +16 -0
- package/src/components/contracts/AlertModal.ts +8 -0
- package/src/components/contracts/Button.ts +16 -0
- package/src/components/contracts/ConfirmModal.ts +46 -0
- package/src/components/contracts/DropdownMenu.ts +20 -0
- package/src/components/contracts/ErrorReportModal.ts +32 -0
- package/src/components/contracts/Input.ts +26 -0
- package/src/components/contracts/LoadingModal.ts +26 -0
- package/src/components/contracts/Modal.ts +21 -0
- package/src/components/contracts/PromptModal.ts +34 -0
- package/src/components/contracts/Select.ts +45 -0
- package/src/components/contracts/Toast.ts +15 -0
- package/src/components/contracts/index.ts +11 -0
- package/src/components/headless/HeadlessButton.vue +51 -0
- package/src/components/headless/HeadlessInput.vue +59 -0
- package/src/components/headless/HeadlessInputDescription.vue +27 -0
- package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
- package/src/components/headless/HeadlessInputInput.vue +75 -0
- package/src/components/headless/HeadlessInputLabel.vue +18 -0
- package/src/components/headless/HeadlessInputTextArea.vue +40 -0
- package/src/components/headless/HeadlessModal.vue +57 -0
- package/src/components/headless/HeadlessModalContent.vue +30 -0
- package/src/components/headless/HeadlessModalDescription.vue +12 -0
- package/src/components/headless/HeadlessModalOverlay.vue +12 -0
- package/src/components/headless/HeadlessModalTitle.vue +12 -0
- package/src/components/headless/HeadlessSelect.vue +120 -0
- package/src/components/headless/HeadlessSelectError.vue +25 -0
- package/src/components/headless/HeadlessSelectLabel.vue +25 -0
- package/src/components/headless/HeadlessSelectOption.vue +34 -0
- package/src/components/headless/HeadlessSelectOptions.vue +42 -0
- package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
- package/src/components/headless/HeadlessSelectValue.vue +18 -0
- package/src/components/headless/HeadlessToast.vue +18 -0
- package/src/components/headless/HeadlessToastAction.vue +13 -0
- package/src/components/headless/index.ts +19 -3
- package/src/components/index.ts +6 -9
- package/src/components/ui/AdvancedOptions.vue +18 -0
- package/src/components/ui/AlertModal.vue +14 -0
- package/src/components/ui/Button.vue +98 -0
- package/src/components/ui/Checkbox.vue +56 -0
- package/src/components/ui/ConfirmModal.vue +45 -0
- package/src/components/ui/DropdownMenu.vue +32 -0
- package/src/components/ui/DropdownMenuOption.vue +14 -0
- package/src/components/ui/DropdownMenuOptions.vue +27 -0
- package/src/components/ui/EditableContent.vue +82 -0
- package/src/components/ui/ErrorMessage.vue +15 -0
- package/src/components/ui/ErrorReportModal.vue +67 -0
- package/src/components/{modals/AGErrorReportModalButtons.vue → ui/ErrorReportModalButtons.vue} +38 -29
- package/src/components/ui/ErrorReportModalTitle.vue +24 -0
- package/src/components/ui/Form.vue +24 -0
- package/src/components/ui/Input.vue +56 -0
- package/src/components/ui/Link.vue +12 -0
- package/src/components/ui/LoadingModal.vue +34 -0
- package/src/components/ui/Markdown.vue +85 -0
- package/src/components/ui/Modal.vue +122 -0
- package/src/components/ui/ModalContext.vue +31 -0
- package/src/components/ui/ProgressBar.vue +51 -0
- package/src/components/ui/PromptModal.vue +38 -0
- package/src/components/ui/Select.vue +27 -0
- package/src/components/ui/SelectLabel.vue +17 -0
- package/src/components/ui/SelectOption.vue +29 -0
- package/src/components/ui/SelectOptions.vue +35 -0
- package/src/components/ui/SelectTrigger.vue +29 -0
- package/src/components/ui/SettingsModal.vue +15 -0
- package/src/components/ui/StartupCrash.vue +31 -0
- package/src/components/ui/Toast.vue +44 -0
- package/src/components/ui/index.ts +30 -0
- package/src/directives/index.ts +13 -5
- package/src/directives/measure.ts +40 -0
- package/src/errors/Errors.state.ts +1 -1
- package/src/errors/Errors.ts +27 -34
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +19 -29
- package/src/errors/utils.ts +35 -0
- package/src/forms/FormController.test.ts +110 -0
- package/src/forms/FormController.ts +246 -0
- package/src/forms/index.ts +3 -2
- package/src/forms/utils.ts +51 -20
- package/src/forms/validation.ts +19 -0
- package/src/index.css +73 -0
- package/src/{main.ts → index.ts} +3 -0
- package/src/jobs/Job.ts +147 -0
- package/src/jobs/index.ts +10 -0
- package/src/jobs/listeners.ts +3 -0
- package/src/jobs/status.ts +4 -0
- package/src/lang/DefaultLangProvider.ts +46 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +44 -29
- package/src/lang/index.ts +12 -6
- package/src/lang/settings/Language.vue +48 -0
- package/src/lang/settings/index.ts +10 -0
- package/src/plugins/Plugin.ts +1 -1
- package/src/plugins/index.ts +10 -7
- package/src/services/App.state.ts +39 -7
- package/src/services/App.ts +49 -6
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +110 -36
- package/src/services/Service.ts +154 -49
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +18 -6
- package/src/services/store.ts +8 -5
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +26 -0
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +14 -12
- package/src/ui/UI.ts +314 -95
- package/src/ui/index.ts +32 -27
- package/src/ui/utils.ts +16 -0
- package/src/utils/classes.ts +49 -0
- package/src/utils/composition/events.ts +4 -5
- package/src/utils/composition/forms.ts +20 -4
- package/src/utils/composition/persistent.test.ts +33 -0
- package/src/utils/composition/persistent.ts +11 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +33 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +19 -6
- package/src/utils/types.ts +3 -0
- package/src/utils/vue.ts +28 -118
- package/.eslintrc.js +0 -3
- package/dist/aerogel-core.cjs.js +0 -2
- package/dist/aerogel-core.cjs.js.map +0 -1
- package/dist/aerogel-core.esm.js +0 -2
- package/dist/aerogel-core.esm.js.map +0 -1
- package/dist/virtual.d.ts +0 -11
- package/noeldemartin.config.js +0 -5
- package/src/components/AGAppLayout.vue +0 -11
- package/src/components/AGAppOverlays.vue +0 -37
- package/src/components/AGAppSnackbars.vue +0 -13
- package/src/components/basic/AGErrorMessage.vue +0 -16
- package/src/components/basic/AGLink.vue +0 -9
- package/src/components/basic/AGMarkdown.vue +0 -36
- package/src/components/basic/index.ts +0 -5
- package/src/components/constants.ts +0 -8
- package/src/components/forms/AGButton.vue +0 -44
- package/src/components/forms/AGCheckbox.vue +0 -35
- package/src/components/forms/AGForm.vue +0 -26
- package/src/components/forms/AGInput.vue +0 -36
- package/src/components/forms/index.ts +0 -4
- package/src/components/headless/forms/AGHeadlessButton.vue +0 -51
- package/src/components/headless/forms/AGHeadlessInput.ts +0 -8
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -54
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -45
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +0 -16
- package/src/components/headless/forms/AGHeadlessSelect.ts +0 -31
- package/src/components/headless/forms/AGHeadlessSelect.vue +0 -45
- package/src/components/headless/forms/AGHeadlessSelectButton.ts +0 -3
- package/src/components/headless/forms/AGHeadlessSelectLabel.ts +0 -3
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +0 -8
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +0 -3
- package/src/components/headless/forms/index.ts +0 -12
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -7
- package/src/components/headless/modals/AGHeadlessModal.vue +0 -88
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -28
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
- package/src/components/headless/modals/index.ts +0 -6
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +0 -10
- package/src/components/headless/snackbars/index.ts +0 -25
- package/src/components/modals/AGAlertModal.vue +0 -25
- package/src/components/modals/AGConfirmModal.vue +0 -30
- package/src/components/modals/AGErrorReportModal.ts +0 -20
- package/src/components/modals/AGErrorReportModal.vue +0 -62
- package/src/components/modals/AGErrorReportModalTitle.vue +0 -25
- package/src/components/modals/AGLoadingModal.vue +0 -19
- package/src/components/modals/AGModal.ts +0 -10
- package/src/components/modals/AGModal.vue +0 -37
- package/src/components/modals/AGModalContext.ts +0 -8
- package/src/components/modals/AGModalContext.vue +0 -22
- package/src/components/modals/AGModalTitle.vue +0 -9
- package/src/components/modals/index.ts +0 -23
- package/src/components/snackbars/AGSnackbar.vue +0 -42
- package/src/components/snackbars/index.ts +0 -3
- package/src/directives/initial-focus.ts +0 -11
- package/src/forms/Form.test.ts +0 -58
- package/src/forms/Form.ts +0 -179
- package/src/forms/composition.ts +0 -6
- package/src/types/virtual.d.ts +0 -11
- package/tsconfig.json +0 -11
- package/vite.config.ts +0 -14
package/src/lang/Lang.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { facade
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import Service from '
|
|
3
|
+
import DefaultLangProvider from './DefaultLangProvider';
|
|
4
|
+
import Service from './Lang.state';
|
|
5
5
|
|
|
6
6
|
export interface LangProvider {
|
|
7
|
-
|
|
7
|
+
getLocale(): string;
|
|
8
|
+
setLocale(locale: string): Promise<void>;
|
|
9
|
+
getFallbackLocale(): string;
|
|
10
|
+
setFallbackLocale(fallbackLocale: string): Promise<void>;
|
|
11
|
+
getLocales(): string[];
|
|
12
|
+
translate(key: string, parameters?: Record<string, unknown> | number): string;
|
|
13
|
+
translateWithDefault(key: string, defaultMessage: string, parameters?: Record<string, unknown> | number): string;
|
|
8
14
|
}
|
|
9
15
|
|
|
10
16
|
export class LangService extends Service {
|
|
@@ -14,47 +20,56 @@ export class LangService extends Service {
|
|
|
14
20
|
constructor() {
|
|
15
21
|
super();
|
|
16
22
|
|
|
17
|
-
this.provider =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return key;
|
|
23
|
-
},
|
|
24
|
-
};
|
|
23
|
+
this.provider = new DefaultLangProvider(
|
|
24
|
+
this.getState('locale') ?? this.getBrowserLocale(),
|
|
25
|
+
this.getState('fallbackLocale'),
|
|
26
|
+
);
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
public setProvider(provider: LangProvider): void {
|
|
29
|
+
public async setProvider(provider: LangProvider): Promise<void> {
|
|
28
30
|
this.provider = provider;
|
|
31
|
+
this.locales = provider.getLocales();
|
|
32
|
+
|
|
33
|
+
await provider.setLocale(this.locale ?? this.getBrowserLocale());
|
|
34
|
+
await provider.setFallbackLocale(this.fallbackLocale);
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
public translate(key: string, parameters?: Record<string, unknown>): string {
|
|
37
|
+
public translate(key: string, parameters?: Record<string, unknown> | number): string {
|
|
32
38
|
return this.provider.translate(key, parameters) ?? key;
|
|
33
39
|
}
|
|
34
40
|
|
|
35
|
-
public translateWithDefault(key: string, defaultMessage: string): string;
|
|
36
|
-
public translateWithDefault(key: string, parameters: Record<string, unknown>, defaultMessage: string): string;
|
|
37
41
|
public translateWithDefault(
|
|
38
42
|
key: string,
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
defaultMessage: string,
|
|
44
|
+
parameters: Record<string, unknown> | number = {},
|
|
41
45
|
): string {
|
|
42
|
-
|
|
46
|
+
return this.provider.translateWithDefault(key, defaultMessage, parameters);
|
|
47
|
+
}
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
const
|
|
49
|
+
public getBrowserLocale(): string {
|
|
50
|
+
const locales = this.getState('locales');
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
return navigator.languages.find((locale) => locales.includes(locale)) ?? 'en';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected override async boot(): Promise<void> {
|
|
56
|
+
if (!globalThis.document) {
|
|
57
|
+
return;
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
|
|
60
|
+
this.requireStore().$subscribe(
|
|
61
|
+
async () => {
|
|
62
|
+
await this.provider.setLocale(this.locale ?? this.getBrowserLocale());
|
|
63
|
+
await this.provider.setFallbackLocale(this.fallbackLocale);
|
|
64
|
+
|
|
65
|
+
this.locale
|
|
66
|
+
? document.querySelector('html')?.setAttribute('lang', this.locale)
|
|
67
|
+
: document.querySelector('html')?.removeAttribute('lang');
|
|
68
|
+
},
|
|
69
|
+
{ immediate: true },
|
|
70
|
+
);
|
|
56
71
|
}
|
|
57
72
|
|
|
58
73
|
}
|
|
59
74
|
|
|
60
|
-
export default facade(
|
|
75
|
+
export default facade(LangService);
|
package/src/lang/index.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import App from '@aerogel/core/services/App';
|
|
2
|
+
import { bootServices } from '@aerogel/core/services';
|
|
3
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
3
4
|
|
|
4
|
-
import Lang
|
|
5
|
+
import Lang from './Lang';
|
|
6
|
+
import settings from './settings';
|
|
7
|
+
import type { LangProvider } from './Lang';
|
|
5
8
|
import { translate, translateWithDefault } from './utils';
|
|
6
9
|
|
|
7
|
-
export { Lang,
|
|
10
|
+
export { Lang, translate, translateWithDefault };
|
|
11
|
+
export type { LangProvider };
|
|
8
12
|
|
|
9
13
|
const services = { $lang: Lang };
|
|
10
14
|
|
|
@@ -15,15 +19,17 @@ export default definePlugin({
|
|
|
15
19
|
app.config.globalProperties.$t ??= translate;
|
|
16
20
|
app.config.globalProperties.$td = translateWithDefault;
|
|
17
21
|
|
|
22
|
+
settings.forEach((setting) => App.addSetting(setting));
|
|
23
|
+
|
|
18
24
|
await bootServices(app, services);
|
|
19
25
|
},
|
|
20
26
|
});
|
|
21
27
|
|
|
22
|
-
declare module '
|
|
28
|
+
declare module '@aerogel/core/services' {
|
|
23
29
|
export interface Services extends LangServices {}
|
|
24
30
|
}
|
|
25
31
|
|
|
26
|
-
declare module '
|
|
32
|
+
declare module 'vue' {
|
|
27
33
|
interface ComponentCustomProperties {
|
|
28
34
|
$td: typeof translateWithDefault;
|
|
29
35
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<Select
|
|
3
|
+
v-model="$lang.locale"
|
|
4
|
+
class="flex flex-col items-start md:flex-row"
|
|
5
|
+
as="div"
|
|
6
|
+
:options
|
|
7
|
+
:render-option="renderLocale"
|
|
8
|
+
>
|
|
9
|
+
<div class="grow">
|
|
10
|
+
<SelectLabel>
|
|
11
|
+
{{ $td('settings.locale', 'Language') }}
|
|
12
|
+
</SelectLabel>
|
|
13
|
+
<Markdown
|
|
14
|
+
lang-key="settings.localeDescription"
|
|
15
|
+
lang-default="Choose the application's language."
|
|
16
|
+
class="mt-1 text-sm text-gray-500"
|
|
17
|
+
/>
|
|
18
|
+
</div>
|
|
19
|
+
<Button variant="ghost" :as="SelectTrigger" class="grid w-auto outline-none" />
|
|
20
|
+
<SelectOptions />
|
|
21
|
+
</Select>
|
|
22
|
+
</template>
|
|
23
|
+
|
|
24
|
+
<script setup lang="ts">
|
|
25
|
+
import Aerogel from 'virtual:aerogel';
|
|
26
|
+
|
|
27
|
+
import { computed } from 'vue';
|
|
28
|
+
|
|
29
|
+
import Markdown from '@aerogel/core/components/ui/Markdown.vue';
|
|
30
|
+
import Button from '@aerogel/core/components/ui/Button.vue';
|
|
31
|
+
import Select from '@aerogel/core/components/ui/Select.vue';
|
|
32
|
+
import SelectLabel from '@aerogel/core/components/ui/SelectLabel.vue';
|
|
33
|
+
import SelectTrigger from '@aerogel/core/components/ui/SelectTrigger.vue';
|
|
34
|
+
import SelectOptions from '@aerogel/core/components/ui/SelectOptions.vue';
|
|
35
|
+
import { Lang, translateWithDefault } from '@aerogel/core/lang';
|
|
36
|
+
|
|
37
|
+
const browserLocale = Lang.getBrowserLocale();
|
|
38
|
+
const options = computed(() => [null, ...Lang.locales]);
|
|
39
|
+
|
|
40
|
+
function renderLocale(locale: string | null): string {
|
|
41
|
+
return (
|
|
42
|
+
(locale && Aerogel.locales[locale]) ??
|
|
43
|
+
translateWithDefault('settings.localeDefault', '{locale} (default)', {
|
|
44
|
+
locale: Aerogel.locales[browserLocale] ?? browserLocale,
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
</script>
|
package/src/plugins/Plugin.ts
CHANGED
package/src/plugins/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
-
import App from '
|
|
3
|
+
import App from '@aerogel/core/services/App';
|
|
4
4
|
|
|
5
5
|
import type { Plugin } from './Plugin';
|
|
6
6
|
|
|
@@ -13,13 +13,16 @@ export function definePlugin<T extends Plugin>(plugin: T): T {
|
|
|
13
13
|
export async function installPlugins(plugins: Plugin[], ...args: GetClosureArgs<Plugin['install']>): Promise<void> {
|
|
14
14
|
App.setState(
|
|
15
15
|
'plugins',
|
|
16
|
-
plugins.reduce(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
plugins.reduce(
|
|
17
|
+
(pluginsMap, plugin) => {
|
|
18
|
+
if (plugin.name) {
|
|
19
|
+
pluginsMap[plugin.name] = plugin;
|
|
20
|
+
}
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
return pluginsMap;
|
|
23
|
+
},
|
|
24
|
+
{} as Record<string, Plugin>,
|
|
25
|
+
),
|
|
23
26
|
);
|
|
24
27
|
|
|
25
28
|
await Promise.all(plugins.map((plugin) => plugin.install(...args)) ?? []);
|
|
@@ -1,18 +1,50 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
3
|
+
import { getEnv } from '@noeldemartin/utils';
|
|
4
|
+
import type { App, Component } from 'vue';
|
|
5
|
+
|
|
6
|
+
import { defineServiceState } from '@aerogel/core/services/Service';
|
|
7
|
+
import type { Plugin } from '@aerogel/core/plugins/Plugin';
|
|
8
|
+
|
|
9
|
+
export interface AppSetting {
|
|
10
|
+
component: Component;
|
|
11
|
+
priority: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function defineSettings<T extends AppSetting[]>(settings: T): T {
|
|
15
|
+
return settings;
|
|
16
|
+
}
|
|
5
17
|
|
|
6
18
|
export default defineServiceState({
|
|
7
19
|
name: 'app',
|
|
8
20
|
initialState: {
|
|
9
21
|
plugins: {} as Record<string, Plugin>,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
22
|
+
instance: null as App | null,
|
|
23
|
+
environment: getEnv() ?? 'development',
|
|
24
|
+
version: Aerogel.version,
|
|
25
|
+
sourceUrl: Aerogel.sourceUrl,
|
|
26
|
+
settings: [] as AppSetting[],
|
|
13
27
|
},
|
|
14
28
|
computed: {
|
|
15
29
|
development: (state) => state.environment === 'development',
|
|
16
|
-
|
|
30
|
+
staging: (state) => state.environment === 'staging',
|
|
31
|
+
testing: (state) => state.environment === 'test' || state.environment === 'testing',
|
|
32
|
+
versionName(state): string {
|
|
33
|
+
if (this.development) {
|
|
34
|
+
return 'dev.' + Aerogel.sourceHash.toString().substring(0, 7);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (this.staging) {
|
|
38
|
+
return 'staging.' + Aerogel.sourceHash.toString().substring(0, 7);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return `v${state.version}`;
|
|
42
|
+
},
|
|
43
|
+
versionUrl(state): string {
|
|
44
|
+
return (
|
|
45
|
+
state.sourceUrl +
|
|
46
|
+
(this.development || this.staging ? `/tree/${Aerogel.sourceHash}` : `/releases/tag/${this.versionName}`)
|
|
47
|
+
);
|
|
48
|
+
},
|
|
17
49
|
},
|
|
18
50
|
});
|
package/src/services/App.ts
CHANGED
|
@@ -1,20 +1,63 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
|
|
4
|
+
import { markRaw } from 'vue';
|
|
5
|
+
|
|
6
|
+
import Events from '@aerogel/core/services/Events';
|
|
7
|
+
import type { Plugin } from '@aerogel/core/plugins';
|
|
8
|
+
import type { AppSetting, Services } from '@aerogel/core/services';
|
|
5
9
|
|
|
6
10
|
import Service from './App.state';
|
|
7
11
|
|
|
12
|
+
export { defineSettings } from './App.state';
|
|
13
|
+
export type { AppSetting } from './App.state';
|
|
14
|
+
|
|
8
15
|
export class AppService extends Service {
|
|
9
16
|
|
|
17
|
+
public readonly name = Aerogel.name;
|
|
18
|
+
public readonly ready = new PromisedValue<void>();
|
|
19
|
+
public readonly mounted = new PromisedValue<void>();
|
|
20
|
+
|
|
21
|
+
public isReady(): boolean {
|
|
22
|
+
return this.ready.isResolved();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public isMounted(): boolean {
|
|
26
|
+
return this.mounted.isResolved();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public addSetting(setting: AppSetting): void {
|
|
30
|
+
this.settings.push(markRaw(setting));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async whenReady<T>(callback: () => T): Promise<T> {
|
|
34
|
+
const result = await this.ready.then(callback);
|
|
35
|
+
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
|
|
40
|
+
queryParameters && updateLocationQueryParameters(queryParameters);
|
|
41
|
+
|
|
42
|
+
location.reload();
|
|
43
|
+
|
|
44
|
+
// Stall until the reload happens
|
|
45
|
+
await forever();
|
|
46
|
+
}
|
|
47
|
+
|
|
10
48
|
public plugin<T extends Plugin = Plugin>(name: string): T | null {
|
|
11
49
|
return (this.plugins[name] as T) ?? null;
|
|
12
50
|
}
|
|
13
51
|
|
|
14
|
-
|
|
15
|
-
|
|
52
|
+
public service<T extends keyof Services>(name: T): Services[T] | null {
|
|
53
|
+
return this.instance?.config.globalProperties[name] ?? null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected override async boot(): Promise<void> {
|
|
57
|
+
Events.once('application-ready', () => this.ready.resolve());
|
|
58
|
+
Events.once('application-mounted', () => this.mounted.resolve());
|
|
16
59
|
}
|
|
17
60
|
|
|
18
61
|
}
|
|
19
62
|
|
|
20
|
-
export default facade(
|
|
63
|
+
export default facade(AppService);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { PromisedValue, facade, tap } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Service from '@aerogel/core/services/Service';
|
|
4
|
+
|
|
5
|
+
export class CacheService extends Service {
|
|
6
|
+
|
|
7
|
+
private cache?: PromisedValue<Cache> = undefined;
|
|
8
|
+
|
|
9
|
+
public async get(url: string): Promise<Response | null> {
|
|
10
|
+
const cache = await this.open();
|
|
11
|
+
const response = await cache.match(url);
|
|
12
|
+
|
|
13
|
+
return response ?? null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public async store(url: string, response: Response): Promise<void> {
|
|
17
|
+
const cache = await this.open();
|
|
18
|
+
|
|
19
|
+
await cache.put(url, response);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public async replace(url: string, response: Response): Promise<void> {
|
|
23
|
+
const cache = await this.open();
|
|
24
|
+
const keys = await cache.keys(url);
|
|
25
|
+
|
|
26
|
+
if (keys.length === 0) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await cache.put(url, response);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected async open(): Promise<Cache> {
|
|
34
|
+
return (this.cache =
|
|
35
|
+
this.cache ??
|
|
36
|
+
tap(new PromisedValue<Cache>(), (cache) => {
|
|
37
|
+
caches.open('app').then((instance) => cache.resolve(instance));
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export default facade(CacheService);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import Events, { EventListenerPriorities } from './Events';
|
|
4
|
+
|
|
5
|
+
describe('Events', () => {
|
|
6
|
+
|
|
7
|
+
beforeEach(() => void Events.reset());
|
|
8
|
+
|
|
9
|
+
it('registers listeners', async () => {
|
|
10
|
+
// Arrange
|
|
11
|
+
let counter = 0;
|
|
12
|
+
|
|
13
|
+
Events.on('application-mounted', () => counter++);
|
|
14
|
+
|
|
15
|
+
// Act
|
|
16
|
+
await Events.emit('application-mounted');
|
|
17
|
+
await Events.emit('application-mounted');
|
|
18
|
+
await Events.emit('application-mounted');
|
|
19
|
+
|
|
20
|
+
// Assert
|
|
21
|
+
expect(counter).toEqual(3);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('triggers listeners by priority', async () => {
|
|
25
|
+
// Arrange
|
|
26
|
+
const storage: string[] = [];
|
|
27
|
+
|
|
28
|
+
Events.on('application-mounted', () => storage.push('second'));
|
|
29
|
+
Events.on('application-mounted', { priority: EventListenerPriorities.Low }, () => storage.push('third'));
|
|
30
|
+
Events.on('application-mounted', { priority: EventListenerPriorities.High }, () => storage.push('first'));
|
|
31
|
+
|
|
32
|
+
// Act
|
|
33
|
+
await Events.emit('application-mounted');
|
|
34
|
+
|
|
35
|
+
// Assert
|
|
36
|
+
expect(storage).toEqual(['first', 'second', 'third']);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
});
|
package/src/services/Events.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { FluentArray } from '@noeldemartin/utils';
|
|
1
|
+
import { arrayRemove, facade, fail, tap } from '@noeldemartin/utils';
|
|
3
2
|
|
|
4
|
-
import Service from '
|
|
3
|
+
import Service from '@aerogel/core/services/Service';
|
|
5
4
|
|
|
6
5
|
export interface EventsPayload {}
|
|
6
|
+
export interface EventListenerOptions {
|
|
7
|
+
priority: EventListenerPriority;
|
|
8
|
+
}
|
|
9
|
+
export type AerogelGlobalEvents = Partial<{ [Event in EventWithoutPayload]: () => unknown }> &
|
|
10
|
+
Partial<{ [Event in EventWithPayload]: EventListener<EventsPayload[Event]> }>;
|
|
7
11
|
|
|
8
12
|
export type EventListener<T = unknown> = (payload: T) => unknown;
|
|
9
|
-
export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
|
|
10
13
|
|
|
11
14
|
export type EventWithoutPayload = {
|
|
12
15
|
[K in keyof EventsPayload]: EventsPayload[K] extends void ? K : never;
|
|
@@ -16,70 +19,141 @@ export type EventWithPayload = {
|
|
|
16
19
|
[K in keyof EventsPayload]: EventsPayload[K] extends void ? never : K;
|
|
17
20
|
}[keyof EventsPayload];
|
|
18
21
|
|
|
22
|
+
export const EventListenerPriorities = {
|
|
23
|
+
Low: -256,
|
|
24
|
+
Default: 0,
|
|
25
|
+
High: 256,
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
export type EventListenerPriority = (typeof EventListenerPriorities)[keyof typeof EventListenerPriorities];
|
|
29
|
+
|
|
19
30
|
export class EventsService extends Service {
|
|
20
31
|
|
|
21
|
-
private listeners: Record<string,
|
|
32
|
+
private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
|
|
33
|
+
|
|
34
|
+
protected override async boot(): Promise<void> {
|
|
35
|
+
Object.entries(globalThis.__aerogelEvents__ ?? {}).forEach(([event, listener]) =>
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
this.on(event as any, listener as EventListener));
|
|
38
|
+
}
|
|
22
39
|
|
|
23
40
|
public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
|
|
24
41
|
public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
|
|
25
|
-
public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
|
|
26
42
|
public async emit(event: string, payload?: unknown): Promise<void> {
|
|
27
|
-
const listeners =
|
|
43
|
+
const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
|
|
28
44
|
|
|
29
|
-
|
|
45
|
+
for (const priority of listeners.priorities) {
|
|
46
|
+
await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
|
|
47
|
+
}
|
|
30
48
|
}
|
|
31
49
|
|
|
50
|
+
/* eslint-disable max-len */
|
|
32
51
|
public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
33
|
-
public on<Event extends
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
): () => void | void;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
public on<Event extends EventWithoutPayload>(event: Event, priority: EventListenerPriority, listener: () => unknown): () => void; // prettier-ignore
|
|
53
|
+
public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
54
|
+
public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
55
|
+
public on<Event extends EventWithPayload>(event: Event, priority: EventListenerPriority, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
56
|
+
public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
57
|
+
/* eslint-enable max-len */
|
|
58
|
+
|
|
59
|
+
public on(
|
|
60
|
+
event: string,
|
|
61
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListenerPriority | EventListener,
|
|
62
|
+
listener?: EventListener,
|
|
63
|
+
): () => void {
|
|
64
|
+
const options =
|
|
65
|
+
typeof optionsOrListener === 'function'
|
|
66
|
+
? {}
|
|
67
|
+
: typeof optionsOrListener === 'number'
|
|
68
|
+
? { priority: optionsOrListener }
|
|
69
|
+
: optionsOrListener;
|
|
70
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
71
|
+
|
|
72
|
+
this.registerListener(event, options, handler);
|
|
73
|
+
|
|
74
|
+
return () => this.off(event, handler);
|
|
43
75
|
}
|
|
44
76
|
|
|
77
|
+
/* eslint-disable max-len */
|
|
45
78
|
public once<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
46
|
-
public once<Event extends
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
public once
|
|
52
|
-
|
|
79
|
+
public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
80
|
+
public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
81
|
+
public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
82
|
+
/* eslint-enable max-len */
|
|
83
|
+
|
|
84
|
+
public once(
|
|
85
|
+
event: string,
|
|
86
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
87
|
+
listener?: EventListener,
|
|
88
|
+
): () => void {
|
|
53
89
|
let onceListener: EventListener | null = null;
|
|
90
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
91
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
54
92
|
|
|
55
93
|
return tap(
|
|
56
94
|
() => onceListener && this.off(event, onceListener),
|
|
57
95
|
(off) => {
|
|
58
|
-
|
|
59
|
-
(
|
|
60
|
-
off();
|
|
96
|
+
onceListener = (...args) => {
|
|
97
|
+
off();
|
|
61
98
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
99
|
+
return handler(...args);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
this.registerListener(event, options, handler);
|
|
65
103
|
},
|
|
66
104
|
);
|
|
67
105
|
}
|
|
68
106
|
|
|
69
107
|
public off(event: string, listener: EventListener): void {
|
|
70
|
-
const
|
|
108
|
+
const listeners = this.listeners[event];
|
|
71
109
|
|
|
72
|
-
if (!
|
|
110
|
+
if (!listeners) {
|
|
73
111
|
return;
|
|
74
112
|
}
|
|
75
113
|
|
|
76
|
-
|
|
114
|
+
const priorities = [...listeners.priorities];
|
|
115
|
+
|
|
116
|
+
for (const priority of priorities) {
|
|
117
|
+
arrayRemove(listeners.handlers[priority] ?? [], listener);
|
|
77
118
|
|
|
78
|
-
|
|
119
|
+
if (listeners.handlers[priority]?.length === 0) {
|
|
120
|
+
delete listeners.handlers[priority];
|
|
121
|
+
arrayRemove(listeners.priorities, priority);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (listeners.priorities.length === 0) {
|
|
79
126
|
delete this.listeners[event];
|
|
80
127
|
}
|
|
81
128
|
}
|
|
82
129
|
|
|
130
|
+
protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
|
|
131
|
+
const priority = options.priority ?? 0;
|
|
132
|
+
|
|
133
|
+
if (!(event in this.listeners)) {
|
|
134
|
+
this.listeners[event] = { priorities: [], handlers: {} };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const priorities =
|
|
138
|
+
this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
|
|
139
|
+
const handlers =
|
|
140
|
+
this.listeners[event]?.handlers ??
|
|
141
|
+
fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
|
|
142
|
+
|
|
143
|
+
if (!priorities.includes(priority)) {
|
|
144
|
+
priorities.push(priority);
|
|
145
|
+
priorities.sort((a, b) => b - a);
|
|
146
|
+
handlers[priority] = [];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
handlers[priority]?.push(handler);
|
|
150
|
+
}
|
|
151
|
+
|
|
83
152
|
}
|
|
84
153
|
|
|
85
|
-
export default facade(
|
|
154
|
+
export default facade(EventsService);
|
|
155
|
+
|
|
156
|
+
declare global {
|
|
157
|
+
// eslint-disable-next-line no-var
|
|
158
|
+
var __aerogelEvents__: AerogelGlobalEvents | undefined;
|
|
159
|
+
}
|