@aerogel/core 0.0.0-next.63f6c97c7c6f262bf6bfb3aa64528e4b599c46ea → 0.0.0-next.824cf5311c4335d119158f507dad094ed4f4f0b6

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 (79) 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 +649 -136
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/package.json +12 -6
  7. package/src/bootstrap/bootstrap.test.ts +3 -3
  8. package/src/bootstrap/index.ts +14 -4
  9. package/src/bootstrap/options.ts +3 -0
  10. package/src/components/AGAppLayout.vue +7 -2
  11. package/src/components/AGAppOverlays.vue +5 -1
  12. package/src/components/AGAppSnackbars.vue +1 -1
  13. package/src/components/forms/AGCheckbox.vue +7 -1
  14. package/src/components/forms/AGInput.vue +8 -6
  15. package/src/components/forms/AGSelect.story.vue +21 -3
  16. package/src/components/forms/AGSelect.vue +10 -3
  17. package/src/components/headless/forms/AGHeadlessInput.ts +21 -1
  18. package/src/components/headless/forms/AGHeadlessInput.vue +4 -1
  19. package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
  20. package/src/components/headless/forms/AGHeadlessSelect.ts +21 -22
  21. package/src/components/headless/forms/AGHeadlessSelect.vue +24 -24
  22. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +4 -1
  23. package/src/components/headless/forms/AGHeadlessSelectOption.vue +6 -6
  24. package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
  25. package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
  26. package/src/components/headless/modals/index.ts +4 -6
  27. package/src/components/headless/snackbars/index.ts +23 -8
  28. package/src/components/index.ts +1 -1
  29. package/src/components/lib/AGMeasured.vue +15 -0
  30. package/src/components/lib/AGStartupCrash.vue +31 -0
  31. package/src/components/lib/index.ts +5 -0
  32. package/src/components/modals/AGAlertModal.ts +15 -0
  33. package/src/components/modals/AGAlertModal.vue +4 -15
  34. package/src/components/modals/AGConfirmModal.ts +27 -0
  35. package/src/components/modals/AGConfirmModal.vue +8 -12
  36. package/src/components/modals/AGErrorReportModal.ts +27 -1
  37. package/src/components/modals/AGErrorReportModal.vue +8 -16
  38. package/src/components/modals/AGErrorReportModalButtons.vue +4 -2
  39. package/src/components/modals/AGErrorReportModalTitle.vue +1 -1
  40. package/src/components/modals/AGLoadingModal.ts +23 -0
  41. package/src/components/modals/AGLoadingModal.vue +4 -8
  42. package/src/components/modals/AGModal.ts +2 -2
  43. package/src/components/modals/AGModal.vue +14 -12
  44. package/src/components/modals/AGPromptModal.ts +30 -0
  45. package/src/components/modals/AGPromptModal.vue +34 -0
  46. package/src/components/modals/index.ts +13 -19
  47. package/src/components/snackbars/AGSnackbar.vue +3 -9
  48. package/src/components/utils.ts +10 -0
  49. package/src/directives/index.ts +3 -1
  50. package/src/directives/measure.ts +12 -0
  51. package/src/errors/Errors.ts +17 -8
  52. package/src/errors/index.ts +9 -23
  53. package/src/forms/Form.ts +1 -0
  54. package/src/jobs/Job.ts +5 -0
  55. package/src/jobs/index.ts +7 -0
  56. package/src/lang/Lang.ts +1 -1
  57. package/src/main.ts +1 -0
  58. package/src/services/App.state.ts +4 -5
  59. package/src/services/App.ts +30 -3
  60. package/src/services/Events.test.ts +39 -0
  61. package/src/services/Events.ts +88 -30
  62. package/src/services/Service.ts +50 -16
  63. package/src/services/index.ts +2 -1
  64. package/src/services/store.ts +8 -5
  65. package/src/ui/UI.ts +93 -12
  66. package/src/ui/index.ts +8 -3
  67. package/src/utils/composition/events.ts +1 -0
  68. package/src/utils/index.ts +1 -0
  69. package/src/utils/tailwindcss.test.ts +26 -0
  70. package/src/utils/tailwindcss.ts +7 -0
  71. package/src/utils/vue.ts +10 -1
  72. package/tsconfig.json +1 -1
  73. package/.eslintrc.js +0 -3
  74. package/dist/virtual.d.ts +0 -11
  75. package/src/components/basic/index.ts +0 -5
  76. package/src/types/virtual.d.ts +0 -11
  77. /package/src/components/{basic → lib}/AGErrorMessage.vue +0 -0
  78. /package/src/components/{basic → lib}/AGLink.vue +0 -0
  79. /package/src/components/{basic → lib}/AGMarkdown.vue +0 -0
