@aerogel/core 0.0.0-next.f8cdd39997c56dcd46e07c26af8a84d04d610fce → 0.0.0-next.fd1bd21aea7a9ab8c4eab69a5f5776db5de8bf35

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 (104) 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 +1175 -237
  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/package.json +14 -5
  8. package/postcss.config.js +6 -0
  9. package/src/assets/histoire.css +3 -0
  10. package/src/bootstrap/bootstrap.test.ts +4 -3
  11. package/src/bootstrap/index.ts +25 -5
  12. package/src/bootstrap/options.ts +3 -0
  13. package/src/components/AGAppLayout.vue +7 -2
  14. package/src/components/AGAppOverlays.vue +5 -1
  15. package/src/components/AGAppSnackbars.vue +1 -1
  16. package/src/components/forms/AGCheckbox.vue +7 -1
  17. package/src/components/forms/AGForm.vue +9 -10
  18. package/src/components/forms/AGInput.vue +10 -6
  19. package/src/components/forms/AGSelect.story.vue +46 -0
  20. package/src/components/forms/AGSelect.vue +60 -0
  21. package/src/components/forms/index.ts +5 -6
  22. package/src/components/headless/forms/AGHeadlessButton.vue +17 -12
  23. package/src/components/headless/forms/AGHeadlessInput.ts +27 -3
  24. package/src/components/headless/forms/AGHeadlessInput.vue +9 -6
  25. package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
  26. package/src/components/headless/forms/AGHeadlessInputInput.vue +40 -4
  27. package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
  28. package/src/components/headless/forms/AGHeadlessInputTextArea.vue +40 -0
  29. package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
  30. package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
  31. package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
  32. package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
  33. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
  34. package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
  35. package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
  36. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
  37. package/src/components/headless/forms/composition.ts +10 -0
  38. package/src/components/headless/forms/index.ts +12 -1
  39. package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
  40. package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
  41. package/src/components/headless/modals/index.ts +4 -6
  42. package/src/components/headless/snackbars/index.ts +23 -8
  43. package/src/components/index.ts +1 -1
  44. package/src/components/{basic → lib}/AGErrorMessage.vue +2 -2
  45. package/src/components/{basic → lib}/AGMarkdown.vue +7 -2
  46. package/src/components/lib/AGMeasured.vue +15 -0
  47. package/src/components/lib/AGStartupCrash.vue +31 -0
  48. package/src/components/lib/index.ts +5 -0
  49. package/src/components/modals/AGAlertModal.ts +15 -0
  50. package/src/components/modals/AGAlertModal.vue +4 -15
  51. package/src/components/modals/AGConfirmModal.ts +27 -0
  52. package/src/components/modals/AGConfirmModal.vue +7 -11
  53. package/src/components/modals/AGErrorReportModal.ts +27 -1
  54. package/src/components/modals/AGErrorReportModal.vue +8 -16
  55. package/src/components/modals/AGErrorReportModalButtons.vue +4 -2
  56. package/src/components/modals/AGErrorReportModalTitle.vue +1 -1
  57. package/src/components/modals/AGLoadingModal.ts +23 -0
  58. package/src/components/modals/AGLoadingModal.vue +4 -8
  59. package/src/components/modals/AGModal.ts +1 -1
  60. package/src/components/modals/AGModal.vue +15 -12
  61. package/src/components/modals/AGModalTitle.vue +9 -0
  62. package/src/components/modals/AGPromptModal.ts +30 -0
  63. package/src/components/modals/AGPromptModal.vue +34 -0
  64. package/src/components/modals/index.ts +13 -17
  65. package/src/components/snackbars/AGSnackbar.vue +3 -9
  66. package/src/components/utils.ts +10 -0
  67. package/src/directives/index.ts +5 -1
  68. package/src/directives/measure.ts +21 -0
  69. package/src/errors/Errors.ts +26 -24
  70. package/src/errors/index.ts +10 -23
  71. package/src/errors/utils.ts +19 -0
  72. package/src/forms/Form.ts +57 -9
  73. package/src/forms/index.ts +1 -0
  74. package/src/forms/utils.ts +15 -0
  75. package/src/jobs/Job.ts +5 -0
  76. package/src/jobs/index.ts +7 -0
  77. package/src/lang/Lang.ts +11 -23
  78. package/src/main.histoire.ts +1 -0
  79. package/src/main.ts +3 -0
  80. package/src/plugins/Plugin.ts +1 -0
  81. package/src/plugins/index.ts +19 -0
  82. package/src/services/App.state.ts +6 -5
  83. package/src/services/App.ts +35 -3
  84. package/src/services/Cache.ts +43 -0
  85. package/src/services/Events.test.ts +39 -0
  86. package/src/services/Events.ts +100 -30
  87. package/src/services/Service.ts +50 -16
  88. package/src/services/index.ts +5 -2
  89. package/src/services/store.ts +8 -5
  90. package/src/testing/index.ts +25 -0
  91. package/src/ui/UI.ts +127 -19
  92. package/src/ui/index.ts +8 -3
  93. package/src/utils/composition/events.ts +1 -0
  94. package/src/utils/index.ts +1 -0
  95. package/src/utils/tailwindcss.test.ts +26 -0
  96. package/src/utils/tailwindcss.ts +7 -0
  97. package/src/utils/vue.ts +13 -4
  98. package/tailwind.config.js +4 -0
  99. package/tsconfig.json +1 -1
  100. package/.eslintrc.js +0 -3
  101. package/dist/virtual.d.ts +0 -11
  102. package/src/components/basic/index.ts +0 -5
  103. package/src/types/virtual.d.ts +0 -11
  104. /package/src/components/{basic → lib}/AGLink.vue +0 -0
