@hyper-fetch/core 9.4.0 → 9.4.2

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.
@@ -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,CAYxE,CAAC"}
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;IAQF,OAAO,CAAC,EAAE,qBAAqB;IAPlD,OAAO,eAAsB;IAC7B,MAAM;;;;;;;;;MAAqC;IAE3C,SAAS,EAAE,OAAO,CAAC;IACnB,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,OAAO,CAAC;gBAEA,OAAO,CAAC,EAAE,qBAAqB,YAAA;IAalD,UAAU,aAMR;IAEF,OAAO,CAAC,eAAe,CAOrB;IAEF,OAAO,CAAC,gBAAgB,CAOtB;IAEF,UAAU,GAAI,WAAW,OAAO,UAQ9B;IAEF,SAAS,GAAI,UAAU,OAAO,UAQ5B;CACH"}
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
- focusEvent?: (setFocused: (isFocused: boolean) => void) => void;
5
- onlineEvent?: (setOnline: (isOnline: boolean) => void) => void;
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;IAChE,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,KAAK,IAAI,CAAC;CAChE,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.0",
3
+ "version": "9.4.2",
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/",
@@ -2,7 +2,7 @@ export const stringifyDefaultOptions = {
2
2
  strict: true,
3
3
  encode: true,
4
4
  arrayFormat: "bracket",
5
- arraySeparator: "bracket",
5
+ arraySeparator: "|",
6
6
  sort: false,
7
7
  skipNull: true,
8
8
  skipEmptyString: true,
@@ -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();
@@ -38,6 +38,9 @@ export const getAdapter = () =>
38
38
  .setFetcher(
39
39
  async ({
40
40
  request,
41
+ url,
42
+ endpoint,
43
+ queryParams,
41
44
  adapterOptions,
42
45
  headers,
43
46
  payload,
@@ -54,9 +57,8 @@ export const getAdapter = () =>
54
57
  onRequestProgress,
55
58
  onSuccess,
56
59
  }) => {
57
- const { method, client, endpoint, queryParams } = request;
58
- const queryString = queryParams ? stringifyQueryParams(queryParams) : "";
59
- const fullUrl = `${client.url}${endpoint}${queryString}`;
60
+ const { method } = request;
61
+ const fullUrl = `${url}${endpoint}${queryParams}`;
60
62
 
61
63
  const controller = getAbortController();
62
64
  const { timeout: timeoutMs = defaultTimeout, streaming = false, ...restOptions } = adapterOptions || {};
@@ -85,6 +85,18 @@ const encodeValue = (
85
85
  return value;
86
86
  };
87
87
 
88
+ const parseValue = (value: QueryParamValuesType, options: QueryStringifyOptionsType): string => {
89
+ if (value instanceof Date) {
90
+ return options.dateParser?.(value) || value.toISOString();
91
+ }
92
+
93
+ if (value !== null && typeof value === "object" && !Array.isArray(value)) {
94
+ return options.objectParser?.(value) || JSON.stringify(value);
95
+ }
96
+
97
+ return String(value);
98
+ };
99
+
88
100
  const encodeParams = (key: string, value: QueryParamType, options: QueryStringifyOptionsType) => {
89
101
  const shouldSkip = !isValidValue(options)(value);
90
102
 
@@ -92,53 +104,42 @@ const encodeParams = (key: string, value: QueryParamType, options: QueryStringif
92
104
  return "";
93
105
  }
94
106
 
95
- const parsedValue = () => {
96
- if (value instanceof Date) {
97
- return options.dateParser?.(value) || value.toISOString();
98
- }
99
-
100
- if (typeof value === "object" && !Array.isArray(value)) {
101
- return options.objectParser?.(value) || JSON.stringify(value);
102
- }
103
-
104
- return String(value);
105
- };
106
-
107
- return `${encodeValue(key, options)}=${encodeValue(parsedValue(), options)}`;
107
+ return `${encodeValue(key, options)}=${encodeValue(parseValue(value as QueryParamValuesType, options), options)}`;
108
108
  };
109
109
 
110
110
  const encodeArray = (key: string, array: QueryParamValuesType[], options: QueryStringifyOptionsType): string => {
111
111
  const { arrayFormat, arraySeparator } = options;
112
+ const separator = arraySeparator || "|";
113
+ const encodedKey = encodeValue(key, options);
112
114
 
113
115
  return array
114
116
  .filter(isValidValue(options))
115
117
  .reduce<string[]>((acc, value, index) => {
118
+ const encodedValue = encodeValue(parseValue(value, options), options);
119
+
116
120
  switch (arrayFormat) {
117
121
  case "index": {
118
- const keyValue = `${encodeValue(key, options)}[${encodeValue(String(index), options)}]=`;
119
- acc.push(`${keyValue}${encodeValue(String(value), options)}`);
122
+ acc.push(`${encodedKey}[${encodeValue(String(index), options)}]=${encodedValue}`);
120
123
  break;
121
124
  }
122
125
  case "bracket": {
123
- const keyValue = `${encodeValue(key, options)}[]=`;
124
- acc.push(`${keyValue}${encodeValue(String(value), options)}`);
126
+ acc.push(`${encodedKey}[]=${encodedValue}`);
125
127
  break;
126
128
  }
127
129
  case "comma": {
128
- const keyValue = (acc.length === 0 && `${encodeValue(key, options)}=`) || "";
129
- return [[...acc, `${keyValue}${encodeValue(String(value), options)}`].join(",")];
130
+ const keyValue = (acc.length === 0 && `${encodedKey}=`) || "";
131
+ return [[...acc, `${keyValue}${encodedValue}`].join(",")];
130
132
  }
131
133
  case "separator": {
132
- const keyValue = (acc.length === 0 && `${encodeValue(key, options)}=`) || "";
133
- return [[...acc, `${keyValue}${encodeValue(String(value), options)}`].join(arraySeparator || "|")];
134
+ const keyValue = (acc.length === 0 && `${encodedKey}=`) || "";
135
+ return [[...acc, `${keyValue}${encodedValue}`].join(separator)];
134
136
  }
135
137
  case "bracket-separator": {
136
- const keyValue = (acc.length === 0 && `${encodeValue(key, options)}[]=`) || "";
137
- return [[...acc, `${keyValue}${encodeValue(String(value), options)}`].join(arraySeparator || "|")];
138
+ const keyValue = (acc.length === 0 && `${encodedKey}[]=`) || "";
139
+ return [[...acc, `${keyValue}${encodedValue}`].join(separator)];
138
140
  }
139
141
  default: {
140
- const keyValue = `${encodeValue(key, options)}=`;
141
- acc.push(`${keyValue}${encodeValue(String(value), options)}`);
142
+ acc.push(`${encodedKey}=${encodedValue}`);
142
143
  }
143
144
  }
144
145
 
@@ -149,8 +150,11 @@ const encodeArray = (key: string, array: QueryParamValuesType[], options: QueryS
149
150
 
150
151
  export const stringifyQueryParams = (
151
152
  queryParams: QueryParamsType | string | EmptyTypes,
152
- options: QueryStringifyOptionsType = stringifyDefaultOptions,
153
+ options?: QueryStringifyOptionsType,
153
154
  ): string => {
155
+ // Merge with defaults so a partial config (e.g. `{ arrayFormat: "comma" }`) keeps encoding and skipping enabled
156
+ const config: QueryStringifyOptionsType = { ...stringifyDefaultOptions, ...options };
157
+
154
158
  if (!queryParams || Object.keys(queryParams).length === 0) {
155
159
  return "";
156
160
  }
@@ -163,10 +167,10 @@ export const stringifyQueryParams = (
163
167
  const stringified = Object.entries(queryParams)
164
168
  .map(([key, value]): string => {
165
169
  if (Array.isArray(value)) {
166
- return encodeArray(key, value, options);
170
+ return encodeArray(key, value, config);
167
171
  }
168
172
 
169
- return encodeParams(key, value, options);
173
+ return encodeParams(key, value, config);
170
174
  })
171
175
  .filter(Boolean)
172
176
  .join("&");
@@ -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: true,
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
- focusEvent(this.setFocused);
40
- onlineEvent(this.setOnline);
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 === "function") {
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 === "function") {
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
- focusEvent?: (setFocused: (isFocused: boolean) => void) => void;
5
- onlineEvent?: (setOnline: (isOnline: boolean) => void) => void;
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,