@@ -0,0 +1,12 @@
1
+ import { defineDirective } from '@/utils/vue';
2
+
3
+ export default defineDirective({
4
+ mounted(element: HTMLElement, { value }: { value?: () => unknown }) {
5
+ const sizes = element.getBoundingClientRect();
6
+
7
+ element.style.setProperty('--width', `${sizes.width}px`);
8
+ element.style.setProperty('--height', `${sizes.height}px`);
9
+
10
+ value?.();
11
+ },
12
+ });
@@ -7,7 +7,9 @@ import { translateWithDefault } from '@/lang/utils';
7
7
 
8
8
  import Service from './Errors.state';
9
9
  import { Colors } from '@/components/constants';
10
+ import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
10
11
  import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
12
+ import type { ModalComponent } from '@/ui/UI.state';
11
13
 
12
14
  export class ErrorsService extends Service {
13
15
 
@@ -23,7 +25,7 @@ export class ErrorsService extends Service {
23
25
  }
24
26
 
25
27
  public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
26
- const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
28
+ const reports = Array.isArray(error) ? (error as ErrorReport[]) : [await this.createErrorReport(error)];
27
29
 
28
30
  if (reports.length === 0) {
29
31
  UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
@@ -31,11 +33,17 @@ export class ErrorsService extends Service {
31
33
  return;
32
34
  }
33
35
 
34
- UI.openModal(UI.requireComponent(UIComponents.ErrorReportModal), { reports });
36
+ UI.openModal<ModalComponent<AGErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
37
+ reports,
38
+ });
35
39
  }
36
40
 