@@ -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';
@@ -6,33 +6,22 @@ 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;
26
17
  };
27
18
 
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
- );
19
+ function setUpErrorHandler(app: App, baseHandler: ErrorHandler = () => false): void {
20
+ const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
21
+
22
+ app.config.errorHandler = errorHandler;
23
+ globalThis.onerror = (event, _, __, ___, error) => errorHandler(error ?? event);
24
+ globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
36
25
  }
37
26
 
38
27
  export type ErrorHandler = (error: ErrorSource) => boolean;
@@ -40,16 +29,14 @@ export type ErrorsServices = typeof services;
40
29
 
41
30
  export default definePlugin({
42
31
  async install(app, options) {
43
- const errorHandler = setUpErrorHandler(options.handleError);
44
-
45
- app.config.errorHandler = errorHandler;
32
+ setUpErrorHandler(app, options.handleError);
46
33
 
47
34
  await bootServices(app, services);
48
35
  },
49
36
  });
50
37
 
51
38
  declare module '@/bootstrap/options' {
52
- interface AerogelOptions {
39
+ export interface AerogelOptions {
53
40
  handleError?(error: ErrorSource): boolean;
54
41
  }
55
42
  }
@@ -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
+ }
package/src/forms/Form.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { MagicObject } from '@noeldemartin/utils';
2
- import { computed, reactive, readonly, ref } from 'vue';
1
+ import { MagicObject, arrayRemove } from '@noeldemartin/utils';
2
+ import { computed, nextTick, reactive, readonly, ref } from 'vue';
3
3
  import type { ObjectValues } from '@noeldemartin/utils';
4
4
  import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
5
5
 
@@ -7,6 +7,8 @@ export const FormFieldTypes = {
7
7
  String: 'string',
8
8
  Number: 'number',
9
9
  Boolean: 'boolean',
10
+ Object: 'object',
11
+ Date: 'date',
10
12
  } as const;
11
13
 
12
14
  export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
@@ -17,6 +19,7 @@ export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType
17
19
 
18
20
  export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
19
21
  export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
22
+ export type FormFieldValue = GetFormFieldValue<FormFieldType>;
20
23
 
