@aerogel/core 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e → 0.0.0-next.d547095ca85c86c1e41f5c7fa170d0ba856c3f4d
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.cjs.js +1 -1
- package/dist/aerogel-core.cjs.js.map +1 -1
- package/dist/aerogel-core.d.ts +1371 -257
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/histoire.config.ts +7 -0
- package/noeldemartin.config.js +4 -1
- package/package.json +13 -4
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +4 -3
- package/src/bootstrap/index.ts +25 -5
- package/src/bootstrap/options.ts +3 -0
- package/src/components/AGAppLayout.vue +7 -2
- package/src/components/AGAppModals.vue +15 -0
- package/src/components/AGAppOverlays.vue +10 -8
- package/src/components/AGAppSnackbars.vue +13 -0
- package/src/components/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +25 -15
- package/src/components/forms/AGCheckbox.vue +7 -1
- package/src/components/forms/AGInput.vue +8 -6
- package/src/components/forms/AGSelect.story.vue +46 -0
- package/src/components/forms/AGSelect.vue +60 -0
- package/src/components/forms/index.ts +5 -6
- package/src/components/headless/forms/AGHeadlessButton.vue +9 -8
- package/src/components/headless/forms/AGHeadlessInput.ts +21 -1
- package/src/components/headless/forms/AGHeadlessInput.vue +8 -5
- package/src/components/headless/forms/AGHeadlessInputInput.vue +2 -0
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
- package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
- package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
- package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
- package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
- package/src/components/headless/forms/index.ts +9 -1
- package/src/components/headless/index.ts +1 -0
- package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
- package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
- package/src/components/headless/modals/index.ts +4 -6
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
- package/src/components/headless/snackbars/index.ts +40 -0
- package/src/components/index.ts +3 -1
- package/src/components/lib/AGErrorMessage.vue +16 -0
- package/src/components/lib/AGLink.vue +9 -0
- package/src/components/{basic → lib}/AGMarkdown.vue +7 -6
- package/src/components/lib/AGMeasured.vue +15 -0
- package/src/components/lib/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +5 -0
- package/src/components/modals/AGAlertModal.ts +15 -0
- package/src/components/modals/AGAlertModal.vue +4 -16
- package/src/components/modals/AGConfirmModal.ts +27 -0
- package/src/components/modals/AGConfirmModal.vue +8 -12
- package/src/components/modals/AGErrorReportModal.ts +46 -0
- package/src/components/modals/AGErrorReportModal.vue +54 -0
- package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
- package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
- package/src/components/modals/AGLoadingModal.ts +23 -0
- package/src/components/modals/AGLoadingModal.vue +4 -8
- package/src/components/modals/AGModal.ts +1 -1
- package/src/components/modals/AGModal.vue +16 -13
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/AGPromptModal.ts +30 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +16 -7
- package/src/components/snackbars/AGSnackbar.vue +36 -0
- package/src/components/snackbars/index.ts +3 -0
- package/src/components/utils.ts +10 -0
- package/src/directives/index.ts +20 -3
- package/src/directives/measure.ts +21 -0
- package/src/errors/Errors.ts +65 -12
- package/src/errors/index.ts +26 -1
- package/src/errors/utils.ts +19 -0
- package/src/forms/Form.ts +15 -6
- package/src/jobs/Job.ts +5 -0
- package/src/jobs/index.ts +7 -0
- package/src/lang/Lang.ts +12 -24
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +3 -2
- package/src/plugins/Plugin.ts +1 -0
- package/src/plugins/index.ts +19 -0
- package/src/services/App.state.ts +8 -4
- package/src/services/App.ts +35 -5
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +100 -30
- package/src/services/Service.ts +55 -16
- package/src/services/index.ts +8 -4
- package/src/services/store.ts +8 -5
- package/src/testing/index.ts +25 -0
- package/src/ui/UI.state.ts +10 -1
- package/src/ui/UI.ts +160 -26
- package/src/ui/index.ts +12 -3
- package/src/utils/composition/events.ts +1 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/markdown.ts +11 -2
- package/src/utils/tailwindcss.test.ts +26 -0
- package/src/utils/tailwindcss.ts +7 -0
- package/src/utils/vue.ts +15 -4
- package/tailwind.config.js +4 -0
- package/tsconfig.json +1 -0
- package/vite.config.ts +2 -1
- package/.eslintrc.js +0 -3
- package/src/components/basic/index.ts +0 -3
- package/src/globals.ts +0 -6
package/src/lang/Lang.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { facade
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
3
|
import App from '@/services/App';
|
|
4
4
|
import Service from '@/services/Service';
|
|
5
5
|
|
|
6
6
|
export interface LangProvider {
|
|
7
7
|
translate(key: string, parameters?: Record<string, unknown>): string;
|
|
8
|
+
translateWithDefault(key: string, defaultMessage: string, parameters?: Record<string, unknown>): string;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
export class LangService extends Service {
|
|
@@ -17,10 +18,16 @@ export class LangService extends Service {
|
|
|
17
18
|
this.provider = {
|
|
18
19
|
translate: (key) => {
|
|
19
20
|
// eslint-disable-next-line no-console
|
|
20
|
-
App.
|
|
21
|
+
App.development && console.warn('Lang provider is missing');
|
|
21
22
|
|
|
22
23
|
return key;
|
|
23
24
|
},
|
|
25
|
+
translateWithDefault: (_, defaultMessage) => {
|
|
26
|
+
// eslint-disable-next-line no-console
|
|
27
|
+
App.development && console.warn('Lang provider is missing');
|
|
28
|
+
|
|
29
|
+
return defaultMessage;
|
|
30
|
+
},
|
|
24
31
|
};
|
|
25
32
|
}
|
|
26
33
|
|
|
@@ -32,29 +39,10 @@ export class LangService extends Service {
|
|
|
32
39
|
return this.provider.translate(key, parameters) ?? key;
|
|
33
40
|
}
|
|
34
41
|
|
|
35
|
-
public translateWithDefault(key: string, defaultMessage: string): string
|
|
36
|
-
|
|
37
|
-
public translateWithDefault(
|
|
38
|
-
key: string,
|
|
39
|
-
defaultMessageOrParameters?: string | Record<string, unknown>,
|
|
40
|
-
defaultMessage?: string,
|
|
41
|
-
): string {
|
|
42
|
-
defaultMessage ??= defaultMessageOrParameters as string;
|
|
43
|
-
|
|
44
|
-
const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters ?? {};
|
|
45
|
-
const message = this.provider.translate(key, parameters) ?? key;
|
|
46
|
-
|
|
47
|
-
if (message === key) {
|
|
48
|
-
return Object.entries(parameters).reduce(
|
|
49
|
-
(renderedMessage, [name, value]) =>
|
|
50
|
-
renderedMessage.replace(new RegExp(`\\{\\s*${name}\\s*\\}`, 'g'), toString(value)),
|
|
51
|
-
defaultMessage,
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return message;
|
|
42
|
+
public translateWithDefault(key: string, defaultMessage: string, parameters: Record<string, unknown> = {}): string {
|
|
43
|
+
return this.provider.translateWithDefault(key, defaultMessage, parameters);
|
|
56
44
|
}
|
|
57
45
|
|
|
58
46
|
}
|
|
59
47
|
|
|
60
|
-
export default facade(
|
|
48
|
+
export default facade(LangService);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './assets/histoire.css';
|
package/src/main.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import './globals';
|
|
2
|
-
|
|
3
1
|
export * from './bootstrap';
|
|
4
2
|
export * from './components';
|
|
3
|
+
export * from './directives';
|
|
5
4
|
export * from './errors';
|
|
6
5
|
export * from './forms';
|
|
6
|
+
export * from './jobs';
|
|
7
7
|
export * from './lang';
|
|
8
8
|
export * from './plugins';
|
|
9
9
|
export * from './services';
|
|
10
|
+
export * from './testing';
|
|
10
11
|
export * from './ui';
|
|
11
12
|
export * from './utils';
|
package/src/plugins/Plugin.ts
CHANGED
package/src/plugins/index.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import App from '@/services/App';
|
|
4
|
+
|
|
1
5
|
import type { Plugin } from './Plugin';
|
|
2
6
|
|
|
3
7
|
export * from './Plugin';
|
|
@@ -5,3 +9,18 @@ export * from './Plugin';
|
|
|
5
9
|
export function definePlugin<T extends Plugin>(plugin: T): T {
|
|
6
10
|
return plugin;
|
|
7
11
|
}
|
|
12
|
+
|
|
13
|
+
export async function installPlugins(plugins: Plugin[], ...args: GetClosureArgs<Plugin['install']>): Promise<void> {
|
|
14
|
+
App.setState(
|
|
15
|
+
'plugins',
|
|
16
|
+
plugins.reduce((pluginsMap, plugin) => {
|
|
17
|
+
if (plugin.name) {
|
|
18
|
+
pluginsMap[plugin.name] = plugin;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return pluginsMap;
|
|
22
|
+
}, {} as Record<string, Plugin>),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
await Promise.all(plugins.map((plugin) => plugin.install(...args)) ?? []);
|
|
26
|
+
}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
|
+
|
|
1
3
|
import { defineServiceState } from '@/services/Service';
|
|
4
|
+
import type { Plugin } from '@/plugins/Plugin';
|
|
2
5
|
|
|
3
6
|
export default defineServiceState({
|
|
4
7
|
name: 'app',
|
|
5
8
|
initialState: {
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
plugins: {} as Record<string, Plugin>,
|
|
10
|
+
environment: Aerogel.environment,
|
|
11
|
+
sourceUrl: Aerogel.sourceUrl,
|
|
8
12
|
},
|
|
9
13
|
computed: {
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
development: (state) => state.environment === 'development',
|
|
15
|
+
testing: (state) => state.environment === 'test' || state.environment === 'testing',
|
|
12
16
|
},
|
|
13
17
|
});
|
package/src/services/App.ts
CHANGED
|
@@ -1,17 +1,47 @@
|
|
|
1
|
-
import { facade } from '@noeldemartin/utils';
|
|
1
|
+
import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
3
|
import Events from '@/services/Events';
|
|
4
|
+
import type { Plugin } from '@/plugins';
|
|
4
5
|
|
|
5
6
|
import Service from './App.state';
|
|
6
7
|
|
|
7
8
|
export class AppService extends Service {
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
public readonly ready = new PromisedValue<void>();
|
|
11
|
+
public readonly mounted = new PromisedValue<void>();
|
|
12
|
+
|
|
13
|
+
public isReady(): boolean {
|
|
14
|
+
return this.ready.isResolved();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public isMounted(): boolean {
|
|
18
|
+
return this.mounted.isResolved();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async whenReady<T>(callback: () => T): Promise<T> {
|
|
22
|
+
const result = await this.ready.then(callback);
|
|
23
|
+
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
11
26
|
|
|
12
|
-
|
|
27
|
+
public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
|
|
28
|
+
queryParameters && updateLocationQueryParameters(queryParameters);
|
|
29
|
+
|
|
30
|
+
location.reload();
|
|
31
|
+
|
|
32
|
+
// Stall until the reload happens
|
|
33
|
+
await forever();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public plugin<T extends Plugin = Plugin>(name: string): T | null {
|
|
37
|
+
return (this.plugins[name] as T) ?? null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected async boot(): Promise<void> {
|
|
41
|
+
Events.once('application-ready', () => this.ready.resolve());
|
|
42
|
+
Events.once('application-mounted', () => this.mounted.resolve());
|
|
13
43
|
}
|
|
14
44
|
|
|
15
45
|
}
|
|
16
46
|
|
|
17
|
-
export default facade(
|
|
47
|
+
export default facade(AppService);
|
|
@@ -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('trigger', () => counter++);
|
|
14
|
+
|
|
15
|
+
// Act
|
|
16
|
+
await Events.emit('trigger');
|
|
17
|
+
await Events.emit('trigger');
|
|
18
|
+
await Events.emit('trigger');
|
|
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('trigger', () => storage.push('second'));
|
|
29
|
+
Events.on('trigger', { priority: EventListenerPriorities.Low }, () => storage.push('third'));
|
|
30
|
+
Events.on('trigger', { priority: EventListenerPriorities.High }, () => storage.push('first'));
|
|
31
|
+
|
|
32
|
+
// Act
|
|
33
|
+
await Events.emit('trigger');
|
|
34
|
+
|
|
35
|
+
// Assert
|
|
36
|
+
expect(storage).toEqual(['first', 'second', 'third']);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
});
|
package/src/services/Events.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { FluentArray } from '@noeldemartin/utils';
|
|
1
|
+
import { arrayRemove, facade, fail, tap } from '@noeldemartin/utils';
|
|
3
2
|
|
|
4
3
|
import Service from '@/services/Service';
|
|
5
4
|
|
|
6
5
|
export interface EventsPayload {}
|
|
6
|
+
export interface EventListenerOptions {
|
|
7
|
+
priority: number;
|
|
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
13
|
export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
|
|
@@ -16,70 +20,136 @@ export type EventWithPayload = {
|
|
|
16
20
|
[K in keyof EventsPayload]: EventsPayload[K] extends void ? never : K;
|
|
17
21
|
}[keyof EventsPayload];
|
|
18
22
|
|
|
23
|
+
export const EventListenerPriorities = {
|
|
24
|
+
Low: -256,
|
|
25
|
+
Default: 0,
|
|
26
|
+
High: 256,
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
19
29
|
export class EventsService extends Service {
|
|
20
30
|
|
|
21
|
-
private listeners: Record<string,
|
|
31
|
+
private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
|
|
32
|
+
|
|
33
|
+
protected async boot(): Promise<void> {
|
|
34
|
+
Object.entries(globalThis.__aerogelEvents__ ?? {}).forEach(([event, listener]) =>
|
|
35
|
+
this.on(event as string, listener as EventListener));
|
|
36
|
+
}
|
|
22
37
|
|
|
23
38
|
public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
|
|
24
39
|
public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
|
|
25
40
|
public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
|
|
26
41
|
public async emit(event: string, payload?: unknown): Promise<void> {
|
|
27
|
-
const listeners =
|
|
42
|
+
const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
|
|
28
43
|
|
|
29
|
-
|
|
44
|
+
for (const priority of listeners.priorities) {
|
|
45
|
+
await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
|
|
46
|
+
}
|
|
30
47
|
}
|
|
31
48
|
|
|
49
|
+
/* eslint-disable max-len */
|
|
32
50
|
public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
33
|
-
public on<Event extends
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
): () => void | void;
|
|
37
|
-
|
|
51
|
+
public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
52
|
+
public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
53
|
+
public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
38
54
|
public on<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
39
|
-
public on(event:
|
|
40
|
-
|
|
55
|
+
public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
56
|
+
/* eslint-enable max-len */
|
|
41
57
|
|
|
42
|
-
|
|
58
|
+
public on(
|
|
59
|
+
event: string,
|
|
60
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
61
|
+
listener?: EventListener,
|
|
62
|
+
): () => void {
|
|
63
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
64
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
65
|
+
|
|
66
|
+
this.registerListener(event, options, handler);
|
|
67
|
+
|
|
68
|
+
return () => this.off(event, handler);
|
|
43
69
|
}
|
|
44
70
|
|
|
71
|
+
/* eslint-disable max-len */
|
|
45
72
|
public once<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
46
|
-
public once<Event extends
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
): () => void | void;
|
|
50
|
-
|
|
73
|
+
public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
74
|
+
public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
75
|
+
public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
51
76
|
public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
52
|
-
public once(event:
|
|
77
|
+
public once<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
78
|
+
/* eslint-enable max-len */
|
|
79
|
+
|
|
80
|
+
public once(
|
|
81
|
+
event: string,
|
|
82
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
83
|
+
listener?: EventListener,
|
|
84
|
+
): () => void {
|
|
53
85
|
let onceListener: EventListener | null = null;
|
|
86
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
87
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
54
88
|
|
|
55
89
|
return tap(
|
|
56
90
|
() => onceListener && this.off(event, onceListener),
|
|
57
91
|
(off) => {
|
|
58
|
-
|
|
59
|
-
(
|
|
60
|
-
off();
|
|
92
|
+
onceListener = (...args) => {
|
|
93
|
+
off();
|
|
61
94
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
95
|
+
return handler(...args);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
this.registerListener(event, options, handler);
|
|
65
99
|
},
|
|
66
100
|
);
|
|
67
101
|
}
|
|
68
102
|
|
|
69
103
|
public off(event: string, listener: EventListener): void {
|
|
70
|
-
const
|
|
104
|
+
const listeners = this.listeners[event];
|
|
71
105
|
|
|
72
|
-
if (!
|
|
106
|
+
if (!listeners) {
|
|
73
107
|
return;
|
|
74
108
|
}
|
|
75
109
|
|
|
76
|
-
|
|
110
|
+
const priorities = [...listeners.priorities];
|
|
111
|
+
|
|
112
|
+
for (const priority of priorities) {
|
|
113
|
+
arrayRemove(listeners.handlers[priority] ?? [], listener);
|
|
77
114
|
|
|
78
|
-
|
|
115
|
+
if (listeners.handlers[priority]?.length === 0) {
|
|
116
|
+
delete listeners.handlers[priority];
|
|
117
|
+
arrayRemove(listeners.priorities, priority);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (listeners.priorities.length === 0) {
|
|
79
122
|
delete this.listeners[event];
|
|
80
123
|
}
|
|
81
124
|
}
|
|
82
125
|
|
|
126
|
+
protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
|
|
127
|
+
const priority = options.priority ?? 0;
|
|
128
|
+
|
|
129
|
+
if (!(event in this.listeners)) {
|
|
130
|
+
this.listeners[event] = { priorities: [], handlers: {} };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const priorities =
|
|
134
|
+
this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
|
|
135
|
+
const handlers =
|
|
136
|
+
this.listeners[event]?.handlers ??
|
|
137
|
+
fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
|
|
138
|
+
|
|
139
|
+
if (!priorities.includes(priority)) {
|
|
140
|
+
priorities.push(priority);
|
|
141
|
+
priorities.sort((a, b) => b - a);
|
|
142
|
+
handlers[priority] = [];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
handlers[priority]?.push(handler);
|
|
146
|
+
}
|
|
147
|
+
|
|
83
148
|
}
|
|
84
149
|
|
|
85
|
-
export default facade(
|
|
150
|
+
export default facade(EventsService);
|
|
151
|
+
|
|
152
|
+
declare global {
|
|
153
|
+
// eslint-disable-next-line no-var
|
|
154
|
+
var __aerogelEvents__: AerogelGlobalEvents | undefined;
|
|
155
|
+
}
|
package/src/services/Service.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
2
2
|
import type { Constructor } from '@noeldemartin/utils';
|
|
3
|
+
import type { MaybeRef } from 'vue';
|
|
3
4
|
import type { Store } from 'pinia';
|
|
4
5
|
|
|
5
6
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
@@ -8,9 +9,12 @@ import { defineServiceStore } from '@/services/store';
|
|
|
8
9
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
10
|
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
11
|
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
|
+
};
|
|
11
15
|
|
|
12
16
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
|
-
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
17
|
+
[K in keyof TComputedState]: (state: UnrefServiceState<TState>) => TComputedState[K];
|
|
14
18
|
} & ThisType<{
|
|
15
19
|
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
20
|
}>;
|
|
@@ -20,12 +24,14 @@ export function defineServiceState<
|
|
|
20
24
|
ComputedState extends ServiceState = {}
|
|
21
25
|
>(options: {
|
|
22
26
|
name: string;
|
|
23
|
-
initialState: State;
|
|
27
|
+
initialState: State | (() => State);
|
|
24
28
|
persist?: (keyof State)[];
|
|
25
29
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
26
30
|
serialize?: (state: Partial<State>) => Partial<State>;
|
|
27
|
-
}): Constructor<State
|
|
28
|
-
|
|
31
|
+
}): Constructor<UnrefServiceState<State>> &
|
|
32
|
+
Constructor<ComputedState> &
|
|
33
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>> {
|
|
34
|
+
return class extends Service<UnrefServiceState<State>, ComputedState> {
|
|
29
35
|
|
|
30
36
|
public static persist = (options.persist as string[]) ?? [];
|
|
31
37
|
|
|
@@ -37,21 +43,41 @@ export function defineServiceState<
|
|
|
37
43
|
return options.name ?? null;
|
|
38
44
|
}
|
|
39
45
|
|
|
40
|
-
protected getInitialState(): State {
|
|
41
|
-
|
|
46
|
+
protected getInitialState(): UnrefServiceState<State> {
|
|
47
|
+
if (typeof options.initialState === 'function') {
|
|
48
|
+
return options.initialState();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return Object.entries(options.initialState).reduce((state, [key, value]) => {
|
|
52
|
+
try {
|
|
53
|
+
value = structuredClone(value);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
// eslint-disable-next-line no-console
|
|
56
|
+
console.warn(
|
|
57
|
+
`Could not clone '${key}' state from ${this.getName()} service, ` +
|
|
58
|
+
'this may cause problems if you\'re using multiple instances of the service ' +
|
|
59
|
+
'(for example, in unit tests).\n' +
|
|
60
|
+
'To fix this problem, declare your initialState as a function instead.',
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
state[key as keyof State] = value;
|
|
65
|
+
|
|
66
|
+
return state;
|
|
67
|
+
}, {} as UnrefServiceState<State>);
|
|
42
68
|
}
|
|
43
69
|
|
|
44
|
-
protected getComputedStateDefinition(): ComputedStateDefinition<State
|
|
45
|
-
return options.computed ??
|
|
70
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<UnrefServiceState<State>, ComputedState> {
|
|
71
|
+
return (options.computed ?? {}) as ComputedStateDefinition<UnrefServiceState<State>, ComputedState>;
|
|
46
72
|
}
|
|
47
73
|
|
|
48
74
|
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
49
75
|
return options.serialize?.(state) ?? state;
|
|
50
76
|
}
|
|
51
|
-
|
|
52
|
-
} as unknown as Constructor<State
|
|
77
|
+
|
|
78
|
+
} as unknown as Constructor<UnrefServiceState<State>> &
|
|
53
79
|
Constructor<ComputedState> &
|
|
54
|
-
Constructor<Service<State
|
|
80
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>>;
|
|
55
81
|
}
|
|
56
82
|
|
|
57
83
|
export default class Service<
|
|
@@ -65,7 +91,7 @@ export default class Service<
|
|
|
65
91
|
protected _name: string;
|
|
66
92
|
private _booted: PromisedValue<void>;
|
|
67
93
|
private _computedStateKeys: Set<keyof State>;
|
|
68
|
-
private _store
|
|
94
|
+
private _store: Store | false;
|
|
69
95
|
|
|
70
96
|
constructor() {
|
|
71
97
|
super();
|
|
@@ -93,7 +119,8 @@ export default class Service<
|
|
|
93
119
|
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
94
120
|
|
|
95
121
|
try {
|
|
96
|
-
this.
|
|
122
|
+
this.frameworkBoot()
|
|
123
|
+
.then(() => this.boot())
|
|
97
124
|
.then(() => this._booted.resolve())
|
|
98
125
|
.catch(handleError);
|
|
99
126
|
} catch (error) {
|
|
@@ -103,6 +130,10 @@ export default class Service<
|
|
|
103
130
|
return this._booted;
|
|
104
131
|
}
|
|
105
132
|
|
|
133
|
+
public hasPersistedState(): boolean {
|
|
134
|
+
return Storage.has(this._name);
|
|
135
|
+
}
|
|
136
|
+
|
|
106
137
|
public hasState<P extends keyof State>(property: P): boolean {
|
|
107
138
|
if (!this._store) {
|
|
108
139
|
return false;
|
|
@@ -161,7 +192,11 @@ export default class Service<
|
|
|
161
192
|
return;
|
|
162
193
|
}
|
|
163
194
|
|
|
164
|
-
const storage = Storage.
|
|
195
|
+
const storage = Storage.get<ServiceStorage>(this._name);
|
|
196
|
+
|
|
197
|
+
if (!storage) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
165
200
|
|
|
166
201
|
Storage.set(this._name, {
|
|
167
202
|
...storage,
|
|
@@ -189,11 +224,15 @@ export default class Service<
|
|
|
189
224
|
return state;
|
|
190
225
|
}
|
|
191
226
|
|
|
227
|
+
protected async frameworkBoot(): Promise<void> {
|
|
228
|
+
this.initializePersistedState();
|
|
229
|
+
}
|
|
230
|
+
|
|
192
231
|
protected async boot(): Promise<void> {
|
|
193
|
-
|
|
232
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
194
233
|
}
|
|
195
234
|
|
|
196
|
-
protected
|
|
235
|
+
protected initializePersistedState(): void {
|
|
197
236
|
// TODO fix this.static()
|
|
198
237
|
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
199
238
|
|
package/src/services/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { getPiniaStore } from './store';
|
|
|
10
10
|
export * from './App';
|
|
11
11
|
export * from './Events';
|
|
12
12
|
export * from './Service';
|
|
13
|
+
export * from './store';
|
|
13
14
|
|
|
14
15
|
export { App, Events, Service };
|
|
15
16
|
|
|
@@ -24,13 +25,16 @@ export interface Services extends DefaultServices {}
|
|
|
24
25
|
|
|
25
26
|
export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
|
|
26
27
|
await Promise.all(
|
|
27
|
-
Object.entries(services).map(async ([
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
Object.entries(services).map(async ([name, service]) => {
|
|
29
|
+
await service
|
|
30
|
+
.launch()
|
|
31
|
+
.catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
|
|
30
32
|
}),
|
|
31
33
|
);
|
|
32
34
|
|
|
33
35
|
Object.assign(app.config.globalProperties, services);
|
|
36
|
+
|
|
37
|
+
App.development && Object.assign(window, services);
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
export default definePlugin({
|
|
@@ -47,7 +51,7 @@ export default definePlugin({
|
|
|
47
51
|
});
|
|
48
52
|
|
|
49
53
|
declare module '@/bootstrap/options' {
|
|
50
|
-
interface AerogelOptions {
|
|
54
|
+
export interface AerogelOptions {
|
|
51
55
|
services?: Record<string, Service>;
|
|
52
56
|
}
|
|
53
57
|
}
|
package/src/services/store.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
import { tap } from '@noeldemartin/utils';
|
|
1
2
|
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
2
3
|
import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
|
|
3
4
|
|
|
4
5
|
let _store: Pinia | null = null;
|
|
5
6
|
|
|
6
7
|
function initializePiniaStore(): Pinia {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
return _store ?? resetPiniaStore();
|
|
9
|
+
}
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
export function resetPiniaStore(): Pinia {
|
|
12
|
+
return tap(createPinia(), (store) => {
|
|
13
|
+
_store = store;
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
setActivePinia(store);
|
|
16
|
+
});
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
export function getPiniaStore(): Pinia {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
import { definePlugin } from '@/plugins';
|
|
5
|
+
|
|
6
|
+
export interface AerogelTestingRuntime {
|
|
7
|
+
on: (typeof Events)['on'];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default definePlugin({
|
|
11
|
+
async install() {
|
|
12
|
+
if (import.meta.env.MODE !== 'testing') {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
globalThis.testingRuntime = {
|
|
17
|
+
on: ((...args: GetClosureArgs<(typeof Events)['on']>) => Events.on(...args)) as (typeof Events)['on'],
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
declare global {
|
|
23
|
+
// eslint-disable-next-line no-var
|
|
24
|
+
var testingRuntime: AerogelTestingRuntime | undefined;
|
|
25
|
+
}
|
package/src/ui/UI.state.ts
CHANGED
|
@@ -17,7 +17,16 @@ export interface ModalComponent<
|
|
|
17
17
|
Result = unknown
|
|
18
18
|
> {}
|
|
19
19
|
|
|
20
|
+
export interface Snackbar {
|
|
21
|
+
id: string;
|
|
22
|
+
component: Component;
|
|
23
|
+
properties: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
20
26
|
export default defineServiceState({
|
|
21
27
|
name: 'ui',
|
|
22
|
-
initialState: {
|
|
28
|
+
initialState: {
|
|
29
|
+
modals: [] as Modal[],
|
|
30
|
+
snackbars: [] as Snackbar[],
|
|
31
|
+
},
|
|
23
32
|
});
|