37
41
  public async report(error: ErrorSource, message?: string): Promise<void> {
38
- if (App.development || App.testing) {
42
+ if (App.testing) {
43
+ throw error;
44
+ }
45
+
46
+ if (App.development) {
39
47
  this.logError(error);
40
48
  }
41
49
 
@@ -43,7 +51,7 @@ export class ErrorsService extends Service {
43
51
  throw error;
44
52
  }
45
53
 
46
- if (!App.isMounted) {
54
+ if (!App.isMounted()) {
47
55
  const startupError = await this.createStartupErrorReport(error);
48
56
 
49
57
  if (startupError) {
@@ -70,9 +78,10 @@ export class ErrorsService extends Service {
70
78
  text: translateWithDefault('errors.viewDetails', 'View details'),
71
79
  dismiss: true,
72
80
  handler: () =>
73
- UI.openModal(UI.requireComponent(UIComponents.ErrorReportModal), {
74
- reports: [report],
75
- }),
81
+ UI.openModal<ModalComponent<AGErrorReportModalProps>>(
82
+ UI.requireComponent(UIComponents.ErrorReportModal),
83
+ { reports: [report] },
84
+ ),
76
85
  },
77
86
  ],
78
87
  },
@@ -180,4 +189,4 @@ export class ErrorsService extends Service {
180
189
 
181
190
  }
182
191
 
183
- export default facade(new ErrorsService());
192
+ export default facade(ErrorsService);
@@ -1,4 +1,4 @@
1
- import { tap } from '@noeldemartin/utils';
1
+ import type { App } from 'vue';
2
2
 
3
3
  import { bootServices } from '@/services';
4
4
  import { definePlugin } from '@/plugins';
@@ -10,29 +10,17 @@ export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
10
10
 
11
11
  const services = { $errors: Errors };
12
12
  const frameworkHandler: ErrorHandler = (error) => {
13
- if (!Errors.instance) {
14
- // eslint-disable-next-line no-console
15
- console.warn('Errors service hasn\'t been initialized properly!');
16
-
17
- // eslint-disable-next-line no-console
18
- console.error(error);
19
-
20
- return true;
21
- }
22
-
23
13
  Errors.report(error);
24
14
 
25
15
  return true;
26
16
  };
27
17
 
28
- function setUpErrorHandler(baseHandler: ErrorHandler = () => false): ErrorHandler {
29
- return tap(
30
- (error) => baseHandler(error) || frameworkHandler(error),
31
- (errorHandler) => {
32
- globalThis.onerror = (message, _, __, ___, error) => errorHandler(error ?? message);
33
- globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
34
- },
35
- );
18
+ function setUpErrorHandler(app: App, baseHandler: ErrorHandler = () => false): void {
19
+ const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
20
+
21
+ app.config.errorHandler = errorHandler;
22
+ globalThis.onerror = (event, _, __, ___, error) => errorHandler(error ?? event);
23
+ globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
36
24
  }
37
25
 
38
26
  export type ErrorHandler = (error: ErrorSource) => boolean;
@@ -40,16 +28,14 @@ export type ErrorsServices = typeof services;
40
28
 
41
29
  export default definePlugin({
42
30
  async install(app, options) {
43
- const errorHandler = setUpErrorHandler(options.handleError);
44
-
45
- app.config.errorHandler = errorHandler;
31
+ setUpErrorHandler(app, options.handleError);
46
32
 
47
33
  await bootServices(app, services);
48
34
  },
49
35
  });
50
36
 
51
37
  declare module '@/bootstrap/options' {
52
- interface AerogelOptions {
38
+ export interface AerogelOptions {
53
39
  handleError?(error: ErrorSource): boolean;
54
40
  }
55
41
  }
package/src/forms/Form.ts CHANGED
@@ -18,6 +18,7 @@ export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType
18
18
 
19
19
  export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
20
20
  export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
21
+ export type FormFieldValue = GetFormFieldValue<FormFieldType>;
21
22
 
22
23
  export type FormData<T> = {
23
24
  -readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
@@ -0,0 +1,5 @@
1
+ export default abstract class Job {
2
+
3
+ public abstract run(): Promise<void>;
4
+
5
+ }
@@ -0,0 +1,7 @@
1
+ import Job from './Job';
2
+
3
+ export { Job };
4
+
5
+ export async function dispatch(job: Job): Promise<void> {
6
+ await job.run();
7
+ }
package/src/lang/Lang.ts CHANGED
@@ -57,4 +57,4 @@ export class LangService extends Service {
57
57
 
58
58
  }
59
59
 
60
- export default facade(new LangService());
60
+ export default facade(LangService);
package/src/main.ts CHANGED
@@ -2,6 +2,7 @@ export * from './bootstrap';
2
2
  export * from './components';
3
3
  export * from './errors';
4
4
  export * from './forms';
5
+ export * from './jobs';
5
6
  export * from './lang';
6
7
  export * from './plugins';
7
8
  export * from './services';
@@ -1,4 +1,4 @@
1
- import Build from 'virtual:aerogel';
1
+ import Aerogel from 'virtual:aerogel';
2
2
 
3
3
  import { defineServiceState } from '@/services/Service';
4
4
  import type { Plugin } from '@/plugins/Plugin';
@@ -7,12 +7,11 @@ export default defineServiceState({
7
7
  name: 'app',
8
8
  initialState: {
9
9
  plugins: {} as Record<string, Plugin>,
10
- environment: Build.environment,
11
- sourceUrl: Build.sourceUrl,
12
- isMounted: false,
10
+ environment: Aerogel.environment,
11
+ sourceUrl: Aerogel.sourceUrl,
13
12
  },
14
13
  computed: {
15
14
  development: (state) => state.environment === 'development',
16
- testing: (state) => state.environment === 'testing',
15
+ testing: (state) => state.environment === 'test' || state.environment === 'testing',
17
16
  },
18
17
  });
@@ -1,4 +1,4 @@
1
- import { facade } from '@noeldemartin/utils';
1
+ import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
2
2
 
3
3
  import Events from '@/services/Events';
4
4
  import type { Plugin } from '@/plugins';
@@ -7,14 +7,41 @@ import Service from './App.state';
7
7
 
8
8
  export class AppService extends Service {
9
9
 
10
+ public readonly ready = new PromisedValue<void>();
11
+ public readonly mounted = new PromisedValue<void>();
12
+
13
+ public isReady(): boolean {
14
+ return this.ready.isResolved();
15
+ }
16
+
17
+ public isMounted(): boolean {
18
+ return this.mounted.isResolved();
19
+ }
20
+
21
+ public async whenReady<T>(callback: () => T): Promise<T> {
22
+ const result = await this.ready.then(callback);
23
+
24
+ return result;
25
+ }
26
+
27
+ public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
28
+ queryParameters && updateLocationQueryParameters(queryParameters);
29
+
30
+ location.reload();
31
+
32
+ // Stall until the reload happens
33
+ await forever();
34
+ }
35
+
10
36
  public plugin<T extends Plugin = Plugin>(name: string): T | null {
11
37
  return (this.plugins[name] as T) ?? null;
12
38
  }
13
39
 
14
40
  protected async boot(): Promise<void> {
15
- Events.once('application-mounted', () => this.setState({ isMounted: true }));
41
+ Events.once('application-ready', () => this.ready.resolve());
42
+ Events.once('application-mounted', () => this.mounted.resolve());
16
43
  }
17
44
 
18
45
  }
19
46
 
20
- export default facade(new AppService());
47
+ export default facade(AppService);
@@ -0,0 +1,39 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+
3
+ import Events, { EventListenerPriorities } from './Events';
4
+
5
+ describe('Events', () => {
6
+
7
+ beforeEach(() => void Events.reset());
8
+
9
+ it('registers listeners', async () => {
10
+ // Arrange
11
+ let counter = 0;
12
+
13
+ Events.on('trigger', () => counter++);
14
+
15
+ // Act
16
+ await Events.emit('trigger');
17
+ await Events.emit('trigger');
18
+ await Events.emit('trigger');
19
+
20
+ // Assert
21
+ expect(counter).toEqual(3);
22
+ });
23
+
24
+ it('triggers listeners by priority', async () => {
25
+ // Arrange
26
+ const storage: string[] = [];
27
+
28
+ Events.on('trigger', () => storage.push('second'));
29
+ Events.on('trigger', { priority: EventListenerPriorities.Low }, () => storage.push('third'));
30
+ Events.on('trigger', { priority: EventListenerPriorities.High }, () => storage.push('first'));
31
+
32
+ // Act
33
+ await Events.emit('trigger');
34
+
35
+ // Assert
36
+ expect(storage).toEqual(['first', 'second', 'third']);
37
+ });
38
+
39
+ });
@@ -1,9 +1,11 @@
1
- import { arr, facade, tap } from '@noeldemartin/utils';
2
- import type { FluentArray } from '@noeldemartin/utils';
1
+ import { arrayRemove, facade, fail, tap } from '@noeldemartin/utils';
3
2
 
4
3
  import Service from '@/services/Service';
5
4
 
6
5
  export interface EventsPayload {}
6
+ export interface EventListenerOptions {
7
+ priority: number;
8
+ }
7
9
 
8
10
  export type EventListener<T = unknown> = (payload: T) => unknown;
9
11
  export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
@@ -16,70 +18,126 @@ export type EventWithPayload = {
16
18
  [K in keyof EventsPayload]: EventsPayload[K] extends void ? never : K;
17
19
  }[keyof EventsPayload];
18
20
 
21
+ export const EventListenerPriorities = {
22
+ Low: -256,
23
+ Default: 0,
24
+ High: 256,
25
+ } as const;
26
+
19
27
  export class EventsService extends Service {
20
28
 
21
- private listeners: Record<string, FluentArray<EventListener>> = {};
29
+ private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
22
30
 
23
31
  public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
24
32
  public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
25
33
  public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
26
34
  public async emit(event: string, payload?: unknown): Promise<void> {
27
- const listeners = [...(this.listeners[event] ?? [])];
35
+ const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
28
36
 
29
- await Promise.all(listeners.map((listener) => listener(payload)) ?? []);
37
+ for (const priority of listeners.priorities) {
38
+ await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
39
+ }
30
40
  }
31
41
 
42
+ /* eslint-disable max-len */
32
43
  public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
33
- public on<Event extends EventWithPayload>(
34
- event: Event,
35
- listener: EventListener<EventsPayload[Event]>
36
- ): () => void | void;
37
-
44
+ public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
45
+ public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
46
+ public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
38
47
  public on<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
39
- public on(event: string, listener: EventListener): () => void {
40
- (this.listeners[event] ??= arr<EventListener>([])).push(listener);
48
+ public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
49
+ /* eslint-enable max-len */
50
+
51
+ public on(
52
+ event: string,
53
+ optionsOrListener: Partial<EventListenerOptions> | EventListener,
54
+ listener?: EventListener,
55
+ ): () => void {
56
+ const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
57
+ const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
41
58
 
42
- return () => this.off(event, listener);
59
+ this.registerListener(event, options, handler);
60
+
61
+ return () => this.off(event, handler);
43
62
  }
44
63
 
64
+ /* eslint-disable max-len */
45
65
  public once<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
46
- public once<Event extends EventWithPayload>(
47
- event: Event,
48
- listener: EventListener<EventsPayload[Event]>
49
- ): () => void | void;
50
-
66
+ public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
67
+ public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
68
+ public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
51
69
  public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
52
- public once(event: string, listener: EventListener): () => void {
70
+ public once<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
71
+ /* eslint-enable max-len */
72
+
73
+ public once(
74
+ event: string,
75
+ optionsOrListener: Partial<EventListenerOptions> | EventListener,
76
+ listener?: EventListener,
77
+ ): () => void {
53
78
  let onceListener: EventListener | null = null;
79
+ const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
80
+ const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
54
81
 
55
82
  return tap(
56
83
  () => onceListener && this.off(event, onceListener),
57
84
  (off) => {
58
- (this.listeners[event] ??= arr<EventListener>([])).push(
59
- (onceListener = (...args) => {
60
- off();
85
+ onceListener = (...args) => {
86
+ off();
87
+
88
+ return handler(...args);
89
+ };
61
90
 
62
- return listener(...args);
63
- }),
64
- );
91
+ this.registerListener(event, options, handler);
65
92
  },
66
93
  );
67
94
  }
68
95
 
69
96
  public off(event: string, listener: EventListener): void {
70
- const eventListeners = this.listeners[event];
97
+ const listeners = this.listeners[event];
71
98
 
72
- if (!eventListeners) {
99
+ if (!listeners) {
73
100
  return;
74
101
  }
75
102
 
76
- eventListeners.remove(listener);
103
+ const priorities = [...listeners.priorities];
77
104
 
78
- if (eventListeners.isEmpty()) {
105
+ for (const priority of priorities) {
106
+ arrayRemove(listeners.handlers[priority] ?? [], listener);
107
+
108
+ if (listeners.handlers[priority]?.length === 0) {
109
+ delete listeners.handlers[priority];
110
+ arrayRemove(listeners.priorities, priority);
111
+ }
112
+ }
113
+
114
+ if (listeners.priorities.length === 0) {
79
115
  delete this.listeners[event];
80
116
  }
81
117
  }
82
118
 
119
+ protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
120
+ const priority = options.priority ?? 0;
121
+
122
+ if (!(event in this.listeners)) {
123
+ this.listeners[event] = { priorities: [], handlers: {} };
124
+ }
125
+
126
+ const priorities =
127
+ this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
128
+ const handlers =
129
+ this.listeners[event]?.handlers ??
130
+ fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
131
+
132
+ if (!priorities.includes(priority)) {
133
+ priorities.push(priority);
134
+ priorities.sort((a, b) => b - a);
135
+ handlers[priority] = [];
136
+ }
137
+
138
+ handlers[priority]?.push(handler);
139
+ }
140
+
83
141
  }
84
142
 
85
- export default facade(new EventsService());
143
+ export default facade(EventsService);
@@ -1,5 +1,6 @@
1
1
  import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
2
2
  import type { Constructor } from '@noeldemartin/utils';
3
+ import type { MaybeRef } from 'vue';
3
4
  import type { Store } from 'pinia';
4
5
 
5
6
  import ServiceBootError from '@/errors/ServiceBootError';
@@ -8,9 +9,12 @@ import { defineServiceStore } from '@/services/store';
8
9
  export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
9
10
  export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
10
11
  export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
12
+ export type UnrefServiceState<State extends ServiceState> = {
13
+ [K in keyof State]: State[K] extends MaybeRef<infer T> ? T : State[K];
14
+ };
11
15
 
12
16
  export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
13
- [K in keyof TComputedState]: (state: TState) => TComputedState[K];
17
+ [K in keyof TComputedState]: (state: UnrefServiceState<TState>) => TComputedState[K];
14
18
  } & ThisType<{
15
19
  readonly [K in keyof TComputedState]: TComputedState[K];
16
20
  }>;
@@ -20,12 +24,14 @@ export function defineServiceState<
20
24
  ComputedState extends ServiceState = {}
21
25
  >(options: {
22
26
  name: string;
23
- initialState: State;
27
+ initialState: State | (() => State);
24
28
  persist?: (keyof State)[];
25
29
  computed?: ComputedStateDefinition<State, ComputedState>;
26
30
  serialize?: (state: Partial<State>) => Partial<State>;
27
- }): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
28
- return class extends Service<State, ComputedState> {
31
+ }): Constructor<UnrefServiceState<State>> &
32
+ Constructor<ComputedState> &
33
+ Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>> {
34
+ return class extends Service<UnrefServiceState<State>, ComputedState> {
29
35
 
30
36
  public static persist = (options.persist as string[]) ?? [];
31
37
 
@@ -37,21 +43,41 @@ export function defineServiceState<
37
43
  return options.name ?? null;
38
44
  }
39
45
 
40
- protected getInitialState(): State {
41
- return options.initialState;
46
+ protected getInitialState(): UnrefServiceState<State> {
47
+ if (typeof options.initialState === 'function') {
48
+ return options.initialState();
49
+ }
50
+
51
+ return Object.entries(options.initialState).reduce((state, [key, value]) => {
52
+ try {
53
+ value = structuredClone(value);
54
+ } catch (error) {
55
+ // eslint-disable-next-line no-console
56
+ console.warn(
57
+ `Could not clone '${key}' state from ${this.getName()} service, ` +
58
+ 'this may cause problems if you\'re using multiple instances of the service ' +
59
+ '(for example, in unit tests).\n' +
60
+ 'To fix this problem, declare your initialState as a function instead.',
61
+ );
62
+ }
63
+
64
+ state[key as keyof State] = value;
65
+
66
+ return state;
67
+ }, {} as UnrefServiceState<State>);
42
68
  }
43
69
 
44
- protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
45
- return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
70
+ protected getComputedStateDefinition(): ComputedStateDefinition<UnrefServiceState<State>, ComputedState> {
71
+ return (options.computed ?? {}) as ComputedStateDefinition<UnrefServiceState<State>, ComputedState>;
46
72
  }
47
73
 
48
74
  protected serializePersistedState(state: Partial<State>): Partial<State> {
49
75
  return options.serialize?.(state) ?? state;
50
76
  }
51
-
52
- } as unknown as Constructor<State> &
77
+
78
+ } as unknown as Constructor<UnrefServiceState<State>> &
53
79
  Constructor<ComputedState> &
54
- Constructor<Service<State, ComputedState, Partial<State>>>;
80
+ Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>>;
55
81
  }
56
82
 
57
83
  export default class Service<
@@ -65,7 +91,7 @@ export default class Service<
65
91
  protected _name: string;
66
92
  private _booted: PromisedValue<void>;
67
93
  private _computedStateKeys: Set<keyof State>;
68
- private _store?: Store | false;
94
+ private _store: Store | false;
69
95
 
70
96
  constructor() {
71
97
  super();
@@ -104,6 +130,10 @@ export default class Service<
104
130
  return this._booted;
105
131
  }
106
132
 
133
+ public hasPersistedState(): boolean {
134
+ return Storage.has(this._name);
135
+ }
136
+
107
137
  public hasState<P extends keyof State>(property: P): boolean {
108
138
  if (!this._store) {
109
139
  return false;
@@ -162,7 +192,11 @@ export default class Service<
162
192
  return;
163
193
  }
164
194
 
165
- const storage = Storage.require<ServiceStorage>(this._name);
195
+ const storage = Storage.get<ServiceStorage>(this._name);
196
+
197
+ if (!storage) {
198
+ return;
199
+ }
166
200
 
167
201
  Storage.set(this._name, {
168
202
  ...storage,
@@ -191,14 +225,14 @@ export default class Service<
191
225
  }
192
226
 
193
227
  protected async frameworkBoot(): Promise<void> {
194
- this.restorePersistedState();
228
+ this.initializePersistedState();
195
229
  }
196
230
 
197
231
  protected async boot(): Promise<void> {
198
- // Override.
232
+ // Placeholder for overrides, don't place any functionality here.
199
233
  }
200
234
 
201
- protected restorePersistedState(): void {
235
+ protected initializePersistedState(): void {
202
236
  // TODO fix this.static()
203
237
  const persist = (this.constructor as unknown as { persist: string[] }).persist;
204
238
 
@@ -10,6 +10,7 @@ import { getPiniaStore } from './store';
10
10
  export * from './App';
11
11
  export * from './Events';
12
12
  export * from './Service';
13
+ export * from './store';
13
14
 
14
15
  export { App, Events, Service };
15
16
 
@@ -50,7 +51,7 @@ export default definePlugin({
50
51
  });
51
52
 
52
53
  declare module '@/bootstrap/options' {
53
- interface AerogelOptions {
54
+ export interface AerogelOptions {
54
55
  services?: Record<string, Service>;
55
56
  }
56
57
  }
@@ -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 {