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

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 (179) hide show
  1. package/dist/aerogel-core.css +1 -0
  2. package/dist/aerogel-core.d.ts +2310 -664
  3. package/dist/aerogel-core.js +3542 -0
  4. package/dist/aerogel-core.js.map +1 -0
  5. package/package.json +38 -34
  6. package/src/bootstrap/bootstrap.test.ts +7 -10
  7. package/src/bootstrap/index.ts +43 -14
  8. package/src/bootstrap/options.ts +4 -1
  9. package/src/components/AppLayout.vue +14 -0
  10. package/src/components/AppModals.vue +14 -0
  11. package/src/components/AppOverlays.vue +9 -0
  12. package/src/components/AppToasts.vue +16 -0
  13. package/src/components/contracts/AlertModal.ts +19 -0
  14. package/src/components/contracts/Button.ts +16 -0
  15. package/src/components/contracts/ConfirmModal.ts +48 -0
  16. package/src/components/contracts/DropdownMenu.ts +25 -0
  17. package/src/components/contracts/ErrorReportModal.ts +33 -0
  18. package/src/components/contracts/Input.ts +26 -0
  19. package/src/components/contracts/LoadingModal.ts +26 -0
  20. package/src/components/contracts/Modal.ts +21 -0
  21. package/src/components/contracts/PromptModal.ts +34 -0
  22. package/src/components/contracts/Select.ts +45 -0
  23. package/src/components/contracts/Toast.ts +15 -0
  24. package/src/components/contracts/index.ts +11 -0
  25. package/src/components/headless/HeadlessButton.vue +51 -0
  26. package/src/components/headless/HeadlessInput.vue +59 -0
  27. package/src/components/headless/HeadlessInputDescription.vue +27 -0
  28. package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
  29. package/src/components/headless/HeadlessInputInput.vue +75 -0
  30. package/src/components/headless/HeadlessInputLabel.vue +18 -0
  31. package/src/components/headless/HeadlessInputTextArea.vue +40 -0
  32. package/src/components/headless/HeadlessModal.vue +57 -0
  33. package/src/components/headless/HeadlessModalContent.vue +30 -0
  34. package/src/components/headless/HeadlessModalDescription.vue +12 -0
  35. package/src/components/headless/HeadlessModalOverlay.vue +12 -0
  36. package/src/components/headless/HeadlessModalTitle.vue +12 -0
  37. package/src/components/headless/HeadlessSelect.vue +120 -0
  38. package/src/components/headless/HeadlessSelectError.vue +25 -0
  39. package/src/components/headless/HeadlessSelectLabel.vue +25 -0
  40. package/src/components/headless/HeadlessSelectOption.vue +34 -0
  41. package/src/components/headless/HeadlessSelectOptions.vue +42 -0
  42. package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
  43. package/src/components/headless/HeadlessSelectValue.vue +18 -0
  44. package/src/components/headless/HeadlessSwitch.vue +96 -0
  45. package/src/components/headless/HeadlessToast.vue +18 -0
  46. package/src/components/headless/HeadlessToastAction.vue +13 -0
  47. package/src/components/headless/index.ts +20 -2
  48. package/src/components/index.ts +6 -7
  49. package/src/components/ui/AdvancedOptions.vue +18 -0
  50. package/src/components/ui/AlertModal.vue +17 -0
  51. package/src/components/ui/Button.vue +100 -0
  52. package/src/components/ui/Checkbox.vue +56 -0
  53. package/src/components/ui/ConfirmModal.vue +50 -0
  54. package/src/components/ui/DropdownMenu.vue +32 -0
  55. package/src/components/ui/DropdownMenuOption.vue +22 -0
  56. package/src/components/ui/DropdownMenuOptions.vue +44 -0
  57. package/src/components/ui/EditableContent.vue +82 -0
  58. package/src/components/ui/ErrorLogs.vue +19 -0
  59. package/src/components/ui/ErrorLogsModal.vue +48 -0
  60. package/src/components/ui/ErrorMessage.vue +15 -0
  61. package/src/components/ui/ErrorReportModal.vue +73 -0
  62. package/src/components/ui/ErrorReportModalButtons.vue +118 -0
  63. package/src/components/ui/ErrorReportModalTitle.vue +24 -0
  64. package/src/components/ui/Form.vue +24 -0
  65. package/src/components/ui/Input.vue +56 -0
  66. package/src/components/ui/Link.vue +12 -0
  67. package/src/components/ui/LoadingModal.vue +34 -0
  68. package/src/components/ui/Markdown.vue +97 -0
  69. package/src/components/ui/Modal.vue +123 -0
  70. package/src/components/ui/ModalContext.vue +31 -0
  71. package/src/components/ui/ProgressBar.vue +51 -0
  72. package/src/components/ui/PromptModal.vue +38 -0
  73. package/src/components/ui/Select.vue +27 -0
  74. package/src/components/ui/SelectLabel.vue +21 -0
  75. package/src/components/ui/SelectOption.vue +29 -0
  76. package/src/components/ui/SelectOptions.vue +35 -0
  77. package/src/components/ui/SelectTrigger.vue +29 -0
  78. package/src/components/ui/SettingsModal.vue +15 -0
  79. package/src/components/ui/StartupCrash.vue +31 -0
  80. package/src/components/ui/Switch.vue +11 -0
  81. package/src/components/ui/Toast.vue +46 -0
  82. package/src/components/ui/index.ts +33 -0
  83. package/src/directives/index.ts +27 -6
  84. package/src/directives/measure.ts +46 -0
  85. package/src/errors/Errors.state.ts +1 -1
  86. package/src/errors/Errors.ts +70 -15
  87. package/src/errors/JobCancelledError.ts +3 -0
  88. package/src/errors/index.ts +38 -6
  89. package/src/errors/settings/Debug.vue +39 -0
  90. package/src/errors/settings/index.ts +10 -0
  91. package/src/errors/utils.ts +35 -0
  92. package/src/forms/FormController.test.ts +110 -0
  93. package/src/forms/FormController.ts +246 -0
  94. package/src/forms/index.ts +3 -2
  95. package/src/forms/utils.ts +51 -20
  96. package/src/forms/validation.ts +19 -0
  97. package/src/index.css +73 -0
  98. package/src/{main.ts → index.ts} +3 -2
  99. package/src/jobs/Job.ts +147 -0
  100. package/src/jobs/index.ts +10 -0
  101. package/src/jobs/listeners.ts +3 -0
  102. package/src/jobs/status.ts +4 -0
  103. package/src/lang/DefaultLangProvider.ts +46 -0
  104. package/src/lang/Lang.state.ts +11 -0
  105. package/src/lang/Lang.ts +44 -29
  106. package/src/lang/index.ts +12 -6
  107. package/src/lang/settings/Language.vue +48 -0
  108. package/src/lang/settings/index.ts +10 -0
  109. package/src/plugins/Plugin.ts +2 -1
  110. package/src/plugins/index.ts +22 -0
  111. package/src/services/App.state.ts +42 -5
  112. package/src/services/App.ts +52 -6
  113. package/src/services/Cache.ts +43 -0
  114. package/src/services/Events.test.ts +39 -0
  115. package/src/services/Events.ts +110 -36
  116. package/src/services/Service.ts +160 -50
  117. package/src/services/Storage.ts +20 -0
  118. package/src/services/index.ts +25 -10
  119. package/src/services/store.ts +8 -5
  120. package/src/services/utils.ts +18 -0
  121. package/src/testing/index.ts +26 -0
  122. package/src/testing/setup.ts +11 -0
  123. package/src/ui/UI.state.ts +20 -9
  124. package/src/ui/UI.ts +322 -74
  125. package/src/ui/index.ts +32 -23
  126. package/src/ui/utils.ts +16 -0
  127. package/src/utils/classes.ts +41 -0
  128. package/src/utils/composition/events.ts +4 -5
  129. package/src/utils/composition/forms.ts +20 -4
  130. package/src/utils/composition/persistent.test.ts +33 -0
  131. package/src/utils/composition/persistent.ts +11 -0
  132. package/src/utils/composition/state.test.ts +47 -0
  133. package/src/utils/composition/state.ts +33 -0
  134. package/src/utils/index.ts +5 -0
  135. package/src/utils/markdown.test.ts +50 -0
  136. package/src/utils/markdown.ts +60 -4
  137. package/src/utils/types.ts +3 -0
  138. package/src/utils/vue.ts +38 -121
  139. package/.eslintrc.js +0 -3
  140. package/dist/aerogel-core.cjs.js +0 -2
  141. package/dist/aerogel-core.cjs.js.map +0 -1
  142. package/dist/aerogel-core.esm.js +0 -2
  143. package/dist/aerogel-core.esm.js.map +0 -1
  144. package/noeldemartin.config.js +0 -2
  145. package/src/components/AGAppLayout.vue +0 -11
  146. package/src/components/AGAppOverlays.vue +0 -39
  147. package/src/components/basic/AGMarkdown.vue +0 -35
  148. package/src/components/basic/index.ts +0 -3
  149. package/src/components/forms/AGButton.vue +0 -34
  150. package/src/components/forms/AGCheckbox.vue +0 -35
  151. package/src/components/forms/AGForm.vue +0 -26
  152. package/src/components/forms/AGInput.vue +0 -36
  153. package/src/components/forms/index.ts +0 -6
  154. package/src/components/headless/forms/AGHeadlessButton.vue +0 -50
  155. package/src/components/headless/forms/AGHeadlessInput.ts +0 -8
  156. package/src/components/headless/forms/AGHeadlessInput.vue +0 -54
  157. package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -45
  158. package/src/components/headless/forms/AGHeadlessInputLabel.vue +0 -16
  159. package/src/components/headless/forms/index.ts +0 -6
  160. package/src/components/headless/modals/AGHeadlessModal.ts +0 -7
  161. package/src/components/headless/modals/AGHeadlessModal.vue +0 -88
  162. package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -28
  163. package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
  164. package/src/components/headless/modals/index.ts +0 -6
  165. package/src/components/modals/AGAlertModal.vue +0 -26
  166. package/src/components/modals/AGConfirmModal.vue +0 -30
  167. package/src/components/modals/AGLoadingModal.vue +0 -19
  168. package/src/components/modals/AGModal.ts +0 -10
  169. package/src/components/modals/AGModal.vue +0 -36
  170. package/src/components/modals/AGModalContext.ts +0 -8
  171. package/src/components/modals/AGModalContext.vue +0 -22
  172. package/src/components/modals/index.ts +0 -8
  173. package/src/directives/initial-focus.ts +0 -11
  174. package/src/forms/Form.test.ts +0 -58
  175. package/src/forms/Form.ts +0 -176
  176. package/src/forms/composition.ts +0 -6
  177. package/src/globals.ts +0 -6
  178. package/tsconfig.json +0 -10
  179. package/vite.config.ts +0 -13
