@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.8323c60b905020dcb3bd9d4b0bc8d9b6529e1082
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 +2547 -422
- package/dist/aerogel-core.js +3722 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +39 -37
- package/src/bootstrap/bootstrap.test.ts +7 -66
- package/src/bootstrap/index.ts +46 -33
- package/src/bootstrap/options.ts +8 -1
- package/src/components/AppLayout.vue +14 -0
- package/src/components/AppModals.vue +14 -0
- 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/HeadlessInputDescription.vue +27 -0
- package/src/components/headless/HeadlessInputError.vue +22 -0
- package/src/components/headless/HeadlessInputInput.vue +86 -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/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 -2
- package/src/components/index.ts +6 -6
- 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/ui/ErrorReportModalButtons.vue +118 -0
- 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 +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 +31 -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 +29 -6
- package/src/directives/measure.ts +46 -0
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +200 -0
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +53 -0
- package/src/errors/settings/Debug.vue +32 -0
- package/src/errors/settings/index.ts +10 -0
- package/src/errors/utils.ts +35 -0
- package/src/forms/FormController.test.ts +113 -0
- package/src/forms/FormController.ts +255 -0
- package/src/forms/index.ts +3 -2
- package/src/forms/utils.ts +87 -14
- package/src/forms/validation.ts +50 -0
- package/src/index.css +76 -0
- package/src/{main.ts → index.ts} +5 -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 +63 -9
- package/src/lang/index.ts +22 -75
- package/src/lang/settings/Language.vue +48 -0
- package/src/lang/settings/index.ts +10 -0
- package/src/lang/utils.ts +4 -0
- package/src/plugins/Plugin.ts +8 -0
- package/src/plugins/index.ts +29 -0
- package/src/services/App.state.ts +50 -0
- package/src/services/App.ts +63 -0
- 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 +273 -35
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +45 -8
- package/src/services/store.ts +30 -0
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +30 -0
- package/src/testing/setup.ts +11 -0
- package/src/types/vite.d.ts +0 -2
- package/src/ui/UI.state.ts +21 -13
- package/src/ui/UI.ts +350 -53
- package/src/ui/index.ts +40 -25
- 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 -5
- package/src/utils/composition/forms.ts +27 -0
- package/src/utils/composition/hooks.ts +9 -0
- 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 +9 -0
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +60 -4
- package/src/utils/types.ts +3 -0
- package/src/utils/vue.ts +38 -121
- 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/noeldemartin.config.js +0 -2
- package/src/bootstrap/hooks.ts +0 -19
- package/src/components/AGAppLayout.vue +0 -11
- package/src/components/AGAppOverlays.vue +0 -39
- package/src/components/basic/AGMarkdown.vue +0 -20
- package/src/components/basic/index.ts +0 -3
- package/src/components/forms/AGButton.vue +0 -11
- package/src/components/forms/AGForm.vue +0 -26
- package/src/components/forms/AGInput.vue +0 -32
- package/src/components/forms/index.ts +0 -5
- 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/AGHeadlessInputError.vue +0 -22
- package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -29
- package/src/components/headless/forms/index.ts +0 -4
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -7
- package/src/components/headless/modals/AGHeadlessModal.vue +0 -84
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -20
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
- package/src/components/headless/modals/index.ts +0 -6
- package/src/components/modals/AGAlertModal.vue +0 -15
- package/src/components/modals/AGModal.ts +0 -6
- package/src/components/modals/AGModal.vue +0 -18
- package/src/components/modals/AGModalContext.ts +0 -8
- package/src/components/modals/AGModalContext.vue +0 -22
- package/src/components/modals/index.ts +0 -5
- package/src/directives/initial-focus.ts +0 -11
- package/src/forms/Form.test.ts +0 -37
- package/src/forms/Form.ts +0 -154
- package/src/forms/composition.ts +0 -6
- package/src/lang/helpers.ts +0 -5
- package/src/models/index.ts +0 -18
- package/src/routing/index.ts +0 -33
- package/src/testing/stubs/lang/en.yaml +0 -1
- package/src/testing/stubs/models/User.ts +0 -3
- package/tsconfig.json +0 -19
- package/vite.config.ts +0 -17
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
|
+
}
|
package/src/services/Service.ts
CHANGED
|
@@ -1,50 +1,160 @@
|
|
|
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';
|
|
12
|
+
import type { Store } from 'pinia';
|
|
4
13
|
|
|
5
|
-
import ServiceBootError from '
|
|
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';
|
|
6
18
|
|
|
7
19
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
8
|
-
export type DefaultServiceState =
|
|
20
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
21
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
10
22
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
23
|
+
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
24
|
+
[K in keyof TComputedState]: (state: Unref<TState>) => TComputedState[K];
|
|
25
|
+
} & ThisType<{
|
|
26
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
27
|
+
}>;
|
|
15
28
|
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
|
|
41
|
+
export function defineServiceState<
|
|
42
|
+
State extends ServiceState = ServiceState,
|
|
43
|
+
ComputedState extends ServiceState = {},
|
|
44
|
+
ServiceStorage = Partial<State>,
|
|
45
|
+
>(options: {
|
|
46
|
+
name: string;
|
|
47
|
+
initialState: State | (() => State);
|
|
48
|
+
persist?: (keyof State)[];
|
|
49
|
+
watch?: StateWatchers<Service, State>;
|
|
50
|
+
computed?: ComputedStateDefinition<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> {
|
|
55
|
+
|
|
56
|
+
public static override persist = (options.persist as string[]) ?? [];
|
|
57
|
+
|
|
58
|
+
protected override usesStore(): boolean {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected override getName(): string | null {
|
|
63
|
+
return options.name ?? null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected override getInitialState(): Unref<State> {
|
|
67
|
+
if (typeof options.initialState === 'function') {
|
|
68
|
+
return options.initialState();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return Object.entries(options.initialState).reduce((state, [key, value]) => {
|
|
72
|
+
try {
|
|
73
|
+
value = structuredClone(value);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
// eslint-disable-next-line no-console
|
|
76
|
+
console.warn(
|
|
77
|
+
`Could not clone '${key}' state from ${this.getName()} service, ` +
|
|
78
|
+
'this may cause problems if you\'re using multiple instances of the service ' +
|
|
79
|
+
'(for example, in unit tests).\n' +
|
|
80
|
+
'To fix this problem, declare your initialState as a function instead.',
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
state[key as keyof State] = value;
|
|
85
|
+
|
|
86
|
+
return state;
|
|
87
|
+
}, {} as Unref<State>);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
protected override getComputedStateDefinition(): ComputedStateDefinition<Unref<State>, ComputedState> {
|
|
91
|
+
return (options.computed ?? {}) as ComputedStateDefinition<Unref<State>, ComputedState>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
protected override getStateWatchers(): StateWatchers<Service, Unref<State>> {
|
|
95
|
+
return (options.watch ?? {}) as StateWatchers<Service, Unref<State>>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
protected override serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
99
|
+
return options.serialize?.(state) ?? (state as ServiceStorage);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected override deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
103
|
+
return options.restore?.(state) ?? (state as Partial<State>);
|
|
18
104
|
}
|
|
19
105
|
|
|
20
|
-
} as unknown as
|
|
106
|
+
} as unknown as ServiceWithState<State, ComputedState, ServiceStorage>;
|
|
21
107
|
}
|
|
22
108
|
|
|
23
|
-
export default class Service<
|
|
109
|
+
export default class Service<
|
|
110
|
+
State extends ServiceState = DefaultServiceState,
|
|
111
|
+
ComputedState extends ServiceState = {},
|
|
112
|
+
ServiceStorage = Partial<State>,
|
|
113
|
+
> extends MagicObject {
|
|
114
|
+
|
|
115
|
+
public static persist: string[] = [];
|
|
24
116
|
|
|
25
|
-
protected
|
|
117
|
+
protected _name: string;
|
|
26
118
|
private _booted: PromisedValue<void>;
|
|
27
|
-
private
|
|
119
|
+
private _computedStateKeys: Set<keyof State>;
|
|
120
|
+
private _watchers: StateWatchers<Service, State>;
|
|
121
|
+
private _store: Store<string, State, ComputedState, {}> | false;
|
|
28
122
|
|
|
29
123
|
constructor() {
|
|
30
124
|
super();
|
|
31
125
|
|
|
32
|
-
|
|
126
|
+
const getters = this.getComputedStateDefinition();
|
|
127
|
+
|
|
128
|
+
this._name = this.getName() ?? new.target.name;
|
|
33
129
|
this._booted = new PromisedValue();
|
|
34
|
-
this.
|
|
130
|
+
this._computedStateKeys = new Set(Object.keys(getters));
|
|
131
|
+
this._watchers = this.getStateWatchers();
|
|
132
|
+
this._store =
|
|
133
|
+
this.usesStore() &&
|
|
134
|
+
defineServiceStore(this._name, {
|
|
135
|
+
state: () => this.getInitialState(),
|
|
136
|
+
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
138
|
+
getters: getters as any,
|
|
139
|
+
});
|
|
35
140
|
}
|
|
36
141
|
|
|
37
142
|
public get booted(): PromisedValue<void> {
|
|
38
143
|
return this._booted;
|
|
39
144
|
}
|
|
40
145
|
|
|
41
|
-
public
|
|
42
|
-
|
|
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
|
+
}
|
|
43
151
|
|
|
44
|
-
|
|
152
|
+
public launch(): Promise<void> {
|
|
153
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
45
154
|
|
|
46
155
|
try {
|
|
47
|
-
this.
|
|
156
|
+
this.frameworkBoot()
|
|
157
|
+
.then(() => this.boot())
|
|
48
158
|
.then(() => this._booted.resolve())
|
|
49
159
|
.catch(handleError);
|
|
50
160
|
} catch (error) {
|
|
@@ -54,38 +164,166 @@ export default class Service<State extends ServiceState = DefaultServiceState> e
|
|
|
54
164
|
return this._booted;
|
|
55
165
|
}
|
|
56
166
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
167
|
+
public hasPersistedState(): boolean {
|
|
168
|
+
return Storage.has(this.storageKey);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
172
|
+
if (!this._store) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
public getState(): State;
|
|
180
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
181
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
182
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
183
|
+
const store = this._store as any;
|
|
184
|
+
|
|
185
|
+
if (property) {
|
|
186
|
+
return store ? store[property] : (undefined as State[P]);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return store ? store : ({} as State);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
193
|
+
public setState(state: Partial<State>): void;
|
|
194
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
195
|
+
if (!this._store) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
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);
|
|
215
|
+
|
|
216
|
+
if (isEmpty(state)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
this.onPersistentStateUpdated(state);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
protected override __get(property: string): unknown {
|
|
224
|
+
if (this.hasState(property)) {
|
|
225
|
+
return this.getState(property);
|
|
60
226
|
}
|
|
61
227
|
|
|
62
|
-
return
|
|
228
|
+
return super.__get(property);
|
|
63
229
|
}
|
|
64
230
|
|
|
65
|
-
protected __set(property: string, value: unknown): void {
|
|
231
|
+
protected override __set(property: string, value: unknown): void {
|
|
66
232
|
this.setState({ [property]: value } as Partial<State>);
|
|
67
233
|
}
|
|
68
234
|
|
|
69
|
-
protected
|
|
70
|
-
return
|
|
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'));
|
|
241
|
+
|
|
242
|
+
if (!isEmpty(persisted)) {
|
|
243
|
+
this.onPersistentStateUpdated(persisted as Partial<State>);
|
|
244
|
+
}
|
|
245
|
+
|
|
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);
|
|
259
|
+
|
|
260
|
+
if (!storage) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
Storage.set(this.storageKey, {
|
|
265
|
+
...storage,
|
|
266
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
267
|
+
});
|
|
71
268
|
}
|
|
72
269
|
|
|
73
|
-
protected
|
|
74
|
-
|
|
75
|
-
protected getState<P extends keyof State>(property?: P): State | State[P] {
|
|
76
|
-
return property ? this._state[property] : this._state;
|
|
270
|
+
protected usesStore(): boolean {
|
|
271
|
+
return false;
|
|
77
272
|
}
|
|
78
273
|
|
|
79
|
-
protected
|
|
80
|
-
|
|
274
|
+
protected getName(): string | null {
|
|
275
|
+
return null;
|
|
81
276
|
}
|
|
82
277
|
|
|
83
278
|
protected getInitialState(): State {
|
|
84
279
|
return {} as State;
|
|
85
280
|
}
|
|
86
281
|
|
|
282
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
283
|
+
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
284
|
+
}
|
|
285
|
+
|
|
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>;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
protected async frameworkBoot(): Promise<void> {
|
|
299
|
+
this.restorePersistedState();
|
|
300
|
+
}
|
|
301
|
+
|
|
87
302
|
protected async boot(): Promise<void> {
|
|
88
|
-
//
|
|
303
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
protected restorePersistedState(): void {
|
|
307
|
+
if (!this.usesStore() || isEmpty(this.static('persist'))) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (Storage.has(this.storageKey)) {
|
|
312
|
+
const persisted = Storage.require<ServiceStorage>(this.storageKey);
|
|
313
|
+
this.setState(this.deserializePersistedState(persisted));
|
|
314
|
+
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
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;
|
|
89
327
|
}
|
|
90
328
|
|
|
91
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
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,35 +1,72 @@
|
|
|
1
|
-
import type { App } from 'vue';
|
|
1
|
+
import type { App as AppInstance } from 'vue';
|
|
2
2
|
|
|
3
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
4
|
+
import { isDevelopment, isTesting } from '@noeldemartin/utils';
|
|
5
|
+
|
|
6
|
+
import App from './App';
|
|
7
|
+
import Cache from './Cache';
|
|
3
8
|
import Events from './Events';
|
|
4
9
|
import Service from './Service';
|
|
5
|
-
import
|
|
10
|
+
import Storage from './Storage';
|
|
11
|
+
import { getPiniaStore } from './store';
|
|
12
|
+
import type { AppSetting } from './App.state';
|
|
6
13
|
|
|
14
|
+
export * from './App';
|
|
15
|
+
export * from './Cache';
|
|
7
16
|
export * from './Events';
|
|
8
17
|
export * from './Service';
|
|
18
|
+
export * from './store';
|
|
19
|
+
export * from './utils';
|
|
9
20
|
|
|
10
|
-
export { Events, Service };
|
|
21
|
+
export { App, Cache, Events, Storage, Service };
|
|
11
22
|
|
|
12
23
|
const defaultServices = {
|
|
24
|
+
$app: App,
|
|
13
25
|
$events: Events,
|
|
26
|
+
$storage: Storage,
|
|
14
27
|
};
|
|
15
28
|
|
|
16
29
|
export type DefaultServices = typeof defaultServices;
|
|
17
30
|
|
|
18
31
|
export interface Services extends DefaultServices {}
|
|
19
32
|
|
|
20
|
-
export async function bootServices(app:
|
|
33
|
+
export async function bootServices(app: AppInstance, services: Record<string, Service>): Promise<void> {
|
|
21
34
|
await Promise.all(
|
|
22
35
|
Object.entries(services).map(async ([name, service]) => {
|
|
23
|
-
|
|
24
|
-
|
|
36
|
+
await service
|
|
37
|
+
.launch()
|
|
38
|
+
.catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
|
|
25
39
|
}),
|
|
26
40
|
);
|
|
27
41
|
|
|
28
42
|
Object.assign(app.config.globalProperties, services);
|
|
43
|
+
|
|
44
|
+
if (isDevelopment() || isTesting()) {
|
|
45
|
+
Object.assign(globalThis, services);
|
|
46
|
+
}
|
|
29
47
|
}
|
|
30
48
|
|
|
31
|
-
export default
|
|
49
|
+
export default definePlugin({
|
|
50
|
+
async install(app, options) {
|
|
51
|
+
const services = {
|
|
52
|
+
...defaultServices,
|
|
53
|
+
...options.services,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
app.use(getPiniaStore());
|
|
57
|
+
options.settings?.forEach((setting) => App.addSetting(setting));
|
|
58
|
+
|
|
59
|
+
await bootServices(app, services);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
64
|
+
export interface AerogelOptions {
|
|
65
|
+
services?: Record<string, Service>;
|
|
66
|
+
settings?: AppSetting[];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
32
69
|
|
|
33
|
-
declare module '
|
|
70
|
+
declare module 'vue' {
|
|
34
71
|
interface ComponentCustomProperties extends Services {}
|
|
35
72
|
}
|