@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.
@@ -91,6 +91,20 @@ describe("Client [ Base ]", () => {
91
91
  client.clear();
92
92
  expect(spy).toHaveBeenCalledTimes(2);
93
93
  });
94
+ it("should remove the window listeners of the previous appManager", async () => {
95
+ const client = new Client({ url: "shared-base-url" });
96
+ const previousAppManager = client.appManager;
97
+
98
+ client.clear();
99
+ window.dispatchEvent(new Event("offline"));
100
+
101
+ expect(previousAppManager.isOnline).toBeTrue();
102
+ expect(client.appManager.isOnline).toBeFalse();
103
+
104
+ window.dispatchEvent(new Event("online"));
105
+ expect(client.appManager.isOnline).toBeTrue();
106
+ client.appManager.unmount();
107
+ });
94
108
  it("should assign new cache", async () => {
95
109
  const spy = vi.fn();
96
110
  const cache = new Cache();
@@ -1,7 +1,7 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
2
  import { waitFor } from "@testing-library/dom";
3
3
  import { Client } from "client";
4
- import { AppManager, appManagerInitialOptions, hasDocument, hasWindow } from "managers";
4
+ import { AppManager, appManagerInitialOptions, hasDocument, hasWindow, isNavigatorOnline } from "managers";
5
5
 
6
6
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
7
7
 
@@ -15,9 +15,14 @@ describe("AppManager [ Base ]", () => {
15
15
  beforeEach(() => {
16
16
  resetMocks();
17
17
  vi.resetAllMocks();
18
+ client.appManager.unmount();
18
19
  client = new Client({ url: "shared-base-url" });
19
20
  });
20
21
 
22
+ afterEach(() => {
23
+ vi.restoreAllMocks();
24
+ });
25
+
21
26
  afterAll(() => {
22
27
  stopServer();
23
28
  });
@@ -121,6 +126,173 @@ describe("AppManager [ Base ]", () => {
121
126
  expect(offlineSpy).toHaveBeenCalledTimes(1);
122
127
  });
123
128
  });
129
+ describe("When app manager reads the initial state", () => {
130
+ it("should apply synchronous initial getters right away", () => {
131
+ const manager = new AppManager({ initiallyOnline: () => false, initiallyFocused: () => false });
132
+
133
+ expect(manager.isOnline).toBeFalse();
134
+ expect(manager.isFocused).toBeFalse();
135
+
136
+ const onlineManager = new AppManager({ initiallyOnline: () => true, initiallyFocused: () => true });
137
+
138
+ expect(onlineManager.isOnline).toBeTrue();
139
+ expect(onlineManager.isFocused).toBeTrue();
140
+ });
141
+ it("should await asynchronous initial getters", async () => {
142
+ const manager = new AppManager({ initiallyOnline: async () => true, initiallyFocused: async () => true });
143
+
144
+ expect(manager.isOnline).toBeFalse();
145
+ expect(manager.isFocused).toBeFalse();
146
+ await waitFor(() => {
147
+ expect(manager.isOnline).toBeTrue();
148
+ expect(manager.isFocused).toBeTrue();
149
+ });
150
+ });
151
+ it("should start offline when the browser is offline", () => {
152
+ vi.spyOn(window.navigator, "onLine", "get").mockReturnValue(false);
153
+
154
+ expect(new AppManager().isOnline).toBeFalse();
155
+ expect(new Client({ url: "shared-base-url" }).appManager.isOnline).toBeFalse();
156
+ });
157
+ it("should start online when the browser is online", () => {
158
+ vi.spyOn(window.navigator, "onLine", "get").mockReturnValue(true);
159
+
160
+ expect(new AppManager().isOnline).toBeTrue();
161
+ });
162
+ it("should prefer the initiallyOnline option over the browser state", () => {
163
+ vi.spyOn(window.navigator, "onLine", "get").mockReturnValue(false);
164
+
165
+ expect(new AppManager({ initiallyOnline: true }).isOnline).toBeTrue();
166
+ });
167
+ it("should assume online when the browser state is unknown", () => {
168
+ vi.spyOn(window.navigator, "onLine", "get").mockReturnValue(undefined as unknown as boolean);
169
+ expect(isNavigatorOnline()).toBeTrue();
170
+
171
+ vi.spyOn(global, "navigator", "get").mockReturnValue(undefined as unknown as Navigator);
172
+ expect(isNavigatorOnline()).toBeTrue();
173
+ });
174
+ });
175
+
176
+ describe("When app manager listens to the browser events", () => {
177
+ it("should track the window online and offline events", () => {
178
+ const manager = new AppManager();
179
+ const onlineSpy = vi.fn();
180
+ const offlineSpy = vi.fn();
181
+ manager.events.onOnline(onlineSpy);
182
+ manager.events.onOffline(offlineSpy);
183
+ manager.initialize();
184
+
185
+ window.dispatchEvent(new Event("offline"));
186
+ expect(manager.isOnline).toBeFalse();
187
+ expect(offlineSpy).toHaveBeenCalledTimes(1);
188
+
189
+ window.dispatchEvent(new Event("online"));
190
+ expect(manager.isOnline).toBeTrue();
191
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
192
+ manager.unmount();
193
+ });
194
+ it("should track the window focus, blur and visibility events", () => {
195
+ const manager = new AppManager();
196
+ const focusSpy = vi.fn();
197
+ const blurSpy = vi.fn();
198
+ manager.events.onFocus(focusSpy);
199
+ manager.events.onBlur(blurSpy);
200
+ manager.initialize();
201
+
202
+ window.dispatchEvent(new Event("blur"));
203
+ expect(manager.isFocused).toBeFalse();
204
+ window.dispatchEvent(new Event("focus"));
205
+ expect(manager.isFocused).toBeTrue();
206
+ window.document.dispatchEvent(new Event("visibilitychange"));
207
+
208
+ expect(blurSpy).toHaveBeenCalledTimes(1);
209
+ expect(focusSpy).toHaveBeenCalledTimes(2);
210
+ manager.unmount();
211
+ });
212
+ it("should not react to the browser events before initialization", () => {
213
+ const manager = new AppManager();
214
+ const offlineSpy = vi.fn();
215
+ manager.events.onOffline(offlineSpy);
216
+
217
+ window.dispatchEvent(new Event("offline"));
218
+
219
+ expect(manager.isOnline).toBeTrue();
220
+ expect(offlineSpy).not.toHaveBeenCalled();
221
+ window.dispatchEvent(new Event("online"));
222
+ });
223
+ it("should remove the browser listeners on unmount and keep the event subscriptions", () => {
224
+ const manager = new AppManager();
225
+ const offlineSpy = vi.fn();
226
+ manager.events.onOffline(offlineSpy);
227
+ manager.initialize();
228
+ manager.unmount();
229
+
230
+ window.dispatchEvent(new Event("offline"));
231
+ window.dispatchEvent(new Event("blur"));
232
+
233
+ expect(manager.isOnline).toBeTrue();
234
+ expect(manager.isFocused).toBeTrue();
235
+ expect(offlineSpy).not.toHaveBeenCalled();
236
+
237
+ manager.setOnline(false);
238
+ expect(offlineSpy).toHaveBeenCalledTimes(1);
239
+ window.dispatchEvent(new Event("online"));
240
+ window.dispatchEvent(new Event("focus"));
241
+ });
242
+ it("should not register the browser listeners twice when initialized again", () => {
243
+ const manager = new AppManager();
244
+ const offlineSpy = vi.fn();
245
+ manager.events.onOffline(offlineSpy);
246
+ manager.initialize();
247
+ manager.initialize();
248
+
249
+ window.dispatchEvent(new Event("offline"));
250
+
251
+ expect(offlineSpy).toHaveBeenCalledTimes(1);
252
+ manager.unmount();
253
+ window.dispatchEvent(new Event("online"));
254
+ });
255
+ it("should allow to initialize again after unmount", () => {
256
+ const manager = new AppManager();
257
+ manager.initialize();
258
+ manager.unmount();
259
+ manager.initialize();
260
+
261
+ window.dispatchEvent(new Event("offline"));
262
+ expect(manager.isOnline).toBeFalse();
263
+ window.dispatchEvent(new Event("online"));
264
+ expect(manager.isOnline).toBeTrue();
265
+ manager.unmount();
266
+ });
267
+ it("should call the cleanup functions returned by custom handlers", () => {
268
+ const unmountFocus = vi.fn();
269
+ const unmountOnline = vi.fn();
270
+ const manager = new AppManager({ focusEvent: () => unmountFocus, onlineEvent: () => unmountOnline });
271
+
272
+ manager.unmount();
273
+ expect(unmountFocus).not.toHaveBeenCalled();
274
+
275
+ manager.initialize();
276
+ expect(unmountFocus).not.toHaveBeenCalled();
277
+ expect(unmountOnline).not.toHaveBeenCalled();
278
+
279
+ manager.unmount();
280
+ manager.unmount();
281
+ expect(unmountFocus).toHaveBeenCalledTimes(1);
282
+ expect(unmountOnline).toHaveBeenCalledTimes(1);
283
+ });
284
+ it("should clean up the previous custom subscription when initialized again", () => {
285
+ const unmountOnline = vi.fn();
286
+ const onlineEvent = vi.fn().mockReturnValue(unmountOnline);
287
+ const manager = new AppManager({ onlineEvent, focusEvent: () => {} });
288
+
289
+ manager.initialize();
290
+ manager.initialize();
291
+
292
+ expect(onlineEvent).toHaveBeenCalledTimes(2);
293
+ expect(unmountOnline).toHaveBeenCalledTimes(1);
294
+ });
295
+ });
124
296
  describe("When using app manager utils", () => {
125
297
  it("should allow to detect window", async () => {
126
298
  expect(hasWindow()).toBeTrue();
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,UAAU,EACV,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,uBAAuB,EACxB,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACpF,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAChF,OAAO,EAA+B,OAAO,EAAY,MAAM,SAAS,CAAC;AACzE,OAAO,KAAK,EACV,UAAU,EACV,gBAAgB,EAChB,wBAAwB,EACxB,yBAAyB,EACzB,6BAA6B,EAC7B,0BAA0B,EAC1B,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,oCAAoC,EACpC,gCAAgC,EACjC,MAAM,OAAO,CAAC;AAKf;;;;;GAKG;AACH,qBAAa,MAAM,CACjB,eAAe,SAAS,eAAe,GAAG,KAAK,EAC/C,OAAO,SAAS,eAAe,GAAG,eAAe;IA2C9B,OAAO,EAAE,iBAAiB,CAAC,MAAM,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;IAzC/E,GAAG,EAAE,MAAM,CAAC;IACZ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,0BAA0B,CAAC,OAAO,CAAC,EACxD,cAAc,SAAS,yBAAyB,CAAC,OAAO,CAAC,EACzD,UAAU,SAAS,wBAAwB,CAAC,OAAO,CAAC,EAEpD,QAAQ,kBAAkB,CAAC,YAAY,EAAE,cAAc,EAAE,UAAU,CAAC,GAAG,gCAAgC,CAAC,OAAO,CAAC;gBAUpG,UAAU;iBACT,cAAc;;;gBADf,UAAU;iBACT,cAAc;;yCA4B7B;CACH"}
package/dist/index.mjs CHANGED
@@ -982,6 +982,7 @@ var AppManager = class {
982
982
  isBrowser;
983
983
  isOnline;
984
984
  isFocused;
985
+ unmountCallbacks = [];
985
986
  constructor(options) {
986
987
  this.options = options;
987
988
  this.emitter?.setMaxListeners(1e3);
@@ -990,22 +991,48 @@ var AppManager = class {
990
991
  this.setInitialOnline(initiallyOnline);
991
992
  this.isBrowser = hasDocument();
992
993
  }
994
+ /**
995
+ * Subscribes to the environment focus and network changes. Calling it again replaces the previous subscriptions,
996
+ * so the listeners are never registered twice.
997
+ */
993
998
  initialize = () => {
994
999
  const { focusEvent = appManagerInitialOptions.focusEvent, onlineEvent = appManagerInitialOptions.onlineEvent } = this.options || appManagerInitialOptions;
995
- focusEvent(this.setFocused);
996
- onlineEvent(this.setOnline);
1000
+ this.unmount();
1001
+ [focusEvent(this.setFocused), onlineEvent(this.setOnline)].forEach((unmount) => {
1002
+ if (typeof unmount === "function") this.unmountCallbacks.push(unmount);
1003
+ });
1004
+ };
1005
+ /**
1006
+ * Removes the environment listeners registered by `initialize()`. Listeners added with `events` are kept.
1007
+ */
1008
+ unmount = () => {
1009
+ const callbacks = this.unmountCallbacks;
1010
+ this.unmountCallbacks = [];
1011
+ callbacks.forEach((unmount) => unmount());
997
1012
  };
998
1013
  setInitialFocus = async (initValue) => {
999
- if (typeof initValue === "function") {
1014
+ if (typeof initValue !== "function") {
1015
+ this.isFocused = initValue;
1016
+ return;
1017
+ }
1018
+ const value = initValue();
1019
+ if (typeof value === "boolean") this.isFocused = value;
1020
+ else {
1000
1021
  this.isFocused = false;
1001
- this.isFocused = await initValue();
1002
- } else this.isFocused = initValue;
1022
+ this.isFocused = await value;
1023
+ }
1003
1024
  };
1004
1025
  setInitialOnline = async (initValue) => {
1005
- if (typeof initValue === "function") {
1026
+ if (typeof initValue !== "function") {
1027
+ this.isOnline = initValue;
1028
+ return;
1029
+ }
1030
+ const value = initValue();
1031
+ if (typeof value === "boolean") this.isOnline = value;
1032
+ else {
1006
1033
  this.isOnline = false;
1007
- this.isOnline = await initValue();
1008
- } else this.isOnline = initValue;
1034
+ this.isOnline = await value;
1035
+ }
1009
1036
  };
1010
1037
  setFocused = (isFocused) => {
1011
1038
  this.isFocused = isFocused;
@@ -1036,6 +1063,18 @@ var hasDocument = () => {
1036
1063
  return false;
1037
1064
  }
1038
1065
  };
1066
+ /**
1067
+ * Reads the current network state from the browser. Falls back to `true` when it is unknown - e.g. in Node.js or
1068
+ * React Native, where `navigator.onLine` is not available.
1069
+ */
1070
+ var isNavigatorOnline = () => {
1071
+ try {
1072
+ return typeof navigator.onLine === "boolean" ? navigator.onLine : true;
1073
+ } catch {
1074
+ /* istanbul ignore next */
1075
+ return true;
1076
+ }
1077
+ };
1039
1078
  var onWindowEvent = (key, listener, options) => {
1040
1079
  /* istanbul ignore next */
1041
1080
  if (hasWindow()) {
@@ -1098,15 +1137,24 @@ var AppEvents = /* @__PURE__ */ function(AppEvents) {
1098
1137
  }({});
1099
1138
  var appManagerInitialOptions = {
1100
1139
  initiallyFocused: true,
1101
- initiallyOnline: true,
1140
+ initiallyOnline: isNavigatorOnline,
1102
1141
  focusEvent: (setFocused) => {
1103
- onDocumentEvent("visibilitychange", () => setFocused(true));
1104
- onWindowEvent("focus", () => setFocused(true));
1105
- onWindowEvent("blur", () => setFocused(false));
1142
+ const unmountVisibility = onDocumentEvent("visibilitychange", () => setFocused(true));
1143
+ const unmountFocus = onWindowEvent("focus", () => setFocused(true));
1144
+ const unmountBlur = onWindowEvent("blur", () => setFocused(false));
1145
+ return () => {
1146
+ unmountVisibility();
1147
+ unmountFocus();
1148
+ unmountBlur();
1149
+ };
1106
1150
  },
1107
1151
  onlineEvent: (setOnline) => {
1108
- onWindowEvent("online", () => setOnline(true));
1109
- onWindowEvent("offline", () => setOnline(false));
1152
+ const unmountOnline = onWindowEvent("online", () => setOnline(true));
1153
+ const unmountOffline = onWindowEvent("offline", () => setOnline(false));
1154
+ return () => {
1155
+ unmountOnline();
1156
+ unmountOffline();
1157
+ };
1110
1158
  }
1111
1159
  };
1112
1160
  //#endregion
@@ -4089,6 +4137,7 @@ var Client = class {
4089
4137
  this.fetchDispatcher.emitter.removeAllListeners();
4090
4138
  this.submitDispatcher.emitter.removeAllListeners();
4091
4139
  this.cache.emitter.removeAllListeners();
4140
+ this.appManager.unmount();
4092
4141
  this.cache = cache?.() || new Cache();
4093
4142
  this.appManager = appManager?.() || new AppManager();
4094
4143
  this.fetchDispatcher = fetchDispatcher?.() || new Dispatcher();
@@ -4304,6 +4353,6 @@ var createConfiguration = () => {
4304
4353
  return (defaults) => defaults;
4305
4354
  };
4306
4355
  //#endregion
4307
- export { AbortError, Adapter, AppEvents, AppManager, Cache, Client, DeletedError, Dispatcher, DispatcherMode, EventEmitter, HttpAdapter, HttpMethods, LoggerManager, Plugin, Request, RequestManager, RequestProcessingError, SAFE_HTTP_METHODS, Time, TimeoutError, UnexpectedError, appManagerInitialOptions, canRetryRequest, createClient, createConfiguration, createRequestHooks, createSdk, defaultMapper, defaultTimeout, getAbortByAbortKey, getAbortByIdKey, getAbortKey, getAdapter, getAdapterBindings, getAdapterHeaders, getAdapterOnError, getAdapterPayload, getAppManagerEvents, getCacheByKey, getCacheData, getCacheEvents, getCacheKey, getDeleteByKey, getDeleteKey, getDispatcherChangeByKey, getDispatcherChangeKey, getDispatcherDrainedByKey, getDispatcherDrainedKey, getDispatcherEvents, getDispatcherRunningByKey, getDispatcherRunningKey, getDispatcherStatusByKey, getDispatcherStatusKey, getDownloadProgressByIdKey, getDownloadProgressByQueryKey, getDownloadProgressKey, getErrorMessage, getInvalidateByKey, getInvalidateKey, getIsEqualTimestamp, getLoadingByCacheKey, getLoadingByIdKey, getLoadingByQueryKey, getLoadingKey, getProgressData, getProgressValue, getRemoveByIdKey, getRemoveByQueryKey, getRemoveKey, getRequestDeduplicatedByCacheKey, getRequestDeduplicatedByIdKey, getRequestDeduplicatedByQueryKey, getRequestDeduplicatedKey, getRequestDispatcher, getRequestEta, getRequestKey, getRequestManagerEvents, getRequestStarByQueryKey, getRequestStartByIdKey, getRequestStartKey, getRequestType, getResponseByCacheKey, getResponseByIdKey, getResponseHeaders, getResponseKey, getResponseStartByIdKey, getResponseStartByQueryKey, getResponseStartKey, getSimpleKey, getTime, getUniqueRequestId, getUploadProgressByIdKey, getUploadProgressByQueryKey, getUploadProgressKey, handleResponse, hasDocument, hasWindow, interceptRequest, interceptResponse, isPlainObject, logLevelOrder, logger, loggerColors, loggerStyles, mapResponseForSend, mocker, onDocumentEvent, onWindowEvent, parseErrorResponse, parseResponse, replaceEqualDeep, resolveClientMode, scopeKey, sendRequest, stringifyDefaultOptions, stringifyKey, stringifyQueryParams, stringifyValue, xhrExtra };
4356
+ export { AbortError, Adapter, AppEvents, AppManager, Cache, Client, DeletedError, Dispatcher, DispatcherMode, EventEmitter, HttpAdapter, HttpMethods, LoggerManager, Plugin, Request, RequestManager, RequestProcessingError, SAFE_HTTP_METHODS, Time, TimeoutError, UnexpectedError, appManagerInitialOptions, canRetryRequest, createClient, createConfiguration, createRequestHooks, createSdk, defaultMapper, defaultTimeout, getAbortByAbortKey, getAbortByIdKey, getAbortKey, getAdapter, getAdapterBindings, getAdapterHeaders, getAdapterOnError, getAdapterPayload, getAppManagerEvents, getCacheByKey, getCacheData, getCacheEvents, getCacheKey, getDeleteByKey, getDeleteKey, getDispatcherChangeByKey, getDispatcherChangeKey, getDispatcherDrainedByKey, getDispatcherDrainedKey, getDispatcherEvents, getDispatcherRunningByKey, getDispatcherRunningKey, getDispatcherStatusByKey, getDispatcherStatusKey, getDownloadProgressByIdKey, getDownloadProgressByQueryKey, getDownloadProgressKey, getErrorMessage, getInvalidateByKey, getInvalidateKey, getIsEqualTimestamp, getLoadingByCacheKey, getLoadingByIdKey, getLoadingByQueryKey, getLoadingKey, getProgressData, getProgressValue, getRemoveByIdKey, getRemoveByQueryKey, getRemoveKey, getRequestDeduplicatedByCacheKey, getRequestDeduplicatedByIdKey, getRequestDeduplicatedByQueryKey, getRequestDeduplicatedKey, getRequestDispatcher, getRequestEta, getRequestKey, getRequestManagerEvents, getRequestStarByQueryKey, getRequestStartByIdKey, getRequestStartKey, getRequestType, getResponseByCacheKey, getResponseByIdKey, getResponseHeaders, getResponseKey, getResponseStartByIdKey, getResponseStartByQueryKey, getResponseStartKey, getSimpleKey, getTime, getUniqueRequestId, getUploadProgressByIdKey, getUploadProgressByQueryKey, getUploadProgressKey, handleResponse, hasDocument, hasWindow, interceptRequest, interceptResponse, isNavigatorOnline, isPlainObject, logLevelOrder, logger, loggerColors, loggerStyles, mapResponseForSend, mocker, onDocumentEvent, onWindowEvent, parseErrorResponse, parseResponse, replaceEqualDeep, resolveClientMode, scopeKey, sendRequest, stringifyDefaultOptions, stringifyKey, stringifyQueryParams, stringifyValue, xhrExtra };
4308
4357
 
4309
4358
  //# sourceMappingURL=index.mjs.map