@aerogel/core 0.0.0-next.97312fd206b83ac5ae520da32b1bb3f12fb55969 → 0.0.0-next.9a02fcd3bcf698211dd7a71d4c48257c96dd7832

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aerogel/core",
3
3
  "description": "The Lightest Solid",
4
- "version": "0.0.0-next.97312fd206b83ac5ae520da32b1bb3f12fb55969",
4
+ "version": "0.0.0-next.9a02fcd3bcf698211dd7a71d4c48257c96dd7832",
5
5
  "main": "dist/aerogel-core.cjs.js",
6
6
  "module": "dist/aerogel-core.esm.js",
7
7
  "types": "dist/aerogel-core.d.ts",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@headlessui/vue": "^1.7.14",
38
- "@noeldemartin/utils": "0.5.1-next.6ecd7cf01a5bad8fbc0dc06312906c9700e79510",
38
+ "@noeldemartin/utils": "0.5.1-next.49cc6c9b4a20930cbf922a949135981791acc5c3",
39
39
  "dompurify": "^3.0.3",
40
40
  "marked": "^5.0.4",
41
41
  "pinia": "^2.1.6",
@@ -6,6 +6,7 @@ import errors from '@/errors';
6
6
  import Events from '@/services/Events';
7
7
  import lang from '@/lang';
8
8
  import services from '@/services';
9
+ import testing from '@/testing';
9
10
  import ui from '@/ui';
10
11
  import { installPlugins } from '@/plugins';
11
12
  import type { AerogelOptions } from '@/bootstrap/options';
@@ -13,12 +14,11 @@ import type { AerogelOptions } from '@/bootstrap/options';
13
14
  export { AerogelOptions };
14
15
 
15
16
  export async function bootstrapApplication(app: App, options: AerogelOptions = {}): Promise<void> {
16
- const plugins = [directives, errors, lang, services, ui, ...(options.plugins ?? [])];
17
+ const plugins = [testing, directives, errors, lang, services, ui, ...(options.plugins ?? [])];
17
18
 
18
19
  await installPlugins(plugins, app, options);
19
20
  await options.install?.(app);
20
-
21
- Events.emit('application-ready');
21
+ await Events.emit('application-ready');
22
22
  }
23
23
 
