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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) 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 +1637 -295
  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 +3 -3
  11. package/src/bootstrap/index.ts +35 -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 +2 -2
  16. package/src/components/composition.ts +23 -0
  17. package/src/components/forms/AGCheckbox.vue +7 -1
  18. package/src/components/forms/AGForm.vue +9 -10
  19. package/src/components/forms/AGInput.vue +10 -6
  20. package/src/components/forms/AGSelect.story.vue +46 -0
  21. package/src/components/forms/AGSelect.vue +60 -0
  22. package/src/components/forms/index.ts +5 -6
  23. package/src/components/headless/forms/AGHeadlessButton.ts +3 -0
  24. package/src/components/headless/forms/AGHeadlessButton.vue +23 -12
  25. package/src/components/headless/forms/AGHeadlessInput.ts +30 -4
  26. package/src/components/headless/forms/AGHeadlessInput.vue +23 -7
  27. package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
  28. package/src/components/headless/forms/AGHeadlessInputInput.vue +44 -5
  29. package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
  30. package/src/components/headless/forms/AGHeadlessInputTextArea.vue +43 -0
  31. package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
  32. package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
  33. package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
  34. package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
  35. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
  36. package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
  37. package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
  38. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
  39. package/src/components/headless/forms/composition.ts +10 -0
  40. package/src/components/headless/forms/index.ts +13 -1
  41. package/src/components/headless/modals/AGHeadlessModal.ts +29 -0
  42. package/src/components/headless/modals/AGHeadlessModal.vue +13 -9
  43. package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -6
  44. package/src/components/headless/modals/AGHeadlessModalTitle.vue +14 -4
  45. package/src/components/headless/modals/index.ts +4 -6
  46. package/src/components/headless/snackbars/index.ts +23 -8
  47. package/src/components/index.ts +3 -1
  48. package/src/components/interfaces.ts +24 -0
  49. package/src/components/{basic → lib}/AGErrorMessage.vue +2 -2
  50. package/src/components/lib/AGMarkdown.vue +54 -0
  51. package/src/components/lib/AGMeasured.vue +16 -0
  52. package/src/components/lib/AGProgressBar.vue +30 -0
  53. package/src/components/lib/AGStartupCrash.vue +31 -0
  54. package/src/components/lib/index.ts +6 -0
  55. package/src/components/modals/AGAlertModal.ts +18 -0
  56. package/src/components/modals/AGAlertModal.vue +4 -15
  57. package/src/components/modals/AGConfirmModal.ts +41 -0
  58. package/src/components/modals/AGConfirmModal.vue +10 -14
  59. package/src/components/modals/AGErrorReportModal.ts +30 -1
  60. package/src/components/modals/AGErrorReportModal.vue +8 -16
  61. package/src/components/modals/AGErrorReportModalButtons.vue +4 -2
  62. package/src/components/modals/AGErrorReportModalTitle.vue +1 -1
  63. package/src/components/modals/AGLoadingModal.ts +29 -0
  64. package/src/components/modals/AGLoadingModal.vue +4 -8
  65. package/src/components/modals/AGModal.ts +2 -1
  66. package/src/components/modals/AGModal.vue +14 -12
  67. package/src/components/modals/AGModalContext.vue +14 -4
  68. package/src/components/modals/AGPromptModal.ts +41 -0
  69. package/src/components/modals/AGPromptModal.vue +34 -0
  70. package/src/components/modals/index.ts +13 -19
  71. package/src/components/snackbars/AGSnackbar.vue +3 -9
  72. package/src/components/utils.ts +10 -0
  73. package/src/directives/index.ts +5 -1
  74. package/src/directives/measure.ts +40 -0
  75. package/src/errors/Errors.ts +26 -24
  76. package/src/errors/JobCancelledError.ts +3 -0
  77. package/src/errors/index.ts +10 -23
  78. package/src/errors/utils.ts +35 -0
  79. package/src/forms/Form.test.ts +28 -0
  80. package/src/forms/Form.ts +77 -11
  81. package/src/forms/index.ts +3 -1
  82. package/src/forms/utils.ts +34 -3
  83. package/src/forms/validation.ts +19 -0
  84. package/src/jobs/Job.ts +147 -0
  85. package/src/jobs/index.ts +10 -0
  86. package/src/jobs/listeners.ts +3 -0
  87. package/src/jobs/status.ts +4 -0
  88. package/src/lang/DefaultLangProvider.ts +43 -0
  89. package/src/lang/Lang.state.ts +11 -0
  90. package/src/lang/Lang.ts +44 -29
  91. package/src/main.histoire.ts +1 -0
  92. package/src/main.ts +3 -0
  93. package/src/plugins/Plugin.ts +1 -0
  94. package/src/plugins/index.ts +19 -0
  95. package/src/services/App.state.ts +23 -5
  96. package/src/services/App.ts +43 -3
  97. package/src/services/Cache.ts +43 -0
  98. package/src/services/Events.test.ts +39 -0
  99. package/src/services/Events.ts +111 -31
  100. package/src/services/Service.ts +145 -40
  101. package/src/services/Storage.ts +20 -0
  102. package/src/services/index.ts +11 -3
  103. package/src/services/store.ts +8 -5
  104. package/src/services/utils.ts +18 -0
  105. package/src/testing/index.ts +25 -0
  106. package/src/testing/setup.ts +27 -0
  107. package/src/ui/UI.state.ts +7 -0
  108. package/src/ui/UI.ts +237 -34
  109. package/src/ui/index.ts +9 -3
  110. package/src/ui/utils.ts +16 -0
  111. package/src/utils/composition/events.ts +1 -0
  112. package/src/utils/composition/persistent.test.ts +33 -0
  113. package/src/utils/composition/persistent.ts +11 -0
  114. package/src/utils/composition/state.test.ts +47 -0
  115. package/src/utils/composition/state.ts +24 -0
  116. package/src/utils/index.ts +2 -0
  117. package/src/utils/markdown.test.ts +50 -0
  118. package/src/utils/markdown.ts +17 -2
  119. package/src/utils/tailwindcss.test.ts +26 -0
  120. package/src/utils/tailwindcss.ts +7 -0
  121. package/src/utils/vue.ts +27 -6
  122. package/tailwind.config.js +4 -0
  123. package/tsconfig.json +1 -1
  124. package/vite.config.ts +4 -1
  125. package/.eslintrc.js +0 -3
  126. package/dist/virtual.d.ts +0 -11
  127. package/src/components/basic/AGMarkdown.vue +0 -36
  128. package/src/components/basic/index.ts +0 -5
  129. package/src/types/virtual.d.ts +0 -11
  130. /package/src/components/{basic → lib}/AGLink.vue +0 -0
