@hyper-fetch/core 9.4.0 → 9.4.1
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/__tests__/features/client/client.base.spec.ts +14 -0
- package/__tests__/features/managers/app/app.manager.base.spec.ts +173 -1
- package/dist/client/client.d.ts.map +1 -1
- package/dist/index.mjs +64 -15
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
- package/dist/managers/app/app.manager.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/app/app.manager.types.d.ts +8 -2
- package/dist/managers/app/app.manager.types.d.ts.map +1 -1
- package/dist/managers/app/app.manager.utils.d.ts +5 -0
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client/client.ts +1 -0
- package/src/managers/app/app.manager.constants.ts +18 -7
- package/src/managers/app/app.manager.ts +44 -10
- package/src/managers/app/app.manager.types.ts +8 -2
- package/src/managers/app/app.manager.utils.ts +13 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.manager.constants.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAI1C,oBAAY,SAAS;IACnB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,MAAM,WAAW;IACjB,OAAO,YAAY;CACpB;AAED,eAAO,MAAM,wBAAwB,EAAE,YAAY,CAAC,qBAAqB,
|
|
1
|
+
{"version":3,"file":"app.manager.constants.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAI1C,oBAAY,SAAS;IACnB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,MAAM,WAAW;IACjB,OAAO,YAAY;CACpB;AAED,eAAO,MAAM,wBAAwB,EAAE,YAAY,CAAC,qBAAqB,CAuBxE,CAAC"}
|
|
@@ -25,8 +25,17 @@ export declare class AppManager {
|
|
|
25
25
|
isBrowser: boolean;
|
|
26
26
|
isOnline: boolean;
|
|
27
27
|
isFocused: boolean;
|
|
28
|
+
private unmountCallbacks;
|
|
28
29
|
constructor(options?: AppManagerOptionsType | undefined);
|
|
30
|
+
/**
|
|
31
|
+
* Subscribes to the environment focus and network changes. Calling it again replaces the previous subscriptions,
|
|
32
|
+
* so the listeners are never registered twice.
|
|
33
|
+
*/
|
|
29
34
|
initialize: () => void;
|
|
35
|
+
/**
|
|
36
|
+
* Removes the environment listeners registered by `initialize()`. Listeners added with `events` are kept.
|
|
37
|
+
*/
|
|
38
|
+
unmount: () => void;
|
|
30
39
|
private setInitialFocus;
|
|
31
40
|
private setInitialOnline;
|
|
32
41
|
setFocused: (isFocused: boolean) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.manager.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC;;;;;;;;GAQG;AACH,qBAAa,UAAU;
|
|
1
|
+
{"version":3,"file":"app.manager.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAEtD,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC;;;;;;;;GAQG;AACH,qBAAa,UAAU;IAUF,OAAO,CAAC,EAAE,qBAAqB;IATlD,OAAO,eAAsB;IAC7B,MAAM;;;;;;;;;MAAqC;IAE3C,SAAS,EAAE,OAAO,CAAC;IACnB,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,OAAO,CAAC;IAEnB,OAAO,CAAC,gBAAgB,CAAsB;gBAE3B,OAAO,CAAC,EAAE,qBAAqB,YAAA;IAalD;;;OAGG;IACH,UAAU,aAWR;IAEF;;OAEG;IACH,OAAO,aAIL;IAEF,OAAO,CAAC,eAAe,CAcrB;IAEF,OAAO,CAAC,gBAAgB,CActB;IAEF,UAAU,GAAI,WAAW,OAAO,UAQ9B;IAEF,SAAS,GAAI,UAAU,OAAO,UAQ5B;CACH"}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
export type AppManagerOptionsType = {
|
|
2
2
|
initiallyFocused?: boolean | (() => boolean | Promise<boolean>);
|
|
3
3
|
initiallyOnline?: boolean | (() => boolean | Promise<boolean>);
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Subscribes to the environment focus changes. Return a cleanup function to let `unmount()` remove the listeners.
|
|
6
|
+
*/
|
|
7
|
+
focusEvent?: (setFocused: (isFocused: boolean) => void) => void | VoidFunction;
|
|
8
|
+
/**
|
|
9
|
+
* Subscribes to the environment network changes. Return a cleanup function to let `unmount()` remove the listeners.
|
|
10
|
+
*/
|
|
11
|
+
onlineEvent?: (setOnline: (isOnline: boolean) => void) => void | VoidFunction;
|
|
6
12
|
};
|
|
7
13
|
//# sourceMappingURL=app.manager.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.manager.types.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,qBAAqB,GAAG;IAClC,gBAAgB,CAAC,EAAE,OAAO,GAAG,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAChE,eAAe,CAAC,EAAE,OAAO,GAAG,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/D,UAAU,CAAC,EAAE,CAAC,UAAU,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,KAAK,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"app.manager.types.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,qBAAqB,GAAG;IAClC,gBAAgB,CAAC,EAAE,OAAO,GAAG,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAChE,eAAe,CAAC,EAAE,OAAO,GAAG,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/D;;OAEG;IACH,UAAU,CAAC,EAAE,CAAC,UAAU,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,KAAK,IAAI,GAAG,YAAY,CAAC;IAC/E;;OAEG;IACH,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,KAAK,IAAI,GAAG,YAAY,CAAC;CAC/E,CAAC"}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export declare const hasWindow: () => boolean;
|
|
2
2
|
export declare const hasDocument: () => boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Reads the current network state from the browser. Falls back to `true` when it is unknown - e.g. in Node.js or
|
|
5
|
+
* React Native, where `navigator.onLine` is not available.
|
|
6
|
+
*/
|
|
7
|
+
export declare const isNavigatorOnline: () => boolean;
|
|
3
8
|
export declare const onWindowEvent: <K extends keyof WindowEventMap>(key: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions | undefined) => VoidFunction;
|
|
4
9
|
export declare const onDocumentEvent: <K extends keyof DocumentEventMap>(key: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions | undefined) => VoidFunction;
|
|
5
10
|
//# sourceMappingURL=app.manager.utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.manager.utils.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,eAOrB,CAAC;AAEF,eAAO,MAAM,WAAW,eAOvB,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,cAAc,EAC1D,KAAK,CAAC,EACN,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,GAAG,EACtD,UAAU,OAAO,GAAG,uBAAuB,GAAG,SAAS,KACtD,YAQF,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,MAAM,gBAAgB,EAC9D,KAAK,CAAC,EACN,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,gBAAgB,CAAC,CAAC,CAAC,KAAK,GAAG,EAC1D,UAAU,OAAO,GAAG,uBAAuB,GAAG,SAAS,KACtD,YAQF,CAAC"}
|
|
1
|
+
{"version":3,"file":"app.manager.utils.d.ts","sourceRoot":"","sources":["../../../src/managers/app/app.manager.utils.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,eAOrB,CAAC;AAEF,eAAO,MAAM,WAAW,eAOvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,eAO7B,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,cAAc,EAC1D,KAAK,CAAC,EACN,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,GAAG,EACtD,UAAU,OAAO,GAAG,uBAAuB,GAAG,SAAS,KACtD,YAQF,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,MAAM,gBAAgB,EAC9D,KAAK,CAAC,EACN,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,gBAAgB,CAAC,CAAC,CAAC,KAAK,GAAG,EAC1D,UAAU,OAAO,GAAG,uBAAuB,GAAG,SAAS,KACtD,YAQF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "9.4.
|
|
3
|
+
"version": "9.4.1",
|
|
4
4
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
package/src/client/client.ts
CHANGED
|
@@ -419,6 +419,7 @@ export class Client<
|
|
|
419
419
|
this.fetchDispatcher.emitter.removeAllListeners();
|
|
420
420
|
this.submitDispatcher.emitter.removeAllListeners();
|
|
421
421
|
this.cache.emitter.removeAllListeners();
|
|
422
|
+
this.appManager.unmount();
|
|
422
423
|
|
|
423
424
|
this.cache = cache?.() || new Cache();
|
|
424
425
|
this.appManager = appManager?.() || new AppManager();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AppManagerOptionsType } from "managers";
|
|
2
2
|
import type { RequiredKeys } from "types";
|
|
3
3
|
|
|
4
|
-
import { onWindowEvent, onDocumentEvent } from "./app.manager.utils";
|
|
4
|
+
import { onWindowEvent, onDocumentEvent, isNavigatorOnline } from "./app.manager.utils";
|
|
5
5
|
|
|
6
6
|
export enum AppEvents {
|
|
7
7
|
FOCUS = "focus",
|
|
@@ -12,14 +12,25 @@ export enum AppEvents {
|
|
|
12
12
|
|
|
13
13
|
export const appManagerInitialOptions: RequiredKeys<AppManagerOptionsType> = {
|
|
14
14
|
initiallyFocused: true,
|
|
15
|
-
initiallyOnline:
|
|
15
|
+
initiallyOnline: isNavigatorOnline,
|
|
16
16
|
focusEvent: (setFocused) => {
|
|
17
|
-
onDocumentEvent("visibilitychange", () => setFocused(true));
|
|
18
|
-
onWindowEvent("focus", () => setFocused(true));
|
|
19
|
-
onWindowEvent("blur", () => setFocused(false));
|
|
17
|
+
const unmountVisibility = onDocumentEvent("visibilitychange", () => setFocused(true));
|
|
18
|
+
const unmountFocus = onWindowEvent("focus", () => setFocused(true));
|
|
19
|
+
const unmountBlur = onWindowEvent("blur", () => setFocused(false));
|
|
20
|
+
|
|
21
|
+
return () => {
|
|
22
|
+
unmountVisibility();
|
|
23
|
+
unmountFocus();
|
|
24
|
+
unmountBlur();
|
|
25
|
+
};
|
|
20
26
|
},
|
|
21
27
|
onlineEvent: (setOnline) => {
|
|
22
|
-
onWindowEvent("online", () => setOnline(true));
|
|
23
|
-
onWindowEvent("offline", () => setOnline(false));
|
|
28
|
+
const unmountOnline = onWindowEvent("online", () => setOnline(true));
|
|
29
|
+
const unmountOffline = onWindowEvent("offline", () => setOnline(false));
|
|
30
|
+
|
|
31
|
+
return () => {
|
|
32
|
+
unmountOnline();
|
|
33
|
+
unmountOffline();
|
|
34
|
+
};
|
|
24
35
|
},
|
|
25
36
|
};
|
|
@@ -19,6 +19,8 @@ export class AppManager {
|
|
|
19
19
|
isOnline: boolean;
|
|
20
20
|
isFocused: boolean;
|
|
21
21
|
|
|
22
|
+
private unmountCallbacks: VoidFunction[] = [];
|
|
23
|
+
|
|
22
24
|
constructor(public options?: AppManagerOptionsType) {
|
|
23
25
|
this.emitter?.setMaxListeners(1000);
|
|
24
26
|
const {
|
|
@@ -32,29 +34,61 @@ export class AppManager {
|
|
|
32
34
|
this.isBrowser = hasDocument();
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Subscribes to the environment focus and network changes. Calling it again replaces the previous subscriptions,
|
|
39
|
+
* so the listeners are never registered twice.
|
|
40
|
+
*/
|
|
35
41
|
initialize = () => {
|
|
36
42
|
const { focusEvent = appManagerInitialOptions.focusEvent, onlineEvent = appManagerInitialOptions.onlineEvent } =
|
|
37
43
|
this.options || appManagerInitialOptions;
|
|
38
44
|
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
this.unmount();
|
|
46
|
+
|
|
47
|
+
[focusEvent(this.setFocused), onlineEvent(this.setOnline)].forEach((unmount) => {
|
|
48
|
+
if (typeof unmount === "function") {
|
|
49
|
+
this.unmountCallbacks.push(unmount);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Removes the environment listeners registered by `initialize()`. Listeners added with `events` are kept.
|
|
56
|
+
*/
|
|
57
|
+
unmount = () => {
|
|
58
|
+
const callbacks = this.unmountCallbacks;
|
|
59
|
+
this.unmountCallbacks = [];
|
|
60
|
+
callbacks.forEach((unmount) => unmount());
|
|
41
61
|
};
|
|
42
62
|
|
|
43
63
|
private setInitialFocus = async (initValue: Exclude<AppManagerOptionsType["initiallyFocused"], undefined>) => {
|
|
44
|
-
if (typeof initValue
|
|
45
|
-
this.isFocused = false;
|
|
46
|
-
this.isFocused = await initValue();
|
|
47
|
-
} else {
|
|
64
|
+
if (typeof initValue !== "function") {
|
|
48
65
|
this.isFocused = initValue;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Synchronous getters apply right away, so there is no moment with a wrong value
|
|
70
|
+
const value = initValue();
|
|
71
|
+
if (typeof value === "boolean") {
|
|
72
|
+
this.isFocused = value;
|
|
73
|
+
} else {
|
|
74
|
+
this.isFocused = false;
|
|
75
|
+
this.isFocused = await value;
|
|
49
76
|
}
|
|
50
77
|
};
|
|
51
78
|
|
|
52
79
|
private setInitialOnline = async (initValue: Exclude<AppManagerOptionsType["initiallyOnline"], undefined>) => {
|
|
53
|
-
if (typeof initValue
|
|
54
|
-
this.isOnline = false;
|
|
55
|
-
this.isOnline = await initValue();
|
|
56
|
-
} else {
|
|
80
|
+
if (typeof initValue !== "function") {
|
|
57
81
|
this.isOnline = initValue;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Synchronous getters apply right away, so there is no moment with a wrong value
|
|
86
|
+
const value = initValue();
|
|
87
|
+
if (typeof value === "boolean") {
|
|
88
|
+
this.isOnline = value;
|
|
89
|
+
} else {
|
|
90
|
+
this.isOnline = false;
|
|
91
|
+
this.isOnline = await value;
|
|
58
92
|
}
|
|
59
93
|
};
|
|
60
94
|
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
export type AppManagerOptionsType = {
|
|
2
2
|
initiallyFocused?: boolean | (() => boolean | Promise<boolean>);
|
|
3
3
|
initiallyOnline?: boolean | (() => boolean | Promise<boolean>);
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Subscribes to the environment focus changes. Return a cleanup function to let `unmount()` remove the listeners.
|
|
6
|
+
*/
|
|
7
|
+
focusEvent?: (setFocused: (isFocused: boolean) => void) => void | VoidFunction;
|
|
8
|
+
/**
|
|
9
|
+
* Subscribes to the environment network changes. Return a cleanup function to let `unmount()` remove the listeners.
|
|
10
|
+
*/
|
|
11
|
+
onlineEvent?: (setOnline: (isOnline: boolean) => void) => void | VoidFunction;
|
|
6
12
|
};
|
|
@@ -16,6 +16,19 @@ export const hasDocument = () => {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Reads the current network state from the browser. Falls back to `true` when it is unknown - e.g. in Node.js or
|
|
21
|
+
* React Native, where `navigator.onLine` is not available.
|
|
22
|
+
*/
|
|
23
|
+
export const isNavigatorOnline = () => {
|
|
24
|
+
try {
|
|
25
|
+
return typeof navigator.onLine === "boolean" ? navigator.onLine : true;
|
|
26
|
+
} catch {
|
|
27
|
+
/* istanbul ignore next */
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
19
32
|
export const onWindowEvent = <K extends keyof WindowEventMap>(
|
|
20
33
|
key: K,
|
|
21
34
|
listener: (this: Window, ev: WindowEventMap[K]) => any,
|