@@ -1,12 +1,15 @@
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
- import Service from '@/services/Service';
3
+ import Service from '@aerogel/core/services/Service';
5
4
 
6
5
  export interface EventsPayload {}
6
+ export interface EventListenerOptions {
7
+ priority: EventListenerPriority;
8
+ }
9
+ export type AerogelGlobalEvents = Partial<{ [Event in EventWithoutPayload]: () => unknown }> &
10
+ Partial<{ [Event in EventWithPayload]: EventListener<EventsPayload[Event]> }>;
7
11
 
8
12
  export type EventListener<T = unknown> = (payload: T) => unknown;
9
- export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
10
13
 
11
14
  export type EventWithoutPayload = {
12
15
  [K in keyof EventsPayload]: EventsPayload[K] extends void ? K : never;
@@ -16,70 +19,141 @@ export type EventWithPayload = {
16
19
  [K in keyof EventsPayload]: EventsPayload[K] extends void ? never : K;
17
20
  }[keyof EventsPayload];
18
21
 
22
+ export const EventListenerPriorities = {
23
+ Low: -256,
24
+ Default: 0,
25
+ High: 256,
26
+ } as const;
27
+
28
+ export type EventListenerPriority = (typeof EventListenerPriorities)[keyof typeof EventListenerPriorities];
29
+
19
30
  export class EventsService extends Service {
20
31
 
21
- private listeners: Record<string, FluentArray<EventListener>> = {};
32
+ private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
33
+
34
+ protected override async boot(): Promise<void> {
35
+ Object.entries(globalThis.__aerogelEvents__ ?? {}).forEach(([event, listener]) =>
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ this.on(event as any, listener as EventListener));
38
+ }
22
39
 
23
40
  public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
24
41
  public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
25
- public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
26
42
  public async emit(event: string, payload?: unknown): Promise<void> {
27
- const listeners = [...(this.listeners[event] ?? [])];
43
+ const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
28
44
 
29
- await Promise.all(listeners.map((listener) => listener(payload)) ?? []);
45
+ for (const priority of listeners.priorities) {
46
+ await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
47
+ }
30
48
  }
31
49
 
50
+ /* eslint-disable max-len */
32
51
  public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
33
- public on<Event extends EventWithPayload>(
34
- event: Event,
35
- listener: EventListener<EventsPayload[Event]>
36
- ): () => void | void;
37
-
38
- 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);
41
-
42
- return () => this.off(event, listener);
52
+ public on<Event extends EventWithoutPayload>(event: Event, priority: EventListenerPriority, listener: () => unknown): () => void; // prettier-ignore
53
+ public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
54
+ public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
55
+ public on<Event extends EventWithPayload>(event: Event, priority: EventListenerPriority, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
56
+ public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
57
+ /* eslint-enable max-len */
58
+
59
+ public on(
60
+ event: string,
61
+ optionsOrListener: Partial<EventListenerOptions> | EventListenerPriority | EventListener,
62
+ listener?: EventListener,
63
+ ): () => void {
64
+ const options =
65
+ typeof optionsOrListener === 'function'
66
+ ? {}
67
+ : typeof optionsOrListener === 'number'
68
+ ? { priority: optionsOrListener }
69
+ : optionsOrListener;
70
+ const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
71
+
72
+ this.registerListener(event, options, handler);
73
+
74
+ return () => this.off(event, handler);
43
75
  }
44
76
 
77
+ /* eslint-disable max-len */
45
78
  public once<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
46
- public once<Event extends EventWithPayload>(
47
- event: Event,
48
- listener: EventListener<EventsPayload[Event]>
49
- ): () => void | void;
50
-
51
- public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
52
- public once(event: string, listener: EventListener): () => void {
79
+ public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
80
+ public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
81
+ public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
82
+ /* eslint-enable max-len */
83
+
84
+ public once(
85
+ event: string,
86
+ optionsOrListener: Partial<EventListenerOptions> | EventListener,
87
+ listener?: EventListener,
88
+ ): () => void {
53
89
  let onceListener: EventListener | null = null;
90
+ const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
91
+ const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
54
92
 
55
93
  return tap(
56
94
  () => onceListener && this.off(event, onceListener),
57
95
  (off) => {
58
- (this.listeners[event] ??= arr<EventListener>([])).push(
59
- (onceListener = (...args) => {
60
- off();
96
+ onceListener = (...args) => {
97
+ off();
61
98
 
62
- return listener(...args);
63
- }),
64
- );
99
+ return handler(...args);
100
+ };
101
+
102
+ this.registerListener(event, options, handler);
65
103
  },
66
104
  );
67
105
  }
68
106
 
69
107
  public off(event: string, listener: EventListener): void {
70
- const eventListeners = this.listeners[event];
108
+ const listeners = this.listeners[event];
71
109
 
72
- if (!eventListeners) {
110
+ if (!listeners) {
73
111
  return;
74
112
  }
75
113
 
76
- eventListeners.remove(listener);
114
+ const priorities = [...listeners.priorities];
115
+
116
+ for (const priority of priorities) {
117
+ arrayRemove(listeners.handlers[priority] ?? [], listener);
77
118
 
78
- if (eventListeners.isEmpty()) {
119
+ if (listeners.handlers[priority]?.length === 0) {
120
+ delete listeners.handlers[priority];
121
+ arrayRemove(listeners.priorities, priority);
122
+ }
123
+ }
124
+
125
+ if (listeners.priorities.length === 0) {
79
126
  delete this.listeners[event];
80
127
  }
81
128
  }
82
129
 
130
+ protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
131
+ const priority = options.priority ?? 0;
132
+
133
+ if (!(event in this.listeners)) {
134
+ this.listeners[event] = { priorities: [], handlers: {} };
135
+ }
136
+
137
+ const priorities =
138
+ this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
139
+ const handlers =
140
+ this.listeners[event]?.handlers ??
141
+ fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
142
+
143
+ if (!priorities.includes(priority)) {
144
+ priorities.push(priority);
145
+ priorities.sort((a, b) => b - a);
146
+ handlers[priority] = [];
147
+ }
148
+
149
+ handlers[priority]?.push(handler);
150
+ }
151
+
83
152
  }
84
153
 
85
- export default facade(new EventsService());
154
+ export default facade(EventsService);
155
+
156
+ declare global {
157
+ // eslint-disable-next-line no-var
158
+ var __aerogelEvents__: AerogelGlobalEvents | undefined;
159
+ }
@@ -1,63 +1,114 @@
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
- import ServiceBootError from '@/errors/ServiceBootError';
6
- import { defineServiceStore } from '@/services/store';
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
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
- public static persist = (options.persist as string[]) ?? [];
55
+ public static override persist = (options.persist as string[]) ?? [];
31
56
 
32
- protected usesStore(): boolean {
57
+ protected override usesStore(): boolean {
33
58
  return true;
34
59
  }
35
60
 
36
- protected getName(): string | null {
61
+ protected override getName(): string | null {
37
62
  return options.name ?? null;
38
63
  }
39
64
 
40
- protected getInitialState(): State {
41
- return options.initialState;
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>>;
42
95
  }
43
96
 
44
- protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
45
- return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
97
+ protected override 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 override 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 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
+ }
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;
@@ -118,10 +182,10 @@ export default class Service<
118
182
  const store = this._store as any;
119
183
 
120
184
  if (property) {
121
- return store ? store[property] : undefined;
185
+ return store ? store[property] : (undefined as State[P]);
122
186
  }
123
187
 
124
- return store ? store : {};
188
+ return store ? store : ({} as State);
125
189
  }
126
190
 
127
191
  public setState<P extends keyof State>(property: P, value: State[P]): void;
@@ -131,16 +195,31 @@ 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
- protected __get(property: string): unknown {
222
+ protected override __get(property: string): unknown {
144
223
  if (this.hasState(property)) {
145
224
  return this.getState(property);
146
225
  }
@@ -148,20 +227,34 @@ export default class Service<
148
227
  return super.__get(property);
149
228
  }
150
229
 
151
- protected __set(property: string, value: unknown): void {
230
+ protected override __set(property: string, value: unknown): void {
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 '@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
+ }
@@ -1,36 +1,49 @@
1
- import type { App as VueApp } from 'vue';
1
+ import type { App as AppInstance } from 'vue';
2
2
 
3
- import { definePlugin } from '@/plugins';
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';
8
11
  import { getPiniaStore } from './store';
12
+ import type { AppSetting } from './App.state';
9
13
 
10
14
  export * from './App';
15
+ export * from './Cache';
11
16
  export * from './Events';
12
17
  export * from './Service';
18
+ export * from './store';
19
+ export * from './utils';
13
20
 
14
- export { App, Events, Service };
21
+ export { App, Cache, Events, Storage, Service };
15
22
 
16
23
  const defaultServices = {
17
24
  $app: App,
18
25
  $events: Events,
26
+ $storage: Storage,
19
27
  };
20
28
 
21
29
  export type DefaultServices = typeof defaultServices;
22
30
 
23
31
  export interface Services extends DefaultServices {}
24
32
 
25
- export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
33
+ export async function bootServices(app: AppInstance, services: Record<string, Service>): Promise<void> {
26
34
  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));
35
+ Object.entries(services).map(async ([name, service]) => {
36
+ await service
37
+ .launch()
38
+ .catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
30
39
  }),
31
40
  );
32
41
 
33
42
  Object.assign(app.config.globalProperties, services);
43
+
44
+ if (isDevelopment() || isTesting()) {
45
+ Object.assign(globalThis, services);
46
+ }
34
47
  }
35
48
 
36
49
  export default definePlugin({
@@ -41,17 +54,19 @@ export default definePlugin({
41
54
  };
42
55
 
43
56
  app.use(getPiniaStore());
57
+ options.settings?.forEach((setting) => App.addSetting(setting));
44
58
 
45
59
  await bootServices(app, services);
46
60
  },
47
61
  });
48
62
 
49
- declare module '@/bootstrap/options' {
50
- interface AerogelOptions {
63
+ declare module '@aerogel/core/bootstrap/options' {
64
+ export interface AerogelOptions {
51
65
  services?: Record<string, Service>;
66
+ settings?: AppSetting[];
52
67
  }
53
68
  }
54
69
 
55
- declare module '@vue/runtime-core' {
70
+ declare module 'vue' {
56
71
  interface ComponentCustomProperties extends Services {}
57
72
  }
@@ -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 {