@@ -0,0 +1,19 @@
1
+ import { arrayFrom } from '@noeldemartin/utils';
2
+
3
+ const builtInRules: Record<string, FormFieldValidator> = {
4
+ required: (value) => (value ? undefined : 'required'),
5
+ };
6
+
7
+ export type FormFieldValidator<T = unknown> = (value: T) => string | string[] | undefined;
8
+
9
+ export const validators: Record<string, FormFieldValidator> = { ...builtInRules };
10
+
11
+ export function defineFormValidationRule<T>(rule: string, validator: FormFieldValidator<T>): void {
12
+ validators[rule] = validator as FormFieldValidator;
13
+ }
14
+
15
+ export function validate(value: unknown, rule: string): string[] {
16
+ const errors = validators[rule]?.(value);
17
+
18
+ return errors ? arrayFrom(errors) : [];
19
+ }
@@ -0,0 +1,147 @@
1
+ import { ListenersManager, PromisedValue, round, tap, toError } from '@noeldemartin/utils';
2
+ import type { Listeners } from '@noeldemartin/utils';
3
+
4
+ import JobCancelledError from '@/errors/JobCancelledError';
5
+
6
+ import type { JobListener } from './listeners';
7
+ import type { JobStatus } from './status';
8
+
9
+ export default abstract class Job<
10
+ Listener extends JobListener = JobListener,
11
+ Status extends JobStatus = JobStatus,
12
+ SerializedStatus extends JobStatus = JobStatus
13
+ > {
14
+
15
+ protected status: Status;
16
+ protected _listeners: ListenersManager<JobListener>;
17
+ protected _progress?: number;
18
+ protected _cancelled?: PromisedValue<void>;
19
+ protected _started: PromisedValue<void>;
20
+ protected _completed: PromisedValue<void>;
21
+
22
+ constructor() {
23
+ this.status = this.getInitialStatus();
24
+ this._listeners = new ListenersManager();
25
+ this._started = new PromisedValue();
26
+ this._completed = new PromisedValue();
27
+ }
28
+
29
+ public async start(): Promise<void> {
30
+ this.beforeStart();
31
+ this._started.resolve();
32
+
33
+ try {
34
+ await this.updateProgress();
35
+ await this.run();
36
+ await this.updateProgress();
37
+
38
+ this._completed.resolve();
39
+ } catch (error) {
40
+ if (error instanceof JobCancelledError) {
41
+ return;
42
+ }
43
+
44
+ throw tap(toError(error), (realError) => {
45
+ this._completed.reject(realError);
46
+ });
47
+ }
48
+ }
49
+
50
+ public async cancel(): Promise<void> {
51
+ this._cancelled = new PromisedValue();
52
+
53
+ await this._cancelled;
54
+ }
55
+
56
+ public serialize(): SerializedStatus {
57
+ return this.serializeStatus(this.status);
58
+ }
59
+
60
+ public get listeners(): Listeners<Listener> {
61
+ return this._listeners;
62
+ }
63
+
64
+ public get progress(): number {
65
+ return this._progress ?? 0;
66
+ }
67
+
68
+ public get cancelled(): boolean {
69
+ return !!this._cancelled?.isResolved();
70
+ }
71
+
72
+ public get started(): Promise<void> {
73
+ return this._started;
74
+ }
75
+
76
+ public get completed(): Promise<void> {
77
+ return this._completed;
78
+ }
79
+
80
+ protected abstract run(): Promise<void>;
81
+
82
+ protected getInitialStatus(): Status {
83
+ return { completed: false } as Status;
84
+ }
85
+
86
+ protected beforeStart(): void {
87
+ if (!this._started.isResolved()) {
88
+ return;
89
+ }
90
+
91
+ if (this._cancelled) {
92
+ delete this._progress;
93
+ delete this._cancelled;
94
+
95
+ return;
96
+ }
97
+
98
+ throw new Error('Job already started!');
99
+ }
100
+
101
+ protected assertNotCancelled(): void {
102
+ if (!this._cancelled) {
103
+ return;
104
+ }
105
+
106
+ this._cancelled.resolve();
107
+
108
+ throw new JobCancelledError();
109
+ }
110
+
111
+ protected calculateCurrentProgress(status?: JobStatus): number {
112
+ status ??= this.status;
113
+
114
+ if (status.completed) {
115
+ return 1;
116
+ }
117
+
118
+ if (!status.children) {
119
+ return 0;
120
+ }
121
+
122
+ return round(
123
+ status.children.reduce((total, child) => total + this.calculateCurrentProgress(child), 0) /
124
+ status.children.length,
125
+ 2,
126
+ );
127
+ }
128
+
129
+ protected async updateProgress(update?: (status: Status) => unknown): Promise<void> {
130
+ await update?.(this.status);
131
+
132
+ const progress = this.calculateCurrentProgress();
133
+
134
+ if (progress === this._progress) {
135
+ return;
136
+ }
137
+
138
+ this._progress = progress;
139
+
140
+ await this._listeners.emit('onUpdated', progress);
141
+ }
142
+
143
+ protected serializeStatus(status: Status): SerializedStatus {
144
+ return { ...status } as unknown as SerializedStatus;
145
+ }
146
+
147
+ }
@@ -0,0 +1,10 @@
1
+ import Job from './Job';
2
+
3
+ export { Job };
4
+ export * from './Job';
5
+ export * from './listeners';
6
+ export * from './status';
7
+
8
+ export async function dispatch(job: Job): Promise<void> {
9
+ await job.start();
10
+ }
@@ -0,0 +1,3 @@
1
+ export interface JobListener {
2
+ onUpdated(progress: number): unknown;
3
+ }
@@ -0,0 +1,4 @@
1
+ export interface JobStatus {
2
+ completed: boolean;
3
+ children?: JobStatus[];
4
+ }
@@ -0,0 +1,43 @@
1
+ import App from '@/services/App';
2
+
3
+ import type { LangProvider } from './Lang';
4
+
5
+ export default class DefaultLangProvider implements LangProvider {
6
+
7
+ constructor(private locale: string, private fallbackLocale: string) {}
8
+
9
+ public getLocale(): string {
10
+ return this.locale;
11
+ }
12
+
13
+ public async setLocale(locale: string): Promise<void> {
14
+ this.locale = locale;
15
+ }
16
+
17
+ public getFallbackLocale(): string {
18
+ return this.fallbackLocale;
19
+ }
20
+
21
+ public async setFallbackLocale(fallbackLocale: string): Promise<void> {
22
+ this.fallbackLocale = fallbackLocale;
23
+ }
24
+
25
+ public getLocales(): string[] {
26
+ return ['en'];
27
+ }
28
+
29
+ public translate(key: string): string {
30
+ // eslint-disable-next-line no-console
31
+ App.development && console.warn('Lang provider is missing');
32
+
33
+ return key;
34
+ }
35
+
36
+ public translateWithDefault(_: string, defaultMessage: string): string {
37
+ // eslint-disable-next-line no-console
38
+ App.development && console.warn('Lang provider is missing');
39
+
40
+ return defaultMessage;
41
+ }
42
+
43
+ }
@@ -0,0 +1,11 @@
1
+ import { defineServiceState } from '@/services/Service';
2
+
3
+ export default defineServiceState({
4
+ name: 'lang',
5
+ persist: ['locale', 'fallbackLocale'],
6
+ initialState: {
7
+ locale: null as string | null,
8
+ locales: ['en'],
9
+ fallbackLocale: 'en',
10
+ },
11
+ });
package/src/lang/Lang.ts CHANGED
@@ -1,10 +1,16 @@
1
- import { facade, toString } from '@noeldemartin/utils';
1
+ import { facade } from '@noeldemartin/utils';
2
2
 
