@aerogel/core 0.0.0-next.c8f032a868370824898e171969aec1bb6827688e → 0.0.0-next.ce4783d09a83f492e439f8d4c39bc0b4998f4cbf
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 +2387 -580
- package/dist/aerogel-core.js +3557 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +39 -34
- package/src/bootstrap/bootstrap.test.ts +7 -10
- package/src/bootstrap/index.ts +41 -9
- package/src/bootstrap/options.ts +4 -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/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
- package/src/components/headless/HeadlessInputInput.vue +75 -0
- package/src/components/headless/HeadlessInputLabel.vue +18 -0
- package/src/components/headless/HeadlessInputTextArea.vue +40 -0
- package/src/components/headless/HeadlessModal.vue +57 -0
- package/src/components/headless/HeadlessModalContent.vue +30 -0
- package/src/components/headless/HeadlessModalDescription.vue +12 -0
- package/src/components/headless/HeadlessModalOverlay.vue +12 -0
- package/src/components/headless/HeadlessModalTitle.vue +12 -0
- package/src/components/headless/HeadlessSelect.vue +120 -0
- package/src/components/headless/HeadlessSelectError.vue +25 -0
- package/src/components/headless/HeadlessSelectLabel.vue +25 -0
- package/src/components/headless/HeadlessSelectOption.vue +34 -0
- package/src/components/headless/HeadlessSelectOptions.vue +42 -0
- package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
- package/src/components/headless/HeadlessSelectValue.vue +18 -0
- package/src/components/headless/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 -7
- 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 +123 -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/SettingsModal.vue +15 -0
- package/src/components/ui/StartupCrash.vue +31 -0
- package/src/components/ui/Switch.vue +11 -0
- package/src/components/ui/Toast.vue +46 -0
- package/src/components/ui/index.ts +33 -0
- package/src/directives/index.ts +27 -6
- package/src/directives/measure.ts +46 -0
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +187 -0
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +53 -0
- package/src/errors/settings/Debug.vue +39 -0
- package/src/errors/settings/index.ts +10 -0
- package/src/errors/utils.ts +35 -0
- package/src/forms/FormController.test.ts +110 -0
- package/src/forms/FormController.ts +246 -0
- package/src/forms/index.ts +3 -2
- package/src/forms/utils.ts +62 -14
- package/src/forms/validation.ts +19 -0
- package/src/index.css +73 -0
- package/src/{main.ts → index.ts} +4 -2
- 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 +45 -22
- package/src/lang/index.ts +14 -10
- 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 +2 -1
- package/src/plugins/index.ts +22 -0
- package/src/services/App.state.ts +43 -3
- package/src/services/App.ts +59 -3
- 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 +245 -53
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +38 -9
- package/src/services/store.ts +30 -0
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +26 -0
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +21 -9
- package/src/ui/UI.ts +327 -64
- package/src/ui/index.ts +33 -22
- package/src/ui/utils.ts +16 -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/persistent.test.ts +33 -0
- package/src/utils/composition/persistent.ts +11 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +33 -0
- package/src/utils/index.ts +6 -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/components/AGAppLayout.vue +0 -11
- package/src/components/AGAppOverlays.vue +0 -39
- package/src/components/basic/AGMarkdown.vue +0 -35
- package/src/components/basic/index.ts +0 -3
- package/src/components/forms/AGButton.vue +0 -21
- 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/AGHeadlessInputInput.vue +0 -33
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +0 -16
- package/src/components/headless/forms/index.ts +0 -6
- package/src/components/headless/modals/AGHeadlessModal.ts +0 -7
- package/src/components/headless/modals/AGHeadlessModal.vue +0 -88
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -24
- 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 -26
- package/src/components/modals/AGConfirmModal.vue +0 -30
- package/src/components/modals/AGModal.ts +0 -10
- 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 -7
- package/src/directives/initial-focus.ts +0 -11
- package/src/forms/Form.test.ts +0 -37
- package/src/forms/Form.ts +0 -166
- package/src/forms/composition.ts +0 -6
- package/src/globals.ts +0 -6
- package/tsconfig.json +0 -10
- package/vite.config.ts +0 -13
package/src/services/Service.ts
CHANGED
|
@@ -1,75 +1,159 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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';
|
|
5
13
|
|
|
6
|
-
import ServiceBootError from '
|
|
14
|
+
import ServiceBootError from '@aerogel/core/errors/ServiceBootError';
|
|
15
|
+
import { defineServiceStore } from '@aerogel/core/services/store';
|
|
16
|
+
import type { Unref } from '@aerogel/core/utils/vue';
|
|
7
17
|
|
|
8
18
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export type DefaultServiceState =
|
|
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];
|
|
24
|
+
} & ThisType<{
|
|
25
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
26
|
+
}>;
|
|
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;
|
|
14
30
|
};
|
|
15
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
|
+
|
|
16
40
|
export function defineServiceState<
|
|
17
41
|
State extends ServiceState = ServiceState,
|
|
18
|
-
ComputedState extends ServiceState = {}
|
|
42
|
+
ComputedState extends ServiceState = {},
|
|
43
|
+
ServiceStorage = Partial<State>,
|
|
19
44
|
>(options: {
|
|
20
|
-
|
|
45
|
+
name: string;
|
|
46
|
+
initialState: State | (() => State);
|
|
47
|
+
persist?: (keyof State)[];
|
|
48
|
+
watch?: StateWatchers<Service, State>;
|
|
21
49
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
22
|
-
|
|
23
|
-
|
|
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> {
|
|
54
|
+
|
|
55
|
+
public static override persist = (options.persist as string[]) ?? [];
|
|
56
|
+
|
|
57
|
+
protected override usesStore(): boolean {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
24
60
|
|
|
25
|
-
protected
|
|
26
|
-
return options.
|
|
61
|
+
protected override getName(): string | null {
|
|
62
|
+
return options.name ?? null;
|
|
27
63
|
}
|
|
28
64
|
|
|
29
|
-
protected
|
|
30
|
-
|
|
65
|
+
protected override 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>);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
protected override getComputedStateDefinition(): ComputedStateDefinition<Unref<State>, ComputedState> {
|
|
90
|
+
return (options.computed ?? {}) as ComputedStateDefinition<Unref<State>, ComputedState>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
protected override getStateWatchers(): StateWatchers<Service, Unref<State>> {
|
|
94
|
+
return (options.watch ?? {}) as StateWatchers<Service, Unref<State>>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected override serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
98
|
+
return options.serialize?.(state) ?? (state as ServiceStorage);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
protected override deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
102
|
+
return options.restore?.(state) ?? (state as Partial<State>);
|
|
31
103
|
}
|
|
32
104
|
|
|
33
|
-
} as unknown as
|
|
105
|
+
} as unknown as ServiceWithState<State, ComputedState, ServiceStorage>;
|
|
34
106
|
}
|
|
35
107
|
|
|
36
108
|
export default class Service<
|
|
37
109
|
State extends ServiceState = DefaultServiceState,
|
|
38
|
-
ComputedState extends ServiceState = {}
|
|
110
|
+
ComputedState extends ServiceState = {},
|
|
111
|
+
ServiceStorage = Partial<State>,
|
|
39
112
|
> extends MagicObject {
|
|
40
113
|
|
|
41
|
-
|
|
114
|
+
public static persist: string[] = [];
|
|
115
|
+
|
|
116
|
+
protected _name: string;
|
|
42
117
|
private _booted: PromisedValue<void>;
|
|
43
|
-
private
|
|
44
|
-
private
|
|
118
|
+
private _computedStateKeys: Set<keyof State>;
|
|
119
|
+
private _watchers: StateWatchers<Service, State>;
|
|
120
|
+
private _store: Store<string, State, ComputedState, {}> | false;
|
|
45
121
|
|
|
46
122
|
constructor() {
|
|
47
123
|
super();
|
|
48
124
|
|
|
49
|
-
|
|
125
|
+
const getters = this.getComputedStateDefinition();
|
|
126
|
+
|
|
127
|
+
this._name = this.getName() ?? new.target.name;
|
|
50
128
|
this._booted = new PromisedValue();
|
|
51
|
-
this.
|
|
52
|
-
this.
|
|
53
|
-
|
|
54
|
-
|
|
129
|
+
this._computedStateKeys = new Set(Object.keys(getters));
|
|
130
|
+
this._watchers = this.getStateWatchers();
|
|
131
|
+
this._store =
|
|
132
|
+
this.usesStore() &&
|
|
133
|
+
defineServiceStore(this._name, {
|
|
134
|
+
state: () => this.getInitialState(),
|
|
55
135
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
);
|
|
136
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
137
|
+
getters: getters as any,
|
|
138
|
+
});
|
|
60
139
|
}
|
|
61
140
|
|
|
62
141
|
public get booted(): PromisedValue<void> {
|
|
63
142
|
return this._booted;
|
|
64
143
|
}
|
|
65
144
|
|
|
66
|
-
public
|
|
67
|
-
|
|
145
|
+
public override static<T extends typeof Service>(): T;
|
|
146
|
+
public override static<T extends typeof Service, K extends keyof T>(property: K): T[K];
|
|
147
|
+
public override static<T extends typeof Service, K extends keyof T>(property?: K): T | T[K] {
|
|
148
|
+
return super.static<T, K>(property as K);
|
|
149
|
+
}
|
|
68
150
|
|
|
69
|
-
|
|
151
|
+
public launch(): Promise<void> {
|
|
152
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
70
153
|
|
|
71
154
|
try {
|
|
72
|
-
this.
|
|
155
|
+
this.frameworkBoot()
|
|
156
|
+
.then(() => this.boot())
|
|
73
157
|
.then(() => this._booted.resolve())
|
|
74
158
|
.catch(handleError);
|
|
75
159
|
} catch (error) {
|
|
@@ -79,42 +163,111 @@ export default class Service<
|
|
|
79
163
|
return this._booted;
|
|
80
164
|
}
|
|
81
165
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
166
|
+
public hasPersistedState(): boolean {
|
|
167
|
+
return Storage.has(this._name);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
171
|
+
if (!this._store) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public getState(): State;
|
|
179
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
180
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
181
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
182
|
+
const store = this._store as any;
|
|
183
|
+
|
|
184
|
+
if (property) {
|
|
185
|
+
return store ? store[property] : (undefined as State[P]);
|
|
85
186
|
}
|
|
86
187
|
|
|
87
|
-
|
|
88
|
-
|
|
188
|
+
return store ? store : ({} as State);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
192
|
+
public setState(state: Partial<State>): void;
|
|
193
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
194
|
+
if (!this._store) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
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
|
+
}
|
|
211
|
+
|
|
212
|
+
const keys = arrayFrom(keyOrKeys) as Array<keyof State>;
|
|
213
|
+
const state = objectOnly(this._store.$state as State, keys);
|
|
214
|
+
|
|
215
|
+
if (isEmpty(state)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
this.onPersistentStateUpdated(state);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
protected override __get(property: string): unknown {
|
|
223
|
+
if (this.hasState(property)) {
|
|
224
|
+
return this.getState(property);
|
|
89
225
|
}
|
|
90
226
|
|
|
91
227
|
return super.__get(property);
|
|
92
228
|
}
|
|
93
229
|
|
|
94
|
-
protected __set(property: string, value: unknown): void {
|
|
230
|
+
protected override __set(property: string, value: unknown): void {
|
|
95
231
|
this.setState({ [property]: value } as Partial<State>);
|
|
96
232
|
}
|
|
97
233
|
|
|
98
|
-
protected
|
|
99
|
-
|
|
100
|
-
}
|
|
234
|
+
protected onStateUpdated(update: Partial<State>, old: Partial<State>): void {
|
|
235
|
+
const persisted = objectOnly(update, this.static('persist'));
|
|
101
236
|
|
|
102
|
-
|
|
103
|
-
|
|
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]);
|
|
249
|
+
}
|
|
104
250
|
}
|
|
105
251
|
|
|
106
|
-
protected
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
252
|
+
protected onPersistentStateUpdated(persisted: Partial<State>): void {
|
|
253
|
+
const storage = Storage.get<ServiceStorage>(this._name);
|
|
254
|
+
|
|
255
|
+
if (!storage) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
Storage.set(this._name, {
|
|
260
|
+
...storage,
|
|
261
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
262
|
+
});
|
|
110
263
|
}
|
|
111
264
|
|
|
112
|
-
protected
|
|
113
|
-
return
|
|
265
|
+
protected usesStore(): boolean {
|
|
266
|
+
return false;
|
|
114
267
|
}
|
|
115
268
|
|
|
116
|
-
protected
|
|
117
|
-
|
|
269
|
+
protected getName(): string | null {
|
|
270
|
+
return null;
|
|
118
271
|
}
|
|
119
272
|
|
|
120
273
|
protected getInitialState(): State {
|
|
@@ -125,8 +278,47 @@ export default class Service<
|
|
|
125
278
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
126
279
|
}
|
|
127
280
|
|
|
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>;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
protected async frameworkBoot(): Promise<void> {
|
|
294
|
+
this.restorePersistedState();
|
|
295
|
+
}
|
|
296
|
+
|
|
128
297
|
protected async boot(): Promise<void> {
|
|
129
|
-
//
|
|
298
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
protected restorePersistedState(): void {
|
|
302
|
+
if (!this.usesStore() || isEmpty(this.static('persist'))) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (Storage.has(this._name)) {
|
|
307
|
+
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
308
|
+
this.setState(this.deserializePersistedState(persisted));
|
|
309
|
+
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
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;
|
|
130
322
|
}
|
|
131
323
|
|
|
132
324
|
}
|
|
@@ -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,43 +1,72 @@
|
|
|
1
|
-
import type { App as
|
|
1
|
+
import type { App as AppInstance } from 'vue';
|
|
2
2
|
|
|
3
|
-
import { definePlugin } from '
|
|
3
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
4
|
+
import { isDevelopment, isTesting } from '@noeldemartin/utils';
|
|
4
5
|
|
|
5
6
|
import App from './App';
|
|
7
|
+
import Cache from './Cache';
|
|
6
8
|
import Events from './Events';
|
|
7
9
|
import Service from './Service';
|
|
10
|
+
import Storage from './Storage';
|
|
11
|
+
import { getPiniaStore } from './store';
|
|
12
|
+
import type { AppSetting } from './App.state';
|
|
8
13
|
|
|
9
14
|
export * from './App';
|
|
15
|
+
export * from './Cache';
|
|
10
16
|
export * from './Events';
|
|
11
17
|
export * from './Service';
|
|
18
|
+
export * from './store';
|
|
19
|
+
export * from './utils';
|
|
12
20
|
|
|
13
|
-
export { App, Events, Service };
|
|
21
|
+
export { App, Cache, Events, Storage, Service };
|
|
14
22
|
|
|
15
23
|
const defaultServices = {
|
|
16
24
|
$app: App,
|
|
17
25
|
$events: Events,
|
|
26
|
+
$storage: Storage,
|
|
18
27
|
};
|
|
19
28
|
|
|
20
29
|
export type DefaultServices = typeof defaultServices;
|
|
21
30
|
|
|
22
31
|
export interface Services extends DefaultServices {}
|
|
23
32
|
|
|
24
|
-
export async function bootServices(app:
|
|
33
|
+
export async function bootServices(app: AppInstance, services: Record<string, Service>): Promise<void> {
|
|
25
34
|
await Promise.all(
|
|
26
35
|
Object.entries(services).map(async ([name, service]) => {
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
await service
|
|
37
|
+
.launch()
|
|
38
|
+
.catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
|
|
29
39
|
}),
|
|
30
40
|
);
|
|
31
41
|
|
|
32
42
|
Object.assign(app.config.globalProperties, services);
|
|
43
|
+
|
|
44
|
+
if (isDevelopment() || isTesting()) {
|
|
45
|
+
Object.assign(globalThis, services);
|
|
46
|
+
}
|
|
33
47
|
}
|
|
34
48
|
|
|
35
49
|
export default definePlugin({
|
|
36
|
-
async install(app) {
|
|
37
|
-
|
|
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);
|
|
38
60
|
},
|
|
39
61
|
});
|
|
40
62
|
|
|
41
|
-
declare module '@
|
|
63
|
+
declare module '@aerogel/core/bootstrap/options' {
|
|
64
|
+
export interface AerogelOptions {
|
|
65
|
+
services?: Record<string, Service>;
|
|
66
|
+
settings?: AppSetting[];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
declare module 'vue' {
|
|
42
71
|
interface ComponentCustomProperties extends Services {}
|
|
43
72
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { tap } from '@noeldemartin/utils';
|
|
2
|
+
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
3
|
+
import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
|
|
4
|
+
|
|
5
|
+
let _store: Pinia | null = null;
|
|
6
|
+
|
|
7
|
+
function initializePiniaStore(): Pinia {
|
|
8
|
+
return _store ?? resetPiniaStore();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function resetPiniaStore(): Pinia {
|
|
12
|
+
return tap(createPinia(), (store) => {
|
|
13
|
+
_store = store;
|
|
14
|
+
|
|
15
|
+
setActivePinia(store);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getPiniaStore(): Pinia {
|
|
20
|
+
return _store ?? initializePiniaStore();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function defineServiceStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(
|
|
24
|
+
name: Id,
|
|
25
|
+
options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>,
|
|
26
|
+
): Store<Id, S, G, A> {
|
|
27
|
+
initializePiniaStore();
|
|
28
|
+
|
|
29
|
+
return defineStore(name, options)();
|
|
30
|
+
}
|
|
@@ -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,26 @@
|
|
|
1
|
+
import { isTesting } from '@noeldemartin/utils';
|
|
2
|
+
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
3
|
+
|
|
4
|
+
import Events from '@aerogel/core/services/Events';
|
|
5
|
+
import { definePlugin } from '@aerogel/core/plugins';
|
|
6
|
+
|
|
7
|
+
export interface AerogelTestingRuntime {
|
|
8
|
+
on: (typeof Events)['on'];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default definePlugin({
|
|
12
|
+
async install() {
|
|
13
|
+
if (!isTesting()) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
globalThis.testingRuntime = {
|
|
18
|
+
on: ((...args: GetClosureArgs<(typeof Events)['on']>) => Events.on(...args)) as (typeof Events)['on'],
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
declare global {
|
|
24
|
+
// eslint-disable-next-line no-var
|
|
25
|
+
var testingRuntime: AerogelTestingRuntime | undefined;
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FakeLocalStorage } from '@noeldemartin/testing';
|
|
2
|
+
import { beforeEach, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
vi.mock('dompurify', async () => {
|
|
5
|
+
return { default: { sanitize: (html: string) => html } };
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
FakeLocalStorage.reset();
|
|
10
|
+
FakeLocalStorage.patchGlobal();
|
|
11
|
+
});
|
package/src/ui/UI.state.ts
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
import type { Component } from 'vue';
|
|
2
2
|
|
|
3
|
-
import { defineServiceState } from '
|
|
3
|
+
import { defineServiceState } from '@aerogel/core/services/Service';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import { Layouts, getCurrentLayout } from './utils';
|
|
6
|
+
|
|
7
|
+
export interface UIModal<T = unknown> {
|
|
6
8
|
id: string;
|
|
7
9
|
properties: Record<string, unknown>;
|
|
8
10
|
component: Component;
|
|
11
|
+
closing: boolean;
|
|
9
12
|
beforeClose: Promise<T | undefined>;
|
|
10
13
|
afterClose: Promise<T | undefined>;
|
|
11
14
|
}
|
|
12
15
|
|
|
13
|
-
export interface
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
> {}
|
|
16
|
+
export interface UIToast {
|
|
17
|
+
id: string;
|
|
18
|
+
component: Component;
|
|
19
|
+
properties: Record<string, unknown>;
|
|
20
|
+
}
|
|
19
21
|
|
|
20
22
|
export default defineServiceState({
|
|
21
|
-
|
|
23
|
+
name: 'ui',
|
|
24
|
+
initialState: {
|
|
25
|
+
modals: [] as UIModal[],
|
|
26
|
+
toasts: [] as UIToast[],
|
|
27
|
+
layout: getCurrentLayout(),
|
|
28
|
+
},
|
|
29
|
+
computed: {
|
|
30
|
+
desktop: ({ layout }) => layout === Layouts.Desktop,
|
|
31
|
+
mobile: ({ layout }) => layout === Layouts.Mobile,
|
|
32
|
+
openModals: ({ modals }) => modals.filter(({ closing }) => !closing),
|
|
33
|
+
},
|
|
22
34
|
});
|