@aerogel/core 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e → 0.0.0-next.bb9dcdbb118a15d146d3a1c4cf861ca2f4f1eebd

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.
Files changed (137) hide show
  1. package/dist/aerogel-core.cjs.js +1 -1
  2. package/dist/aerogel-core.cjs.js.map +1 -1
  3. package/dist/aerogel-core.d.ts +1880 -250
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/histoire.config.ts +7 -0
  7. package/noeldemartin.config.js +4 -1
  8. package/package.json +13 -4
  9. package/postcss.config.js +6 -0
  10. package/src/assets/histoire.css +3 -0
  11. package/src/bootstrap/bootstrap.test.ts +3 -3
  12. package/src/bootstrap/index.ts +35 -5
  13. package/src/bootstrap/options.ts +3 -0
  14. package/src/components/AGAppLayout.vue +7 -2
  15. package/src/components/AGAppModals.vue +15 -0
  16. package/src/components/AGAppOverlays.vue +10 -8
  17. package/src/components/AGAppSnackbars.vue +13 -0
  18. package/src/components/composition.ts +23 -0
  19. package/src/components/constants.ts +8 -0
  20. package/src/components/forms/AGButton.vue +25 -15
  21. package/src/components/forms/AGCheckbox.vue +7 -1
  22. package/src/components/forms/AGForm.vue +9 -10
  23. package/src/components/forms/AGInput.vue +10 -6
  24. package/src/components/forms/AGSelect.story.vue +46 -0
  25. package/src/components/forms/AGSelect.vue +60 -0
  26. package/src/components/forms/index.ts +5 -6
  27. package/src/components/headless/forms/AGHeadlessButton.ts +3 -0
  28. package/src/components/headless/forms/AGHeadlessButton.vue +24 -12
  29. package/src/components/headless/forms/AGHeadlessInput.ts +30 -4
  30. package/src/components/headless/forms/AGHeadlessInput.vue +23 -7
  31. package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
  32. package/src/components/headless/forms/AGHeadlessInputInput.vue +44 -5
  33. package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
  34. package/src/components/headless/forms/AGHeadlessInputTextArea.vue +43 -0
  35. package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
  36. package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
  37. package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
  38. package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
  39. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
  40. package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
  41. package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
  42. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
  43. package/src/components/headless/forms/composition.ts +10 -0
  44. package/src/components/headless/forms/index.ts +13 -1
  45. package/src/components/headless/index.ts +1 -0
  46. package/src/components/headless/modals/AGHeadlessModal.ts +29 -0
  47. package/src/components/headless/modals/AGHeadlessModal.vue +13 -9
  48. package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -6
  49. package/src/components/headless/modals/AGHeadlessModalTitle.vue +14 -4
  50. package/src/components/headless/modals/index.ts +4 -6
  51. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
  52. package/src/components/headless/snackbars/index.ts +40 -0
  53. package/src/components/index.ts +5 -1
  54. package/src/components/interfaces.ts +24 -0
  55. package/src/components/lib/AGErrorMessage.vue +16 -0
  56. package/src/components/lib/AGLink.vue +9 -0
  57. package/src/components/lib/AGMarkdown.vue +54 -0
  58. package/src/components/lib/AGMeasured.vue +16 -0
  59. package/src/components/lib/AGProgressBar.vue +30 -0
  60. package/src/components/lib/AGStartupCrash.vue +31 -0
  61. package/src/components/lib/index.ts +6 -0
  62. package/src/components/modals/AGAlertModal.ts +18 -0
  63. package/src/components/modals/AGAlertModal.vue +4 -16
  64. package/src/components/modals/AGConfirmModal.ts +41 -0
  65. package/src/components/modals/AGConfirmModal.vue +10 -14
  66. package/src/components/modals/AGErrorReportModal.ts +49 -0
  67. package/src/components/modals/AGErrorReportModal.vue +54 -0
  68. package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
  69. package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
  70. package/src/components/modals/AGLoadingModal.ts +29 -0
  71. package/src/components/modals/AGLoadingModal.vue +4 -8
  72. package/src/components/modals/AGModal.ts +2 -1
  73. package/src/components/modals/AGModal.vue +16 -13
  74. package/src/components/modals/AGModalContext.vue +14 -4
  75. package/src/components/modals/AGModalTitle.vue +9 -0
  76. package/src/components/modals/AGPromptModal.ts +41 -0
  77. package/src/components/modals/AGPromptModal.vue +34 -0
  78. package/src/components/modals/index.ts +16 -7
  79. package/src/components/snackbars/AGSnackbar.vue +36 -0
  80. package/src/components/snackbars/index.ts +3 -0
  81. package/src/components/utils.ts +10 -0
  82. package/src/directives/index.ts +20 -3
  83. package/src/directives/measure.ts +40 -0
  84. package/src/errors/Errors.ts +65 -12
  85. package/src/errors/JobCancelledError.ts +3 -0
  86. package/src/errors/index.ts +26 -1
  87. package/src/errors/utils.ts +35 -0
  88. package/src/forms/Form.test.ts +28 -0
  89. package/src/forms/Form.ts +80 -14
  90. package/src/forms/index.ts +3 -1
  91. package/src/forms/utils.ts +34 -3
  92. package/src/forms/validation.ts +19 -0
  93. package/src/jobs/Job.ts +147 -0
  94. package/src/jobs/index.ts +10 -0
  95. package/src/jobs/listeners.ts +3 -0
  96. package/src/jobs/status.ts +4 -0
  97. package/src/lang/DefaultLangProvider.ts +43 -0
  98. package/src/lang/Lang.state.ts +11 -0
  99. package/src/lang/Lang.ts +44 -29
  100. package/src/main.histoire.ts +1 -0
  101. package/src/main.ts +3 -2
  102. package/src/plugins/Plugin.ts +1 -0
  103. package/src/plugins/index.ts +19 -0
  104. package/src/services/App.state.ts +25 -4
  105. package/src/services/App.ts +43 -5
  106. package/src/services/Cache.ts +43 -0
  107. package/src/services/Events.test.ts +39 -0
  108. package/src/services/Events.ts +111 -31
  109. package/src/services/Service.ts +151 -41
  110. package/src/services/Storage.ts +20 -0
  111. package/src/services/index.ts +16 -5
  112. package/src/services/store.ts +8 -5
  113. package/src/services/utils.ts +18 -0
  114. package/src/testing/index.ts +25 -0
  115. package/src/testing/setup.ts +27 -0
  116. package/src/ui/UI.state.ts +17 -1
  117. package/src/ui/UI.ts +267 -35
  118. package/src/ui/index.ts +13 -3
  119. package/src/ui/utils.ts +16 -0
  120. package/src/utils/composition/events.ts +1 -0
  121. package/src/utils/composition/persistent.test.ts +33 -0
  122. package/src/utils/composition/persistent.ts +11 -0
  123. package/src/utils/composition/state.test.ts +47 -0
  124. package/src/utils/composition/state.ts +24 -0
  125. package/src/utils/index.ts +2 -0
  126. package/src/utils/markdown.test.ts +50 -0
  127. package/src/utils/markdown.ts +26 -2
  128. package/src/utils/tailwindcss.test.ts +26 -0
  129. package/src/utils/tailwindcss.ts +7 -0
  130. package/src/utils/vue.ts +29 -6
  131. package/tailwind.config.js +4 -0
  132. package/tsconfig.json +1 -0
  133. package/vite.config.ts +6 -2
  134. package/.eslintrc.js +0 -3
  135. package/src/components/basic/AGMarkdown.vue +0 -35
  136. package/src/components/basic/index.ts +0 -3
  137. package/src/globals.ts +0 -6