3
- import App from '@/services/App';
4
- import Service from '@/services/Service';
3
+ import DefaultLangProvider from './DefaultLangProvider';
4
+ import Service from './Lang.state';
5
5
 
6
6
  export interface LangProvider {
7
- translate(key: string, parameters?: Record<string, unknown>): string;
7
+ getLocale(): string;
8
+ setLocale(locale: string): Promise<void>;
9
+ getFallbackLocale(): string;
10
+ setFallbackLocale(fallbackLocale: string): Promise<void>;
11
+ getLocales(): string[];
12
+ translate(key: string, parameters?: Record<string, unknown> | number): string;
13
+ translateWithDefault(key: string, defaultMessage: string, parameters?: Record<string, unknown> | number): string;
8
14
  }
9
15
 
10
16
  export class LangService extends Service {
@@ -14,47 +20,56 @@ export class LangService extends Service {
14
20
  constructor() {
15
21
  super();
16
22
 
17
- this.provider = {
18
- translate: (key) => {
19
- // eslint-disable-next-line no-console
20
- App.development && console.warn('Lang provider is missing');
21
-
22
- return key;
23
- },
24
- };
23
+ this.provider = new DefaultLangProvider(
24
+ this.getState('locale') ?? this.getBrowserLocale(),
25
+ this.getState('fallbackLocale'),
26
+ );
25
27
  }
26
28
 
27
- public setProvider(provider: LangProvider): void {
29
+ public async setProvider(provider: LangProvider): Promise<void> {
28
30
  this.provider = provider;
31
+ this.locales = provider.getLocales();
32
+
33
+ await provider.setLocale(this.locale ?? this.getBrowserLocale());
34
+ await provider.setFallbackLocale(this.fallbackLocale);
29
35
  }
30
36
 
31
- public translate(key: string, parameters?: Record<string, unknown>): string {
37
+ public translate(key: string, parameters?: Record<string, unknown> | number): string {
32
38
  return this.provider.translate(key, parameters) ?? key;
33
39
  }
34
40
 
35
- public translateWithDefault(key: string, defaultMessage: string): string;
36
- public translateWithDefault(key: string, parameters: Record<string, unknown>, defaultMessage: string): string;
37
41
  public translateWithDefault(
38
42
  key: string,
39
- defaultMessageOrParameters?: string | Record<string, unknown>,
40
- defaultMessage?: string,
43
+ defaultMessage: string,
44
+ parameters: Record<string, unknown> | number = {},
41
45
  ): string {
42
- defaultMessage ??= defaultMessageOrParameters as string;
46
+ return this.provider.translateWithDefault(key, defaultMessage, parameters);
47
+ }
43
48
 
44
- const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters ?? {};
45
- const message = this.provider.translate(key, parameters) ?? key;
49
+ public getBrowserLocale(): string {
50
+ const locales = this.getState('locales');
46
51
 
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
- );
52
+ return navigator.languages.find((locale) => locales.includes(locale)) ?? 'en';
53
+ }
54
+
55
+ protected async boot(): Promise<void> {
56
+ if (!globalThis.document) {
57
+ return;
53
58
  }
54
59
 
55
- return message;
60
+ this.requireStore().$subscribe(
61
+ async () => {
62
+ await this.provider.setLocale(this.locale ?? this.getBrowserLocale());
63
+ await this.provider.setFallbackLocale(this.fallbackLocale);
64
+
65
+ this.locale
66
+ ? document.querySelector('html')?.setAttribute('lang', this.locale)
67
+ : document.querySelector('html')?.removeAttribute('lang');
68
+ },
69
+ { immediate: true },
70
+ );
56
71
  }
57
72
 
58
73
  }
59
74
 
60
- export default facade(new LangService());
75
+ 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,34 @@
1
- import Build from 'virtual:aerogel';
1
+ import Aerogel from 'virtual:aerogel';
2
+
3
+ import type { App } from 'vue';
2
4
 
3
5
  import { defineServiceState } from '@/services/Service';
6
+ import type { Plugin } from '@/plugins/Plugin';
4
7
 
5
8
  export default defineServiceState({
6
9
  name: 'app',
7
10
  initialState: {
8
- environment: Build.environment,
9
- sourceUrl: Build.sourceUrl,
10
- isMounted: false,
11
+ plugins: {} as Record<string, Plugin>,
12
+ instance: null as App | null,
13
+ environment: Aerogel.environment,
14
+ version: Aerogel.version,
15
+ sourceUrl: Aerogel.sourceUrl,
11
16
  },
12
17
  computed: {
13
18
  development: (state) => state.environment === 'development',
14
- testing: (state) => state.environment === 'testing',
19
+ testing: (state) => state.environment === 'test' || state.environment === 'testing',
20
+ versionName(state): string {
21
+ if (this.development) {
22
+ return 'dev.' + Aerogel.sourceHash.toString().substring(0, 7);
23
+ }
24
+
25
+ return `v${state.version}`;
26
+ },
27
+ versionUrl(state): string {
28
+ return (
29
+ state.sourceUrl +
30
+ (this.development ? `/tree/${Aerogel.sourceHash}` : `/releases/tag/${this.versionName}`)
31
+ );
32
+ },
15
33
  },
16
34
  });
@@ -1,15 +1,55 @@
1
- import { facade } from '@noeldemartin/utils';
1
+ import Aerogel from 'virtual:aerogel';
2
+
3
+ import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
2
4
 
3
5
  import Events from '@/services/Events';
6
+ import type { Plugin } from '@/plugins';
7
+ import type { Services } from '@/services';
4
8
 
5
9
  import Service from './App.state';
6
10
 
7
11
  export class AppService extends Service {
8
12
 
13
+ public readonly name = Aerogel.name;
14
+ public readonly ready = new PromisedValue<void>();
15
+ public readonly mounted = new PromisedValue<void>();
16
+
17
+ public isReady(): boolean {
18
+ return this.ready.isResolved();
19
+ }
20
+
21
+ public isMounted(): boolean {
22
+ return this.mounted.isResolved();
23
+ }
24
+
25
+ public async whenReady<T>(callback: () => T): Promise<T> {
26
+ const result = await this.ready.then(callback);
27
+
28
+ return result;
29
+ }
30
+
31
+ public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
32
+ queryParameters && updateLocationQueryParameters(queryParameters);
33
+
34
+ location.reload();
35
+
36
+ // Stall until the reload happens
37
+ await forever();
38
+ }
39
+
40
+ public plugin<T extends Plugin = Plugin>(name: string): T | null {
41
+ return (this.plugins[name] as T) ?? null;
42
+ }
43
+
44
+ public service<T extends keyof Services>(name: T): Services[T] | null {
45
+ return this.instance?.config.globalProperties[name] ?? null;
46
+ }
47
+
9
48
  protected async boot(): Promise<void> {
10
- Events.once('application-mounted', () => this.setState({ isMounted: true }));
49
+ Events.once('application-ready', () => this.ready.resolve());
50
+ Events.once('application-mounted', () => this.mounted.resolve());
11
51
  }
12
52
 
13
53
  }
14
54
 
15
- export default facade(new AppService());
55
+ 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
+ });