21
24
  export type FormData<T> = {
22
25
  -readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
@@ -36,17 +39,26 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
36
39
  ? number
37
40
  : TType extends typeof FormFieldTypes.Boolean
38
41
  ? boolean
42
+ : TType extends typeof FormFieldTypes.Object
43
+ ? object
44
+ : TType extends typeof FormFieldTypes.Date
45
+ ? Date
39
46
  : never;
40
47
 
48
+ const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
49
+
50
+ export type SubmitFormListener = () => unknown;
51
+ export type FocusFormListener = (input: string) => unknown;
52
+
41
53
  export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
42
54
 
43
55
  public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
44
56
 
45
57
  private _fields: Fields;
46
58
  private _data: FormData<Fields>;
47
- private _valid: ComputedRef<boolean>;
48
59
  private _submitted: Ref<boolean>;
49
60
  private _errors: FormErrors<Fields>;
61
+ private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
50
62
 
51
63
  constructor(fields: Fields) {
52
64
  super();
@@ -55,13 +67,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
55
67
  this._submitted = ref(false);
56
68
  this._data = this.getInitialData(fields);
57
69
  this._errors = this.getInitialErrors(fields);
58
- this._valid = computed(() => !Object.values(this._errors).some((error) => error !== null));
70
+
71
+ validForms.set(
72
+ this,
73
+ computed(() => !Object.values(this._errors).some((error) => error !== null)),
74
+ );
59
75
 
60
76
  this.errors = readonly(this._errors);
61
77
  }
62
78
 
63
79
  public get valid(): boolean {
64
- return this._valid.value;
80
+ return !!validForms.get(this)?.value;
65
81
  }
66
82
 
67
83
  public get submitted(): boolean {
@@ -80,6 +96,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
80
96
  return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
81
97
  }
82
98
 
99
+ public data(): FormData<Fields> {
100
+ return { ...this._data };
101
+ }
102
+
83
103
  public validate(): boolean {
84
104
  const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
85
105
  formErrors[name] = this.getFieldErrors(name, definition);
@@ -92,17 +112,45 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
92
112
  return this.valid;
93
113
  }
94
114
 
95
- public reset(): void {
115
+ public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
96
116
  this._submitted.value = false;
97
117
 
98
- this.resetData();
99
- this.resetErrors();
118
+ options.keepData || this.resetData();
119
+ options.keepErrors || this.resetErrors();
100
120
  }
101
121
 
102
122
  public submit(): boolean {
103
123
  this._submitted.value = true;
104
124
 
105
- return this.validate();
125
+ const valid = this.validate();
126
+
127
+ valid && this._listeners['submit']?.forEach((listener) => listener());
128
+
129
+ return valid;
130
+ }
131
+
132
+ public on(event: 'focus', listener: FocusFormListener): () => void;
133
+ public on(event: 'submit', listener: SubmitFormListener): () => void;
134
+ public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
135
+ this._listeners[event] ??= [];
136
+
137
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
138
+ this._listeners[event]?.push(listener as any);
139
+
140
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
141
+ return () => this.off(event as any, listener);
142
+ }
143
+
144
+ public off(event: 'focus', listener: FocusFormListener): void;
145
+ public off(event: 'submit', listener: SubmitFormListener): void;
146
+ public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
147
+ arrayRemove(this._listeners[event] ?? [], listener);
148
+ }
149
+
150
+ public async focus(input: string): Promise<void> {
151
+ await nextTick();
152
+
153
+ this._listeners['focus']?.forEach((listener) => listener(input));
106
154
  }
107
155
 
108
156
  protected __get(property: string): unknown {
@@ -1,3 +1,4 @@
1
1
  export * from './Form';
2
2
  export * from './composition';
3
3
  export * from './utils';
4
+ export { default as Form } from './Form';
@@ -8,6 +8,13 @@ export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof
8
8
  };
9
9
  }
10
10
 
11
+ export function dateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
12
+ return {
13
+ default: defaultValue,
14
+ type: FormFieldTypes.Date,
15
+ };
16
+ }
17
+
11
18
  export function requiredBooleanInput(
12
19
  defaultValue?: boolean,
13
20
  ): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
@@ -18,6 +25,14 @@ export function requiredBooleanInput(
18
25
  };
19
26
  }
20
27
 
28
+ export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
29
+ return {
30
+ default: defaultValue,
31
+ type: FormFieldTypes.Date,
32
+ rules: 'required',
33
+ };
34
+ }
35
+
21
36
  export function requiredNumberInput(
22
37
  defaultValue?: number,
23
38
  ): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
@@ -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);
@@ -0,0 +1 @@
1
+ import './assets/histoire.css';
package/src/main.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  export * from './bootstrap';
2
2
  export * from './components';
3
+ export * from './directives';
3
4
  export * from './errors';
4
5
  export * from './forms';
6
+ export * from './jobs';
5
7
  export * from './lang';
6
8
  export * from './plugins';
7
9
  export * from './services';
10
+ export * from './testing';
8
11
  export * from './ui';
9
12
  export * from './utils';
@@ -3,5 +3,6 @@ import type { App } from 'vue';
3
3
  import type { AerogelOptions } from '@/bootstrap/options';