@@ -1,31 +1,56 @@
1
- import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
2
- import type { Constructor } from '@noeldemartin/utils';
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>) => Partial<State>;
27
- }): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
28
- return class extends Service<State, ComputedState> {
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
- return options.initialState;
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>);
87
+ }
88
+
89
+ protected getComputedStateDefinition(): ComputedStateDefinition<Unref<State>, ComputedState> {
90
+ return (options.computed ?? {}) as ComputedStateDefinition<Unref<State>, ComputedState>;
91
+ }
92
+
93
+ protected getStateWatchers(): StateWatchers<Service, Unref<State>> {
94
+ return (options.watch ?? {}) as StateWatchers<Service, Unref<State>>;
42
95
  }
43
96
 
44
- protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
45
- return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
97
+ protected serializePersistedState(state: Partial<State>): ServiceStorage {
98
+ return options.serialize?.(state) ?? (state as ServiceStorage);
46
99
  }
47
100
 
48
- protected serializePersistedState(state: Partial<State>): Partial<State> {
49
- return options.serialize?.(state) ?? state;
101
+ protected deserializePersistedState(state: ServiceStorage): Partial<State> {
102
+ return options.restore?.(state) ?? (state as Partial<State>);
50
103
  }
51
104
 
52
- } as unknown as Constructor<State> &
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 extends Partial<State> = Partial<State>
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 _store?: Store | false;
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,11 +142,18 @@ 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
 
95
154
  try {
96
- this.boot()
155
+ this.frameworkBoot()
156
+ .then(() => this.boot())
97
157
  .then(() => this._booted.resolve())
98
158
  .catch(handleError);
99
159
  } catch (error) {
@@ -103,6 +163,10 @@ export default class Service<
103
163
  return this._booted;
104
164
  }
105
165
 
166
+ public hasPersistedState(): boolean {
167
+ return Storage.has(this._name);
168
+ }
169
+
106
170
  public hasState<P extends keyof State>(property: P): boolean {
107
171
  if (!this._store) {
108
172
  return false;
@@ -131,13 +195,28 @@ export default class Service<
131
195
  return;
132
196
  }
133
197
 
134
- const state = (
135
- typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty
136
- ) as Partial<State>;
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
+ }
137
204
 
138
- Object.assign(this._store.$state, state);
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
+ }
139
211
 
140
- this.onStateUpdated(state);
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);
141
220
  }
142
221
 
143
222
  protected __get(property: string): unknown {
@@ -152,16 +231,30 @@ export default class Service<
152
231
  this.setState({ [property]: value } as Partial<State>);
153
232
  }
