@aerogel/core 0.0.0-next.fd1bd21aea7a9ab8c4eab69a5f5776db5de8bf35 → 0.1.0
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 +2263 -1698
- package/dist/aerogel-core.js +3809 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +32 -37
- package/src/bootstrap/bootstrap.test.ts +4 -8
- package/src/bootstrap/index.ts +25 -16
- package/src/bootstrap/options.ts +1 -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 +19 -0
- package/src/components/contracts/Button.ts +16 -0
- package/src/components/contracts/ConfirmModal.ts +48 -0
- package/src/components/contracts/DropdownMenu.ts +25 -0
- package/src/components/contracts/ErrorReportModal.ts +33 -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/{forms/AGHeadlessInputDescription.vue → HeadlessInputDescription.vue} +7 -8
- package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
- package/src/components/headless/HeadlessInputInput.vue +86 -0
- package/src/components/headless/{forms/AGHeadlessInputLabel.vue → HeadlessInputLabel.vue} +3 -7
- package/src/components/headless/{forms/AGHeadlessInputTextArea.vue → HeadlessInputTextArea.vue} +11 -11
- 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/{forms/AGHeadlessSelectError.vue → HeadlessSelectError.vue} +5 -6
- 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/HeadlessSwitch.vue +96 -0
- package/src/components/headless/HeadlessToast.vue +18 -0
- package/src/components/headless/HeadlessToastAction.vue +13 -0
- package/src/components/headless/index.ts +20 -3
- package/src/components/index.ts +6 -9
- package/src/components/ui/AdvancedOptions.vue +18 -0
- package/src/components/ui/AlertModal.vue +17 -0
- package/src/components/ui/Button.vue +115 -0
- package/src/components/ui/Checkbox.vue +56 -0
- package/src/components/ui/ConfirmModal.vue +50 -0
- package/src/components/ui/DropdownMenu.vue +32 -0
- package/src/components/ui/DropdownMenuOption.vue +22 -0
- package/src/components/ui/DropdownMenuOptions.vue +44 -0
- package/src/components/ui/EditableContent.vue +82 -0
- package/src/components/ui/ErrorLogs.vue +19 -0
- package/src/components/ui/ErrorLogsModal.vue +48 -0
- package/src/components/ui/ErrorMessage.vue +15 -0
- package/src/components/ui/ErrorReportModal.vue +73 -0
- package/src/components/{modals/AGErrorReportModalButtons.vue → ui/ErrorReportModalButtons.vue} +34 -27
- package/src/components/ui/ErrorReportModalTitle.vue +24 -0
- package/src/components/{forms/AGForm.vue → ui/Form.vue} +4 -5
- 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 +97 -0
- package/src/components/ui/Modal.vue +131 -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 +21 -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/Setting.vue +31 -0
- package/src/components/ui/SettingsModal.vue +15 -0
- package/src/components/ui/StartupCrash.vue +76 -0
- package/src/components/ui/Switch.vue +11 -0
- package/src/components/ui/TextArea.vue +56 -0
- package/src/components/ui/Toast.vue +46 -0
- package/src/components/ui/index.ts +35 -0
- package/src/directives/index.ts +9 -5
- package/src/directives/measure.ts +33 -8
- package/src/errors/Errors.state.ts +2 -1
- package/src/errors/Errors.ts +56 -33
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +15 -8
- package/src/errors/settings/Debug.vue +14 -0
- package/src/errors/settings/index.ts +10 -0
- package/src/errors/utils.ts +17 -1
- package/src/forms/FormController.test.ts +113 -0
- package/src/forms/{Form.ts → FormController.ts} +73 -42
- package/src/forms/index.ts +3 -3
- package/src/forms/utils.ts +65 -24
- package/src/forms/validation.ts +50 -0
- package/src/index.css +76 -0
- package/src/jobs/Job.ts +144 -2
- package/src/jobs/index.ts +4 -1
- 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 +48 -21
- 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 +36 -3
- package/src/services/App.ts +19 -3
- package/src/services/Cache.ts +1 -1
- package/src/services/Events.test.ts +8 -8
- package/src/services/Events.ts +16 -12
- package/src/services/Service.ts +135 -59
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +16 -7
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +8 -3
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +14 -12
- package/src/ui/UI.ts +250 -123
- package/src/ui/index.ts +28 -28
- package/src/ui/utils.ts +16 -0
- package/src/utils/app.ts +7 -0
- package/src/utils/classes.ts +41 -0
- package/src/utils/composition/events.ts +4 -6
- 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 +6 -1
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +53 -6
- package/src/utils/types.ts +3 -0
- package/src/utils/vue.ts +38 -132
- 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/histoire.config.ts +0 -7
- package/noeldemartin.config.js +0 -5
- package/postcss.config.js +0 -6
- package/src/assets/histoire.css +0 -3
- package/src/components/AGAppLayout.vue +0 -16
- package/src/components/AGAppOverlays.vue +0 -41
- package/src/components/AGAppSnackbars.vue +0 -13
- package/src/components/constants.ts +0 -8
- package/src/components/forms/AGButton.vue +0 -44
- package/src/components/forms/AGCheckbox.vue +0 -41
- package/src/components/forms/AGInput.vue +0 -40
- package/src/components/forms/AGSelect.story.vue +0 -46
- package/src/components/forms/AGSelect.vue +0 -60
- package/src/components/forms/index.ts +0 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +0 -56
- package/src/components/headless/forms/AGHeadlessInput.ts +0 -32
- package/src/components/headless/forms/AGHeadlessInput.vue +0 -57
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -81
- package/src/components/headless/forms/AGHeadlessSelect.ts +0 -42
- package/src/components/headless/forms/AGHeadlessSelect.vue +0 -77
- package/src/components/headless/forms/AGHeadlessSelectButton.vue +0 -24
- package/src/components/headless/forms/AGHeadlessSelectLabel.vue +0 -24
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +0 -4
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +0 -39
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +0 -3
- package/src/components/headless/forms/composition.ts +0 -10
- package/src/components/headless/forms/index.ts +0 -17
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -34
- package/src/components/headless/modals/AGHeadlessModal.vue +0 -86
- 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 -4
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +0 -10
- package/src/components/headless/snackbars/index.ts +0 -40
- package/src/components/lib/AGErrorMessage.vue +0 -16
- package/src/components/lib/AGLink.vue +0 -9
- package/src/components/lib/AGMarkdown.vue +0 -41
- package/src/components/lib/AGMeasured.vue +0 -15
- package/src/components/lib/AGStartupCrash.vue +0 -31
- package/src/components/lib/index.ts +0 -5
- package/src/components/modals/AGAlertModal.ts +0 -15
- package/src/components/modals/AGAlertModal.vue +0 -14
- package/src/components/modals/AGConfirmModal.ts +0 -27
- package/src/components/modals/AGConfirmModal.vue +0 -26
- package/src/components/modals/AGErrorReportModal.ts +0 -46
- package/src/components/modals/AGErrorReportModal.vue +0 -54
- package/src/components/modals/AGErrorReportModalTitle.vue +0 -25
- package/src/components/modals/AGLoadingModal.ts +0 -23
- package/src/components/modals/AGLoadingModal.vue +0 -15
- package/src/components/modals/AGModal.ts +0 -10
- package/src/components/modals/AGModal.vue +0 -39
- 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/AGPromptModal.ts +0 -30
- package/src/components/modals/AGPromptModal.vue +0 -34
- package/src/components/modals/index.ts +0 -17
- package/src/components/snackbars/AGSnackbar.vue +0 -36
- package/src/components/snackbars/index.ts +0 -3
- package/src/components/utils.ts +0 -10
- package/src/directives/initial-focus.ts +0 -11
- package/src/forms/Form.test.ts +0 -58
- package/src/forms/composition.ts +0 -6
- package/src/main.histoire.ts +0 -1
- package/src/utils/tailwindcss.test.ts +0 -26
- package/src/utils/tailwindcss.ts +0 -7
- package/tailwind.config.js +0 -4
- package/tsconfig.json +0 -11
- package/vite.config.ts +0 -14
- /package/src/{main.ts → index.ts} +0 -0
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,17 +1,50 @@
|
|
|
1
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
|
-
|
|
22
|
+
instance: null as App | null,
|
|
23
|
+
environment: getEnv() ?? 'development',
|
|
24
|
+
version: Aerogel.version,
|
|
11
25
|
sourceUrl: Aerogel.sourceUrl,
|
|
26
|
+
settings: [] as AppSetting[],
|
|
12
27
|
},
|
|
13
28
|
computed: {
|
|
14
29
|
development: (state) => state.environment === 'development',
|
|
30
|
+
staging: (state) => state.environment === 'staging',
|
|
15
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
|
+
},
|
|
16
49
|
},
|
|
17
50
|
});
|
package/src/services/App.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
|
+
|
|
1
3
|
import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
|
|
4
|
+
import { markRaw } from 'vue';
|
|
2
5
|
|
|
3
|
-
import Events from '
|
|
4
|
-
import type { Plugin } from '
|
|
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;
|
|
10
18
|
public readonly ready = new PromisedValue<void>();
|
|
11
19
|
public readonly mounted = new PromisedValue<void>();
|
|
12
20
|
|
|
@@ -18,6 +26,10 @@ export class AppService extends Service {
|
|
|
18
26
|
return this.mounted.isResolved();
|
|
19
27
|
}
|
|
20
28
|
|
|
29
|
+
public addSetting(setting: AppSetting): void {
|
|
30
|
+
this.settings.push(markRaw(setting));
|
|
31
|
+
}
|
|
32
|
+
|
|
21
33
|
public async whenReady<T>(callback: () => T): Promise<T> {
|
|
22
34
|
const result = await this.ready.then(callback);
|
|
23
35
|
|
|
@@ -37,7 +49,11 @@ export class AppService extends Service {
|
|
|
37
49
|
return (this.plugins[name] as T) ?? null;
|
|
38
50
|
}
|
|
39
51
|
|
|
40
|
-
|
|
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> {
|
|
41
57
|
Events.once('application-ready', () => this.ready.resolve());
|
|
42
58
|
Events.once('application-mounted', () => this.mounted.resolve());
|
|
43
59
|
}
|
package/src/services/Cache.ts
CHANGED
|
@@ -10,12 +10,12 @@ describe('Events', () => {
|
|
|
10
10
|
// Arrange
|
|
11
11
|
let counter = 0;
|
|
12
12
|
|
|
13
|
-
Events.on('
|
|
13
|
+
Events.on('application-mounted', () => counter++);
|
|
14
14
|
|
|
15
15
|
// Act
|
|
16
|
-
await Events.emit('
|
|
17
|
-
await Events.emit('
|
|
18
|
-
await Events.emit('
|
|
16
|
+
await Events.emit('application-mounted');
|
|
17
|
+
await Events.emit('application-mounted');
|
|
18
|
+
await Events.emit('application-mounted');
|
|
19
19
|
|
|
20
20
|
// Assert
|
|
21
21
|
expect(counter).toEqual(3);
|
|
@@ -25,12 +25,12 @@ describe('Events', () => {
|
|
|
25
25
|
// Arrange
|
|
26
26
|
const storage: string[] = [];
|
|
27
27
|
|
|
28
|
-
Events.on('
|
|
29
|
-
Events.on('
|
|
30
|
-
Events.on('
|
|
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
31
|
|
|
32
32
|
// Act
|
|
33
|
-
await Events.emit('
|
|
33
|
+
await Events.emit('application-mounted');
|
|
34
34
|
|
|
35
35
|
// Assert
|
|
36
36
|
expect(storage).toEqual(['first', 'second', 'third']);
|
package/src/services/Events.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { arrayRemove, facade, fail, tap } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
-
import Service from '
|
|
3
|
+
import Service from '@aerogel/core/services/Service';
|
|
4
4
|
|
|
5
5
|
export interface EventsPayload {}
|
|
6
6
|
export interface EventListenerOptions {
|
|
7
|
-
priority:
|
|
7
|
+
priority: EventListenerPriority;
|
|
8
8
|
}
|
|
9
9
|
export type AerogelGlobalEvents = Partial<{ [Event in EventWithoutPayload]: () => unknown }> &
|
|
10
10
|
Partial<{ [Event in EventWithPayload]: EventListener<EventsPayload[Event]> }>;
|
|
11
11
|
|
|
12
12
|
export type EventListener<T = unknown> = (payload: T) => unknown;
|
|
13
|
-
export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
|
|
14
13
|
|
|
15
14
|
export type EventWithoutPayload = {
|
|
16
15
|
[K in keyof EventsPayload]: EventsPayload[K] extends void ? K : never;
|
|
@@ -26,18 +25,20 @@ export const EventListenerPriorities = {
|
|
|
26
25
|
High: 256,
|
|
27
26
|
} as const;
|
|
28
27
|
|
|
28
|
+
export type EventListenerPriority = (typeof EventListenerPriorities)[keyof typeof EventListenerPriorities];
|
|
29
|
+
|
|
29
30
|
export class EventsService extends Service {
|
|
30
31
|
|
|
31
32
|
private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
|
|
32
33
|
|
|
33
|
-
protected async boot(): Promise<void> {
|
|
34
|
+
protected override async boot(): Promise<void> {
|
|
34
35
|
Object.entries(globalThis.__aerogelEvents__ ?? {}).forEach(([event, listener]) =>
|
|
35
|
-
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
this.on(event as any, listener as EventListener));
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
|
|
39
41
|
public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
|
|
40
|
-
public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
|
|
41
42
|
public async emit(event: string, payload?: unknown): Promise<void> {
|
|
42
43
|
const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
|
|
43
44
|
|
|
@@ -48,19 +49,24 @@ export class EventsService extends Service {
|
|
|
48
49
|
|
|
49
50
|
/* eslint-disable max-len */
|
|
50
51
|
public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
52
|
+
public on<Event extends EventWithoutPayload>(event: Event, priority: EventListenerPriority, listener: () => unknown): () => void; // prettier-ignore
|
|
51
53
|
public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
52
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
|
|
53
56
|
public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
54
|
-
public on<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
55
|
-
public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
56
57
|
/* eslint-enable max-len */
|
|
57
58
|
|
|
58
59
|
public on(
|
|
59
60
|
event: string,
|
|
60
|
-
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
61
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListenerPriority | EventListener,
|
|
61
62
|
listener?: EventListener,
|
|
62
63
|
): () => void {
|
|
63
|
-
const options =
|
|
64
|
+
const options =
|
|
65
|
+
typeof optionsOrListener === 'function'
|
|
66
|
+
? {}
|
|
67
|
+
: typeof optionsOrListener === 'number'
|
|
68
|
+
? { priority: optionsOrListener }
|
|
69
|
+
: optionsOrListener;
|
|
64
70
|
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
65
71
|
|
|
66
72
|
this.registerListener(event, options, handler);
|
|
@@ -73,8 +79,6 @@ export class EventsService extends Service {
|
|
|
73
79
|
public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
74
80
|
public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
75
81
|
public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
76
|
-
public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
77
|
-
public once<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
78
82
|
/* eslint-enable max-len */
|
|
79
83
|
|
|
80
84
|
public once(
|
package/src/services/Service.ts
CHANGED
|
@@ -1,49 +1,69 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
MagicObject,
|
|
3
|
+
PromisedValue,
|
|
4
|
+
Storage,
|
|
5
|
+
arrayFrom,
|
|
6
|
+
fail,
|
|
7
|
+
isEmpty,
|
|
8
|
+
objectDeepClone,
|
|
9
|
+
objectOnly,
|
|
10
|
+
} from '@noeldemartin/utils';
|
|
11
|
+
import type { Constructor, Nullable } from '@noeldemartin/utils';
|
|
4
12
|
import type { Store } from 'pinia';
|
|
5
13
|
|
|
6
|
-
import ServiceBootError from '
|
|
7
|
-
import {
|
|
14
|
+
import ServiceBootError from '@aerogel/core/errors/ServiceBootError';
|
|
15
|
+
import { appNamespace } from '@aerogel/core/utils/app';
|
|
16
|
+
import { defineServiceStore } from '@aerogel/core/services/store';
|
|
17
|
+
import type { Unref } from '@aerogel/core/utils/vue';
|
|
8
18
|
|
|
9
19
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
20
|
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
11
21
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
12
|
-
export type UnrefServiceState<State extends ServiceState> = {
|
|
13
|
-
[K in keyof State]: State[K] extends MaybeRef<infer T> ? T : State[K];
|
|
14
|
-
};
|
|
15
22
|
|
|
16
23
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
17
|
-
[K in keyof TComputedState]: (state:
|
|
24
|
+
[K in keyof TComputedState]: (state: Unref<TState>) => TComputedState[K];
|
|
18
25
|
} & ThisType<{
|
|
19
26
|
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
20
27
|
}>;
|
|
21
28
|
|
|
29
|
+
export type StateWatchers<TService extends Service, TState extends ServiceState> = {
|
|
30
|
+
[K in keyof TState]?: (this: TService, value: TState[K], oldValue: TState[K]) => unknown;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type ServiceWithState<
|
|
34
|
+
State extends ServiceState = ServiceState,
|
|
35
|
+
ComputedState extends ServiceState = {},
|
|
36
|
+
ServiceStorage = Partial<State>,
|
|
37
|
+
> = Constructor<Unref<State>> &
|
|
38
|
+
Constructor<ComputedState> &
|
|
39
|
+
Constructor<Service<Unref<State>, ComputedState, Unref<ServiceStorage>>>;
|
|
40
|
+
|
|
22
41
|
export function defineServiceState<
|
|
23
42
|
State extends ServiceState = ServiceState,
|
|
24
|
-
ComputedState extends ServiceState = {}
|
|
43
|
+
ComputedState extends ServiceState = {},
|
|
44
|
+
ServiceStorage = Partial<State>,
|
|
25
45
|
>(options: {
|
|
26
46
|
name: string;
|
|
27
47
|
initialState: State | (() => State);
|
|
28
48
|
persist?: (keyof State)[];
|
|
49
|
+
watch?: StateWatchers<Service, State>;
|
|
29
50
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
30
|
-
serialize?: (state: Partial<State>) =>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return class extends Service<UnrefServiceState<State>, ComputedState> {
|
|
51
|
+
serialize?: (state: Partial<State>) => ServiceStorage;
|
|
52
|
+
restore?: (state: ServiceStorage) => Partial<State>;
|
|
53
|
+
}): ServiceWithState<State, ComputedState, ServiceStorage> {
|
|
54
|
+
return class extends Service<Unref<State>, ComputedState, ServiceStorage> {
|
|
35
55
|
|
|
36
|
-
public static persist = (options.persist as string[]) ?? [];
|
|
56
|
+
public static override persist = (options.persist as string[]) ?? [];
|
|
37
57
|
|
|
38
|
-
protected usesStore(): boolean {
|
|
58
|
+
protected override usesStore(): boolean {
|
|
39
59
|
return true;
|
|
40
60
|
}
|
|
41
61
|
|
|
42
|
-
protected getName(): string | null {
|
|
62
|
+
protected override getName(): string | null {
|
|
43
63
|
return options.name ?? null;
|
|
44
64
|
}
|
|
45
65
|
|
|
46
|
-
protected getInitialState():
|
|
66
|
+
protected override getInitialState(): Unref<State> {
|
|
47
67
|
if (typeof options.initialState === 'function') {
|
|
48
68
|
return options.initialState();
|
|
49
69
|
}
|
|
@@ -64,26 +84,32 @@ export function defineServiceState<
|
|
|
64
84
|
state[key as keyof State] = value;
|
|
65
85
|
|
|
66
86
|
return state;
|
|
67
|
-
}, {} as
|
|
87
|
+
}, {} as Unref<State>);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
protected override getComputedStateDefinition(): ComputedStateDefinition<Unref<State>, ComputedState> {
|
|
91
|
+
return (options.computed ?? {}) as ComputedStateDefinition<Unref<State>, ComputedState>;
|
|
68
92
|
}
|
|
69
93
|
|
|
70
|
-
protected
|
|
71
|
-
return (options.
|
|
94
|
+
protected override getStateWatchers(): StateWatchers<Service, Unref<State>> {
|
|
95
|
+
return (options.watch ?? {}) as StateWatchers<Service, Unref<State>>;
|
|
72
96
|
}
|
|
73
97
|
|
|
74
|
-
protected serializePersistedState(state: Partial<State>):
|
|
75
|
-
return options.serialize?.(state) ?? state;
|
|
98
|
+
protected override serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
99
|
+
return options.serialize?.(state) ?? (state as ServiceStorage);
|
|
76
100
|
}
|
|
77
101
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
102
|
+
protected override deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
103
|
+
return options.restore?.(state) ?? (state as Partial<State>);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
} as unknown as ServiceWithState<State, ComputedState, ServiceStorage>;
|
|
81
107
|
}
|
|
82
108
|
|
|
83
109
|
export default class Service<
|
|
84
110
|
State extends ServiceState = DefaultServiceState,
|
|
85
111
|
ComputedState extends ServiceState = {},
|
|
86
|
-
ServiceStorage
|
|
112
|
+
ServiceStorage = Partial<State>,
|
|
87
113
|
> extends MagicObject {
|
|
88
114
|
|
|
89
115
|
public static persist: string[] = [];
|
|
@@ -91,7 +117,8 @@ export default class Service<
|
|
|
91
117
|
protected _name: string;
|
|
92
118
|
private _booted: PromisedValue<void>;
|
|
93
119
|
private _computedStateKeys: Set<keyof State>;
|
|
94
|
-
private
|
|
120
|
+
private _watchers: StateWatchers<Service, State>;
|
|
121
|
+
private _store: Store<string, State, ComputedState, {}> | false;
|
|
95
122
|
|
|
96
123
|
constructor() {
|
|
97
124
|
super();
|
|
@@ -101,6 +128,7 @@ export default class Service<
|
|
|
101
128
|
this._name = this.getName() ?? new.target.name;
|
|
102
129
|
this._booted = new PromisedValue();
|
|
103
130
|
this._computedStateKeys = new Set(Object.keys(getters));
|
|
131
|
+
this._watchers = this.getStateWatchers();
|
|
104
132
|
this._store =
|
|
105
133
|
this.usesStore() &&
|
|
106
134
|
defineServiceStore(this._name, {
|
|
@@ -115,6 +143,12 @@ export default class Service<
|
|
|
115
143
|
return this._booted;
|
|
116
144
|
}
|
|
117
145
|
|
|
146
|
+
public override static<T extends typeof Service>(): T;
|
|
147
|
+
public override static<T extends typeof Service, K extends keyof T>(property: K): T[K];
|
|
148
|
+
public override static<T extends typeof Service, K extends keyof T>(property?: K): T | T[K] {
|
|
149
|
+
return super.static<T, K>(property as K);
|
|
150
|
+
}
|
|
151
|
+
|
|
118
152
|
public launch(): Promise<void> {
|
|
119
153
|
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
120
154
|
|
|
@@ -131,7 +165,7 @@ export default class Service<
|
|
|
131
165
|
}
|
|
132
166
|
|
|
133
167
|
public hasPersistedState(): boolean {
|
|
134
|
-
return Storage.has(this.
|
|
168
|
+
return Storage.has(this.storageKey);
|
|
135
169
|
}
|
|
136
170
|
|
|
137
171
|
public hasState<P extends keyof State>(property: P): boolean {
|
|
@@ -149,10 +183,10 @@ export default class Service<
|
|
|
149
183
|
const store = this._store as any;
|
|
150
184
|
|
|
151
185
|
if (property) {
|
|
152
|
-
return store ? store[property] : undefined;
|
|
186
|
+
return store ? store[property] : (undefined as State[P]);
|
|
153
187
|
}
|
|
154
188
|
|
|
155
|
-
return store ? store : {};
|
|
189
|
+
return store ? store : ({} as State);
|
|
156
190
|
}
|
|
157
191
|
|
|
158
192
|
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
@@ -162,16 +196,31 @@ export default class Service<
|
|
|
162
196
|
return;
|
|
163
197
|
}
|
|
164
198
|
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
199
|
+
const update = typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty;
|
|
200
|
+
const old = objectOnly(this._store.$state as State, Object.keys(update));
|
|
201
|
+
|
|
202
|
+
Object.assign(this._store.$state, update);
|
|
203
|
+
this.onStateUpdated(update as Partial<State>, old as Partial<State>);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public updatePersistedState<T extends keyof State>(key: T): void;
|
|
207
|
+
public updatePersistedState<T extends keyof State>(keys: T[]): void;
|
|
208
|
+
public updatePersistedState<T extends keyof State>(keyOrKeys: T | T[]): void {
|
|
209
|
+
if (!this._store) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const keys = arrayFrom(keyOrKeys) as Array<keyof State>;
|
|
214
|
+
const state = objectOnly(this._store.$state as State, keys);
|
|
168
215
|
|
|
169
|
-
|
|
216
|
+
if (isEmpty(state)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
170
219
|
|
|
171
|
-
this.
|
|
220
|
+
this.onPersistentStateUpdated(state);
|
|
172
221
|
}
|
|
173
222
|
|
|
174
|
-
protected __get(property: string): unknown {
|
|
223
|
+
protected override __get(property: string): unknown {
|
|
175
224
|
if (this.hasState(property)) {
|
|
176
225
|
return this.getState(property);
|
|
177
226
|
}
|
|
@@ -179,26 +228,40 @@ export default class Service<
|
|
|
179
228
|
return super.__get(property);
|
|
180
229
|
}
|
|
181
230
|
|
|
182
|
-
protected __set(property: string, value: unknown): void {
|
|
231
|
+
protected override __set(property: string, value: unknown): void {
|
|
183
232
|
this.setState({ [property]: value } as Partial<State>);
|
|
184
233
|
}
|
|
185
234
|
|
|
186
|
-
protected
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
235
|
+
protected get storageKey(): string {
|
|
236
|
+
return `${appNamespace()}:${this._name}`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
protected onStateUpdated(update: Partial<State>, old: Partial<State>): void {
|
|
240
|
+
const persisted = objectOnly(update, this.static('persist'));
|
|
190
241
|
|
|
191
|
-
if (isEmpty(persisted)) {
|
|
192
|
-
|
|
242
|
+
if (!isEmpty(persisted)) {
|
|
243
|
+
this.onPersistentStateUpdated(persisted as Partial<State>);
|
|
193
244
|
}
|
|
194
245
|
|
|
195
|
-
const
|
|
246
|
+
for (const property in update) {
|
|
247
|
+
const watcher = this._watchers[property] as Nullable<(value: unknown, oldValue: unknown) => unknown>;
|
|
248
|
+
|
|
249
|
+
if (!watcher || update[property] === old[property]) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
watcher.call(this, update[property], old[property]);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
protected onPersistentStateUpdated(persisted: Partial<State>): void {
|
|
258
|
+
const storage = Storage.get<ServiceStorage>(this.storageKey);
|
|
196
259
|
|
|
197
260
|
if (!storage) {
|
|
198
261
|
return;
|
|
199
262
|
}
|
|
200
263
|
|
|
201
|
-
Storage.set(this.
|
|
264
|
+
Storage.set(this.storageKey, {
|
|
202
265
|
...storage,
|
|
203
266
|
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
204
267
|
});
|
|
@@ -220,34 +283,47 @@ export default class Service<
|
|
|
220
283
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
221
284
|
}
|
|
222
285
|
|
|
223
|
-
protected
|
|
224
|
-
return
|
|
286
|
+
protected getStateWatchers(): StateWatchers<Service, State> {
|
|
287
|
+
return {};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
protected serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
291
|
+
return state as ServiceStorage;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
protected deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
295
|
+
return state as Partial<State>;
|
|
225
296
|
}
|
|
226
297
|
|
|
227
298
|
protected async frameworkBoot(): Promise<void> {
|
|
228
|
-
this.
|
|
299
|
+
this.restorePersistedState();
|
|
229
300
|
}
|
|
230
301
|
|
|
231
302
|
protected async boot(): Promise<void> {
|
|
232
303
|
// Placeholder for overrides, don't place any functionality here.
|
|
233
304
|
}
|
|
234
305
|
|
|
235
|
-
protected
|
|
236
|
-
|
|
237
|
-
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
238
|
-
|
|
239
|
-
if (!this.usesStore() || isEmpty(persist)) {
|
|
306
|
+
protected restorePersistedState(): void {
|
|
307
|
+
if (!this.usesStore() || isEmpty(this.static('persist'))) {
|
|
240
308
|
return;
|
|
241
309
|
}
|
|
242
310
|
|
|
243
|
-
if (Storage.has(this.
|
|
244
|
-
const persisted = Storage.require<ServiceStorage>(this.
|
|
245
|
-
this.setState(persisted);
|
|
311
|
+
if (Storage.has(this.storageKey)) {
|
|
312
|
+
const persisted = Storage.require<ServiceStorage>(this.storageKey);
|
|
313
|
+
this.setState(this.deserializePersistedState(persisted));
|
|
246
314
|
|
|
247
315
|
return;
|
|
248
316
|
}
|
|
249
317
|
|
|
250
|
-
Storage.set(this.
|
|
318
|
+
Storage.set(this.storageKey, objectOnly(this.getState(), this.static('persist')));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
protected requireStore(): Store<string, State, ComputedState, {}> {
|
|
322
|
+
if (!this._store) {
|
|
323
|
+
return fail(`Failed getting '${this._name}' store`);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return this._store;
|
|
251
327
|
}
|
|
252
328
|
|
|
253
329
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@aerogel/core/services/Events';
|
|
4
|
+
import Service from '@aerogel/core/services/Service';
|
|
5
|
+
|
|
6
|
+
export class StorageService extends Service {
|
|
7
|
+
|
|
8
|
+
public async purge(): Promise<void> {
|
|
9
|
+
await Events.emit('purge-storage');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default facade(StorageService);
|
|
15
|
+
|
|
16
|
+
declare module '@aerogel/core/services/Events' {
|
|
17
|
+
export interface EventsPayload {
|
|
18
|
+
'purge-storage': void;
|
|
19
|
+
}
|
|
20
|
+
}
|