@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.824cf5311c4335d119158f507dad094ed4f4f0b6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aerogel-core.cjs.js +1 -1
- package/dist/aerogel-core.cjs.js.map +1 -1
- package/dist/aerogel-core.d.ts +1630 -204
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/histoire.config.ts +7 -0
- package/noeldemartin.config.js +4 -1
- package/package.json +15 -8
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +4 -59
- package/src/bootstrap/index.ts +31 -28
- package/src/bootstrap/options.ts +8 -1
- package/src/components/AGAppLayout.vue +7 -2
- package/src/components/AGAppModals.vue +15 -0
- package/src/components/AGAppOverlays.vue +10 -8
- package/src/components/AGAppSnackbars.vue +13 -0
- package/src/components/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +36 -3
- package/src/components/forms/AGCheckbox.vue +41 -0
- package/src/components/forms/AGInput.vue +15 -9
- package/src/components/forms/AGSelect.story.vue +46 -0
- package/src/components/forms/AGSelect.vue +60 -0
- package/src/components/forms/index.ts +5 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +7 -7
- package/src/components/headless/forms/AGHeadlessInput.ts +23 -3
- package/src/components/headless/forms/AGHeadlessInput.vue +13 -10
- package/src/components/headless/forms/AGHeadlessInputError.vue +9 -5
- package/src/components/headless/forms/AGHeadlessInputInput.vue +20 -4
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +22 -0
- package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
- package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
- package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
- package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
- package/src/components/headless/forms/index.ts +14 -4
- package/src/components/headless/index.ts +1 -0
- package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
- package/src/components/headless/modals/AGHeadlessModal.vue +8 -6
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -2
- package/src/components/headless/modals/index.ts +4 -6
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
- package/src/components/headless/snackbars/index.ts +40 -0
- package/src/components/index.ts +5 -2
- package/src/components/lib/AGErrorMessage.vue +16 -0
- package/src/components/lib/AGLink.vue +9 -0
- package/src/components/lib/AGMarkdown.vue +36 -0
- package/src/components/lib/AGMeasured.vue +15 -0
- package/src/components/lib/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +5 -0
- package/src/components/modals/AGAlertModal.ts +15 -0
- package/src/components/modals/AGAlertModal.vue +4 -5
- package/src/components/modals/AGConfirmModal.ts +27 -0
- package/src/components/modals/AGConfirmModal.vue +26 -0
- package/src/components/modals/AGErrorReportModal.ts +46 -0
- package/src/components/modals/AGErrorReportModal.vue +54 -0
- package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
- package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
- package/src/components/modals/AGLoadingModal.ts +23 -0
- package/src/components/modals/AGLoadingModal.vue +15 -0
- package/src/components/modals/AGModal.ts +5 -1
- package/src/components/modals/AGModal.vue +26 -5
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/AGPromptModal.ts +30 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +16 -4
- package/src/components/snackbars/AGSnackbar.vue +36 -0
- package/src/components/snackbars/index.ts +3 -0
- package/src/components/utils.ts +10 -0
- package/src/directives/index.ts +21 -4
- package/src/directives/measure.ts +12 -0
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +192 -0
- package/src/errors/index.ts +45 -0
- package/src/forms/Form.test.ts +21 -0
- package/src/forms/Form.ts +48 -17
- package/src/forms/utils.ts +17 -0
- package/src/jobs/Job.ts +5 -0
- package/src/jobs/index.ts +7 -0
- package/src/lang/Lang.ts +48 -9
- package/src/lang/index.ts +17 -76
- package/src/lang/utils.ts +4 -0
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +3 -0
- package/src/plugins/Plugin.ts +8 -0
- package/src/plugins/index.ts +26 -0
- package/src/services/App.state.ts +17 -0
- package/src/services/App.ts +47 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +88 -30
- package/src/services/Service.ts +195 -33
- package/src/services/index.ts +33 -7
- package/src/services/store.ts +30 -0
- package/src/types/vite.d.ts +0 -2
- package/src/ui/UI.state.ts +12 -6
- package/src/ui/UI.ts +177 -12
- package/src/ui/index.ts +31 -16
- package/src/utils/composition/events.ts +1 -0
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/composition/hooks.ts +9 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/markdown.ts +11 -2
- package/src/utils/tailwindcss.test.ts +26 -0
- package/src/utils/tailwindcss.ts +7 -0
- package/src/utils/vue.ts +15 -4
- package/tailwind.config.js +4 -0
- package/tsconfig.json +2 -10
- package/vite.config.ts +3 -6
- package/.eslintrc.js +0 -3
- package/src/bootstrap/hooks.ts +0 -19
- package/src/components/basic/AGMarkdown.vue +0 -20
- package/src/components/basic/index.ts +0 -3
- package/src/lang/helpers.ts +0 -5
- package/src/models/index.ts +0 -18
- package/src/routing/index.ts +0 -33
- package/src/testing/stubs/lang/en.yaml +0 -1
- package/src/testing/stubs/models/User.ts +0 -3
package/src/lang/index.ts
CHANGED
|
@@ -1,89 +1,30 @@
|
|
|
1
|
-
import { createI18n } from 'vue-i18n';
|
|
2
|
-
import { fail, stringMatch } from '@noeldemartin/utils';
|
|
3
|
-
import type { I18nOptions } from 'vue-i18n';
|
|
4
|
-
import type { Plugin } from 'vue';
|
|
5
|
-
|
|
6
|
-
import { defineBootstrapHook, onAppMounted } from '@/bootstrap/hooks';
|
|
7
1
|
import { bootServices } from '@/services';
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
import Lang from './Lang';
|
|
11
|
-
|
|
12
|
-
const services = { $lang: Lang };
|
|
13
|
-
|
|
14
|
-
function getLangOptions(options: BootstrapOptions): LangOptions | null {
|
|
15
|
-
if (options.lang) {
|
|
16
|
-
return options.lang;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (options.langMessages) {
|
|
20
|
-
return { messages: options.langMessages };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
2
|
+
import { definePlugin } from '@/plugins';
|
|
25
3
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const locale = stringMatch<2>(fileName, /.*\/lang\/(.+)\.yaml/)?.[1];
|
|
4
|
+
import Lang, { LangProvider } from './Lang';
|
|
5
|
+
import { translate, translateWithDefault } from './utils';
|
|
29
6
|
|
|
30
|
-
|
|
31
|
-
loaders[locale] = () =>
|
|
32
|
-
(loader as () => Promise<{ default: Record<string, unknown> }>)().then(
|
|
33
|
-
({ default: messages }) => messages,
|
|
34
|
-
);
|
|
35
|
-
}
|
|
7
|
+
export { Lang, LangProvider, translate, translateWithDefault };
|
|
36
8
|
|
|
37
|
-
|
|
38
|
-
}, {} as Record<string, LazyMessages>);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function createAppI18n(options: LangOptions): Promise<Plugin> {
|
|
42
|
-
const locale = options.defaultLocale ?? 'en';
|
|
43
|
-
const fallbackLocale = options.fallbackLocale ?? 'en';
|
|
44
|
-
const messageLoaders = getMessageLoaders(options.messages);
|
|
45
|
-
const lazyMessages = messageLoaders[locale] ?? fail<LazyMessages>(`Missing messages for '${locale}' locale`);
|
|
46
|
-
const messages = { [locale]: await lazyMessages() } as I18nOptions['messages'];
|
|
47
|
-
|
|
48
|
-
return createI18n({ locale, fallbackLocale, messages });
|
|
49
|
-
}
|
|
9
|
+
const services = { $lang: Lang };
|
|
50
10
|
|
|
51
11
|
export type LangServices = typeof services;
|
|
52
12
|
|
|
53
|
-
export
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
defaultLocale?: string;
|
|
58
|
-
fallbackLocale?: string;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export * from './helpers';
|
|
62
|
-
export { Lang };
|
|
63
|
-
|
|
64
|
-
export default defineBootstrapHook(async (app, options) => {
|
|
65
|
-
const langOptions = getLangOptions(options);
|
|
66
|
-
|
|
67
|
-
if (!langOptions) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
onAppMounted(() => Lang.setup());
|
|
72
|
-
|
|
73
|
-
const plugin = await createAppI18n(langOptions);
|
|
13
|
+
export default definePlugin({
|
|
14
|
+
async install(app) {
|
|
15
|
+
app.config.globalProperties.$t ??= translate;
|
|
16
|
+
app.config.globalProperties.$td = translateWithDefault;
|
|
74
17
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
await bootServices(app, services);
|
|
18
|
+
await bootServices(app, services);
|
|
19
|
+
},
|
|
78
20
|
});
|
|
79
21
|
|
|
80
|
-
declare module '@/
|
|
81
|
-
interface
|
|
82
|
-
lang?: LangOptions;
|
|
83
|
-
langMessages?: Record<string, unknown>;
|
|
84
|
-
}
|
|
22
|
+
declare module '@/services' {
|
|
23
|
+
export interface Services extends LangServices {}
|
|
85
24
|
}
|
|
86
25
|
|
|
87
|
-
declare module '
|
|
88
|
-
interface
|
|
26
|
+
declare module '@vue/runtime-core' {
|
|
27
|
+
interface ComponentCustomProperties {
|
|
28
|
+
$td: typeof translateWithDefault;
|
|
29
|
+
}
|
|
89
30
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './assets/histoire.css';
|
package/src/main.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export * from './bootstrap';
|
|
2
2
|
export * from './components';
|
|
3
|
+
export * from './errors';
|
|
3
4
|
export * from './forms';
|
|
5
|
+
export * from './jobs';
|
|
4
6
|
export * from './lang';
|
|
7
|
+
export * from './plugins';
|
|
5
8
|
export * from './services';
|
|
6
9
|
export * from './ui';
|
|
7
10
|
export * from './utils';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import App from '@/services/App';
|
|
4
|
+
|
|
5
|
+
import type { Plugin } from './Plugin';
|
|
6
|
+
|
|
7
|
+
export * from './Plugin';
|
|
8
|
+
|
|
9
|
+
export function definePlugin<T extends Plugin>(plugin: T): T {
|
|
10
|
+
return plugin;
|
|
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
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
|
+
|
|
3
|
+
import { defineServiceState } from '@/services/Service';
|
|
4
|
+
import type { Plugin } from '@/plugins/Plugin';
|
|
5
|
+
|
|
6
|
+
export default defineServiceState({
|
|
7
|
+
name: 'app',
|
|
8
|
+
initialState: {
|
|
9
|
+
plugins: {} as Record<string, Plugin>,
|
|
10
|
+
environment: Aerogel.environment,
|
|
11
|
+
sourceUrl: Aerogel.sourceUrl,
|
|
12
|
+
},
|
|
13
|
+
computed: {
|
|
14
|
+
development: (state) => state.environment === 'development',
|
|
15
|
+
testing: (state) => state.environment === 'test' || state.environment === 'testing',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
import type { Plugin } from '@/plugins';
|
|
5
|
+
|
|
6
|
+
import Service from './App.state';
|
|
7
|
+
|
|
8
|
+
export class AppService extends Service {
|
|
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
|
+
|
|
40
|
+
protected async boot(): Promise<void> {
|
|
41
|
+
Events.once('application-ready', () => this.ready.resolve());
|
|
42
|
+
Events.once('application-mounted', () => this.mounted.resolve());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
|
|
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
|
+
});
|
package/src/services/Events.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { FluentArray } from '@noeldemartin/utils';
|
|
1
|
+
import { arrayRemove, facade, fail, tap } from '@noeldemartin/utils';
|
|
3
2
|
|
|
4
3
|
import Service from '@/services/Service';
|
|
5
4
|
|
|
6
5
|
export interface EventsPayload {}
|
|
6
|
+
export interface EventListenerOptions {
|
|
7
|
+
priority: number;
|
|
8
|
+
}
|
|
7
9
|
|
|
8
10
|
export type EventListener<T = unknown> = (payload: T) => unknown;
|
|
9
11
|
export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
|
|
@@ -16,70 +18,126 @@ export type EventWithPayload = {
|
|
|
16
18
|
[K in keyof EventsPayload]: EventsPayload[K] extends void ? never : K;
|
|
17
19
|
}[keyof EventsPayload];
|
|
18
20
|
|
|
21
|
+
export const EventListenerPriorities = {
|
|
22
|
+
Low: -256,
|
|
23
|
+
Default: 0,
|
|
24
|
+
High: 256,
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
19
27
|
export class EventsService extends Service {
|
|
20
28
|
|
|
21
|
-
private listeners: Record<string,
|
|
29
|
+
private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
|
|
22
30
|
|
|
23
31
|
public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
|
|
24
32
|
public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
|
|
25
33
|
public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
|
|
26
34
|
public async emit(event: string, payload?: unknown): Promise<void> {
|
|
27
|
-
const listeners =
|
|
35
|
+
const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
|
|
28
36
|
|
|
29
|
-
|
|
37
|
+
for (const priority of listeners.priorities) {
|
|
38
|
+
await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
|
|
39
|
+
}
|
|
30
40
|
}
|
|
31
41
|
|
|
42
|
+
/* eslint-disable max-len */
|
|
32
43
|
public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
33
|
-
public on<Event extends
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
): () => void | void;
|
|
37
|
-
|
|
44
|
+
public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
45
|
+
public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
46
|
+
public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
38
47
|
public on<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
39
|
-
public on(event:
|
|
40
|
-
|
|
48
|
+
public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
49
|
+
/* eslint-enable max-len */
|
|
50
|
+
|
|
51
|
+
public on(
|
|
52
|
+
event: string,
|
|
53
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
54
|
+
listener?: EventListener,
|
|
55
|
+
): () => void {
|
|
56
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
57
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
41
58
|
|
|
42
|
-
|
|
59
|
+
this.registerListener(event, options, handler);
|
|
60
|
+
|
|
61
|
+
return () => this.off(event, handler);
|
|
43
62
|
}
|
|
44
63
|
|
|
64
|
+
/* eslint-disable max-len */
|
|
45
65
|
public once<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
46
|
-
public once<Event extends
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
): () => void | void;
|
|
50
|
-
|
|
66
|
+
public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
67
|
+
public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
68
|
+
public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
51
69
|
public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
52
|
-
public once(event:
|
|
70
|
+
public once<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
71
|
+
/* eslint-enable max-len */
|
|
72
|
+
|
|
73
|
+
public once(
|
|
74
|
+
event: string,
|
|
75
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
76
|
+
listener?: EventListener,
|
|
77
|
+
): () => void {
|
|
53
78
|
let onceListener: EventListener | null = null;
|
|
79
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
80
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
54
81
|
|
|
55
82
|
return tap(
|
|
56
83
|
() => onceListener && this.off(event, onceListener),
|
|
57
84
|
(off) => {
|
|
58
|
-
|
|
59
|
-
(
|
|
60
|
-
|
|
85
|
+
onceListener = (...args) => {
|
|
86
|
+
off();
|
|
87
|
+
|
|
88
|
+
return handler(...args);
|
|
89
|
+
};
|
|
61
90
|
|
|
62
|
-
|
|
63
|
-
}),
|
|
64
|
-
);
|
|
91
|
+
this.registerListener(event, options, handler);
|
|
65
92
|
},
|
|
66
93
|
);
|
|
67
94
|
}
|
|
68
95
|
|
|
69
96
|
public off(event: string, listener: EventListener): void {
|
|
70
|
-
const
|
|
97
|
+
const listeners = this.listeners[event];
|
|
71
98
|
|
|
72
|
-
if (!
|
|
99
|
+
if (!listeners) {
|
|
73
100
|
return;
|
|
74
101
|
}
|
|
75
102
|
|
|
76
|
-
|
|
103
|
+
const priorities = [...listeners.priorities];
|
|
77
104
|
|
|
78
|
-
|
|
105
|
+
for (const priority of priorities) {
|
|
106
|
+
arrayRemove(listeners.handlers[priority] ?? [], listener);
|
|
107
|
+
|
|
108
|
+
if (listeners.handlers[priority]?.length === 0) {
|
|
109
|
+
delete listeners.handlers[priority];
|
|
110
|
+
arrayRemove(listeners.priorities, priority);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (listeners.priorities.length === 0) {
|
|
79
115
|
delete this.listeners[event];
|
|
80
116
|
}
|
|
81
117
|
}
|
|
82
118
|
|
|
119
|
+
protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
|
|
120
|
+
const priority = options.priority ?? 0;
|
|
121
|
+
|
|
122
|
+
if (!(event in this.listeners)) {
|
|
123
|
+
this.listeners[event] = { priorities: [], handlers: {} };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const priorities =
|
|
127
|
+
this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
|
|
128
|
+
const handlers =
|
|
129
|
+
this.listeners[event]?.handlers ??
|
|
130
|
+
fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
|
|
131
|
+
|
|
132
|
+
if (!priorities.includes(priority)) {
|
|
133
|
+
priorities.push(priority);
|
|
134
|
+
priorities.sort((a, b) => b - a);
|
|
135
|
+
handlers[priority] = [];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
handlers[priority]?.push(handler);
|
|
139
|
+
}
|
|
140
|
+
|
|
83
141
|
}
|
|
84
142
|
|
|
85
|
-
export default facade(
|
|
143
|
+
export default facade(EventsService);
|