4
4
 
5
5
  export interface Plugin {
6
+ name?: string;
6
7
  install(app: App, options: AerogelOptions): void | Promise<void>;
7
8
  }
@@ -1,3 +1,7 @@
1
+ import type { GetClosureArgs } from '@noeldemartin/utils';
2
+
3
+ import App from '@/services/App';
4
+
1
5
  import type { Plugin } from './Plugin';
2
6
 
3
7
  export * from './Plugin';
@@ -5,3 +9,18 @@ export * from './Plugin';
5
9
  export function definePlugin<T extends Plugin>(plugin: T): T {
6
10
  return plugin;
7
11
  }
12
+
13
+ export async function installPlugins(plugins: Plugin[], ...args: GetClosureArgs<Plugin['install']>): Promise<void> {
14
+ App.setState(
15
+ 'plugins',
16
+ plugins.reduce((pluginsMap, plugin) => {
17
+ if (plugin.name) {
18
+ pluginsMap[plugin.name] = plugin;
19
+ }
20
+
21
+ return pluginsMap;
22
+ }, {} as Record<string, Plugin>),
23
+ );
24
+
25
+ await Promise.all(plugins.map((plugin) => plugin.install(...args)) ?? []);
26
+ }
@@ -1,16 +1,17 @@
1
- import Build from 'virtual:aerogel';
1
+ import Aerogel from 'virtual:aerogel';
2
2
 
3
3
  import { defineServiceState } from '@/services/Service';
4
+ import type { Plugin } from '@/plugins/Plugin';
4
5
 
5
6
  export default defineServiceState({
6
7
  name: 'app',
7
8
  initialState: {
8
- environment: Build.environment,
9
- sourceUrl: Build.sourceUrl,
10
- isMounted: false,
9
+ plugins: {} as Record<string, Plugin>,
10
+ environment: Aerogel.environment,
11
+ sourceUrl: Aerogel.sourceUrl,
11
12
  },
12
13
  computed: {
13
14
  development: (state) => state.environment === 'development',
14
- testing: (state) => state.environment === 'testing',
15
+ testing: (state) => state.environment === 'test' || state.environment === 'testing',
15
16
  },
16
17
  });
@@ -1,15 +1,47 @@
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
+ import type { Plugin } from '@/plugins';
4
5
 
5
6
  import Service from './App.state';
6
7
 
7
8
  export class AppService extends Service {
8
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
+
36
+ public plugin<T extends Plugin = Plugin>(name: string): T | null {
37
+ return (this.plugins[name] as T) ?? null;
38
+ }
39
+
9
40
  protected async boot(): Promise<void> {
10
- Events.once('application-mounted', () => this.setState({ isMounted: true }));
41
+ Events.once('application-ready', () => this.ready.resolve());
42
+ Events.once('application-mounted', () => this.mounted.resolve());
11
43
  }
12
44
 
13
45
  }
14
46
 
15
- export default facade(new AppService());
47
+ export default facade(AppService);
@@ -0,0 +1,43 @@
1
+ import { PromisedValue, facade, tap } from '@noeldemartin/utils';
2
+
3
+ import Service from '@/services/Service';
4
+
5
+ export class CacheService extends Service {
6
+
7
+ private cache?: PromisedValue<Cache> = undefined;
8
+
9
+ public async get(url: string): Promise<Response | null> {
10
+ const cache = await this.open();
11
+ const response = await cache.match(url);
12
+
13
+ return response ?? null;
14
+ }
15
+
16
+ public async store(url: string, response: Response): Promise<void> {
17
+ const cache = await this.open();
18
+
19
+ await cache.put(url, response);
20
+ }
21
+
22
+ public async replace(url: string, response: Response): Promise<void> {
23
+ const cache = await this.open();
24
+ const keys = await cache.keys(url);
25
+
26
+ if (keys.length === 0) {
27
+ return;
28
+ }
29
+
30
+ await cache.put(url, response);
31
+ }
32
+
33
+ protected async open(): Promise<Cache> {
34
+ return (this.cache =
35
+ this.cache ??
36
+ tap(new PromisedValue<Cache>(), (cache) => {
37
+ caches.open('app').then((instance) => cache.resolve(instance));
38
+ }));
39
+ }
40
+
41
+ }
42
+
43
+ export default facade(CacheService);
@@ -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
+ });