154
233
 
155
- protected onStateUpdated(state: Partial<State>): void {
156
- // TODO fix this.static()
157
- const persist = (this.constructor as unknown as { persist: string[] }).persist;
158
- const persisted = objectOnly(state, persist);
234
+ protected onStateUpdated(update: Partial<State>, old: Partial<State>): void {
235
+ const persisted = objectOnly(update, this.static('persist'));
159
236
 
160
- if (isEmpty(persisted)) {
161
- return;
237
+ if (!isEmpty(persisted)) {
238
+ this.onPersistentStateUpdated(persisted as Partial<State>);
162
239
  }
163
240
 
164
- const storage = Storage.require<ServiceStorage>(this._name);
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
+ }
250
+ }
251
+
252
+ protected onPersistentStateUpdated(persisted: Partial<State>): void {
253
+ const storage = Storage.get<ServiceStorage>(this._name);
254
+
255
+ if (!storage) {
256
+ return;
257
+ }
165
258
 
166
259
  Storage.set(this._name, {
167
260
  ...storage,
@@ -185,30 +278,47 @@ export default class Service<
185
278
  return {} as ComputedStateDefinition<State, ComputedState>;
186
279
  }
187
280
 
188
- protected serializePersistedState(state: Partial<State>): Partial<State> {
189
- return state;
281
+ protected getStateWatchers(): StateWatchers<Service, State> {
282
+ return {};
190
283
  }
191
284
 
192
- protected async boot(): Promise<void> {
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> {
193
294
  this.restorePersistedState();
194
295
  }
195
296
 
196
- protected restorePersistedState(): void {
197
- // TODO fix this.static()
198
- const persist = (this.constructor as unknown as { persist: string[] }).persist;
297
+ protected async boot(): Promise<void> {
298
+ // Placeholder for overrides, don't place any functionality here.
299
+ }
199
300
 
200
- if (!this.usesStore() || isEmpty(persist)) {
301
+ protected restorePersistedState(): void {
302
+ if (!this.usesStore() || isEmpty(this.static('persist'))) {
201
303
  return;
202
304
  }
203
305
 
204
306
  if (Storage.has(this._name)) {
205
307
  const persisted = Storage.require<ServiceStorage>(this._name);
206
- this.setState(persisted);
308
+ this.setState(this.deserializePersistedState(persisted));
207
309
 
208
310
  return;
209
311
  }
210
312
 
211
- 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;
212
322
  }
213
323
 
214
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
+ }
@@ -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;
@@ -24,13 +30,18 @@ export interface Services extends DefaultServices {}
24
30
 
25
31
  export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
26
32
  await Promise.all(
27
- Object.entries(services).map(async ([_, service]) => {
28
- // eslint-disable-next-line no-console
29
- await service.launch().catch((error) => console.error(error));
33
+ Object.entries(services).map(async ([name, service]) => {
34
+ await service
35
+ .launch()
36
+ .catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
30
37
  }),
31
38
  );
32
39
 
33
40
  Object.assign(app.config.globalProperties, services);
41
+
42
+ if (App.development || App.testing) {
43
+ Object.assign(globalThis, services);
44
+ }
34
45
  }
35
46
 
36
47
  export default definePlugin({
@@ -47,7 +58,7 @@ export default definePlugin({
47
58
  });
48
59
 
49
60
  declare module '@/bootstrap/options' {
50
- interface AerogelOptions {
61
+ export interface AerogelOptions {
51
62
  services?: Record<string, Service>;
52
63
  }
53
64
  }
@@ -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
- if (!_store) {
8
- _store = createPinia();
8
+ return _store ?? resetPiniaStore();
9
+ }
9
10
 
10
- setActivePinia(_store);
11
- }
11
+ export function resetPiniaStore(): Pinia {
12
+ return tap(createPinia(), (store) => {
13
+ _store = store;
12
14
 
13
- return _store;
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
+ });
@@ -2,6 +2,8 @@ import type { Component } from 'vue';
2
2
 
3
3
  import { defineServiceState } from '@/services/Service';
4
4
 
5
+ import { Layouts, getCurrentLayout } from './utils';
6
+
5
7
  export interface Modal<T = unknown> {
6
8
  id: string;
7
9
  properties: Record<string, unknown>;
@@ -17,7 +19,21 @@ export interface ModalComponent<
17
19
  Result = unknown
18
20
  > {}
19
21
 
22
+ export interface Snackbar {
23
+ id: string;
24
+ component: Component;
25
+ properties: Record<string, unknown>;
26
+ }
27
+
20
28
  export default defineServiceState({
21
29
  name: 'ui',
22
- initialState: { modals: [] as Modal[] },
30
+ initialState: {
31
+ modals: [] as Modal[],
32
+ snackbars: [] as Snackbar[],
33
+ layout: getCurrentLayout(),
34
+ },
35
+ computed: {
36
+ mobile: ({ layout }) => layout === Layouts.Mobile,
37
+ desktop: ({ layout }) => layout === Layouts.Desktop,
38
+ },
23
39
  });