@aerogel/core 0.0.0-next.926bde19326fe7b6b24b277666936862b64d8295 → 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e
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.d.ts +213 -53
- package/dist/aerogel-core.esm.js +1 -1
- package/package.json +2 -1
- package/src/bootstrap/index.ts +4 -1
- package/src/components/basic/AGMarkdown.vue +3 -3
- package/src/components/forms/AGButton.vue +21 -8
- package/src/components/forms/AGCheckbox.vue +35 -0
- package/src/components/forms/AGInput.vue +8 -4
- package/src/components/forms/index.ts +2 -1
- package/src/components/headless/forms/AGHeadlessButton.vue +3 -4
- package/src/components/headless/forms/AGHeadlessInput.ts +2 -2
- package/src/components/headless/forms/AGHeadlessInput.vue +3 -3
- package/src/components/headless/forms/AGHeadlessInputError.vue +1 -1
- package/src/components/headless/forms/AGHeadlessInputInput.vue +15 -3
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +5 -1
- package/src/components/modals/AGLoadingModal.vue +19 -0
- package/src/components/modals/AGModal.vue +20 -2
- package/src/components/modals/index.ts +2 -1
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +132 -0
- package/src/errors/index.ts +21 -0
- package/src/forms/Form.ts +12 -9
- package/src/forms/utils.ts +17 -0
- package/src/lang/Lang.ts +11 -3
- package/src/lang/index.ts +3 -5
- package/src/lang/utils.ts +4 -0
- package/src/main.ts +1 -0
- package/src/services/App.state.ts +3 -0
- package/src/services/App.ts +11 -1
- package/src/services/Service.ts +126 -44
- package/src/services/index.ts +18 -4
- package/src/services/store.ts +27 -0
- package/src/ui/UI.state.ts +1 -0
- package/src/ui/UI.ts +15 -0
- package/src/ui/index.ts +3 -1
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { JSError, facade, isObject } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import App from '@/services/App';
|
|
4
|
+
import ServiceBootError from '@/errors/ServiceBootError';
|
|
5
|
+
import UI from '@/ui/UI';
|
|
6
|
+
import { translate } from '@/lang/utils';
|
|
7
|
+
|
|
8
|
+
import Service from './Errors.state';
|
|
9
|
+
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
10
|
+
|
|
11
|
+
export class ErrorsService extends Service {
|
|
12
|
+
|
|
13
|
+
public forceReporting: boolean = false;
|
|
14
|
+
private enabled: boolean = true;
|
|
15
|
+
|
|
16
|
+
public enable(): void {
|
|
17
|
+
this.enabled = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public disable(): void {
|
|
21
|
+
this.enabled = false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
|
|
25
|
+
const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
|
|
26
|
+
|
|
27
|
+
// TODO open errors modal
|
|
28
|
+
reports;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
32
|
+
if (App.isDevelopment || App.isTesting) {
|
|
33
|
+
this.logError(error);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!this.enabled) {
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!App.isMounted) {
|
|
41
|
+
const startupError = await this.createStartupErrorReport(error);
|
|
42
|
+
|
|
43
|
+
if (startupError) {
|
|
44
|
+
this.setState({ startupErrors: this.startupErrors.concat(startupError) });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const report = await this.createErrorReport(error);
|
|
51
|
+
const log: ErrorReportLog = {
|
|
52
|
+
report,
|
|
53
|
+
seen: false,
|
|
54
|
+
date: new Date(),
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// TODO open error snackbar
|
|
58
|
+
UI.alert(message ?? 'Something went wrong, but it\'s not your fault! (look at the console for details)');
|
|
59
|
+
|
|
60
|
+
this.setState({ logs: [log].concat(this.logs) });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public see(report: ErrorReport): void {
|
|
64
|
+
this.setState({
|
|
65
|
+
logs: this.logs.map((log) => {
|
|
66
|
+
if (log.report !== report) {
|
|
67
|
+
return log;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
...log,
|
|
72
|
+
seen: true,
|
|
73
|
+
};
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public seeAll(): void {
|
|
79
|
+
this.setState({
|
|
80
|
+
logs: this.logs.map((log) => ({
|
|
81
|
+
...log,
|
|
82
|
+
seen: true,
|
|
83
|
+
})),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private logError(error: unknown): void {
|
|
88
|
+
// eslint-disable-next-line no-console
|
|
89
|
+
console.error(error);
|
|
90
|
+
|
|
91
|
+
if (isObject(error) && error.cause) {
|
|
92
|
+
this.logError(error.cause);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private async createErrorReport(error: ErrorSource): Promise<ErrorReport> {
|
|
97
|
+
if (typeof error === 'string') {
|
|
98
|
+
return { title: error };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (error instanceof Error || error instanceof JSError) {
|
|
102
|
+
return this.createErrorReportFromError(error);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
title: translate('errors.unknown'),
|
|
107
|
+
error,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private async createStartupErrorReport(error: ErrorSource): Promise<ErrorReport | null> {
|
|
112
|
+
if (error instanceof ServiceBootError) {
|
|
113
|
+
// Ignore second-order boot errors in order to have a cleaner startup crash screen.
|
|
114
|
+
return error.cause instanceof ServiceBootError ? null : this.createErrorReport(error.cause);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return this.createErrorReport(error);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private createErrorReportFromError(error: Error | JSError, defaults: Partial<ErrorReport> = {}): ErrorReport {
|
|
121
|
+
return {
|
|
122
|
+
title: error.name,
|
|
123
|
+
description: error.message,
|
|
124
|
+
details: error.stack,
|
|
125
|
+
error,
|
|
126
|
+
...defaults,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export default facade(new ErrorsService());
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { bootServices } from '@/services';
|
|
2
|
+
import { definePlugin } from '@/plugins';
|
|
3
|
+
|
|
4
|
+
import Errors from './Errors';
|
|
5
|
+
import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
6
|
+
|
|
7
|
+
export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
|
|
8
|
+
|
|
9
|
+
const services = { $errors: Errors };
|
|
10
|
+
|
|
11
|
+
export type ErrorsServices = typeof services;
|
|
12
|
+
|
|
13
|
+
export default definePlugin({
|
|
14
|
+
async install(app) {
|
|
15
|
+
await bootServices(app, services);
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
declare module '@/services' {
|
|
20
|
+
export interface Services extends ErrorsServices {}
|
|
21
|
+
}
|
package/src/forms/Form.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
|
6
6
|
export const FormFieldTypes = {
|
|
7
7
|
String: 'string',
|
|
8
8
|
Number: 'number',
|
|
9
|
+
Boolean: 'boolean',
|
|
9
10
|
} as const;
|
|
10
11
|
|
|
11
12
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
@@ -33,6 +34,8 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
|
|
|
33
34
|
? string
|
|
34
35
|
: TType extends typeof FormFieldTypes.Number
|
|
35
36
|
? number
|
|
37
|
+
: TType extends typeof FormFieldTypes.Boolean
|
|
38
|
+
? boolean
|
|
36
39
|
: never;
|
|
37
40
|
|
|
38
41
|
export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
@@ -78,10 +81,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
public validate(): boolean {
|
|
81
|
-
const errors = Object.entries(this._fields).reduce((
|
|
82
|
-
|
|
84
|
+
const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
|
|
85
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
83
86
|
|
|
84
|
-
return
|
|
87
|
+
return formErrors;
|
|
85
88
|
}, {} as Record<string, string[] | null>);
|
|
86
89
|
|
|
87
90
|
this.resetErrors(errors);
|
|
@@ -135,10 +138,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
135
138
|
return {} as FormData<Fields>;
|
|
136
139
|
}
|
|
137
140
|
|
|
138
|
-
const data = Object.entries(fields).reduce((
|
|
139
|
-
|
|
141
|
+
const data = Object.entries(fields).reduce((formData, [name, definition]) => {
|
|
142
|
+
formData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
140
143
|
|
|
141
|
-
return
|
|
144
|
+
return formData;
|
|
142
145
|
}, {} as FormData<Fields>);
|
|
143
146
|
|
|
144
147
|
return reactive(data) as FormData<Fields>;
|
|
@@ -149,10 +152,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
149
152
|
return {} as FormErrors<Fields>;
|
|
150
153
|
}
|
|
151
154
|
|
|
152
|
-
const errors = Object.keys(fields).reduce((
|
|
153
|
-
|
|
155
|
+
const errors = Object.keys(fields).reduce((formErrors, name) => {
|
|
156
|
+
formErrors[name as keyof Fields] = null;
|
|
154
157
|
|
|
155
|
-
return
|
|
158
|
+
return formErrors;
|
|
156
159
|
}, {} as FormErrors<Fields>);
|
|
157
160
|
|
|
158
161
|
return reactive(errors) as FormErrors<Fields>;
|
package/src/forms/utils.ts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { FormFieldTypes } from './Form';
|
|
2
2
|
import type { FormFieldDefinition } from './Form';
|
|
3
3
|
|
|
4
|
+
export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof FormFieldTypes.Boolean> {
|
|
5
|
+
return {
|
|
6
|
+
default: defaultValue,
|
|
7
|
+
type: FormFieldTypes.Boolean,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function requiredBooleanInput(
|
|
12
|
+
defaultValue?: boolean,
|
|
13
|
+
): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
|
|
14
|
+
return {
|
|
15
|
+
default: defaultValue,
|
|
16
|
+
type: FormFieldTypes.Boolean,
|
|
17
|
+
rules: 'required',
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
4
21
|
export function requiredNumberInput(
|
|
5
22
|
defaultValue?: number,
|
|
6
23
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
package/src/lang/Lang.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { facade } from '@noeldemartin/utils';
|
|
1
|
+
import { facade, toString } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
3
|
import App from '@/services/App';
|
|
4
4
|
import Service from '@/services/Service';
|
|
@@ -41,10 +41,18 @@ export class LangService extends Service {
|
|
|
41
41
|
): string {
|
|
42
42
|
defaultMessage ??= defaultMessageOrParameters as string;
|
|
43
43
|
|
|
44
|
-
const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters;
|
|
44
|
+
const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters ?? {};
|
|
45
45
|
const message = this.provider.translate(key, parameters) ?? key;
|
|
46
46
|
|
|
47
|
-
|
|
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;
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
}
|
package/src/lang/index.ts
CHANGED
|
@@ -2,16 +2,14 @@ import { bootServices } from '@/services';
|
|
|
2
2
|
import { definePlugin } from '@/plugins';
|
|
3
3
|
|
|
4
4
|
import Lang, { LangProvider } from './Lang';
|
|
5
|
+
import { translate, translateWithDefault } from './utils';
|
|
5
6
|
|
|
6
|
-
export { Lang, LangProvider };
|
|
7
|
+
export { Lang, LangProvider, translate, translateWithDefault };
|
|
7
8
|
|
|
8
9
|
const services = { $lang: Lang };
|
|
9
10
|
|
|
10
11
|
export type LangServices = typeof services;
|
|
11
12
|
|
|
12
|
-
export const translate = Lang.translate.bind(Lang);
|
|
13
|
-
export const translateWithDefault = Lang.translateWithDefault.bind(Lang);
|
|
14
|
-
|
|
15
13
|
export default definePlugin({
|
|
16
14
|
async install(app) {
|
|
17
15
|
app.config.globalProperties.$t ??= translate;
|
|
@@ -22,7 +20,7 @@ export default definePlugin({
|
|
|
22
20
|
});
|
|
23
21
|
|
|
24
22
|
declare module '@/services' {
|
|
25
|
-
interface Services extends LangServices {}
|
|
23
|
+
export interface Services extends LangServices {}
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
declare module '@vue/runtime-core' {
|
package/src/main.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { defineServiceState } from '@/services/Service';
|
|
2
2
|
|
|
3
3
|
export default defineServiceState({
|
|
4
|
+
name: 'app',
|
|
4
5
|
initialState: {
|
|
5
6
|
environment: __AG_ENV,
|
|
7
|
+
isMounted: false,
|
|
6
8
|
},
|
|
7
9
|
computed: {
|
|
8
10
|
isDevelopment: (state) => state.environment === 'development',
|
|
11
|
+
isTesting: (state) => state.environment === 'testing',
|
|
9
12
|
},
|
|
10
13
|
});
|
package/src/services/App.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { facade } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
|
|
3
5
|
import Service from './App.state';
|
|
4
6
|
|
|
5
|
-
export class AppService extends Service {
|
|
7
|
+
export class AppService extends Service {
|
|
8
|
+
|
|
9
|
+
protected async boot(): Promise<void> {
|
|
10
|
+
await super.boot();
|
|
11
|
+
|
|
12
|
+
Events.once('application-mounted', () => this.setState({ isMounted: true }));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
}
|
|
6
16
|
|
|
7
17
|
export default facade(new AppService());
|
package/src/services/Service.ts
CHANGED
|
@@ -1,27 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MagicObject, PromisedValue } from '@noeldemartin/utils';
|
|
3
|
-
import type { ComputedRef } from 'vue';
|
|
1
|
+
import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
4
2
|
import type { Constructor } from '@noeldemartin/utils';
|
|
3
|
+
import type { Store } from 'pinia';
|
|
5
4
|
|
|
6
5
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
6
|
+
import { defineServiceStore } from '@/services/store';
|
|
7
7
|
|
|
8
8
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export type DefaultServiceState =
|
|
9
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
10
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
11
11
|
|
|
12
12
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
13
|
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
14
|
-
}
|
|
14
|
+
} & ThisType<{
|
|
15
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
|
+
}>;
|
|
15
17
|
|
|
16
18
|
export function defineServiceState<
|
|
17
19
|
State extends ServiceState = ServiceState,
|
|
18
20
|
ComputedState extends ServiceState = {}
|
|
19
21
|
>(options: {
|
|
22
|
+
name: string;
|
|
20
23
|
initialState: State;
|
|
24
|
+
persist?: (keyof State)[];
|
|
21
25
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
22
|
-
|
|
26
|
+
serialize?: (state: Partial<State>) => Partial<State>;
|
|
27
|
+
}): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
|
|
23
28
|
return class extends Service<State, ComputedState> {
|
|
24
29
|
|
|
30
|
+
public static persist = (options.persist as string[]) ?? [];
|
|
31
|
+
|
|
32
|
+
protected usesStore(): boolean {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected getName(): string | null {
|
|
37
|
+
return options.name ?? null;
|
|
38
|
+
}
|
|
39
|
+
|
|
25
40
|
protected getInitialState(): State {
|
|
26
41
|
return options.initialState;
|
|
27
42
|
}
|
|
@@ -29,44 +44,53 @@ export function defineServiceState<
|
|
|
29
44
|
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
30
45
|
return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
|
|
31
46
|
}
|
|
47
|
+
|
|
48
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
49
|
+
return options.serialize?.(state) ?? state;
|
|
50
|
+
}
|
|
32
51
|
|
|
33
|
-
} as unknown as Constructor<State> &
|
|
52
|
+
} as unknown as Constructor<State> &
|
|
53
|
+
Constructor<ComputedState> &
|
|
54
|
+
Constructor<Service<State, ComputedState, Partial<State>>>;
|
|
34
55
|
}
|
|
35
56
|
|
|
36
57
|
export default class Service<
|
|
37
58
|
State extends ServiceState = DefaultServiceState,
|
|
38
|
-
ComputedState extends ServiceState = {}
|
|
59
|
+
ComputedState extends ServiceState = {},
|
|
60
|
+
ServiceStorage extends Partial<State> = Partial<State>
|
|
39
61
|
> extends MagicObject {
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
public static persist: string[] = [];
|
|
64
|
+
|
|
65
|
+
protected _name: string;
|
|
42
66
|
private _booted: PromisedValue<void>;
|
|
43
|
-
private
|
|
44
|
-
private
|
|
67
|
+
private _computedStateKeys: Set<keyof State>;
|
|
68
|
+
private _store?: Store | false;
|
|
45
69
|
|
|
46
70
|
constructor() {
|
|
47
71
|
super();
|
|
48
72
|
|
|
49
|
-
|
|
50
|
-
this._booted = new PromisedValue();
|
|
51
|
-
this._state = reactive(this.getInitialState());
|
|
52
|
-
this._computedState = Object.entries(this.getComputedStateDefinition()).reduce(
|
|
53
|
-
(computedState, [name, method]) => {
|
|
54
|
-
computedState[name as keyof ComputedState] = computed(() => method(this._state));
|
|
73
|
+
const getters = this.getComputedStateDefinition();
|
|
55
74
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
75
|
+
this._name = this.getName() ?? new.target.name;
|
|
76
|
+
this._booted = new PromisedValue();
|
|
77
|
+
this._computedStateKeys = new Set(Object.keys(getters));
|
|
78
|
+
this._store =
|
|
79
|
+
this.usesStore() &&
|
|
80
|
+
defineServiceStore(this._name, {
|
|
81
|
+
state: () => this.getInitialState(),
|
|
82
|
+
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
getters: getters as any,
|
|
85
|
+
});
|
|
60
86
|
}
|
|
61
87
|
|
|
62
88
|
public get booted(): PromisedValue<void> {
|
|
63
89
|
return this._booted;
|
|
64
90
|
}
|
|
65
91
|
|
|
66
|
-
public launch(
|
|
67
|
-
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this.
|
|
68
|
-
|
|
69
|
-
this._namespace = namespace ?? this._namespace;
|
|
92
|
+
public launch(): Promise<void> {
|
|
93
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
70
94
|
|
|
71
95
|
try {
|
|
72
96
|
this.boot()
|
|
@@ -79,15 +103,48 @@ export default class Service<
|
|
|
79
103
|
return this._booted;
|
|
80
104
|
}
|
|
81
105
|
|
|
106
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
107
|
+
if (!this._store) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public getState(): State;
|
|
115
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
116
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
118
|
+
const store = this._store as any;
|
|
119
|
+
|
|
120
|
+
if (property) {
|
|
121
|
+
return store ? store[property] : undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return store ? store : {};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
128
|
+
public setState(state: Partial<State>): void;
|
|
129
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
130
|
+
if (!this._store) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const state = (
|
|
135
|
+
typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty
|
|
136
|
+
) as Partial<State>;
|
|
137
|
+
|
|
138
|
+
Object.assign(this._store.$state, state);
|
|
139
|
+
|
|
140
|
+
this.onStateUpdated(state);
|
|
141
|
+
}
|
|
142
|
+
|
|
82
143
|
protected __get(property: string): unknown {
|
|
83
144
|
if (this.hasState(property)) {
|
|
84
145
|
return this.getState(property);
|
|
85
146
|
}
|
|
86
147
|
|
|
87
|
-
if (this.hasComputedState(property)) {
|
|
88
|
-
return this.getComputedState(property);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
148
|
return super.__get(property);
|
|
92
149
|
}
|
|
93
150
|
|
|
@@ -95,26 +152,29 @@ export default class Service<
|
|
|
95
152
|
this.setState({ [property]: value } as Partial<State>);
|
|
96
153
|
}
|
|
97
154
|
|
|
98
|
-
protected
|
|
99
|
-
|
|
100
|
-
|
|
155
|
+
protected onStateUpdated(state: Partial<State>): void {
|
|
156
|
+
// TODO fix this.static()
|
|
157
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
158
|
+
const persisted = objectOnly(state, persist);
|
|
101
159
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
160
|
+
if (isEmpty(persisted)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const storage = Storage.require<ServiceStorage>(this._name);
|
|
105
165
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
166
|
+
Storage.set(this._name, {
|
|
167
|
+
...storage,
|
|
168
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
169
|
+
});
|
|
110
170
|
}
|
|
111
171
|
|
|
112
|
-
protected
|
|
113
|
-
return
|
|
172
|
+
protected usesStore(): boolean {
|
|
173
|
+
return false;
|
|
114
174
|
}
|
|
115
175
|
|
|
116
|
-
protected
|
|
117
|
-
|
|
176
|
+
protected getName(): string | null {
|
|
177
|
+
return null;
|
|
118
178
|
}
|
|
119
179
|
|
|
120
180
|
protected getInitialState(): State {
|
|
@@ -125,8 +185,30 @@ export default class Service<
|
|
|
125
185
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
126
186
|
}
|
|
127
187
|
|
|
188
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
189
|
+
return state;
|
|
190
|
+
}
|
|
191
|
+
|
|
128
192
|
protected async boot(): Promise<void> {
|
|
129
|
-
|
|
193
|
+
this.restorePersistedState();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
protected restorePersistedState(): void {
|
|
197
|
+
// TODO fix this.static()
|
|
198
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
199
|
+
|
|
200
|
+
if (!this.usesStore() || isEmpty(persist)) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (Storage.has(this._name)) {
|
|
205
|
+
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
206
|
+
this.setState(persisted);
|
|
207
|
+
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
130
212
|
}
|
|
131
213
|
|
|
132
214
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { definePlugin } from '@/plugins';
|
|
|
5
5
|
import App from './App';
|
|
6
6
|
import Events from './Events';
|
|
7
7
|
import Service from './Service';
|
|
8
|
+
import { getPiniaStore } from './store';
|
|
8
9
|
|
|
9
10
|
export * from './App';
|
|
10
11
|
export * from './Events';
|
|
@@ -23,9 +24,9 @@ export interface Services extends DefaultServices {}
|
|
|
23
24
|
|
|
24
25
|
export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
|
|
25
26
|
await Promise.all(
|
|
26
|
-
Object.entries(services).map(async ([
|
|
27
|
+
Object.entries(services).map(async ([_, service]) => {
|
|
27
28
|
// eslint-disable-next-line no-console
|
|
28
|
-
await service.launch(
|
|
29
|
+
await service.launch().catch((error) => console.error(error));
|
|
29
30
|
}),
|
|
30
31
|
);
|
|
31
32
|
|
|
@@ -33,11 +34,24 @@ export async function bootServices(app: VueApp, services: Record<string, Service
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export default definePlugin({
|
|
36
|
-
async install(app) {
|
|
37
|
-
|
|
37
|
+
async install(app, options) {
|
|
38
|
+
const services = {
|
|
39
|
+
...defaultServices,
|
|
40
|
+
...options.services,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
app.use(getPiniaStore());
|
|
44
|
+
|
|
45
|
+
await bootServices(app, services);
|
|
38
46
|
},
|
|
39
47
|
});
|
|
40
48
|
|
|
49
|
+
declare module '@/bootstrap/options' {
|
|
50
|
+
interface AerogelOptions {
|
|
51
|
+
services?: Record<string, Service>;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
41
55
|
declare module '@vue/runtime-core' {
|
|
42
56
|
interface ComponentCustomProperties extends Services {}
|
|
43
57
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
2
|
+
import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
|
|
3
|
+
|
|
4
|
+
let _store: Pinia | null = null;
|
|
5
|
+
|
|
6
|
+
function initializePiniaStore(): Pinia {
|
|
7
|
+
if (!_store) {
|
|
8
|
+
_store = createPinia();
|
|
9
|
+
|
|
10
|
+
setActivePinia(_store);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return _store;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getPiniaStore(): Pinia {
|
|
17
|
+
return _store ?? initializePiniaStore();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function defineServiceStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(
|
|
21
|
+
name: Id,
|
|
22
|
+
options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>,
|
|
23
|
+
): Store<Id, S, G, A> {
|
|
24
|
+
initializePiniaStore();
|
|
25
|
+
|
|
26
|
+
return defineStore(name, options)();
|
|
27
|
+
}
|