24
24
  export async function bootstrap(rootComponent: Component, options: AerogelOptions = {}): Promise<void> {
@@ -29,11 +29,12 @@ export async function bootstrap(rootComponent: Component, options: AerogelOption
29
29
  app.mount('#app');
30
30
  app._container?.classList.remove('loading');
31
31
 
32
- Events.emit('application-mounted');
32
+ await Events.emit('application-mounted');
33
33
  }
34
34
 
35
35
  declare module '@/services/Events' {
36
36
  export interface EventsPayload {
37
+ 'application-ready': void;
37
38
  'application-mounted': void;
38
39
  }
39
40
  }
@@ -10,7 +10,7 @@ import { objectWithoutEmpty } from '@noeldemartin/utils';
10
10
 
11
11
  import { booleanProp, objectProp, stringProp } from '@/utils/vue';
12
12
 
13
- const { href, url, route, routeParams, routeQuery, submit } = defineProps({
13
+ const props = defineProps({
14
14
  href: stringProp(),
15
15
  url: stringProp(),
16
16
  route: stringProp(),
@@ -20,32 +20,32 @@ const { href, url, route, routeParams, routeQuery, submit } = defineProps({
20
20
  });
21
21
 
22
22
  const component = computed(() => {
23
- if (route) {
23
+ if (props.route) {
24
24
  return {
25
25
  tag: 'router-link',
26
26
  props: {
27
27
  to: objectWithoutEmpty({
28
- name: route,
29
- params: routeParams,
30
- query: routeQuery,
28
+ name: props.route,
29
+ params: props.routeParams,
30
+ query: props.routeQuery,
31
31
  }),
32
32
  },
33
33
  };
34
34
  }
35
35
 
36
- if (href || url) {
36
+ if (props.href || props.url) {
37
37
  return {
38
38
  tag: 'a',
39
39
  props: {
40
40
  target: '_blank',
41
- href: href || url,
41
+ href: props.href || props.url,
42
42
  },
43
43
  };
44
44
  }
45
45
 
46
46
  return {
47
47
  tag: 'button',
48
- props: { type: submit ? 'submit' : 'button' },
48
+ props: { type: props.submit ? 'submit' : 'button' },
49
49
  };
50
50
  });
51
51
  </script>
@@ -2,6 +2,7 @@
2
2
  <input
3
3
  :id="input.id"
4
4
  ref="$input"
5
+ :name="name"
5
6
  :type="type"
6
7
  :value="value"
7
8
  :aria-invalid="input.errors ? 'true' : 'false'"
@@ -26,6 +27,7 @@ const input = injectReactiveOrFail<IAGHeadlessInput>(
26
27
  'input',
27
28
  '<AGHeadlessInputInput> must be a child of a <AGHeadlessInput>',
28
29
  );
30
+ const name = computed(() => input.name ?? undefined);
29
31
  const value = computed(() => input.value);
30
32
  const checked = computed(() => {
31
33
  if (props.type !== 'checkbox') {
@@ -5,12 +5,12 @@
5
5
  <script setup lang="ts">
6
6
  import { computed } from 'vue';
7
7
 
8
- import Errors from '@/errors/Errors';
9
8
  import { requiredObjectProp } from '@/utils/vue';
9
+ import { getErrorMessage } from '@/errors/utils';
10
10
  import type { ErrorSource } from '@/errors/Errors.state';
11
11
 
12
12
  import AGMarkdown from './AGMarkdown.vue';
13
13
 
14
14
  const props = defineProps({ error: requiredObjectProp<ErrorSource>() });
15
- const message = computed(() => Errors.getErrorMessage(props.error));
15
+ const message = computed(() => getErrorMessage(props.error));
16
16
  </script>
@@ -7,6 +7,7 @@ import { translateWithDefault } from '@/lang/utils';
7
7
 
8
8
  import Service from './Errors.state';
9
9
  import { Colors } from '@/components/constants';
10
+ import { Events } from '@/services';
10
11
  import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
11
12
  import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
12
13
  import type { ModalComponent } from '@/ui/UI.state';
@@ -39,7 +40,13 @@ export class ErrorsService extends Service {
39
40
  }
40
41
 
41
42
  public async report(error: ErrorSource, message?: string): Promise<void> {
42
- if (App.development || App.testing) {
43
+ await Events.emit('error', { error, message });
44
+
45
+ if (App.testing) {
46
+ throw error;
47
+ }
48
+
49
+ if (App.development) {
43
50
  this.logError(error);
44
51
  }
45
52
 
@@ -110,22 +117,6 @@ export class ErrorsService extends Service {
110
117
  });
111
118
  }
112
119
 
113
- public getErrorMessage(error: ErrorSource): string {
114
- if (typeof error === 'string') {
115
- return error;
116
- }
117
-
118
- if (error instanceof Error || error instanceof JSError) {
119
- return error.message;
120
- }
121
-
122
- if (isObject(error)) {
123
- return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
124
- }
125
-
126
- return translateWithDefault('errors.unknown', 'Unknown Error');
127
- }
128
-
129
120
  private logError(error: unknown): void {
130
121
  // eslint-disable-next-line no-console
131
122
  console.error(error);
@@ -185,4 +176,10 @@ export class ErrorsService extends Service {
185
176
 
186
177
  }
187
178
 
188
- export default facade(new ErrorsService());
179
+ export default facade(ErrorsService);
180
+
181
+ declare module '@/services/Events' {
182
+ export interface EventsPayload {
183
+ error: { error: ErrorSource; message?: string };
184
+ }
185
+ }
@@ -6,20 +6,11 @@ import { definePlugin } from '@/plugins';
6
6
  import Errors from './Errors';
7
7
  import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
8
8
 
9
+ export * from './utils';
9
10
  export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
10
11
 
11
12
  const services = { $errors: Errors };
12
13
  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
14
  Errors.report(error);
24
15
 
25
16
  return true;
@@ -0,0 +1,19 @@
1
+ import { JSError, isObject, toString } from '@noeldemartin/utils';
2
+ import { translateWithDefault } from '@/lang/utils';
3
+ import type { ErrorSource } from './Errors.state';
4
+
5
+ export function getErrorMessage(error: ErrorSource): string {
6
+ if (typeof error === 'string') {
7
+ return error;
8
+ }
9
+
10
+ if (error instanceof Error || error instanceof JSError) {
11
+ return error.message;
12
+ }
13
+
14
+ if (isObject(error)) {
15
+ return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
16
+ }
17
+
18
+ return translateWithDefault('errors.unknown', 'Unknown Error');
19
+ }
@@ -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
@@ -1,10 +1,11 @@
1
- import { facade, toString } from '@noeldemartin/utils';
1
+ import { facade } from '@noeldemartin/utils';
2
2
 
3
3
  import App from '@/services/App';
4
4
  import Service from '@/services/Service';
5
5
 
6
6
  export interface LangProvider {
7
7
  translate(key: string, parameters?: Record<string, unknown>): string;
8
+ translateWithDefault(key: string, defaultMessage: string, parameters?: Record<string, unknown>): string;
8
9
  }
9
10
 
10
11
  export class LangService extends Service {
@@ -21,6 +22,12 @@ export class LangService extends Service {
21
22
 
22
23
  return key;
23
24
  },
25
+ translateWithDefault: (_, defaultMessage) => {
26
+ // eslint-disable-next-line no-console
27
+ App.development && console.warn('Lang provider is missing');
28
+
29
+ return defaultMessage;
30
+ },
24
31
  };
25
32
  }
26
33
 
@@ -32,29 +39,10 @@ export class LangService extends Service {
32
39
  return this.provider.translate(key, parameters) ?? key;
33
40
  }
34
41
 
35
- public translateWithDefault(key: string, defaultMessage: string): string;
36
- public translateWithDefault(key: string, parameters: Record<string, unknown>, defaultMessage: string): string;
37
- public translateWithDefault(
38
- key: string,
39
- defaultMessageOrParameters?: string | Record<string, unknown>,
40
- defaultMessage?: string,
41
- ): string {
42
- defaultMessage ??= defaultMessageOrParameters as string;
43
-
44
- const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters ?? {};
45
- const message = this.provider.translate(key, parameters) ?? key;
46
-
47
- if (message === key) {
48
- return Object.entries(parameters).reduce(
49
- (renderedMessage, [name, value]) =>
50
- renderedMessage.replace(new RegExp(`\\{\\s*${name}\\s*\\}`, 'g'), toString(value)),
51
- defaultMessage,
52
- );
53
- }
54
-
55
- return message;
42
+ public translateWithDefault(key: string, defaultMessage: string, parameters: Record<string, unknown> = {}): string {
43
+ return this.provider.translateWithDefault(key, defaultMessage, parameters);
56
44
  }
57
45
 
58
46
  }
59
47
 
60
- export default facade(new LangService());
48
+ export default facade(LangService);
package/src/main.ts CHANGED
@@ -2,8 +2,10 @@ 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';
9
+ export * from './testing';
8
10
  export * from './ui';
9
11
  export * from './utils';
@@ -12,6 +12,6 @@ export default defineServiceState({
12
12
  },
13
13
  computed: {
14
14
  development: (state) => state.environment === 'development',
15
- testing: (state) => state.environment === 'testing',
15
+ testing: (state) => state.environment === 'test' || state.environment === 'testing',
16
16
  },
17
17
  });
@@ -18,6 +18,12 @@ export class AppService extends Service {
18
18
  return this.mounted.isResolved();
19
19
  }
20
20
 
21
+ public async whenReady<T>(callback: () => T): Promise<T> {
22
+ const result = await this.ready.then(callback);
23
+
24
+ return result;
25
+ }
26
+
21
27
  public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
22
28
  queryParameters && updateLocationQueryParameters(queryParameters);
23
29
 
@@ -38,4 +44,4 @@ export class AppService extends Service {
38
44
 
39
45
  }
40
46
 
41
- 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,13 @@
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
+ }
9
+ export type AerogelGlobalEvents = Partial<{ [Event in EventWithoutPayload]: () => unknown }> &
10
+ Partial<{ [Event in EventWithPayload]: EventListener<EventsPayload[Event]> }>;
7
11
 
8
12
  export type EventListener<T = unknown> = (payload: T) => unknown;
9
13
  export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
@@ -16,70 +20,136 @@ export type EventWithPayload = {
16
20
  [K in keyof EventsPayload]: EventsPayload[K] extends void ? never : K;
17
21
  }[keyof EventsPayload];
18
22
 
23
+ export const EventListenerPriorities = {
24
+ Low: -256,
25
+ Default: 0,
26
+ High: 256,
27
+ } as const;
28
+
19
29
  export class EventsService extends Service {
20
30
 
21
- private listeners: Record<string, FluentArray<EventListener>> = {};
31
+ private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
32
+
33
+ protected async boot(): Promise<void> {
34
+ Object.entries(globalThis.__aerogelEvents__ ?? {}).forEach(([event, listener]) =>
35
+ this.on(event as string, listener as EventListener));
36
+ }
22
37
 
23
38
  public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
24
39
  public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
25
40
  public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
26
41
  public async emit(event: string, payload?: unknown): Promise<void> {
27
- const listeners = [...(this.listeners[event] ?? [])];
42
+ const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
28
43
 
29
- await Promise.all(listeners.map((listener) => listener(payload)) ?? []);
44
+ for (const priority of listeners.priorities) {
45
+ await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
46
+ }
30
47
  }
31
48
 
49
+ /* eslint-disable max-len */
32
50
  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
-
51
+ public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
52
+ public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
53
+ public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
38
54
  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);
55
+ public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
56
+ /* eslint-enable max-len */
41
57
 
42
- return () => this.off(event, listener);
58
+ public on(
59
+ event: string,
60
+ optionsOrListener: Partial<EventListenerOptions> | EventListener,
61
+ listener?: EventListener,
62
+ ): () => void {
63
+ const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
64
+ const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
65
+
66
+ this.registerListener(event, options, handler);
67
+
68
+ return () => this.off(event, handler);
43
69
  }
44
70
 
71
+ /* eslint-disable max-len */
45
72
  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
-
73
+ public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
74
+ public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
75
+ public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
51
76
  public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
52
- public once(event: string, listener: EventListener): () => void {
77
+ public once<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
78
+ /* eslint-enable max-len */
79
+
80
+ public once(
81
+ event: string,
82
+ optionsOrListener: Partial<EventListenerOptions> | EventListener,
83
+ listener?: EventListener,
84
+ ): () => void {
53
85
  let onceListener: EventListener | null = null;
86
+ const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
87
+ const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
54
88
 
55
89
  return tap(
56
90
  () => onceListener && this.off(event, onceListener),
57
91
  (off) => {
58
- (this.listeners[event] ??= arr<EventListener>([])).push(
59
- (onceListener = (...args) => {
60
- off();
92
+ onceListener = (...args) => {
93
+ off();
61
94
 
62
- return listener(...args);
63
- }),
64
- );
95
+ return handler(...args);
96
+ };
97
+
98
+ this.registerListener(event, options, handler);
65
99
  },
66
100
  );
67
101
  }
68
102
 
69
103
  public off(event: string, listener: EventListener): void {
70
- const eventListeners = this.listeners[event];
104
+ const listeners = this.listeners[event];
71
105
 
72
- if (!eventListeners) {
106
+ if (!listeners) {
73
107
  return;
74
108
  }
75
109
 
76
- eventListeners.remove(listener);
110
+ const priorities = [...listeners.priorities];
111
+
112
+ for (const priority of priorities) {
113
+ arrayRemove(listeners.handlers[priority] ?? [], listener);
77
114
 
78
- if (eventListeners.isEmpty()) {
115
+ if (listeners.handlers[priority]?.length === 0) {
116
+ delete listeners.handlers[priority];
117
+ arrayRemove(listeners.priorities, priority);
118
+ }
119
+ }
120
+
121
+ if (listeners.priorities.length === 0) {
79
122
  delete this.listeners[event];
80
123
  }
81
124
  }
82
125
 
126
+ protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
127
+ const priority = options.priority ?? 0;
128
+
129
+ if (!(event in this.listeners)) {
130
+ this.listeners[event] = { priorities: [], handlers: {} };
131
+ }
132
+
133
+ const priorities =
134
+ this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
135
+ const handlers =
136
+ this.listeners[event]?.handlers ??
137
+ fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
138
+
139
+ if (!priorities.includes(priority)) {
140
+ priorities.push(priority);
141
+ priorities.sort((a, b) => b - a);
142
+ handlers[priority] = [];
143
+ }
144
+
145
+ handlers[priority]?.push(handler);
146
+ }
147
+
83
148
  }
84
149
 
85
- export default facade(new EventsService());
150
+ export default facade(EventsService);
151
+
152
+ declare global {
153
+ // eslint-disable-next-line no-var
154
+ var __aerogelEvents__: AerogelGlobalEvents | undefined;
155
+ }
@@ -14,7 +14,7 @@ export type UnrefServiceState<State extends ServiceState> = {
14
14
  };
15
15
 
16
16
  export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
17
- [K in keyof TComputedState]: (state: TState) => TComputedState[K];
17
+ [K in keyof TComputedState]: (state: UnrefServiceState<TState>) => TComputedState[K];
18
18
  } & ThisType<{
19
19
  readonly [K in keyof TComputedState]: TComputedState[K];
20
20
  }>;
@@ -24,7 +24,7 @@ export function defineServiceState<
24
24
  ComputedState extends ServiceState = {}
25
25
  >(options: {
26
26
  name: string;
27
- initialState: State;
27
+ initialState: State | (() => State);
28
28
  persist?: (keyof State)[];
29
29
  computed?: ComputedStateDefinition<State, ComputedState>;
30
30
  serialize?: (state: Partial<State>) => Partial<State>;
@@ -44,7 +44,27 @@ export function defineServiceState<
44
44
  }
45
45
 
46
46
  protected getInitialState(): UnrefServiceState<State> {
47
- return options.initialState;
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>);
48
68
  }
49
69
 
50
70
  protected getComputedStateDefinition(): ComputedStateDefinition<UnrefServiceState<State>, ComputedState> {
@@ -54,7 +74,7 @@ export function defineServiceState<
54
74
  protected serializePersistedState(state: Partial<State>): Partial<State> {
55
75
  return options.serialize?.(state) ?? state;
56
76
  }
57
-
77
+
58
78
  } as unknown as Constructor<UnrefServiceState<State>> &
59
79
  Constructor<ComputedState> &
60
80
  Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>>;
@@ -71,7 +91,7 @@ export default class Service<
71
91
  protected _name: string;
72
92
  private _booted: PromisedValue<void>;
73
93
  private _computedStateKeys: Set<keyof State>;
74
- private _store?: Store | false;
94
+ private _store: Store | false;
75
95
 
76
96
  constructor() {
77
97
  super();
@@ -110,6 +130,10 @@ export default class Service<
110
130
  return this._booted;
111
131
  }
112
132
 
133
+ public hasPersistedState(): boolean {
134
+ return Storage.has(this._name);
135
+ }
136
+
113
137
  public hasState<P extends keyof State>(property: P): boolean {
114
138
  if (!this._store) {
115
139
  return false;
@@ -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