@hyper-fetch/core 1.6.0
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/.eslintrc.json +7 -0
- package/README.md +83 -0
- package/dist/builder/builder.constants.d.ts +9 -0
- package/dist/builder/builder.d.ts +141 -0
- package/dist/builder/builder.types.d.ts +45 -0
- package/dist/builder/builder.utils.d.ts +10 -0
- package/dist/builder/index.d.ts +4 -0
- package/dist/cache/cache.d.ts +76 -0
- package/dist/cache/cache.events.d.ts +28 -0
- package/dist/cache/cache.types.d.ts +49 -0
- package/dist/cache/cache.utils.d.ts +6 -0
- package/dist/cache/index.d.ts +4 -0
- package/dist/client/fetch.client.bindings.d.ts +25 -0
- package/dist/client/fetch.client.constants.d.ts +1 -0
- package/dist/client/fetch.client.d.ts +2 -0
- package/dist/client/fetch.client.types.d.ts +57 -0
- package/dist/client/fetch.client.utils.d.ts +5 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/command/command.d.ts +87 -0
- package/dist/command/command.types.d.ts +196 -0
- package/dist/command/command.utils.d.ts +22 -0
- package/dist/command/index.d.ts +3 -0
- package/dist/constants/http.constants.d.ts +7 -0
- package/dist/constants/time.constants.d.ts +11 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +6 -0
- package/dist/dispatcher/dispatcher.d.ts +155 -0
- package/dist/dispatcher/dispatcher.events.d.ts +11 -0
- package/dist/dispatcher/dispatcher.types.d.ts +31 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +10 -0
- package/dist/dispatcher/index.d.ts +5 -0
- package/dist/effect/fetch.effect.d.ts +14 -0
- package/dist/effect/fetch.effect.types.d.ts +32 -0
- package/dist/effect/index.d.ts +2 -0
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/managers/app/app.manager.constants.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.events.d.ts +11 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.utils.d.ts +4 -0
- package/dist/managers/app/index.d.ts +5 -0
- package/dist/managers/command/command.manager.d.ts +43 -0
- package/dist/managers/command/command.manager.events.d.ts +36 -0
- package/dist/managers/command/command.manager.types.d.ts +23 -0
- package/dist/managers/command/command.manager.utils.d.ts +16 -0
- package/dist/managers/command/index.d.ts +4 -0
- package/dist/managers/index.d.ts +3 -0
- package/dist/managers/logger/index.d.ts +4 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +4 -0
- package/dist/managers/logger/logger.manager.d.ts +19 -0
- package/dist/managers/logger/logger.manager.types.d.ts +30 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +3 -0
- package/dist/types/fetch.types.d.ts +15 -0
- package/dist/types/helpers.types.d.ts +11 -0
- package/dist/types/http.types.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/uuid.utils.d.ts +1 -0
- package/package.json +60 -0
- package/src/builder/builder.constants.ts +9 -0
- package/src/builder/builder.ts +309 -0
- package/src/builder/builder.types.ts +57 -0
- package/src/builder/builder.utils.ts +178 -0
- package/src/builder/index.ts +4 -0
- package/src/cache/cache.events.ts +44 -0
- package/src/cache/cache.ts +221 -0
- package/src/cache/cache.types.ts +56 -0
- package/src/cache/cache.utils.ts +29 -0
- package/src/cache/index.ts +4 -0
- package/src/client/fetch.client.bindings.ts +290 -0
- package/src/client/fetch.client.constants.ts +3 -0
- package/src/client/fetch.client.ts +98 -0
- package/src/client/fetch.client.types.ts +83 -0
- package/src/client/fetch.client.utils.ts +28 -0
- package/src/client/index.ts +5 -0
- package/src/command/command.ts +539 -0
- package/src/command/command.types.ts +268 -0
- package/src/command/command.utils.ts +179 -0
- package/src/command/index.ts +3 -0
- package/src/constants/http.constants.ts +7 -0
- package/src/constants/time.constants.ts +11 -0
- package/src/dispatcher/dispatcher.constants.ts +6 -0
- package/src/dispatcher/dispatcher.events.ts +42 -0
- package/src/dispatcher/dispatcher.ts +596 -0
- package/src/dispatcher/dispatcher.types.ts +39 -0
- package/src/dispatcher/dispatcher.utils.ts +55 -0
- package/src/dispatcher/index.ts +5 -0
- package/src/effect/fetch.effect.ts +28 -0
- package/src/effect/fetch.effect.types.ts +35 -0
- package/src/effect/index.ts +2 -0
- package/src/index.ts +12 -0
- package/src/managers/app/app.manager.constants.ts +24 -0
- package/src/managers/app/app.manager.events.ts +34 -0
- package/src/managers/app/app.manager.ts +74 -0
- package/src/managers/app/app.manager.types.ts +8 -0
- package/src/managers/app/app.manager.utils.ts +34 -0
- package/src/managers/app/index.ts +5 -0
- package/src/managers/command/command.manager.events.ts +212 -0
- package/src/managers/command/command.manager.ts +68 -0
- package/src/managers/command/command.manager.types.ts +25 -0
- package/src/managers/command/command.manager.utils.ts +19 -0
- package/src/managers/command/index.ts +4 -0
- package/src/managers/index.ts +3 -0
- package/src/managers/logger/index.ts +4 -0
- package/src/managers/logger/logger.manager.constants.ts +24 -0
- package/src/managers/logger/logger.manager.ts +70 -0
- package/src/managers/logger/logger.manager.types.ts +35 -0
- package/src/managers/logger/logger.manager.utils.ts +27 -0
- package/src/types/fetch.types.ts +39 -0
- package/src/types/helpers.types.ts +15 -0
- package/src/types/http.types.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +3 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ExtractError } from "types";
|
|
2
|
+
import { FetchEffectConfig } from "effect";
|
|
3
|
+
import { CommandInstance } from "command";
|
|
4
|
+
import { ClientResponseErrorType, ClientResponseType, ClientResponseSuccessType } from "client";
|
|
5
|
+
|
|
6
|
+
export class FetchEffect<T extends CommandInstance> {
|
|
7
|
+
constructor(public config: FetchEffectConfig<T>) {}
|
|
8
|
+
|
|
9
|
+
getEffectKey = () => {
|
|
10
|
+
return this.config.effectKey;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
onTrigger = (command: T) => {
|
|
14
|
+
this.config.onTrigger?.(command);
|
|
15
|
+
};
|
|
16
|
+
onStart = (command: T) => {
|
|
17
|
+
this.config.onStart?.(command);
|
|
18
|
+
};
|
|
19
|
+
onSuccess = (response: ClientResponseSuccessType<ResponseType>, command: T) => {
|
|
20
|
+
this.config.onSuccess?.(response, command);
|
|
21
|
+
};
|
|
22
|
+
onError = (response: ClientResponseErrorType<ExtractError<T>>, command: T) => {
|
|
23
|
+
this.config.onError?.(response, command);
|
|
24
|
+
};
|
|
25
|
+
onFinished = (response: ClientResponseType<ResponseType, ExtractError<T>>, command: T) => {
|
|
26
|
+
this.config.onFinished?.(response, command);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { CommandInstance } from "command";
|
|
2
|
+
import { ClientResponseErrorType, ClientResponseType, ClientResponseSuccessType } from "client";
|
|
3
|
+
import { FetchEffect } from "effect";
|
|
4
|
+
import { ExtractError } from "types";
|
|
5
|
+
|
|
6
|
+
export type FetchEffectLifecycle = "trigger" | "start" | "success" | "error" | "finished";
|
|
7
|
+
|
|
8
|
+
export type FetchEffectInstance = FetchEffect<CommandInstance>;
|
|
9
|
+
|
|
10
|
+
export type FetchEffectConfig<T extends CommandInstance> = {
|
|
11
|
+
/**
|
|
12
|
+
* It should match effectKey on the command for which given effect should be triggered.
|
|
13
|
+
*/
|
|
14
|
+
effectKey: string;
|
|
15
|
+
/**
|
|
16
|
+
* Callback that will be executed when request gets triggered
|
|
17
|
+
*/
|
|
18
|
+
onTrigger?: (command: CommandInstance) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Callback that will be executed when request starts
|
|
21
|
+
*/
|
|
22
|
+
onStart?: (command: CommandInstance) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Callback that will be executed when response is successful
|
|
25
|
+
*/
|
|
26
|
+
onSuccess?: (response: ClientResponseSuccessType<ResponseType>, command: CommandInstance) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Callback that will be executed when response is failed
|
|
29
|
+
*/
|
|
30
|
+
onError?: (response: ClientResponseErrorType<ExtractError<T>>, command: CommandInstance) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Callback that will be executed when response is finished
|
|
33
|
+
*/
|
|
34
|
+
onFinished?: (response: ClientResponseType<ResponseType, ExtractError<T>>, command: CommandInstance) => void;
|
|
35
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// rollup builder entry point
|
|
2
|
+
|
|
3
|
+
export * from "./effect";
|
|
4
|
+
export * from "./builder";
|
|
5
|
+
export * from "./cache";
|
|
6
|
+
export * from "./client";
|
|
7
|
+
export * from "./command";
|
|
8
|
+
export * from "./constants/time.constants";
|
|
9
|
+
export * from "./constants/http.constants";
|
|
10
|
+
export * from "./managers";
|
|
11
|
+
export * from "./dispatcher";
|
|
12
|
+
export * from "./types";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { RequiredKeys } from "types";
|
|
2
|
+
import { AppManagerOptionsType } from "managers";
|
|
3
|
+
import { onWindowEvent, onDocumentEvent } from "./app.manager.utils";
|
|
4
|
+
|
|
5
|
+
export enum AppEvents {
|
|
6
|
+
focus = "focus",
|
|
7
|
+
blur = "blur",
|
|
8
|
+
online = "online",
|
|
9
|
+
offline = "offline",
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const appManagerInitialOptions: RequiredKeys<AppManagerOptionsType> = {
|
|
13
|
+
initiallyFocused: true,
|
|
14
|
+
initiallyOnline: true,
|
|
15
|
+
focusEvent: (setFocused) => {
|
|
16
|
+
onDocumentEvent("visibilitychange", () => setFocused(true));
|
|
17
|
+
onWindowEvent("focus", () => setFocused(true));
|
|
18
|
+
onWindowEvent("blur", () => setFocused(false));
|
|
19
|
+
},
|
|
20
|
+
onlineEvent: (setOnline) => {
|
|
21
|
+
onWindowEvent("online", () => setOnline(true));
|
|
22
|
+
onWindowEvent("offline", () => setOnline(false));
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import EventEmitter from "events";
|
|
2
|
+
|
|
3
|
+
import { AppEvents } from "managers";
|
|
4
|
+
|
|
5
|
+
export const getAppManagerEvents = (emitter: EventEmitter) => ({
|
|
6
|
+
emitFocus: (): void => {
|
|
7
|
+
emitter.emit(AppEvents.focus);
|
|
8
|
+
},
|
|
9
|
+
emitBlur: (): void => {
|
|
10
|
+
emitter.emit(AppEvents.blur);
|
|
11
|
+
},
|
|
12
|
+
emitOnline: (): void => {
|
|
13
|
+
emitter.emit(AppEvents.online);
|
|
14
|
+
},
|
|
15
|
+
emitOffline: (): void => {
|
|
16
|
+
emitter.emit(AppEvents.offline);
|
|
17
|
+
},
|
|
18
|
+
onFocus: (callback: () => void): VoidFunction => {
|
|
19
|
+
emitter.on(AppEvents.focus, callback);
|
|
20
|
+
return () => emitter.removeListener(AppEvents.focus, callback);
|
|
21
|
+
},
|
|
22
|
+
onBlur: (callback: () => void): VoidFunction => {
|
|
23
|
+
emitter.on(AppEvents.blur, callback);
|
|
24
|
+
return () => emitter.removeListener(AppEvents.blur, callback);
|
|
25
|
+
},
|
|
26
|
+
onOnline: (callback: () => void): VoidFunction => {
|
|
27
|
+
emitter.on(AppEvents.online, callback);
|
|
28
|
+
return () => emitter.removeListener(AppEvents.online, callback);
|
|
29
|
+
},
|
|
30
|
+
onOffline: (callback: () => void): VoidFunction => {
|
|
31
|
+
emitter.on(AppEvents.offline, callback);
|
|
32
|
+
return () => emitter.removeListener(AppEvents.offline, callback);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import EventEmitter from "events";
|
|
2
|
+
|
|
3
|
+
import { BuilderInstance } from "builder";
|
|
4
|
+
import { appManagerInitialOptions, AppManagerOptionsType, getAppManagerEvents } from "managers";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* App manager handles main application states - focus and online. Those two values can answer questions:
|
|
8
|
+
* - Is the tab or current view instance focused and visible for user?
|
|
9
|
+
* - Is our application online or offline?
|
|
10
|
+
* With the app manager it is not a problem to get the valid answer for this question.
|
|
11
|
+
*
|
|
12
|
+
* @caution
|
|
13
|
+
* Make sure to apply valid focus/online handlers for different environments like for example for native mobile applications.
|
|
14
|
+
*/
|
|
15
|
+
export class AppManager {
|
|
16
|
+
emitter = new EventEmitter();
|
|
17
|
+
events = getAppManagerEvents(this.emitter);
|
|
18
|
+
|
|
19
|
+
isOnline: boolean;
|
|
20
|
+
isFocused: boolean;
|
|
21
|
+
|
|
22
|
+
constructor(public builder: BuilderInstance, public options?: AppManagerOptionsType) {
|
|
23
|
+
const {
|
|
24
|
+
focusEvent = appManagerInitialOptions.focusEvent,
|
|
25
|
+
onlineEvent = appManagerInitialOptions.onlineEvent,
|
|
26
|
+
initiallyFocused = appManagerInitialOptions.initiallyFocused,
|
|
27
|
+
initiallyOnline = appManagerInitialOptions.initiallyOnline,
|
|
28
|
+
} = this.options || appManagerInitialOptions;
|
|
29
|
+
|
|
30
|
+
this.setInitialFocus(initiallyFocused);
|
|
31
|
+
this.setInitialOnline(initiallyOnline);
|
|
32
|
+
|
|
33
|
+
focusEvent(this.setFocused, builder);
|
|
34
|
+
onlineEvent(this.setOnline, builder);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private setInitialFocus = async (initValue: Exclude<AppManagerOptionsType["initiallyFocused"], undefined>) => {
|
|
38
|
+
if (typeof initValue === "function") {
|
|
39
|
+
this.isFocused = false;
|
|
40
|
+
this.isFocused = await initValue();
|
|
41
|
+
} else {
|
|
42
|
+
this.isFocused = initValue;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
private setInitialOnline = async (initValue: Exclude<AppManagerOptionsType["initiallyOnline"], undefined>) => {
|
|
47
|
+
if (typeof initValue === "function") {
|
|
48
|
+
this.isOnline = false;
|
|
49
|
+
this.isOnline = await initValue();
|
|
50
|
+
} else {
|
|
51
|
+
this.isOnline = initValue;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
setFocused = (isFocused: boolean) => {
|
|
56
|
+
this.isFocused = isFocused;
|
|
57
|
+
|
|
58
|
+
if (isFocused) {
|
|
59
|
+
this.events.emitFocus();
|
|
60
|
+
} else {
|
|
61
|
+
this.events.emitBlur();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
setOnline = (isOnline: boolean) => {
|
|
66
|
+
this.isOnline = isOnline;
|
|
67
|
+
|
|
68
|
+
if (isOnline) {
|
|
69
|
+
this.events.emitOnline();
|
|
70
|
+
} else {
|
|
71
|
+
this.events.emitOffline();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BuilderInstance } from "builder";
|
|
2
|
+
|
|
3
|
+
export type AppManagerOptionsType = {
|
|
4
|
+
initiallyFocused?: boolean | (() => boolean | Promise<boolean>);
|
|
5
|
+
initiallyOnline?: boolean | (() => boolean | Promise<boolean>);
|
|
6
|
+
focusEvent?: (setFocused: (isFocused: boolean) => void, builder: BuilderInstance) => void;
|
|
7
|
+
onlineEvent?: (setOnline: (isOnline: boolean) => void, builder: BuilderInstance) => void;
|
|
8
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const hasWindow = () => {
|
|
2
|
+
try {
|
|
3
|
+
return Boolean(window && window.addEventListener);
|
|
4
|
+
} catch (err) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
export const hasDocument = () => {
|
|
9
|
+
try {
|
|
10
|
+
return Boolean(hasWindow() && window.document && window.document.addEventListener);
|
|
11
|
+
} catch (err) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const onWindowEvent = <K extends keyof WindowEventMap>(
|
|
17
|
+
key: K,
|
|
18
|
+
listener: (this: Window, ev: WindowEventMap[K]) => any,
|
|
19
|
+
options?: boolean | AddEventListenerOptions | undefined,
|
|
20
|
+
) => {
|
|
21
|
+
if (hasWindow()) {
|
|
22
|
+
window.addEventListener(key, listener, options);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const onDocumentEvent = <K extends keyof DocumentEventMap>(
|
|
27
|
+
key: K,
|
|
28
|
+
listener: (this: Document, ev: DocumentEventMap[K]) => any,
|
|
29
|
+
options?: boolean | AddEventListenerOptions | undefined,
|
|
30
|
+
) => {
|
|
31
|
+
if (hasDocument()) {
|
|
32
|
+
window.document.addEventListener(key, listener, options);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import EventEmitter from "events";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getRequestStartEventKey,
|
|
5
|
+
getResponseStartEventKey,
|
|
6
|
+
getDownloadProgressEventKey,
|
|
7
|
+
getUploadProgressEventKey,
|
|
8
|
+
getResponseIdEventKey,
|
|
9
|
+
getAbortEventKey,
|
|
10
|
+
getAbortByIdEventKey,
|
|
11
|
+
getResponseEventKey,
|
|
12
|
+
getUploadProgressIdEventKey,
|
|
13
|
+
getDownloadProgressIdEventKey,
|
|
14
|
+
getResponseStartIdEventKey,
|
|
15
|
+
getRequestStartIdEventKey,
|
|
16
|
+
getLoadingEventKey,
|
|
17
|
+
getLoadingIdEventKey,
|
|
18
|
+
CommandEventDetails,
|
|
19
|
+
CommandResponseDetails,
|
|
20
|
+
CommandLoadingEventType,
|
|
21
|
+
getRemoveEventKey,
|
|
22
|
+
getRemoveIdEventKey,
|
|
23
|
+
} from "managers";
|
|
24
|
+
import { FetchProgressType, ClientResponseType } from "client";
|
|
25
|
+
import { CommandInstance } from "command";
|
|
26
|
+
|
|
27
|
+
export const getCommandManagerEvents = (emitter: EventEmitter) => ({
|
|
28
|
+
/**
|
|
29
|
+
* Emiter
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
// Loading
|
|
33
|
+
emitLoading: (queueKey: string, requestId: string, values: CommandLoadingEventType): void => {
|
|
34
|
+
emitter.emit(getLoadingIdEventKey(requestId), values);
|
|
35
|
+
emitter.emit(getLoadingEventKey(queueKey), values);
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
// Start
|
|
39
|
+
emitRequestStart: (queueKey: string, requestId: string, details: CommandEventDetails<CommandInstance>): void => {
|
|
40
|
+
emitter.emit(getRequestStartIdEventKey(requestId), details);
|
|
41
|
+
emitter.emit(getRequestStartEventKey(queueKey), details);
|
|
42
|
+
},
|
|
43
|
+
emitResponseStart: (queueKey: string, requestId: string, details: CommandEventDetails<CommandInstance>): void => {
|
|
44
|
+
emitter.emit(getResponseStartIdEventKey(requestId), details);
|
|
45
|
+
emitter.emit(getResponseStartEventKey(queueKey), details);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
// Progress
|
|
49
|
+
emitUploadProgress: (
|
|
50
|
+
queueKey: string,
|
|
51
|
+
requestId: string,
|
|
52
|
+
values: FetchProgressType,
|
|
53
|
+
details: CommandEventDetails<CommandInstance>,
|
|
54
|
+
): void => {
|
|
55
|
+
emitter.emit(getUploadProgressIdEventKey(requestId), values, details);
|
|
56
|
+
emitter.emit(getUploadProgressEventKey(queueKey), values, details);
|
|
57
|
+
},
|
|
58
|
+
emitDownloadProgress: (
|
|
59
|
+
queueKey: string,
|
|
60
|
+
requestId: string,
|
|
61
|
+
values: FetchProgressType,
|
|
62
|
+
details: CommandEventDetails<CommandInstance>,
|
|
63
|
+
): void => {
|
|
64
|
+
emitter.emit(getDownloadProgressIdEventKey(requestId), values, details);
|
|
65
|
+
emitter.emit(getDownloadProgressEventKey(queueKey), values, details);
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
// Response
|
|
69
|
+
emitResponse: (
|
|
70
|
+
cacheKey: string,
|
|
71
|
+
requestId: string,
|
|
72
|
+
response: ClientResponseType<unknown, unknown>,
|
|
73
|
+
details: CommandResponseDetails,
|
|
74
|
+
): void => {
|
|
75
|
+
emitter.emit(getResponseIdEventKey(requestId), response, details);
|
|
76
|
+
emitter.emit(getResponseEventKey(cacheKey), response, details);
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
// Abort
|
|
80
|
+
emitAbort: (abortKey: string, requestId: string, command: CommandInstance): void => {
|
|
81
|
+
emitter.emit(getAbortByIdEventKey(requestId), command);
|
|
82
|
+
emitter.emit(getAbortEventKey(abortKey), command);
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
// Remove
|
|
86
|
+
emitRemove: <T extends CommandInstance>(
|
|
87
|
+
queueKey: string,
|
|
88
|
+
requestId: string,
|
|
89
|
+
details: CommandEventDetails<T>,
|
|
90
|
+
): void => {
|
|
91
|
+
emitter.emit(getRemoveEventKey(queueKey), details);
|
|
92
|
+
emitter.emit(getRemoveIdEventKey(requestId), details);
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Listeners
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
// Loading
|
|
100
|
+
onLoading: (queueKey: string, callback: (values: CommandLoadingEventType) => void): VoidFunction => {
|
|
101
|
+
emitter.on(getLoadingEventKey(queueKey), callback);
|
|
102
|
+
return () => emitter.removeListener(getLoadingEventKey(queueKey), callback);
|
|
103
|
+
},
|
|
104
|
+
onLoadingById: (requestId: string, callback: (values: CommandLoadingEventType) => void): VoidFunction => {
|
|
105
|
+
emitter.on(getLoadingIdEventKey(requestId), callback);
|
|
106
|
+
return () => emitter.removeListener(getLoadingIdEventKey(requestId), callback);
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
// Request Start
|
|
110
|
+
onRequestStart: <T extends CommandInstance>(
|
|
111
|
+
queueKey: string,
|
|
112
|
+
callback: (details: CommandEventDetails<T>) => void,
|
|
113
|
+
): VoidFunction => {
|
|
114
|
+
emitter.on(getRequestStartEventKey(queueKey), callback);
|
|
115
|
+
return () => emitter.removeListener(getRequestStartEventKey(queueKey), callback);
|
|
116
|
+
},
|
|
117
|
+
onRequestStartById: <T extends CommandInstance>(
|
|
118
|
+
requestId: string,
|
|
119
|
+
callback: (details: CommandEventDetails<T>) => void,
|
|
120
|
+
): VoidFunction => {
|
|
121
|
+
emitter.on(getRequestStartIdEventKey(requestId), callback);
|
|
122
|
+
return () => emitter.removeListener(getRequestStartIdEventKey(requestId), callback);
|
|
123
|
+
},
|
|
124
|
+
// Response Start
|
|
125
|
+
onResponseStart: <T extends CommandInstance>(
|
|
126
|
+
queueKey: string,
|
|
127
|
+
callback: (details: CommandEventDetails<T>) => void,
|
|
128
|
+
): VoidFunction => {
|
|
129
|
+
emitter.on(getResponseStartEventKey(queueKey), callback);
|
|
130
|
+
return () => emitter.removeListener(getResponseStartEventKey(queueKey), callback);
|
|
131
|
+
},
|
|
132
|
+
onResponseStartById: <T extends CommandInstance>(
|
|
133
|
+
requestId: string,
|
|
134
|
+
callback: (details: CommandEventDetails<T>) => void,
|
|
135
|
+
): VoidFunction => {
|
|
136
|
+
emitter.on(getResponseStartIdEventKey(requestId), callback);
|
|
137
|
+
return () => emitter.removeListener(getResponseStartIdEventKey(requestId), callback);
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
// Progress
|
|
141
|
+
onUploadProgress: <T extends CommandInstance = CommandInstance>(
|
|
142
|
+
queueKey: string,
|
|
143
|
+
callback: (values: FetchProgressType, details: CommandEventDetails<T>) => void,
|
|
144
|
+
): VoidFunction => {
|
|
145
|
+
emitter.on(getUploadProgressEventKey(queueKey), callback);
|
|
146
|
+
return () => emitter.removeListener(getUploadProgressEventKey(queueKey), callback);
|
|
147
|
+
},
|
|
148
|
+
onUploadProgressById: <T extends CommandInstance = CommandInstance>(
|
|
149
|
+
requestId: string,
|
|
150
|
+
callback: (values: FetchProgressType, details: CommandEventDetails<T>) => void,
|
|
151
|
+
): VoidFunction => {
|
|
152
|
+
emitter.on(getUploadProgressIdEventKey(requestId), callback);
|
|
153
|
+
return () => emitter.removeListener(getUploadProgressIdEventKey(requestId), callback);
|
|
154
|
+
},
|
|
155
|
+
onDownloadProgress: <T extends CommandInstance = CommandInstance>(
|
|
156
|
+
queueKey: string,
|
|
157
|
+
callback: (values: FetchProgressType, details: CommandEventDetails<T>) => void,
|
|
158
|
+
): VoidFunction => {
|
|
159
|
+
emitter.on(getDownloadProgressEventKey(queueKey), callback);
|
|
160
|
+
return () => emitter.removeListener(getDownloadProgressEventKey(queueKey), callback);
|
|
161
|
+
},
|
|
162
|
+
onDownloadProgressById: <T extends CommandInstance = CommandInstance>(
|
|
163
|
+
requestId: string,
|
|
164
|
+
callback: (values: FetchProgressType, details: CommandEventDetails<T>) => void,
|
|
165
|
+
): VoidFunction => {
|
|
166
|
+
emitter.on(getDownloadProgressIdEventKey(requestId), callback);
|
|
167
|
+
return () => emitter.removeListener(getDownloadProgressIdEventKey(requestId), callback);
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
// Response
|
|
171
|
+
onResponse: <ResponseType, ErrorType>(
|
|
172
|
+
cacheKey: string,
|
|
173
|
+
callback: (response: ClientResponseType<ResponseType, ErrorType>, details: CommandResponseDetails) => void,
|
|
174
|
+
): VoidFunction => {
|
|
175
|
+
emitter.on(getResponseEventKey(cacheKey), callback);
|
|
176
|
+
return () => emitter.removeListener(getResponseEventKey(cacheKey), callback);
|
|
177
|
+
},
|
|
178
|
+
// Response by requestId
|
|
179
|
+
onResponseById: <ResponseType, ErrorType>(
|
|
180
|
+
requestId: string,
|
|
181
|
+
callback: (response: ClientResponseType<ResponseType, ErrorType>, details: CommandResponseDetails) => void,
|
|
182
|
+
): VoidFunction => {
|
|
183
|
+
emitter.on(getResponseIdEventKey(requestId), callback);
|
|
184
|
+
return () => emitter.removeListener(getResponseIdEventKey(requestId), callback);
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
// Abort
|
|
188
|
+
onAbort: (abortKey: string, callback: (command: CommandInstance) => void): VoidFunction => {
|
|
189
|
+
emitter.on(getAbortEventKey(abortKey), callback);
|
|
190
|
+
return () => emitter.removeListener(getAbortEventKey(abortKey), callback);
|
|
191
|
+
},
|
|
192
|
+
onAbortById: (requestId: string, callback: (command: CommandInstance) => void): VoidFunction => {
|
|
193
|
+
emitter.on(getAbortByIdEventKey(requestId), callback);
|
|
194
|
+
return () => emitter.removeListener(getAbortByIdEventKey(requestId), callback);
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
// Remove
|
|
198
|
+
onRemove: <T extends CommandInstance = CommandInstance>(
|
|
199
|
+
queueKey: string,
|
|
200
|
+
callback: (details: CommandEventDetails<T>) => void,
|
|
201
|
+
): VoidFunction => {
|
|
202
|
+
emitter.on(getRemoveEventKey(queueKey), callback);
|
|
203
|
+
return () => emitter.removeListener(getRemoveEventKey(queueKey), callback);
|
|
204
|
+
},
|
|
205
|
+
onRemoveById: <T extends CommandInstance = CommandInstance>(
|
|
206
|
+
requestId: string,
|
|
207
|
+
callback: (details: CommandEventDetails<T>) => void,
|
|
208
|
+
): VoidFunction => {
|
|
209
|
+
emitter.on(getRemoveIdEventKey(requestId), callback);
|
|
210
|
+
return () => emitter.removeListener(getRemoveIdEventKey(requestId), callback);
|
|
211
|
+
},
|
|
212
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import EventEmitter from "events";
|
|
2
|
+
|
|
3
|
+
import { getCommandManagerEvents } from "managers";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* **Command Manager** is used to emit `command lifecycle events` like - request start, request end, upload and download progress.
|
|
7
|
+
* It is also the place of `request aborting` system, here we store all the keys and controllers that are isolated for each builder instance.
|
|
8
|
+
*/
|
|
9
|
+
export class CommandManager {
|
|
10
|
+
emitter = new EventEmitter();
|
|
11
|
+
events = getCommandManagerEvents(this.emitter);
|
|
12
|
+
|
|
13
|
+
abortControllers = new Map<string, Map<string, AbortController>>();
|
|
14
|
+
|
|
15
|
+
addAbortController = (abortKey: string, requestId: string) => {
|
|
16
|
+
let abortGroup = this.abortControllers.get(abortKey);
|
|
17
|
+
if (!abortGroup) {
|
|
18
|
+
const newAbortGroup = new Map();
|
|
19
|
+
abortGroup = newAbortGroup;
|
|
20
|
+
this.abortControllers.set(abortKey, newAbortGroup);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const abortController = abortGroup.get(requestId);
|
|
24
|
+
if (!abortController || abortController.signal.aborted) {
|
|
25
|
+
abortGroup.set(requestId, new AbortController());
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
getAbortController = (abortKey: string, requestId: string) => {
|
|
30
|
+
return this.abortControllers.get(abortKey)?.get(requestId);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
removeAbortController = (abortKey: string, requestId: string) => {
|
|
34
|
+
this.abortControllers.get(abortKey)?.delete(requestId);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Aborting
|
|
38
|
+
|
|
39
|
+
useAbortController = (abortKey: string, requestId: string) => {
|
|
40
|
+
const controller = this.abortControllers.get(abortKey)?.get(requestId);
|
|
41
|
+
controller?.abort();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
abortByKey = (abortKey: string) => {
|
|
45
|
+
const controllers = this.abortControllers.get(abortKey);
|
|
46
|
+
|
|
47
|
+
if (controllers) {
|
|
48
|
+
const entries = Array.from(controllers.entries());
|
|
49
|
+
entries.forEach(([key]) => {
|
|
50
|
+
this.useAbortController(abortKey, key);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
abortByRequestId = (abortKey: string, requestId: string) => {
|
|
56
|
+
this.useAbortController(abortKey, requestId);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
abortAll = () => {
|
|
60
|
+
const entries = Array.from(this.abortControllers.entries());
|
|
61
|
+
entries.forEach(([abortKey, value]) => {
|
|
62
|
+
const controllers = Array.from(value.entries());
|
|
63
|
+
controllers.forEach(([requestId]) => {
|
|
64
|
+
this.useAbortController(abortKey, requestId);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { CommandInstance } from "command";
|
|
2
|
+
|
|
3
|
+
// Events
|
|
4
|
+
export type CommandLoadingEventType = {
|
|
5
|
+
queueKey: string;
|
|
6
|
+
requestId: string;
|
|
7
|
+
loading: boolean;
|
|
8
|
+
isRetry: boolean;
|
|
9
|
+
isOffline: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type CommandEventDetails<T extends CommandInstance> = {
|
|
13
|
+
requestId: string;
|
|
14
|
+
command: T;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type CommandResponseDetails = {
|
|
18
|
+
retries: number;
|
|
19
|
+
timestamp: number;
|
|
20
|
+
isFailed: boolean;
|
|
21
|
+
isCanceled: boolean;
|
|
22
|
+
isOffline: boolean;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type CommandRemoveDetails = { queueKey: string; requestId: string };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Events
|
|
2
|
+
|
|
3
|
+
export const getLoadingEventKey = (key: string): string => `${key}-loading-event`;
|
|
4
|
+
export const getLoadingIdEventKey = (key: string): string => `${key}-loading-event-by-id`;
|
|
5
|
+
export const getRemoveEventKey = (key: string): string => `${key}-remove-event`;
|
|
6
|
+
export const getRemoveIdEventKey = (key: string): string => `${key}-remove-event-by-id`;
|
|
7
|
+
|
|
8
|
+
export const getAbortEventKey = (key: string) => `${key}-request-abort`;
|
|
9
|
+
export const getAbortByIdEventKey = (key: string) => `${key}-request-abort-by-id`;
|
|
10
|
+
export const getResponseEventKey = (key: string) => `${key}-response`;
|
|
11
|
+
export const getResponseIdEventKey = (key: string) => `${key}-response-by-id`;
|
|
12
|
+
export const getRequestStartEventKey = (key: string) => `${key}-request-start`;
|
|
13
|
+
export const getRequestStartIdEventKey = (key: string) => `${key}-request-start-by-id`;
|
|
14
|
+
export const getResponseStartEventKey = (key: string) => `${key}-response-start`;
|
|
15
|
+
export const getResponseStartIdEventKey = (key: string) => `${key}-response-start-by-id`;
|
|
16
|
+
export const getUploadProgressEventKey = (key: string) => `${key}-request-progress`;
|
|
17
|
+
export const getUploadProgressIdEventKey = (key: string) => `${key}-request-progress-by-id`;
|
|
18
|
+
export const getDownloadProgressEventKey = (key: string) => `${key}-response-progress`;
|
|
19
|
+
export const getDownloadProgressIdEventKey = (key: string) => `${key}-response-progress-by-id`;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { LoggerLevelType } from "./logger.manager.types";
|
|
2
|
+
|
|
3
|
+
const defaultStyles = "background:rgba(0,0,0,0.2);padding:2px 5px;border-radius:5px;font-weight:bold;";
|
|
4
|
+
|
|
5
|
+
export const loggerStyles: Record<LoggerLevelType, string> = {
|
|
6
|
+
error: `${defaultStyles}color:#db2525`,
|
|
7
|
+
warning: `${defaultStyles}color:#e1941e`,
|
|
8
|
+
info: `${defaultStyles}color:#1e74e1`,
|
|
9
|
+
debug: `${defaultStyles}color:#adadad`,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const loggerIconLevels: Record<LoggerLevelType, string> = {
|
|
13
|
+
error: `🚨`,
|
|
14
|
+
warning: `🚧`,
|
|
15
|
+
info: `ℹ️`,
|
|
16
|
+
debug: `🛩️`,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const severity: Record<LoggerLevelType, number> = {
|
|
20
|
+
error: 0,
|
|
21
|
+
warning: 1,
|
|
22
|
+
info: 2,
|
|
23
|
+
debug: 3,
|
|
24
|
+
};
|