@aerogel/core 0.0.0-next.6c539d8e63b397d4bb6c3d61a7f20a4d108b1cdd → 0.0.0-next.7035064d9ec6a82a936ee8dfcc4b58ed2e25a399
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 +328 -72
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/bootstrap/index.ts +12 -2
- package/src/components/headless/modals/AGHeadlessModal.ts +3 -1
- package/src/components/headless/modals/AGHeadlessModal.vue +10 -4
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -6
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +14 -4
- package/src/components/lib/AGMarkdown.vue +14 -1
- package/src/components/lib/AGProgressBar.vue +30 -0
- package/src/components/lib/index.ts +1 -0
- package/src/components/modals/AGAlertModal.ts +5 -2
- package/src/components/modals/AGConfirmModal.ts +13 -5
- package/src/components/modals/AGConfirmModal.vue +1 -1
- package/src/components/modals/AGErrorReportModal.ts +5 -2
- package/src/components/modals/AGLoadingModal.ts +10 -4
- package/src/components/modals/AGModal.ts +1 -0
- package/src/components/modals/AGModalContext.vue +14 -4
- package/src/components/modals/AGPromptModal.ts +9 -4
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/utils.ts +16 -0
- package/src/forms/Form.ts +10 -3
- package/src/forms/index.ts +2 -1
- package/src/forms/utils.ts +20 -4
- package/src/forms/validation.ts +19 -0
- package/src/jobs/Job.ts +144 -2
- package/src/jobs/index.ts +4 -1
- package/src/jobs/listeners.ts +3 -0
- package/src/jobs/status.ts +4 -0
- package/src/services/App.state.ts +9 -1
- package/src/services/App.ts +5 -0
- package/src/services/Events.ts +13 -3
- package/src/services/Service.ts +107 -44
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +7 -2
- package/src/services/utils.ts +18 -0
- package/src/testing/setup.ts +11 -3
- package/src/ui/UI.ts +108 -38
- package/src/utils/composition/persistent.test.ts +33 -0
- package/src/utils/composition/persistent.ts +11 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +24 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +17 -2
- package/src/utils/vue.ts +4 -1
package/src/jobs/Job.ts
CHANGED
|
@@ -1,5 +1,147 @@
|
|
|
1
|
-
|
|
1
|
+
import { ListenersManager, PromisedValue, round, tap, toError } from '@noeldemartin/utils';
|
|
2
|
+
import type { Listeners } from '@noeldemartin/utils';
|
|
2
3
|
|
|
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
|
+
}
|
|
4
146
|
|
|
5
147
|
}
|
package/src/jobs/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import Aerogel from 'virtual:aerogel';
|
|
2
2
|
|
|
3
|
+
import type { App } from 'vue';
|
|
4
|
+
|
|
3
5
|
import { defineServiceState } from '@/services/Service';
|
|
4
6
|
import type { Plugin } from '@/plugins/Plugin';
|
|
5
7
|
|
|
@@ -7,24 +9,30 @@ export default defineServiceState({
|
|
|
7
9
|
name: 'app',
|
|
8
10
|
initialState: {
|
|
9
11
|
plugins: {} as Record<string, Plugin>,
|
|
12
|
+
instance: null as App | null,
|
|
10
13
|
environment: Aerogel.environment,
|
|
11
14
|
version: Aerogel.version,
|
|
12
15
|
sourceUrl: Aerogel.sourceUrl,
|
|
13
16
|
},
|
|
14
17
|
computed: {
|
|
15
18
|
development: (state) => state.environment === 'development',
|
|
19
|
+
staging: (state) => state.environment === 'staging',
|
|
16
20
|
testing: (state) => state.environment === 'test' || state.environment === 'testing',
|
|
17
21
|
versionName(state): string {
|
|
18
22
|
if (this.development) {
|
|
19
23
|
return 'dev.' + Aerogel.sourceHash.toString().substring(0, 7);
|
|
20
24
|
}
|
|
21
25
|
|
|
26
|
+
if (this.staging) {
|
|
27
|
+
return 'staging.' + Aerogel.sourceHash.toString().substring(0, 7);
|
|
28
|
+
}
|
|
29
|
+
|
|
22
30
|
return `v${state.version}`;
|
|
23
31
|
},
|
|
24
32
|
versionUrl(state): string {
|
|
25
33
|
return (
|
|
26
34
|
state.sourceUrl +
|
|
27
|
-
(this.development ? `/tree/${Aerogel.sourceHash}` : `/releases/tag/${this.versionName}`)
|
|
35
|
+
(this.development || this.staging ? `/tree/${Aerogel.sourceHash}` : `/releases/tag/${this.versionName}`)
|
|
28
36
|
);
|
|
29
37
|
},
|
|
30
38
|
},
|
package/src/services/App.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { PromisedValue, facade, forever, updateLocationQueryParameters } from '@
|
|
|
4
4
|
|
|
5
5
|
import Events from '@/services/Events';
|
|
6
6
|
import type { Plugin } from '@/plugins';
|
|
7
|
+
import type { Services } from '@/services';
|
|
7
8
|
|
|
8
9
|
import Service from './App.state';
|
|
9
10
|
|
|
@@ -40,6 +41,10 @@ export class AppService extends Service {
|
|
|
40
41
|
return (this.plugins[name] as T) ?? null;
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
public service<T extends keyof Services>(name: T): Services[T] | null {
|
|
45
|
+
return this.instance?.config.globalProperties[name] ?? null;
|
|
46
|
+
}
|
|
47
|
+
|
|
43
48
|
protected async boot(): Promise<void> {
|
|
44
49
|
Events.once('application-ready', () => this.ready.resolve());
|
|
45
50
|
Events.once('application-mounted', () => this.mounted.resolve());
|
package/src/services/Events.ts
CHANGED
|
@@ -4,7 +4,7 @@ import Service from '@/services/Service';
|
|
|
4
4
|
|
|
5
5
|
export interface EventsPayload {}
|
|
6
6
|
export interface EventListenerOptions {
|
|
7
|
-
priority:
|
|
7
|
+
priority: EventListenerPriority;
|
|
8
8
|
}
|
|
9
9
|
export type AerogelGlobalEvents = Partial<{ [Event in EventWithoutPayload]: () => unknown }> &
|
|
10
10
|
Partial<{ [Event in EventWithPayload]: EventListener<EventsPayload[Event]> }>;
|
|
@@ -26,6 +26,8 @@ export const EventListenerPriorities = {
|
|
|
26
26
|
High: 256,
|
|
27
27
|
} as const;
|
|
28
28
|
|
|
29
|
+
export type EventListenerPriority = (typeof EventListenerPriorities)[keyof typeof EventListenerPriorities];
|
|
30
|
+
|
|
29
31
|
export class EventsService extends Service {
|
|
30
32
|
|
|
31
33
|
private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
|
|
@@ -48,19 +50,27 @@ export class EventsService extends Service {
|
|
|
48
50
|
|
|
49
51
|
/* eslint-disable max-len */
|
|
50
52
|
public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
53
|
+
public on<Event extends EventWithoutPayload>(event: Event, priority: EventListenerPriority, listener: () => unknown): () => void; // prettier-ignore
|
|
51
54
|
public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
52
55
|
public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
56
|
+
public on<Event extends EventWithPayload>(event: Event, priority: EventListenerPriority, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
53
57
|
public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
54
58
|
public on<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
59
|
+
public on<Event extends string>(event: UnknownEvent<Event>, priority: EventListenerPriority, listener: EventListener): () => void; // prettier-ignore
|
|
55
60
|
public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
56
61
|
/* eslint-enable max-len */
|
|
57
62
|
|
|
58
63
|
public on(
|
|
59
64
|
event: string,
|
|
60
|
-
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
65
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListenerPriority | EventListener,
|
|
61
66
|
listener?: EventListener,
|
|
62
67
|
): () => void {
|
|
63
|
-
const options =
|
|
68
|
+
const options =
|
|
69
|
+
typeof optionsOrListener === 'function'
|
|
70
|
+
? {}
|
|
71
|
+
: typeof optionsOrListener === 'number'
|
|
72
|
+
? { priority: optionsOrListener }
|
|
73
|
+
: optionsOrListener;
|
|
64
74
|
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
65
75
|
|
|
66
76
|
this.registerListener(event, options, handler);
|
package/src/services/Service.ts
CHANGED
|
@@ -1,37 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
MagicObject,
|
|
3
|
+
PromisedValue,
|
|
4
|
+
Storage,
|
|
5
|
+
arrayFrom,
|
|
6
|
+
fail,
|
|
7
|
+
isEmpty,
|
|
8
|
+
objectDeepClone,
|
|
9
|
+
objectOnly,
|
|
10
|
+
} from '@noeldemartin/utils';
|
|
11
|
+
import type { Constructor, Nullable } from '@noeldemartin/utils';
|
|
4
12
|
import type { Store } from 'pinia';
|
|
5
13
|
|
|
6
14
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
7
15
|
import { defineServiceStore } from '@/services/store';
|
|
16
|
+
import type { Unref } from '@/utils/vue';
|
|
8
17
|
|
|
9
18
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
19
|
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
11
20
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
12
|
-
export type UnrefServiceState<State extends ServiceState> = {
|
|
13
|
-
[K in keyof State]: State[K] extends MaybeRef<infer T> ? T : State[K];
|
|
14
|
-
};
|
|
15
21
|
|
|
16
22
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
17
|
-
[K in keyof TComputedState]: (state:
|
|
23
|
+
[K in keyof TComputedState]: (state: Unref<TState>) => TComputedState[K];
|
|
18
24
|
} & ThisType<{
|
|
19
25
|
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
20
26
|
}>;
|
|
21
27
|
|
|
28
|
+
export type StateWatchers<TService extends Service, TState extends ServiceState> = {
|
|
29
|
+
[K in keyof TState]?: (this: TService, value: TState[K], oldValue: TState[K]) => unknown;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type ServiceWithState<
|
|
33
|
+
State extends ServiceState = ServiceState,
|
|
34
|
+
ComputedState extends ServiceState = {},
|
|
35
|
+
ServiceStorage = Partial<State>
|
|
36
|
+
> = Constructor<Unref<State>> &
|
|
37
|
+
Constructor<ComputedState> &
|
|
38
|
+
Constructor<Service<Unref<State>, ComputedState, Unref<ServiceStorage>>>;
|
|
39
|
+
|
|
22
40
|
export function defineServiceState<
|
|
23
41
|
State extends ServiceState = ServiceState,
|
|
24
|
-
ComputedState extends ServiceState = {}
|
|
42
|
+
ComputedState extends ServiceState = {},
|
|
43
|
+
ServiceStorage = Partial<State>
|
|
25
44
|
>(options: {
|
|
26
45
|
name: string;
|
|
27
46
|
initialState: State | (() => State);
|
|
28
47
|
persist?: (keyof State)[];
|
|
48
|
+
watch?: StateWatchers<Service, State>;
|
|
29
49
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
30
|
-
serialize?: (state: Partial<State>) =>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return class extends Service<UnrefServiceState<State>, ComputedState> {
|
|
50
|
+
serialize?: (state: Partial<State>) => ServiceStorage;
|
|
51
|
+
restore?: (state: ServiceStorage) => Partial<State>;
|
|
52
|
+
}): ServiceWithState<State, ComputedState, ServiceStorage> {
|
|
53
|
+
return class extends Service<Unref<State>, ComputedState, ServiceStorage> {
|
|
35
54
|
|
|
36
55
|
public static persist = (options.persist as string[]) ?? [];
|
|
37
56
|
|
|
@@ -43,7 +62,7 @@ export function defineServiceState<
|
|
|
43
62
|
return options.name ?? null;
|
|
44
63
|
}
|
|
45
64
|
|
|
46
|
-
protected getInitialState():
|
|
65
|
+
protected getInitialState(): Unref<State> {
|
|
47
66
|
if (typeof options.initialState === 'function') {
|
|
48
67
|
return options.initialState();
|
|
49
68
|
}
|
|
@@ -64,26 +83,32 @@ export function defineServiceState<
|
|
|
64
83
|
state[key as keyof State] = value;
|
|
65
84
|
|
|
66
85
|
return state;
|
|
67
|
-
}, {} as
|
|
86
|
+
}, {} as Unref<State>);
|
|
68
87
|
}
|
|
69
88
|
|
|
70
|
-
protected getComputedStateDefinition(): ComputedStateDefinition<
|
|
71
|
-
return (options.computed ?? {}) as ComputedStateDefinition<
|
|
89
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<Unref<State>, ComputedState> {
|
|
90
|
+
return (options.computed ?? {}) as ComputedStateDefinition<Unref<State>, ComputedState>;
|
|
72
91
|
}
|
|
73
92
|
|
|
74
|
-
protected
|
|
75
|
-
return options.
|
|
93
|
+
protected getStateWatchers(): StateWatchers<Service, Unref<State>> {
|
|
94
|
+
return (options.watch ?? {}) as StateWatchers<Service, Unref<State>>;
|
|
76
95
|
}
|
|
77
96
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
97
|
+
protected serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
98
|
+
return options.serialize?.(state) ?? (state as ServiceStorage);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
protected deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
102
|
+
return options.restore?.(state) ?? (state as Partial<State>);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
} as unknown as ServiceWithState<State, ComputedState, ServiceStorage>;
|
|
81
106
|
}
|
|
82
107
|
|
|
83
108
|
export default class Service<
|
|
84
109
|
State extends ServiceState = DefaultServiceState,
|
|
85
110
|
ComputedState extends ServiceState = {},
|
|
86
|
-
ServiceStorage
|
|
111
|
+
ServiceStorage = Partial<State>
|
|
87
112
|
> extends MagicObject {
|
|
88
113
|
|
|
89
114
|
public static persist: string[] = [];
|
|
@@ -91,6 +116,7 @@ export default class Service<
|
|
|
91
116
|
protected _name: string;
|
|
92
117
|
private _booted: PromisedValue<void>;
|
|
93
118
|
private _computedStateKeys: Set<keyof State>;
|
|
119
|
+
private _watchers: StateWatchers<Service, State>;
|
|
94
120
|
private _store: Store<string, State, ComputedState, {}> | false;
|
|
95
121
|
|
|
96
122
|
constructor() {
|
|
@@ -101,6 +127,7 @@ export default class Service<
|
|
|
101
127
|
this._name = this.getName() ?? new.target.name;
|
|
102
128
|
this._booted = new PromisedValue();
|
|
103
129
|
this._computedStateKeys = new Set(Object.keys(getters));
|
|
130
|
+
this._watchers = this.getStateWatchers();
|
|
104
131
|
this._store =
|
|
105
132
|
this.usesStore() &&
|
|
106
133
|
defineServiceStore(this._name, {
|
|
@@ -115,6 +142,12 @@ export default class Service<
|
|
|
115
142
|
return this._booted;
|
|
116
143
|
}
|
|
117
144
|
|
|
145
|
+
public static<T extends typeof Service>(): T;
|
|
146
|
+
public static<T extends typeof Service, K extends keyof T>(property: K): T[K];
|
|
147
|
+
public static<T extends typeof Service, K extends keyof T>(property?: K): T | T[K] {
|
|
148
|
+
return super.static<T, K>(property as K);
|
|
149
|
+
}
|
|
150
|
+
|
|
118
151
|
public launch(): Promise<void> {
|
|
119
152
|
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
120
153
|
|
|
@@ -162,13 +195,28 @@ export default class Service<
|
|
|
162
195
|
return;
|
|
163
196
|
}
|
|
164
197
|
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
) as Partial<State>;
|
|
198
|
+
const update = typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty;
|
|
199
|
+
const old = objectOnly(this._store.$state as State, Object.keys(update));
|
|
168
200
|
|
|
169
|
-
Object.assign(this._store.$state,
|
|
201
|
+
Object.assign(this._store.$state, update);
|
|
202
|
+
this.onStateUpdated(update as Partial<State>, old as Partial<State>);
|
|
203
|
+
}
|
|
170
204
|
|
|
171
|
-
|
|
205
|
+
public updatePersistedState<T extends keyof State>(key: T): void;
|
|
206
|
+
public updatePersistedState<T extends keyof State>(keys: T[]): void;
|
|
207
|
+
public updatePersistedState<T extends keyof State>(keyOrKeys: T | T[]): void {
|
|
208
|
+
if (!this._store) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const keys = arrayFrom(keyOrKeys) as Array<keyof State>;
|
|
213
|
+
const state = objectOnly(this._store.$state as State, keys);
|
|
214
|
+
|
|
215
|
+
if (isEmpty(state)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
this.onPersistentStateUpdated(state);
|
|
172
220
|
}
|
|
173
221
|
|
|
174
222
|
protected __get(property: string): unknown {
|
|
@@ -183,15 +231,25 @@ export default class Service<
|
|
|
183
231
|
this.setState({ [property]: value } as Partial<State>);
|
|
184
232
|
}
|
|
185
233
|
|
|
186
|
-
protected onStateUpdated(
|
|
187
|
-
|
|
188
|
-
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
189
|
-
const persisted = objectOnly(state, persist);
|
|
234
|
+
protected onStateUpdated(update: Partial<State>, old: Partial<State>): void {
|
|
235
|
+
const persisted = objectOnly(update, this.static('persist'));
|
|
190
236
|
|
|
191
|
-
if (isEmpty(persisted)) {
|
|
192
|
-
|
|
237
|
+
if (!isEmpty(persisted)) {
|
|
238
|
+
this.onPersistentStateUpdated(persisted as Partial<State>);
|
|
193
239
|
}
|
|
194
240
|
|
|
241
|
+
for (const property in update) {
|
|
242
|
+
const watcher = this._watchers[property] as Nullable<(value: unknown, oldValue: unknown) => unknown>;
|
|
243
|
+
|
|
244
|
+
if (!watcher || update[property] === old[property]) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
watcher.call(this, update[property], old[property]);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
protected onPersistentStateUpdated(persisted: Partial<State>): void {
|
|
195
253
|
const storage = Storage.get<ServiceStorage>(this._name);
|
|
196
254
|
|
|
197
255
|
if (!storage) {
|
|
@@ -220,34 +278,39 @@ export default class Service<
|
|
|
220
278
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
221
279
|
}
|
|
222
280
|
|
|
223
|
-
protected
|
|
224
|
-
return
|
|
281
|
+
protected getStateWatchers(): StateWatchers<Service, State> {
|
|
282
|
+
return {};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
protected serializePersistedState(state: Partial<State>): ServiceStorage {
|
|
286
|
+
return state as ServiceStorage;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
protected deserializePersistedState(state: ServiceStorage): Partial<State> {
|
|
290
|
+
return state as Partial<State>;
|
|
225
291
|
}
|
|
226
292
|
|
|
227
293
|
protected async frameworkBoot(): Promise<void> {
|
|
228
|
-
this.
|
|
294
|
+
this.restorePersistedState();
|
|
229
295
|
}
|
|
230
296
|
|
|
231
297
|
protected async boot(): Promise<void> {
|
|
232
298
|
// Placeholder for overrides, don't place any functionality here.
|
|
233
299
|
}
|
|
234
300
|
|
|
235
|
-
protected
|
|
236
|
-
|
|
237
|
-
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
238
|
-
|
|
239
|
-
if (!this.usesStore() || isEmpty(persist)) {
|
|
301
|
+
protected restorePersistedState(): void {
|
|
302
|
+
if (!this.usesStore() || isEmpty(this.static('persist'))) {
|
|
240
303
|
return;
|
|
241
304
|
}
|
|
242
305
|
|
|
243
306
|
if (Storage.has(this._name)) {
|
|
244
307
|
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
245
|
-
this.setState(persisted);
|
|
308
|
+
this.setState(this.deserializePersistedState(persisted));
|
|
246
309
|
|
|
247
310
|
return;
|
|
248
311
|
}
|
|
249
312
|
|
|
250
|
-
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
313
|
+
Storage.set(this._name, objectOnly(this.getState(), this.static('persist')));
|
|
251
314
|
}
|
|
252
315
|
|
|
253
316
|
protected requireStore(): Store<string, State, ComputedState, {}> {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
import Service from '@/services/Service';
|
|
5
|
+
|
|
6
|
+
export class StorageService extends Service {
|
|
7
|
+
|
|
8
|
+
public async purge(): Promise<void> {
|
|
9
|
+
await Events.emit('purge-storage');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default facade(StorageService);
|
|
15
|
+
|
|
16
|
+
declare module '@/services/Events' {
|
|
17
|
+
export interface EventsPayload {
|
|
18
|
+
'purge-storage': void;
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ import App from './App';
|
|
|
6
6
|
import Cache from './Cache';
|
|
7
7
|
import Events from './Events';
|
|
8
8
|
import Service from './Service';
|
|
9
|
+
import Storage from './Storage';
|
|
9
10
|
import { getPiniaStore } from './store';
|
|
10
11
|
|
|
11
12
|
export * from './App';
|
|
@@ -13,12 +14,14 @@ export * from './Cache';
|
|
|
13
14
|
export * from './Events';
|
|
14
15
|
export * from './Service';
|
|
15
16
|
export * from './store';
|
|
17
|
+
export * from './utils';
|
|
16
18
|
|
|
17
|
-
export { App, Cache, Events, Service };
|
|
19
|
+
export { App, Cache, Events, Storage, Service };
|
|
18
20
|
|
|
19
21
|
const defaultServices = {
|
|
20
22
|
$app: App,
|
|
21
23
|
$events: Events,
|
|
24
|
+
$storage: Storage,
|
|
22
25
|
};
|
|
23
26
|
|
|
24
27
|
export type DefaultServices = typeof defaultServices;
|
|
@@ -36,7 +39,9 @@ export async function bootServices(app: VueApp, services: Record<string, Service
|
|
|
36
39
|
|
|
37
40
|
Object.assign(app.config.globalProperties, services);
|
|
38
41
|
|
|
39
|
-
App.development
|
|
42
|
+
if (App.development || App.testing) {
|
|
43
|
+
Object.assign(globalThis, services);
|
|
44
|
+
}
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
export default definePlugin({
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { objectOnly } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
export type Replace<
|
|
4
|
+
TOriginal extends Record<string, unknown>,
|
|
5
|
+
TReplacements extends Partial<Record<keyof TOriginal, unknown>>
|
|
6
|
+
> = {
|
|
7
|
+
[K in keyof TOriginal]: TReplacements extends Record<K, infer Replacement> ? Replacement : TOriginal[K];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function replaceExisting<
|
|
11
|
+
TOriginal extends Record<string, unknown>,
|
|
12
|
+
TReplacements extends Partial<Record<keyof TOriginal, unknown>>
|
|
13
|
+
>(original: TOriginal, replacements: TReplacements): Replace<TOriginal, TReplacements> {
|
|
14
|
+
return {
|
|
15
|
+
...original,
|
|
16
|
+
...objectOnly(replacements, Object.keys(original)),
|
|
17
|
+
} as Replace<TOriginal, TReplacements>;
|
|
18
|
+
}
|
package/src/testing/setup.ts
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
import { mock, tap } from '@noeldemartin/utils';
|
|
1
|
+
import { mock, tap, toString } from '@noeldemartin/utils';
|
|
2
2
|
import { beforeEach, vi } from 'vitest';
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
5
|
tap(globalThis, (global: any) => {
|
|
6
|
+
const localStorage: Record<string, string> = {};
|
|
7
|
+
|
|
6
8
|
global.jest = vi;
|
|
7
9
|
global.navigator = { languages: ['en'] };
|
|
8
10
|
global.localStorage = mock<Storage>({
|
|
9
|
-
getItem: () => null,
|
|
10
|
-
setItem
|
|
11
|
+
getItem: (key) => localStorage[key] ?? null,
|
|
12
|
+
setItem(key, value) {
|
|
13
|
+
localStorage[key] = toString(value);
|
|
14
|
+
},
|
|
11
15
|
});
|
|
12
16
|
});
|
|
13
17
|
|
|
18
|
+
vi.mock('dompurify', async () => {
|
|
19
|
+
return { default: { sanitize: (html: string) => html } };
|
|
20
|
+
});
|
|
21
|
+
|
|
14
22
|
beforeEach(() => {
|
|
15
23
|
vi.stubGlobal('document', {
|
|
16
24
|
querySelector: () => null,
|