@aerogel/core 0.0.0-next.88c59e62f64db70aedfbc4c31b5bbc287be44483 → 0.0.0-next.906ec80f260b7e5cb54a0c97bd4905bdaf4bf916
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 +1511 -217
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/histoire.config.ts +7 -0
- package/package.json +14 -5
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +3 -3
- package/src/bootstrap/index.ts +32 -5
- package/src/bootstrap/options.ts +3 -0
- package/src/components/AGAppLayout.vue +7 -2
- package/src/components/AGAppOverlays.vue +5 -1
- package/src/components/AGAppSnackbars.vue +2 -2
- package/src/components/composition.ts +23 -0
- package/src/components/forms/AGCheckbox.vue +7 -1
- package/src/components/forms/AGForm.vue +9 -10
- package/src/components/forms/AGInput.vue +10 -6
- package/src/components/forms/AGSelect.story.vue +46 -0
- package/src/components/forms/AGSelect.vue +60 -0
- package/src/components/forms/index.ts +1 -0
- package/src/components/headless/forms/AGHeadlessButton.ts +3 -0
- package/src/components/headless/forms/AGHeadlessButton.vue +23 -12
- package/src/components/headless/forms/AGHeadlessInput.ts +30 -4
- package/src/components/headless/forms/AGHeadlessInput.vue +24 -8
- package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
- package/src/components/headless/forms/AGHeadlessInputInput.vue +44 -5
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
- package/src/components/headless/forms/AGHeadlessInputTextArea.vue +43 -0
- package/src/components/headless/forms/AGHeadlessSelect.ts +28 -17
- package/src/components/headless/forms/AGHeadlessSelect.vue +60 -28
- 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 +0 -4
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
- package/src/components/headless/forms/composition.ts +10 -0
- package/src/components/headless/forms/index.ts +9 -3
- package/src/components/headless/modals/AGHeadlessModal.ts +29 -0
- package/src/components/headless/modals/AGHeadlessModal.vue +13 -9
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -6
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +14 -4
- package/src/components/headless/modals/index.ts +4 -6
- package/src/components/headless/snackbars/index.ts +23 -8
- package/src/components/index.ts +3 -1
- package/src/components/interfaces.ts +24 -0
- package/src/components/{basic → lib}/AGErrorMessage.vue +2 -2
- package/src/components/lib/AGMarkdown.vue +54 -0
- package/src/components/lib/AGMeasured.vue +16 -0
- package/src/components/lib/AGProgressBar.vue +55 -0
- package/src/components/lib/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +6 -0
- package/src/components/modals/AGAlertModal.ts +18 -0
- package/src/components/modals/AGAlertModal.vue +4 -15
- package/src/components/modals/AGConfirmModal.ts +42 -0
- package/src/components/modals/AGConfirmModal.vue +11 -15
- package/src/components/modals/AGErrorReportModal.ts +30 -1
- package/src/components/modals/AGErrorReportModal.vue +8 -16
- package/src/components/modals/AGErrorReportModalButtons.vue +4 -2
- package/src/components/modals/AGErrorReportModalTitle.vue +1 -1
- package/src/components/modals/AGLoadingModal.ts +29 -0
- package/src/components/modals/AGLoadingModal.vue +4 -8
- package/src/components/modals/AGModal.ts +3 -2
- package/src/components/modals/AGModal.vue +14 -12
- package/src/components/modals/AGModalContext.vue +14 -4
- package/src/components/modals/AGPromptModal.ts +41 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +13 -19
- package/src/components/snackbars/AGSnackbar.vue +3 -9
- package/src/components/utils.ts +10 -0
- package/src/directives/index.ts +5 -1
- package/src/directives/measure.ts +40 -0
- package/src/errors/Errors.ts +26 -24
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +12 -23
- package/src/errors/utils.ts +35 -0
- package/src/forms/Form.test.ts +28 -0
- package/src/forms/Form.ts +74 -11
- package/src/forms/index.ts +3 -1
- package/src/forms/utils.ts +34 -3
- package/src/forms/validation.ts +19 -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 +43 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +44 -29
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +3 -0
- package/src/services/App.state.ts +26 -5
- package/src/services/App.ts +38 -3
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +111 -31
- package/src/services/Service.ts +145 -40
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +11 -3
- package/src/services/store.ts +8 -5
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +25 -0
- package/src/testing/setup.ts +27 -0
- package/src/ui/UI.state.ts +7 -0
- package/src/ui/UI.ts +240 -34
- package/src/ui/index.ts +9 -3
- package/src/ui/utils.ts +16 -0
- package/src/utils/composition/events.ts +1 -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 +24 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +17 -2
- package/src/utils/tailwindcss.test.ts +26 -0
- package/src/utils/tailwindcss.ts +7 -0
- package/src/utils/vue.ts +26 -5
- package/tailwind.config.js +4 -0
- package/tsconfig.json +1 -1
- package/vite.config.ts +4 -1
- package/.eslintrc.js +0 -3
- package/dist/virtual.d.ts +0 -11
- package/src/components/basic/AGMarkdown.vue +0 -36
- package/src/components/basic/index.ts +0 -5
- package/src/components/headless/forms/AGHeadlessSelectButton.ts +0 -3
- package/src/components/headless/forms/AGHeadlessSelectLabel.ts +0 -3
- package/src/types/virtual.d.ts +0 -11
- /package/src/components/{basic → lib}/AGLink.vue +0 -0
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: 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
13
|
export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
|
|
@@ -16,70 +20,146 @@ 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
|
+
|
|
29
|
+
export type EventListenerPriority = (typeof EventListenerPriorities)[keyof typeof EventListenerPriorities];
|
|
30
|
+
|
|
19
31
|
export class EventsService extends Service {
|
|
20
32
|
|
|
21
|
-
private listeners: Record<string,
|
|
33
|
+
private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
|
|
34
|
+
|
|
35
|
+
protected async boot(): Promise<void> {
|
|
36
|
+
Object.entries(globalThis.__aerogelEvents__ ?? {}).forEach(([event, listener]) =>
|
|
37
|
+
this.on(event as string, 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
42
|
public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
|
|
26
43
|
public async emit(event: string, payload?: unknown): Promise<void> {
|
|
27
|
-
const listeners =
|
|
44
|
+
const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
|
|
28
45
|
|
|
29
|
-
|
|
46
|
+
for (const priority of listeners.priorities) {
|
|
47
|
+
await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
|
|
48
|
+
}
|
|
30
49
|
}
|
|
31
50
|
|
|
51
|
+
/* eslint-disable max-len */
|
|
32
52
|
public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
33
|
-
public on<Event extends
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
): () => void | void;
|
|
37
|
-
|
|
53
|
+
public on<Event extends EventWithoutPayload>(event: Event, priority: EventListenerPriority, listener: () => unknown): () => void; // prettier-ignore
|
|
54
|
+
public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
55
|
+
public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
56
|
+
public on<Event extends EventWithPayload>(event: Event, priority: EventListenerPriority, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
57
|
+
public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
38
58
|
public on<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
39
|
-
public on(event:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
59
|
+
public on<Event extends string>(event: UnknownEvent<Event>, priority: EventListenerPriority, listener: EventListener): () => void; // prettier-ignore
|
|
60
|
+
public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
61
|
+
/* eslint-enable max-len */
|
|
62
|
+
|
|
63
|
+
public on(
|
|
64
|
+
event: string,
|
|
65
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListenerPriority | EventListener,
|
|
66
|
+
listener?: EventListener,
|
|
67
|
+
): () => void {
|
|
68
|
+
const options =
|
|
69
|
+
typeof optionsOrListener === 'function'
|
|
70
|
+
? {}
|
|
71
|
+
: typeof optionsOrListener === 'number'
|
|
72
|
+
? { priority: optionsOrListener }
|
|
73
|
+
: optionsOrListener;
|
|
74
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
75
|
+
|
|
76
|
+
this.registerListener(event, options, handler);
|
|
77
|
+
|
|
78
|
+
return () => this.off(event, handler);
|
|
43
79
|
}
|
|
44
80
|
|
|
81
|
+
/* eslint-disable max-len */
|
|
45
82
|
public once<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
46
|
-
public once<Event extends
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
): () => void | void;
|
|
50
|
-
|
|
83
|
+
public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
84
|
+
public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
85
|
+
public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
51
86
|
public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
52
|
-
public once(event:
|
|
87
|
+
public once<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
88
|
+
/* eslint-enable max-len */
|
|
89
|
+
|
|
90
|
+
public once(
|
|
91
|
+
event: string,
|
|
92
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
93
|
+
listener?: EventListener,
|
|
94
|
+
): () => void {
|
|
53
95
|
let onceListener: EventListener | null = null;
|
|
96
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
97
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
54
98
|
|
|
55
99
|
return tap(
|
|
56
100
|
() => onceListener && this.off(event, onceListener),
|
|
57
101
|
(off) => {
|
|
58
|
-
|
|
59
|
-
(
|
|
60
|
-
off();
|
|
102
|
+
onceListener = (...args) => {
|
|
103
|
+
off();
|
|
61
104
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
105
|
+
return handler(...args);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
this.registerListener(event, options, handler);
|
|
65
109
|
},
|
|
66
110
|
);
|
|
67
111
|
}
|
|
68
112
|
|
|
69
113
|
public off(event: string, listener: EventListener): void {
|
|
70
|
-
const
|
|
114
|
+
const listeners = this.listeners[event];
|
|
71
115
|
|
|
72
|
-
if (!
|
|
116
|
+
if (!listeners) {
|
|
73
117
|
return;
|
|
74
118
|
}
|
|
75
119
|
|
|
76
|
-
|
|
120
|
+
const priorities = [...listeners.priorities];
|
|
121
|
+
|
|
122
|
+
for (const priority of priorities) {
|
|
123
|
+
arrayRemove(listeners.handlers[priority] ?? [], listener);
|
|
77
124
|
|
|
78
|
-
|
|
125
|
+
if (listeners.handlers[priority]?.length === 0) {
|
|
126
|
+
delete listeners.handlers[priority];
|
|
127
|
+
arrayRemove(listeners.priorities, priority);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (listeners.priorities.length === 0) {
|
|
79
132
|
delete this.listeners[event];
|
|
80
133
|
}
|
|
81
134
|
}
|
|
82
135
|
|
|
136
|
+
protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
|
|
137
|
+
const priority = options.priority ?? 0;
|
|
138
|
+
|
|
139
|
+
if (!(event in this.listeners)) {
|
|
140
|
+
this.listeners[event] = { priorities: [], handlers: {} };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const priorities =
|
|
144
|
+
this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
|
|
145
|
+
const handlers =
|
|
146
|
+
this.listeners[event]?.handlers ??
|
|
147
|
+
fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
|
|
148
|
+
|
|
149
|
+
if (!priorities.includes(priority)) {
|
|
150
|
+
priorities.push(priority);
|
|
151
|
+
priorities.sort((a, b) => b - a);
|
|
152
|
+
handlers[priority] = [];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
handlers[priority]?.push(handler);
|
|
156
|
+
}
|
|
157
|
+
|
|
83
158
|
}
|
|
84
159
|
|
|
85
|
-
export default facade(
|
|
160
|
+
export default facade(EventsService);
|
|
161
|
+
|
|
162
|
+
declare global {
|
|
163
|
+
// eslint-disable-next-line no-var
|
|
164
|
+
var __aerogelEvents__: AerogelGlobalEvents | undefined;
|
|
165
|
+
}
|
package/src/services/Service.ts
CHANGED
|
@@ -1,31 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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';
|
|
3
12
|
import type { Store } from 'pinia';
|
|
4
13
|
|
|
5
14
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
6
15
|
import { defineServiceStore } from '@/services/store';
|
|
16
|
+
import type { Unref } from '@/utils/vue';
|
|
7
17
|
|
|
8
18
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
19
|
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
20
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
11
21
|
|
|
12
22
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
|
-
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
23
|
+
[K in keyof TComputedState]: (state: Unref<TState>) => TComputedState[K];
|
|
14
24
|
} & ThisType<{
|
|
15
25
|
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
26
|
}>;
|
|
17
27
|
|
|
28
|
+
export type StateWatchers<TService extends Service, TState extends ServiceState> = {
|
|
29
|
+
[K in keyof TState]?: (this: TService, value: TState[K], oldValue: TState[K]) => unknown;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type ServiceWithState<
|
|
33
|
+
State extends ServiceState = ServiceState,
|
|
34
|
+
ComputedState extends ServiceState = {},
|
|
35
|
+
ServiceStorage = Partial<State>
|
|
36
|
+
> = Constructor<Unref<State>> &
|
|
37
|
+
Constructor<ComputedState> &
|
|
38
|
+
Constructor<Service<Unref<State>, ComputedState, Unref<ServiceStorage>>>;
|
|
39
|
+
|
|
18
40
|
export function defineServiceState<
|
|
19
41
|
State extends ServiceState = ServiceState,
|
|
20
|
-
ComputedState extends ServiceState = {}
|
|
42
|
+
ComputedState extends ServiceState = {},
|
|
43
|
+
ServiceStorage = Partial<State>
|
|
21
44
|
>(options: {
|
|
22
45
|
name: string;
|
|
23
|
-
initialState: State;
|
|
46
|
+
initialState: State | (() => State);
|
|
24
47
|
persist?: (keyof State)[];
|
|
48
|
+
watch?: StateWatchers<Service, State>;
|
|
25
49
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
26
|
-
serialize?: (state: Partial<State>) =>
|
|
27
|
-
|
|
28
|
-
|
|
50
|
+
serialize?: (state: Partial<State>) => ServiceStorage;
|
|
51
|
+
restore?: (state: ServiceStorage) => Partial<State>;
|
|
52
|
+
}): ServiceWithState<State, ComputedState, ServiceStorage> {
|
|
53
|
+
return class extends Service<Unref<State>, ComputedState, ServiceStorage> {
|
|
29
54
|
|
|
30
55
|
public static persist = (options.persist as string[]) ?? [];
|
|
31
56
|
|
|
@@ -37,27 +62,53 @@ export function defineServiceState<
|
|
|
37
62
|
return options.name ?? null;
|
|
38
63
|
}
|
|
39
64
|
|
|
40
|
-
protected getInitialState(): State {
|
|
41
|
-
|
|
65
|
+
protected getInitialState(): Unref<State> {
|
|
66
|
+
if (typeof options.initialState === 'function') {
|
|
67
|
+
return options.initialState();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return Object.entries(options.initialState).reduce((state, [key, value]) => {
|
|
71
|
+
try {
|
|
72
|
+
value = structuredClone(value);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
// eslint-disable-next-line no-console
|
|
75
|
+
console.warn(
|
|
76
|
+
`Could not clone '${key}' state from ${this.getName()} service, ` +
|
|
77
|
+
'this may cause problems if you\'re using multiple instances of the service ' +
|
|
78
|
+
'(for example, in unit tests).\n' +
|
|
79
|
+
'To fix this problem, declare your initialState as a function instead.',
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
state[key as keyof State] = value;
|
|
84
|
+
|
|
85
|
+
return state;
|
|
86
|
+
}, {} as Unref<State>);
|
|
42
87
|
}
|
|
43
88
|
|
|
44
|
-
protected getComputedStateDefinition(): ComputedStateDefinition<State
|
|
45
|
-
return options.computed ??
|
|
89
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<Unref<State>, ComputedState> {
|
|
90
|
+
return (options.computed ?? {}) as ComputedStateDefinition<Unref<State>, ComputedState>;
|
|
46
91
|
}
|
|
47
92
|
|
|
48
|
-
protected
|
|
49
|
-
return options.
|
|
93
|
+
protected getStateWatchers(): StateWatchers<Service, Unref<State>> {
|
|
94
|
+
return (options.watch ?? {}) as StateWatchers<Service, Unref<State>>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
98
|
+
return options.serialize?.(state) ?? (state as ServiceStorage);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
protected deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
102
|
+
return options.restore?.(state) ?? (state as Partial<State>);
|
|
50
103
|
}
|
|
51
104
|
|
|
52
|
-
} as unknown as
|
|
53
|
-
Constructor<ComputedState> &
|
|
54
|
-
Constructor<Service<State, ComputedState, Partial<State>>>;
|
|
105
|
+
} as unknown as ServiceWithState<State, ComputedState, ServiceStorage>;
|
|
55
106
|
}
|
|
56
107
|
|
|
57
108
|
export default class Service<
|
|
58
109
|
State extends ServiceState = DefaultServiceState,
|
|
59
110
|
ComputedState extends ServiceState = {},
|
|
60
|
-
ServiceStorage
|
|
111
|
+
ServiceStorage = Partial<State>
|
|
61
112
|
> extends MagicObject {
|
|
62
113
|
|
|
63
114
|
public static persist: string[] = [];
|
|
@@ -65,7 +116,8 @@ export default class Service<
|
|
|
65
116
|
protected _name: string;
|
|
66
117
|
private _booted: PromisedValue<void>;
|
|
67
118
|
private _computedStateKeys: Set<keyof State>;
|
|
68
|
-
private
|
|
119
|
+
private _watchers: StateWatchers<Service, State>;
|
|
120
|
+
private _store: Store<string, State, ComputedState, {}> | false;
|
|
69
121
|
|
|
70
122
|
constructor() {
|
|
71
123
|
super();
|
|
@@ -75,6 +127,7 @@ export default class Service<
|
|
|
75
127
|
this._name = this.getName() ?? new.target.name;
|
|
76
128
|
this._booted = new PromisedValue();
|
|
77
129
|
this._computedStateKeys = new Set(Object.keys(getters));
|
|
130
|
+
this._watchers = this.getStateWatchers();
|
|
78
131
|
this._store =
|
|
79
132
|
this.usesStore() &&
|
|
80
133
|
defineServiceStore(this._name, {
|
|
@@ -89,6 +142,12 @@ export default class Service<
|
|
|
89
142
|
return this._booted;
|
|
90
143
|
}
|
|
91
144
|
|
|
145
|
+
public static<T extends typeof Service>(): T;
|
|
146
|
+
public static<T extends typeof Service, K extends keyof T>(property: K): T[K];
|
|
147
|
+
public static<T extends typeof Service, K extends keyof T>(property?: K): T | T[K] {
|
|
148
|
+
return super.static<T, K>(property as K);
|
|
149
|
+
}
|
|
150
|
+
|
|
92
151
|
public launch(): Promise<void> {
|
|
93
152
|
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
94
153
|
|
|
@@ -104,6 +163,10 @@ export default class Service<
|
|
|
104
163
|
return this._booted;
|
|
105
164
|
}
|
|
106
165
|
|
|
166
|
+
public hasPersistedState(): boolean {
|
|
167
|
+
return Storage.has(this._name);
|
|
168
|
+
}
|
|
169
|
+
|
|
107
170
|
public hasState<P extends keyof State>(property: P): boolean {
|
|
108
171
|
if (!this._store) {
|
|
109
172
|
return false;
|
|
@@ -132,13 +195,28 @@ export default class Service<
|
|
|
132
195
|
return;
|
|
133
196
|
}
|
|
134
197
|
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
198
|
+
const update = typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty;
|
|
199
|
+
const old = objectOnly(this._store.$state as State, Object.keys(update));
|
|
200
|
+
|
|
201
|
+
Object.assign(this._store.$state, update);
|
|
202
|
+
this.onStateUpdated(update as Partial<State>, old as Partial<State>);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public updatePersistedState<T extends keyof State>(key: T): void;
|
|
206
|
+
public updatePersistedState<T extends keyof State>(keys: T[]): void;
|
|
207
|
+
public updatePersistedState<T extends keyof State>(keyOrKeys: T | T[]): void {
|
|
208
|
+
if (!this._store) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
138
211
|
|
|
139
|
-
|
|
212
|
+
const keys = arrayFrom(keyOrKeys) as Array<keyof State>;
|
|
213
|
+
const state = objectOnly(this._store.$state as State, keys);
|
|
140
214
|
|
|
141
|
-
|
|
215
|
+
if (isEmpty(state)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
this.onPersistentStateUpdated(state);
|
|
142
220
|
}
|
|
143
221
|
|
|
144
222
|
protected __get(property: string): unknown {
|
|
@@ -153,16 +231,30 @@ export default class Service<
|
|
|
153
231
|
this.setState({ [property]: value } as Partial<State>);
|
|
154
232
|
}
|
|
155
233
|
|
|
156
|
-
protected onStateUpdated(
|
|
157
|
-
|
|
158
|
-
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
159
|
-
const persisted = objectOnly(state, persist);
|
|
234
|
+
protected onStateUpdated(update: Partial<State>, old: Partial<State>): void {
|
|
235
|
+
const persisted = objectOnly(update, this.static('persist'));
|
|
160
236
|
|
|
161
|
-
if (isEmpty(persisted)) {
|
|
162
|
-
|
|
237
|
+
if (!isEmpty(persisted)) {
|
|
238
|
+
this.onPersistentStateUpdated(persisted as Partial<State>);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
for (const property in update) {
|
|
242
|
+
const watcher = this._watchers[property] as Nullable<(value: unknown, oldValue: unknown) => unknown>;
|
|
243
|
+
|
|
244
|
+
if (!watcher || update[property] === old[property]) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
watcher.call(this, update[property], old[property]);
|
|
163
249
|
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
protected onPersistentStateUpdated(persisted: Partial<State>): void {
|
|
253
|
+
const storage = Storage.get<ServiceStorage>(this._name);
|
|
164
254
|
|
|
165
|
-
|
|
255
|
+
if (!storage) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
166
258
|
|
|
167
259
|
Storage.set(this._name, {
|
|
168
260
|
...storage,
|
|
@@ -186,8 +278,16 @@ export default class Service<
|
|
|
186
278
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
187
279
|
}
|
|
188
280
|
|
|
189
|
-
protected
|
|
190
|
-
return
|
|
281
|
+
protected getStateWatchers(): StateWatchers<Service, State> {
|
|
282
|
+
return {};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
protected serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
286
|
+
return state as ServiceStorage;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
protected deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
290
|
+
return state as Partial<State>;
|
|
191
291
|
}
|
|
192
292
|
|
|
193
293
|
protected async frameworkBoot(): Promise<void> {
|
|
@@ -195,25 +295,30 @@ export default class Service<
|
|
|
195
295
|
}
|
|
196
296
|
|
|
197
297
|
protected async boot(): Promise<void> {
|
|
198
|
-
//
|
|
298
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
199
299
|
}
|
|
200
300
|
|
|
201
301
|
protected restorePersistedState(): void {
|
|
202
|
-
|
|
203
|
-
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
204
|
-
|
|
205
|
-
if (!this.usesStore() || isEmpty(persist)) {
|
|
302
|
+
if (!this.usesStore() || isEmpty(this.static('persist'))) {
|
|
206
303
|
return;
|
|
207
304
|
}
|
|
208
305
|
|
|
209
306
|
if (Storage.has(this._name)) {
|
|
210
307
|
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
211
|
-
this.setState(persisted);
|
|
308
|
+
this.setState(this.deserializePersistedState(persisted));
|
|
212
309
|
|
|
213
310
|
return;
|
|
214
311
|
}
|
|
215
312
|
|
|
216
|
-
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
313
|
+
Storage.set(this._name, objectOnly(this.getState(), this.static('persist')));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
protected requireStore(): Store<string, State, ComputedState, {}> {
|
|
317
|
+
if (!this._store) {
|
|
318
|
+
return fail(`Failed getting '${this._name}' store`);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return this._store;
|
|
217
322
|
}
|
|
218
323
|
|
|
219
324
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
import Service from '@/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 '@/services/Events' {
|
|
17
|
+
export interface EventsPayload {
|
|
18
|
+
'purge-storage': void;
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -3,19 +3,25 @@ import type { App as VueApp } from 'vue';
|
|
|
3
3
|
import { definePlugin } from '@/plugins';
|
|
4
4
|
|
|
5
5
|
import App from './App';
|
|
6
|
+
import Cache from './Cache';
|
|
6
7
|
import Events from './Events';
|
|
7
8
|
import Service from './Service';
|
|
9
|
+
import Storage from './Storage';
|
|
8
10
|
import { getPiniaStore } from './store';
|
|
9
11
|
|
|
10
12
|
export * from './App';
|
|
13
|
+
export * from './Cache';
|
|
11
14
|
export * from './Events';
|
|
12
15
|
export * from './Service';
|
|
16
|
+
export * from './store';
|
|
17
|
+
export * from './utils';
|
|
13
18
|
|
|
14
|
-
export { App, Events, Service };
|
|
19
|
+
export { App, Cache, Events, Storage, Service };
|
|
15
20
|
|
|
16
21
|
const defaultServices = {
|
|
17
22
|
$app: App,
|
|
18
23
|
$events: Events,
|
|
24
|
+
$storage: Storage,
|
|
19
25
|
};
|
|
20
26
|
|
|
21
27
|
export type DefaultServices = typeof defaultServices;
|
|
@@ -33,7 +39,9 @@ export async function bootServices(app: VueApp, services: Record<string, Service
|
|
|
33
39
|
|
|
34
40
|
Object.assign(app.config.globalProperties, services);
|
|
35
41
|
|
|
36
|
-
App.development
|
|
42
|
+
if (App.development || App.testing) {
|
|
43
|
+
Object.assign(globalThis, services);
|
|
44
|
+
}
|
|
37
45
|
}
|
|
38
46
|
|
|
39
47
|
export default definePlugin({
|
|
@@ -50,7 +58,7 @@ export default definePlugin({
|
|
|
50
58
|
});
|
|
51
59
|
|
|
52
60
|
declare module '@/bootstrap/options' {
|
|
53
|
-
interface AerogelOptions {
|
|
61
|
+
export interface AerogelOptions {
|
|
54
62
|
services?: Record<string, Service>;
|
|
55
63
|
}
|
|
56
64
|
}
|
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,18 @@
|
|
|
1
|
+
import { objectOnly } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
export type Replace<
|
|
4
|
+
TOriginal extends Record<string, unknown>,
|
|
5
|
+
TReplacements extends Partial<Record<keyof TOriginal, unknown>>
|
|
6
|
+
> = {
|
|
7
|
+
[K in keyof TOriginal]: TReplacements extends Record<K, infer Replacement> ? Replacement : TOriginal[K];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function replaceExisting<
|
|
11
|
+
TOriginal extends Record<string, unknown>,
|
|
12
|
+
TReplacements extends Partial<Record<keyof TOriginal, unknown>>
|
|
13
|
+
>(original: TOriginal, replacements: TReplacements): Replace<TOriginal, TReplacements> {
|
|
14
|
+
return {
|
|
15
|
+
...original,
|
|
16
|
+
...objectOnly(replacements, Object.keys(original)),
|
|
17
|
+
} as Replace<TOriginal, TReplacements>;
|
|
18
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { mock, tap, toString } from '@noeldemartin/utils';
|
|
2
|
+
import { beforeEach, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
tap(globalThis, (global: any) => {
|
|
6
|
+
const localStorage: Record<string, string> = {};
|
|
7
|
+
|
|
8
|
+
global.jest = vi;
|
|
9
|
+
global.navigator = { languages: ['en'] };
|
|
10
|
+
global.localStorage = mock<Storage>({
|
|
11
|
+
getItem: (key) => localStorage[key] ?? null,
|
|
12
|
+
setItem(key, value) {
|
|
13
|
+
localStorage[key] = toString(value);
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
vi.mock('dompurify', async () => {
|
|
19
|
+
return { default: { sanitize: (html: string) => html } };
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
vi.stubGlobal('document', {
|
|
24
|
+
querySelector: () => null,
|
|
25
|
+
getElementById: () => null,
|
|
26
|
+
});
|
|
27
|
+
});
|