@tanstack/query-core 4.24.10 → 5.0.0-alpha.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/build/lib/focusManager.d.ts +1 -3
- package/build/lib/focusManager.esm.js +19 -36
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +19 -38
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/focusManager.mjs +19 -36
- package/build/lib/focusManager.mjs.map +1 -1
- package/build/lib/hydration.esm.js +21 -23
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +21 -25
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +21 -23
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +1 -2
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +2 -8
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/infiniteQueryBehavior.d.ts +3 -7
- package/build/lib/infiniteQueryBehavior.esm.js +52 -75
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +50 -77
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +52 -75
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +4 -4
- package/build/lib/infiniteQueryObserver.esm.js +18 -26
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +18 -28
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +18 -26
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +11 -22
- package/build/lib/mutation.esm.js +73 -105
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +73 -107
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +73 -105
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +4 -6
- package/build/lib/mutationCache.esm.js +23 -32
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +23 -34
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +23 -32
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.d.ts +4 -9
- package/build/lib/mutationObserver.esm.js +43 -72
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +43 -74
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +43 -72
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/notifyManager.esm.js +7 -17
- package/build/lib/notifyManager.esm.js.map +1 -1
- package/build/lib/notifyManager.js +7 -19
- package/build/lib/notifyManager.js.map +1 -1
- package/build/lib/notifyManager.mjs +7 -17
- package/build/lib/notifyManager.mjs.map +1 -1
- package/build/lib/onlineManager.d.ts +1 -3
- package/build/lib/onlineManager.esm.js +16 -30
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +16 -32
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/onlineManager.mjs +16 -30
- package/build/lib/onlineManager.mjs.map +1 -1
- package/build/lib/queriesObserver.d.ts +3 -10
- package/build/lib/queriesObserver.esm.js +47 -71
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +49 -75
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +47 -71
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +14 -21
- package/build/lib/query.esm.js +140 -194
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +139 -195
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +140 -194
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +12 -7
- package/build/lib/queryCache.esm.js +21 -45
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +20 -46
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +21 -45
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.d.ts +18 -46
- package/build/lib/queryClient.esm.js +137 -216
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +136 -217
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +137 -216
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +4 -29
- package/build/lib/queryObserver.esm.js +176 -258
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +176 -260
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +176 -258
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.d.ts +3 -3
- package/build/lib/removable.esm.js +10 -14
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +10 -16
- package/build/lib/removable.js.map +1 -1
- package/build/lib/removable.mjs +10 -14
- package/build/lib/removable.mjs.map +1 -1
- package/build/lib/retryer.d.ts +5 -5
- package/build/lib/retryer.esm.js +27 -44
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +27 -46
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/retryer.mjs +27 -44
- package/build/lib/retryer.mjs.map +1 -1
- package/build/lib/subscribable.esm.js +4 -7
- package/build/lib/subscribable.esm.js.map +1 -1
- package/build/lib/subscribable.js +4 -9
- package/build/lib/subscribable.js.map +1 -1
- package/build/lib/subscribable.mjs +4 -7
- package/build/lib/subscribable.mjs.map +1 -1
- package/build/lib/tests/utils.d.ts +3 -12
- package/build/lib/types.d.ts +111 -99
- package/build/lib/utils.d.ts +8 -18
- package/build/lib/utils.esm.js +39 -132
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +42 -144
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +39 -132
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +868 -1398
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/focusManager.ts +17 -24
- package/src/index.ts +1 -11
- package/src/infiniteQueryBehavior.ts +54 -94
- package/src/infiniteQueryObserver.ts +10 -12
- package/src/mutation.ts +68 -92
- package/src/mutationCache.ts +27 -27
- package/src/mutationObserver.ts +60 -97
- package/src/onlineManager.ts +14 -14
- package/src/queriesObserver.ts +50 -54
- package/src/query.ts +106 -110
- package/src/queryCache.ts +42 -41
- package/src/queryClient.ts +155 -434
- package/src/queryObserver.ts +155 -192
- package/src/removable.ts +13 -13
- package/src/retryer.ts +5 -5
- package/src/tests/focusManager.test.tsx +25 -25
- package/src/tests/hydration.test.tsx +167 -81
- package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
- package/src/tests/infiniteQueryObserver.test.tsx +6 -2
- package/src/tests/mutationCache.test.tsx +127 -127
- package/src/tests/mutationObserver.test.tsx +1 -31
- package/src/tests/mutations.test.tsx +62 -43
- package/src/tests/onlineManager.test.tsx +12 -4
- package/src/tests/queriesObserver.test.tsx +41 -77
- package/src/tests/query.test.tsx +175 -243
- package/src/tests/queryCache.test.tsx +170 -93
- package/src/tests/queryClient.test.tsx +229 -378
- package/src/tests/queryObserver.test.tsx +23 -147
- package/src/tests/utils.test.tsx +84 -29
- package/src/tests/utils.ts +9 -18
- package/src/types.ts +187 -140
- package/src/utils.ts +31 -124
- package/build/lib/logger.d.ts +0 -8
- package/build/lib/logger.esm.js +0 -4
- package/build/lib/logger.esm.js.map +0 -1
- package/build/lib/logger.js +0 -8
- package/build/lib/logger.js.map +0 -1
- package/build/lib/logger.mjs +0 -4
- package/build/lib/logger.mjs.map +0 -1
- package/build/lib/logger.native.d.ts +0 -6
- package/build/lib/logger.native.esm.js +0 -12
- package/build/lib/logger.native.esm.js.map +0 -1
- package/build/lib/logger.native.js +0 -16
- package/build/lib/logger.native.js.map +0 -1
- package/build/lib/logger.native.mjs +0 -12
- package/build/lib/logger.native.mjs.map +0 -1
- package/src/logger.native.ts +0 -11
- package/src/logger.ts +0 -9
|
@@ -1,38 +1,31 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var utils = require('./utils.js');
|
|
6
4
|
|
|
5
|
+
// TYPES
|
|
6
|
+
|
|
7
7
|
function createNotifyManager() {
|
|
8
8
|
let queue = [];
|
|
9
9
|
let transactions = 0;
|
|
10
|
-
|
|
11
10
|
let notifyFn = callback => {
|
|
12
11
|
callback();
|
|
13
12
|
};
|
|
14
|
-
|
|
15
13
|
let batchNotifyFn = callback => {
|
|
16
14
|
callback();
|
|
17
15
|
};
|
|
18
|
-
|
|
19
16
|
const batch = callback => {
|
|
20
17
|
let result;
|
|
21
18
|
transactions++;
|
|
22
|
-
|
|
23
19
|
try {
|
|
24
20
|
result = callback();
|
|
25
21
|
} finally {
|
|
26
22
|
transactions--;
|
|
27
|
-
|
|
28
23
|
if (!transactions) {
|
|
29
24
|
flush();
|
|
30
25
|
}
|
|
31
26
|
}
|
|
32
|
-
|
|
33
27
|
return result;
|
|
34
28
|
};
|
|
35
|
-
|
|
36
29
|
const schedule = callback => {
|
|
37
30
|
if (transactions) {
|
|
38
31
|
queue.push(callback);
|
|
@@ -42,11 +35,10 @@ function createNotifyManager() {
|
|
|
42
35
|
});
|
|
43
36
|
}
|
|
44
37
|
};
|
|
38
|
+
|
|
45
39
|
/**
|
|
46
40
|
* All calls to the wrapped function will be batched.
|
|
47
41
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
42
|
const batchCalls = callback => {
|
|
51
43
|
return (...args) => {
|
|
52
44
|
schedule(() => {
|
|
@@ -54,11 +46,9 @@ function createNotifyManager() {
|
|
|
54
46
|
});
|
|
55
47
|
};
|
|
56
48
|
};
|
|
57
|
-
|
|
58
49
|
const flush = () => {
|
|
59
50
|
const originalQueue = queue;
|
|
60
51
|
queue = [];
|
|
61
|
-
|
|
62
52
|
if (originalQueue.length) {
|
|
63
53
|
utils.scheduleMicrotask(() => {
|
|
64
54
|
batchNotifyFn(() => {
|
|
@@ -69,25 +59,22 @@ function createNotifyManager() {
|
|
|
69
59
|
});
|
|
70
60
|
}
|
|
71
61
|
};
|
|
62
|
+
|
|
72
63
|
/**
|
|
73
64
|
* Use this method to set a custom notify function.
|
|
74
65
|
* This can be used to for example wrap notifications with `React.act` while running tests.
|
|
75
66
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
67
|
const setNotifyFunction = fn => {
|
|
79
68
|
notifyFn = fn;
|
|
80
69
|
};
|
|
70
|
+
|
|
81
71
|
/**
|
|
82
72
|
* Use this method to set a custom function to batch notifications together into a single tick.
|
|
83
73
|
* By default React Query will use the batch function provided by ReactDOM or React Native.
|
|
84
74
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
87
75
|
const setBatchNotifyFunction = fn => {
|
|
88
76
|
batchNotifyFn = fn;
|
|
89
77
|
};
|
|
90
|
-
|
|
91
78
|
return {
|
|
92
79
|
batch,
|
|
93
80
|
batchCalls,
|
|
@@ -95,8 +82,9 @@ function createNotifyManager() {
|
|
|
95
82
|
setNotifyFunction,
|
|
96
83
|
setBatchNotifyFunction
|
|
97
84
|
};
|
|
98
|
-
}
|
|
85
|
+
}
|
|
99
86
|
|
|
87
|
+
// SINGLETON
|
|
100
88
|
const notifyManager = createNotifyManager();
|
|
101
89
|
|
|
102
90
|
exports.createNotifyManager = createNotifyManager;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notifyManager.js","sources":["../../src/notifyManager.ts"],"sourcesContent":["import { scheduleMicrotask } from './utils'\n\n// TYPES\n\ntype NotifyCallback = () => void\n\ntype NotifyFunction = (callback: () => void) => void\n\ntype BatchNotifyFunction = (callback: () => void) => void\n\nexport function createNotifyManager() {\n let queue: NotifyCallback[] = []\n let transactions = 0\n let notifyFn: NotifyFunction = (callback) => {\n callback()\n }\n let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => {\n callback()\n }\n\n const batch = <T>(callback: () => T): T => {\n let result\n transactions++\n try {\n result = callback()\n } finally {\n transactions--\n if (!transactions) {\n flush()\n }\n }\n return result\n }\n\n const schedule = (callback: NotifyCallback): void => {\n if (transactions) {\n queue.push(callback)\n } else {\n scheduleMicrotask(() => {\n notifyFn(callback)\n })\n }\n }\n\n /**\n * All calls to the wrapped function will be batched.\n */\n const batchCalls = <T extends Function>(callback: T): T => {\n return ((...args: any[]) => {\n schedule(() => {\n callback(...args)\n })\n }) as any\n }\n\n const flush = (): void => {\n const originalQueue = queue\n queue = []\n if (originalQueue.length) {\n scheduleMicrotask(() => {\n batchNotifyFn(() => {\n originalQueue.forEach((callback) => {\n notifyFn(callback)\n })\n })\n })\n }\n }\n\n /**\n * Use this method to set a custom notify function.\n * This can be used to for example wrap notifications with `React.act` while running tests.\n */\n const setNotifyFunction = (fn: NotifyFunction) => {\n notifyFn = fn\n }\n\n /**\n * Use this method to set a custom function to batch notifications together into a single tick.\n * By default React Query will use the batch function provided by ReactDOM or React Native.\n */\n const setBatchNotifyFunction = (fn: BatchNotifyFunction) => {\n batchNotifyFn = fn\n }\n\n return {\n batch,\n batchCalls,\n schedule,\n setNotifyFunction,\n setBatchNotifyFunction,\n } as const\n}\n\n// SINGLETON\nexport const notifyManager = createNotifyManager()\n"],"names":["createNotifyManager","queue","transactions","notifyFn","callback","batchNotifyFn","batch","result","flush","schedule","push","scheduleMicrotask","batchCalls","args","originalQueue","length","forEach","setNotifyFunction","fn","setBatchNotifyFunction","notifyManager"],"mappings":"
|
|
1
|
+
{"version":3,"file":"notifyManager.js","sources":["../../src/notifyManager.ts"],"sourcesContent":["import { scheduleMicrotask } from './utils'\n\n// TYPES\n\ntype NotifyCallback = () => void\n\ntype NotifyFunction = (callback: () => void) => void\n\ntype BatchNotifyFunction = (callback: () => void) => void\n\nexport function createNotifyManager() {\n let queue: NotifyCallback[] = []\n let transactions = 0\n let notifyFn: NotifyFunction = (callback) => {\n callback()\n }\n let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => {\n callback()\n }\n\n const batch = <T>(callback: () => T): T => {\n let result\n transactions++\n try {\n result = callback()\n } finally {\n transactions--\n if (!transactions) {\n flush()\n }\n }\n return result\n }\n\n const schedule = (callback: NotifyCallback): void => {\n if (transactions) {\n queue.push(callback)\n } else {\n scheduleMicrotask(() => {\n notifyFn(callback)\n })\n }\n }\n\n /**\n * All calls to the wrapped function will be batched.\n */\n const batchCalls = <T extends Function>(callback: T): T => {\n return ((...args: any[]) => {\n schedule(() => {\n callback(...args)\n })\n }) as any\n }\n\n const flush = (): void => {\n const originalQueue = queue\n queue = []\n if (originalQueue.length) {\n scheduleMicrotask(() => {\n batchNotifyFn(() => {\n originalQueue.forEach((callback) => {\n notifyFn(callback)\n })\n })\n })\n }\n }\n\n /**\n * Use this method to set a custom notify function.\n * This can be used to for example wrap notifications with `React.act` while running tests.\n */\n const setNotifyFunction = (fn: NotifyFunction) => {\n notifyFn = fn\n }\n\n /**\n * Use this method to set a custom function to batch notifications together into a single tick.\n * By default React Query will use the batch function provided by ReactDOM or React Native.\n */\n const setBatchNotifyFunction = (fn: BatchNotifyFunction) => {\n batchNotifyFn = fn\n }\n\n return {\n batch,\n batchCalls,\n schedule,\n setNotifyFunction,\n setBatchNotifyFunction,\n } as const\n}\n\n// SINGLETON\nexport const notifyManager = createNotifyManager()\n"],"names":["createNotifyManager","queue","transactions","notifyFn","callback","batchNotifyFn","batch","result","flush","schedule","push","scheduleMicrotask","batchCalls","args","originalQueue","length","forEach","setNotifyFunction","fn","setBatchNotifyFunction","notifyManager"],"mappings":";;;;AAEA;;AAQO,SAASA,mBAAmB,GAAG;EACpC,IAAIC,KAAuB,GAAG,EAAE,CAAA;EAChC,IAAIC,YAAY,GAAG,CAAC,CAAA;EACpB,IAAIC,QAAwB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D,QAAQ,CAACC,QAAQ,CAAC,CAAA;AACpB,WAAC,CAAC,CAAA;AACJ,SAAC,CAAC,CAAA;AACJ,OAAC,CAAC,CAAA;AACJ,KAAA;GACD,CAAA;;AAED;AACF;AACA;AACA;EACE,MAAMa,iBAAiB,GAAIC,EAAkB,IAAK;AAChDf,IAAAA,QAAQ,GAAGe,EAAE,CAAA;GACd,CAAA;;AAED;AACF;AACA;AACA;EACE,MAAMC,sBAAsB,GAAID,EAAuB,IAAK;AAC1Db,IAAAA,aAAa,GAAGa,EAAE,CAAA;GACnB,CAAA;EAED,OAAO;IACLZ,KAAK;IACLM,UAAU;IACVH,QAAQ;IACRQ,iBAAiB;AACjBE,IAAAA,sBAAAA;GACD,CAAA;AACH,CAAA;;AAEA;AACaC,MAAAA,aAAa,GAAGpB,mBAAmB;;;;;"}
|
|
@@ -1,34 +1,29 @@
|
|
|
1
1
|
import { scheduleMicrotask } from './utils.mjs';
|
|
2
2
|
|
|
3
|
+
// TYPES
|
|
4
|
+
|
|
3
5
|
function createNotifyManager() {
|
|
4
6
|
let queue = [];
|
|
5
7
|
let transactions = 0;
|
|
6
|
-
|
|
7
8
|
let notifyFn = callback => {
|
|
8
9
|
callback();
|
|
9
10
|
};
|
|
10
|
-
|
|
11
11
|
let batchNotifyFn = callback => {
|
|
12
12
|
callback();
|
|
13
13
|
};
|
|
14
|
-
|
|
15
14
|
const batch = callback => {
|
|
16
15
|
let result;
|
|
17
16
|
transactions++;
|
|
18
|
-
|
|
19
17
|
try {
|
|
20
18
|
result = callback();
|
|
21
19
|
} finally {
|
|
22
20
|
transactions--;
|
|
23
|
-
|
|
24
21
|
if (!transactions) {
|
|
25
22
|
flush();
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
|
-
|
|
29
25
|
return result;
|
|
30
26
|
};
|
|
31
|
-
|
|
32
27
|
const schedule = callback => {
|
|
33
28
|
if (transactions) {
|
|
34
29
|
queue.push(callback);
|
|
@@ -38,11 +33,10 @@ function createNotifyManager() {
|
|
|
38
33
|
});
|
|
39
34
|
}
|
|
40
35
|
};
|
|
36
|
+
|
|
41
37
|
/**
|
|
42
38
|
* All calls to the wrapped function will be batched.
|
|
43
39
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
40
|
const batchCalls = callback => {
|
|
47
41
|
return (...args) => {
|
|
48
42
|
schedule(() => {
|
|
@@ -50,11 +44,9 @@ function createNotifyManager() {
|
|
|
50
44
|
});
|
|
51
45
|
};
|
|
52
46
|
};
|
|
53
|
-
|
|
54
47
|
const flush = () => {
|
|
55
48
|
const originalQueue = queue;
|
|
56
49
|
queue = [];
|
|
57
|
-
|
|
58
50
|
if (originalQueue.length) {
|
|
59
51
|
scheduleMicrotask(() => {
|
|
60
52
|
batchNotifyFn(() => {
|
|
@@ -65,25 +57,22 @@ function createNotifyManager() {
|
|
|
65
57
|
});
|
|
66
58
|
}
|
|
67
59
|
};
|
|
60
|
+
|
|
68
61
|
/**
|
|
69
62
|
* Use this method to set a custom notify function.
|
|
70
63
|
* This can be used to for example wrap notifications with `React.act` while running tests.
|
|
71
64
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
65
|
const setNotifyFunction = fn => {
|
|
75
66
|
notifyFn = fn;
|
|
76
67
|
};
|
|
68
|
+
|
|
77
69
|
/**
|
|
78
70
|
* Use this method to set a custom function to batch notifications together into a single tick.
|
|
79
71
|
* By default React Query will use the batch function provided by ReactDOM or React Native.
|
|
80
72
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
73
|
const setBatchNotifyFunction = fn => {
|
|
84
74
|
batchNotifyFn = fn;
|
|
85
75
|
};
|
|
86
|
-
|
|
87
76
|
return {
|
|
88
77
|
batch,
|
|
89
78
|
batchCalls,
|
|
@@ -91,8 +80,9 @@ function createNotifyManager() {
|
|
|
91
80
|
setNotifyFunction,
|
|
92
81
|
setBatchNotifyFunction
|
|
93
82
|
};
|
|
94
|
-
}
|
|
83
|
+
}
|
|
95
84
|
|
|
85
|
+
// SINGLETON
|
|
96
86
|
const notifyManager = createNotifyManager();
|
|
97
87
|
|
|
98
88
|
export { createNotifyManager, notifyManager };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notifyManager.mjs","sources":["../../src/notifyManager.ts"],"sourcesContent":["import { scheduleMicrotask } from './utils'\n\n// TYPES\n\ntype NotifyCallback = () => void\n\ntype NotifyFunction = (callback: () => void) => void\n\ntype BatchNotifyFunction = (callback: () => void) => void\n\nexport function createNotifyManager() {\n let queue: NotifyCallback[] = []\n let transactions = 0\n let notifyFn: NotifyFunction = (callback) => {\n callback()\n }\n let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => {\n callback()\n }\n\n const batch = <T>(callback: () => T): T => {\n let result\n transactions++\n try {\n result = callback()\n } finally {\n transactions--\n if (!transactions) {\n flush()\n }\n }\n return result\n }\n\n const schedule = (callback: NotifyCallback): void => {\n if (transactions) {\n queue.push(callback)\n } else {\n scheduleMicrotask(() => {\n notifyFn(callback)\n })\n }\n }\n\n /**\n * All calls to the wrapped function will be batched.\n */\n const batchCalls = <T extends Function>(callback: T): T => {\n return ((...args: any[]) => {\n schedule(() => {\n callback(...args)\n })\n }) as any\n }\n\n const flush = (): void => {\n const originalQueue = queue\n queue = []\n if (originalQueue.length) {\n scheduleMicrotask(() => {\n batchNotifyFn(() => {\n originalQueue.forEach((callback) => {\n notifyFn(callback)\n })\n })\n })\n }\n }\n\n /**\n * Use this method to set a custom notify function.\n * This can be used to for example wrap notifications with `React.act` while running tests.\n */\n const setNotifyFunction = (fn: NotifyFunction) => {\n notifyFn = fn\n }\n\n /**\n * Use this method to set a custom function to batch notifications together into a single tick.\n * By default React Query will use the batch function provided by ReactDOM or React Native.\n */\n const setBatchNotifyFunction = (fn: BatchNotifyFunction) => {\n batchNotifyFn = fn\n }\n\n return {\n batch,\n batchCalls,\n schedule,\n setNotifyFunction,\n setBatchNotifyFunction,\n } as const\n}\n\n// SINGLETON\nexport const notifyManager = createNotifyManager()\n"],"names":["createNotifyManager","queue","transactions","notifyFn","callback","batchNotifyFn","batch","result","flush","schedule","push","scheduleMicrotask","batchCalls","args","originalQueue","length","forEach","setNotifyFunction","fn","setBatchNotifyFunction","notifyManager"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"notifyManager.mjs","sources":["../../src/notifyManager.ts"],"sourcesContent":["import { scheduleMicrotask } from './utils'\n\n// TYPES\n\ntype NotifyCallback = () => void\n\ntype NotifyFunction = (callback: () => void) => void\n\ntype BatchNotifyFunction = (callback: () => void) => void\n\nexport function createNotifyManager() {\n let queue: NotifyCallback[] = []\n let transactions = 0\n let notifyFn: NotifyFunction = (callback) => {\n callback()\n }\n let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => {\n callback()\n }\n\n const batch = <T>(callback: () => T): T => {\n let result\n transactions++\n try {\n result = callback()\n } finally {\n transactions--\n if (!transactions) {\n flush()\n }\n }\n return result\n }\n\n const schedule = (callback: NotifyCallback): void => {\n if (transactions) {\n queue.push(callback)\n } else {\n scheduleMicrotask(() => {\n notifyFn(callback)\n })\n }\n }\n\n /**\n * All calls to the wrapped function will be batched.\n */\n const batchCalls = <T extends Function>(callback: T): T => {\n return ((...args: any[]) => {\n schedule(() => {\n callback(...args)\n })\n }) as any\n }\n\n const flush = (): void => {\n const originalQueue = queue\n queue = []\n if (originalQueue.length) {\n scheduleMicrotask(() => {\n batchNotifyFn(() => {\n originalQueue.forEach((callback) => {\n notifyFn(callback)\n })\n })\n })\n }\n }\n\n /**\n * Use this method to set a custom notify function.\n * This can be used to for example wrap notifications with `React.act` while running tests.\n */\n const setNotifyFunction = (fn: NotifyFunction) => {\n notifyFn = fn\n }\n\n /**\n * Use this method to set a custom function to batch notifications together into a single tick.\n * By default React Query will use the batch function provided by ReactDOM or React Native.\n */\n const setBatchNotifyFunction = (fn: BatchNotifyFunction) => {\n batchNotifyFn = fn\n }\n\n return {\n batch,\n batchCalls,\n schedule,\n setNotifyFunction,\n setBatchNotifyFunction,\n } as const\n}\n\n// SINGLETON\nexport const notifyManager = createNotifyManager()\n"],"names":["createNotifyManager","queue","transactions","notifyFn","callback","batchNotifyFn","batch","result","flush","schedule","push","scheduleMicrotask","batchCalls","args","originalQueue","length","forEach","setNotifyFunction","fn","setBatchNotifyFunction","notifyManager"],"mappings":";;AAEA;;AAQO,SAASA,mBAAmB,GAAG;EACpC,IAAIC,KAAuB,GAAG,EAAE,CAAA;EAChC,IAAIC,YAAY,GAAG,CAAC,CAAA;EACpB,IAAIC,QAAwB,GAAIC,QAAQ,IAAK;AAC3CA,IAAAA,QAAQ,EAAE,CAAA;GACX,CAAA;EACD,IAAIC,aAAkC,GAAID,QAAoB,IAAK;AACjEA,IAAAA,QAAQ,EAAE,CAAA;GACX,CAAA;EAED,MAAME,KAAK,GAAOF,QAAiB,IAAQ;AACzC,IAAA,IAAIG,MAAM,CAAA;AACVL,IAAAA,YAAY,EAAE,CAAA;IACd,IAAI;MACFK,MAAM,GAAGH,QAAQ,EAAE,CAAA;AACrB,KAAC,SAAS;AACRF,MAAAA,YAAY,EAAE,CAAA;MACd,IAAI,CAACA,YAAY,EAAE;AACjBM,QAAAA,KAAK,EAAE,CAAA;AACT,OAAA;AACF,KAAA;AACA,IAAA,OAAOD,MAAM,CAAA;GACd,CAAA;EAED,MAAME,QAAQ,GAAIL,QAAwB,IAAW;AACnD,IAAA,IAAIF,YAAY,EAAE;AAChBD,MAAAA,KAAK,CAACS,IAAI,CAACN,QAAQ,CAAC,CAAA;AACtB,KAAC,MAAM;AACLO,MAAAA,iBAAiB,CAAC,MAAM;QACtBR,QAAQ,CAACC,QAAQ,CAAC,CAAA;AACpB,OAAC,CAAC,CAAA;AACJ,KAAA;GACD,CAAA;;AAED;AACF;AACA;EACE,MAAMQ,UAAU,GAAwBR,QAAW,IAAQ;IACzD,OAAQ,CAAC,GAAGS,IAAW,KAAK;AAC1BJ,MAAAA,QAAQ,CAAC,MAAM;QACbL,QAAQ,CAAC,GAAGS,IAAI,CAAC,CAAA;AACnB,OAAC,CAAC,CAAA;KACH,CAAA;GACF,CAAA;EAED,MAAML,KAAK,GAAG,MAAY;IACxB,MAAMM,aAAa,GAAGb,KAAK,CAAA;AAC3BA,IAAAA,KAAK,GAAG,EAAE,CAAA;IACV,IAAIa,aAAa,CAACC,MAAM,EAAE;AACxBJ,MAAAA,iBAAiB,CAAC,MAAM;AACtBN,QAAAA,aAAa,CAAC,MAAM;AAClBS,UAAAA,aAAa,CAACE,OAAO,CAAEZ,QAAQ,IAAK;YAClCD,QAAQ,CAACC,QAAQ,CAAC,CAAA;AACpB,WAAC,CAAC,CAAA;AACJ,SAAC,CAAC,CAAA;AACJ,OAAC,CAAC,CAAA;AACJ,KAAA;GACD,CAAA;;AAED;AACF;AACA;AACA;EACE,MAAMa,iBAAiB,GAAIC,EAAkB,IAAK;AAChDf,IAAAA,QAAQ,GAAGe,EAAE,CAAA;GACd,CAAA;;AAED;AACF;AACA;AACA;EACE,MAAMC,sBAAsB,GAAID,EAAuB,IAAK;AAC1Db,IAAAA,aAAa,GAAGa,EAAE,CAAA;GACnB,CAAA;EAED,OAAO;IACLZ,KAAK;IACLM,UAAU;IACVH,QAAQ;IACRQ,iBAAiB;AACjBE,IAAAA,sBAAAA;GACD,CAAA;AACH,CAAA;;AAEA;AACaC,MAAAA,aAAa,GAAGpB,mBAAmB;;;;"}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { Subscribable } from './subscribable';
|
|
2
2
|
declare type SetupFn = (setOnline: (online?: boolean) => void) => (() => void) | undefined;
|
|
3
3
|
export declare class OnlineManager extends Subscribable {
|
|
4
|
-
private
|
|
5
|
-
private cleanup?;
|
|
6
|
-
private setup;
|
|
4
|
+
#private;
|
|
7
5
|
constructor();
|
|
8
6
|
protected onSubscribe(): void;
|
|
9
7
|
protected onUnsubscribe(): void;
|
|
@@ -2,16 +2,17 @@ import { Subscribable } from './subscribable.esm.js';
|
|
|
2
2
|
import { isServer } from './utils.esm.js';
|
|
3
3
|
|
|
4
4
|
class OnlineManager extends Subscribable {
|
|
5
|
+
#online;
|
|
6
|
+
#cleanup;
|
|
7
|
+
#setup;
|
|
5
8
|
constructor() {
|
|
6
9
|
super();
|
|
7
|
-
|
|
8
|
-
this.setup = onOnline => {
|
|
10
|
+
this.#setup = onOnline => {
|
|
9
11
|
// addEventListener does not exist in React Native, but window does
|
|
10
12
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
11
13
|
if (!isServer && window.addEventListener) {
|
|
12
|
-
const listener = () => onOnline();
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
const listener = () => onOnline();
|
|
15
|
+
// Listen to online
|
|
15
16
|
window.addEventListener('online', listener, false);
|
|
16
17
|
window.addEventListener('offline', listener, false);
|
|
17
18
|
return () => {
|
|
@@ -20,32 +21,24 @@ class OnlineManager extends Subscribable {
|
|
|
20
21
|
window.removeEventListener('offline', listener);
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
|
-
|
|
24
24
|
return;
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
|
|
28
27
|
onSubscribe() {
|
|
29
|
-
if (!this
|
|
30
|
-
this.setEventListener(this
|
|
28
|
+
if (!this.#cleanup) {
|
|
29
|
+
this.setEventListener(this.#setup);
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
|
-
|
|
34
32
|
onUnsubscribe() {
|
|
35
33
|
if (!this.hasListeners()) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
|
|
39
|
-
this.cleanup = undefined;
|
|
34
|
+
this.#cleanup?.();
|
|
35
|
+
this.#cleanup = undefined;
|
|
40
36
|
}
|
|
41
37
|
}
|
|
42
|
-
|
|
43
38
|
setEventListener(setup) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this
|
|
47
|
-
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
|
|
48
|
-
this.cleanup = setup(online => {
|
|
39
|
+
this.#setup = setup;
|
|
40
|
+
this.#cleanup?.();
|
|
41
|
+
this.#cleanup = setup(online => {
|
|
49
42
|
if (typeof online === 'boolean') {
|
|
50
43
|
this.setOnline(online);
|
|
51
44
|
} else {
|
|
@@ -53,33 +46,26 @@ class OnlineManager extends Subscribable {
|
|
|
53
46
|
}
|
|
54
47
|
});
|
|
55
48
|
}
|
|
56
|
-
|
|
57
49
|
setOnline(online) {
|
|
58
|
-
this
|
|
59
|
-
|
|
50
|
+
this.#online = online;
|
|
60
51
|
if (online) {
|
|
61
52
|
this.onOnline();
|
|
62
53
|
}
|
|
63
54
|
}
|
|
64
|
-
|
|
65
55
|
onOnline() {
|
|
66
56
|
this.listeners.forEach(listener => {
|
|
67
57
|
listener();
|
|
68
58
|
});
|
|
69
59
|
}
|
|
70
|
-
|
|
71
60
|
isOnline() {
|
|
72
|
-
if (typeof this
|
|
73
|
-
return this
|
|
61
|
+
if (typeof this.#online === 'boolean') {
|
|
62
|
+
return this.#online;
|
|
74
63
|
}
|
|
75
|
-
|
|
76
64
|
if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
|
|
77
65
|
return true;
|
|
78
66
|
}
|
|
79
|
-
|
|
80
67
|
return navigator.onLine;
|
|
81
68
|
}
|
|
82
|
-
|
|
83
69
|
}
|
|
84
70
|
const onlineManager = new OnlineManager();
|
|
85
71
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"onlineManager.esm.js","sources":["../../src/onlineManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\nimport { isServer } from './utils'\n\ntype SetupFn = (\n setOnline: (online?: boolean) => void,\n) => (() => void) | undefined\n\nexport class OnlineManager extends Subscribable {\n
|
|
1
|
+
{"version":3,"file":"onlineManager.esm.js","sources":["../../src/onlineManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\nimport { isServer } from './utils'\n\ntype SetupFn = (\n setOnline: (online?: boolean) => void,\n) => (() => void) | undefined\n\nexport class OnlineManager extends Subscribable {\n #online?: boolean\n #cleanup?: () => void\n\n #setup: SetupFn\n\n constructor() {\n super()\n this.#setup = (onOnline) => {\n // addEventListener does not exist in React Native, but window does\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!isServer && window.addEventListener) {\n const listener = () => onOnline()\n // Listen to online\n window.addEventListener('online', listener, false)\n window.addEventListener('offline', listener, false)\n\n return () => {\n // Be sure to unsubscribe if a new handler is set\n window.removeEventListener('online', listener)\n window.removeEventListener('offline', listener)\n }\n }\n\n return\n }\n }\n\n protected onSubscribe(): void {\n if (!this.#cleanup) {\n this.setEventListener(this.#setup)\n }\n }\n\n protected onUnsubscribe() {\n if (!this.hasListeners()) {\n this.#cleanup?.()\n this.#cleanup = undefined\n }\n }\n\n setEventListener(setup: SetupFn): void {\n this.#setup = setup\n this.#cleanup?.()\n this.#cleanup = setup((online?: boolean) => {\n if (typeof online === 'boolean') {\n this.setOnline(online)\n } else {\n this.onOnline()\n }\n })\n }\n\n setOnline(online?: boolean): void {\n this.#online = online\n\n if (online) {\n this.onOnline()\n }\n }\n\n onOnline(): void {\n this.listeners.forEach((listener) => {\n listener()\n })\n }\n\n isOnline(): boolean {\n if (typeof this.#online === 'boolean') {\n return this.#online\n }\n\n if (\n typeof navigator === 'undefined' ||\n typeof navigator.onLine === 'undefined'\n ) {\n return true\n }\n\n return navigator.onLine\n }\n}\n\nexport const onlineManager = new OnlineManager()\n"],"names":["OnlineManager","Subscribable","constructor","onOnline","isServer","window","addEventListener","listener","removeEventListener","onSubscribe","setEventListener","onUnsubscribe","hasListeners","undefined","setup","online","setOnline","listeners","forEach","isOnline","navigator","onLine","onlineManager"],"mappings":";;;AAOO,MAAMA,aAAa,SAASC,YAAY,CAAC;AAC9C,EAAA,OAAO,CAAA;AACP,EAAA,QAAQ,CAAA;AAER,EAAA,MAAM,CAAA;AAENC,EAAAA,WAAW,GAAG;AACZ,IAAA,KAAK,EAAE,CAAA;AACP,IAAA,IAAI,CAAC,MAAM,GAAIC,QAAQ,IAAK;AAC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AE,CAAA;AACjB,OAAA;AACF,KAAC,CAAC,CAAA;AACJ,GAAA;EAEAa,SAAS,CAACD,MAAgB,EAAQ;AAChC,IAAA,IAAI,CAAC,OAAO,GAAGA,MAAM,CAAA;AAErB,IAAA,IAAIA,MAAM,EAAE;MACV,IAAI,CAACZ,QAAQ,EAAE,CAAA;AACjB,KAAA;AACF,GAAA;AAEAA,EAAAA,QAAQ,GAAS;AACf,IAAA,IAAI,CAACc,SAAS,CAACC,OAAO,CAAEX,QAAQ,IAAK;AACnCA,MAAAA,QAAQ,EAAE,CAAA;AACZ,KAAC,CAAC,CAAA;AACJ,GAAA;AAEAY,EAAAA,QAAQ,GAAY;AAClB,IAAA,IAAI,OAAO,IAAI,CAAC,OAAO,KAAK,SAAS,EAAE;MACrC,OAAO,IAAI,CAAC,OAAO,CAAA;AACrB,KAAA;IAEA,IACE,OAAOC,SAAS,KAAK,WAAW,IAChC,OAAOA,SAAS,CAACC,MAAM,KAAK,WAAW,EACvC;AACA,MAAA,OAAO,IAAI,CAAA;AACb,KAAA;IAEA,OAAOD,SAAS,CAACC,MAAM,CAAA;AACzB,GAAA;AACF,CAAA;AAEaC,MAAAA,aAAa,GAAG,IAAItB,aAAa;;;;"}
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var subscribable = require('./subscribable.js');
|
|
6
4
|
var utils = require('./utils.js');
|
|
7
5
|
|
|
8
6
|
class OnlineManager extends subscribable.Subscribable {
|
|
7
|
+
#online;
|
|
8
|
+
#cleanup;
|
|
9
|
+
#setup;
|
|
9
10
|
constructor() {
|
|
10
11
|
super();
|
|
11
|
-
|
|
12
|
-
this.setup = onOnline => {
|
|
12
|
+
this.#setup = onOnline => {
|
|
13
13
|
// addEventListener does not exist in React Native, but window does
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
15
15
|
if (!utils.isServer && window.addEventListener) {
|
|
16
|
-
const listener = () => onOnline();
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
const listener = () => onOnline();
|
|
17
|
+
// Listen to online
|
|
19
18
|
window.addEventListener('online', listener, false);
|
|
20
19
|
window.addEventListener('offline', listener, false);
|
|
21
20
|
return () => {
|
|
@@ -24,32 +23,24 @@ class OnlineManager extends subscribable.Subscribable {
|
|
|
24
23
|
window.removeEventListener('offline', listener);
|
|
25
24
|
};
|
|
26
25
|
}
|
|
27
|
-
|
|
28
26
|
return;
|
|
29
27
|
};
|
|
30
28
|
}
|
|
31
|
-
|
|
32
29
|
onSubscribe() {
|
|
33
|
-
if (!this
|
|
34
|
-
this.setEventListener(this
|
|
30
|
+
if (!this.#cleanup) {
|
|
31
|
+
this.setEventListener(this.#setup);
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
|
-
|
|
38
34
|
onUnsubscribe() {
|
|
39
35
|
if (!this.hasListeners()) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
|
|
43
|
-
this.cleanup = undefined;
|
|
36
|
+
this.#cleanup?.();
|
|
37
|
+
this.#cleanup = undefined;
|
|
44
38
|
}
|
|
45
39
|
}
|
|
46
|
-
|
|
47
40
|
setEventListener(setup) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
this
|
|
51
|
-
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
|
|
52
|
-
this.cleanup = setup(online => {
|
|
41
|
+
this.#setup = setup;
|
|
42
|
+
this.#cleanup?.();
|
|
43
|
+
this.#cleanup = setup(online => {
|
|
53
44
|
if (typeof online === 'boolean') {
|
|
54
45
|
this.setOnline(online);
|
|
55
46
|
} else {
|
|
@@ -57,33 +48,26 @@ class OnlineManager extends subscribable.Subscribable {
|
|
|
57
48
|
}
|
|
58
49
|
});
|
|
59
50
|
}
|
|
60
|
-
|
|
61
51
|
setOnline(online) {
|
|
62
|
-
this
|
|
63
|
-
|
|
52
|
+
this.#online = online;
|
|
64
53
|
if (online) {
|
|
65
54
|
this.onOnline();
|
|
66
55
|
}
|
|
67
56
|
}
|
|
68
|
-
|
|
69
57
|
onOnline() {
|
|
70
58
|
this.listeners.forEach(listener => {
|
|
71
59
|
listener();
|
|
72
60
|
});
|
|
73
61
|
}
|
|
74
|
-
|
|
75
62
|
isOnline() {
|
|
76
|
-
if (typeof this
|
|
77
|
-
return this
|
|
63
|
+
if (typeof this.#online === 'boolean') {
|
|
64
|
+
return this.#online;
|
|
78
65
|
}
|
|
79
|
-
|
|
80
66
|
if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
|
|
81
67
|
return true;
|
|
82
68
|
}
|
|
83
|
-
|
|
84
69
|
return navigator.onLine;
|
|
85
70
|
}
|
|
86
|
-
|
|
87
71
|
}
|
|
88
72
|
const onlineManager = new OnlineManager();
|
|
89
73
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"onlineManager.js","sources":["../../src/onlineManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\nimport { isServer } from './utils'\n\ntype SetupFn = (\n setOnline: (online?: boolean) => void,\n) => (() => void) | undefined\n\nexport class OnlineManager extends Subscribable {\n
|
|
1
|
+
{"version":3,"file":"onlineManager.js","sources":["../../src/onlineManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\nimport { isServer } from './utils'\n\ntype SetupFn = (\n setOnline: (online?: boolean) => void,\n) => (() => void) | undefined\n\nexport class OnlineManager extends Subscribable {\n #online?: boolean\n #cleanup?: () => void\n\n #setup: SetupFn\n\n constructor() {\n super()\n this.#setup = (onOnline) => {\n // addEventListener does not exist in React Native, but window does\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!isServer && window.addEventListener) {\n const listener = () => onOnline()\n // Listen to online\n window.addEventListener('online', listener, false)\n window.addEventListener('offline', listener, false)\n\n return () => {\n // Be sure to unsubscribe if a new handler is set\n window.removeEventListener('online', listener)\n window.removeEventListener('offline', listener)\n }\n }\n\n return\n }\n }\n\n protected onSubscribe(): void {\n if (!this.#cleanup) {\n this.setEventListener(this.#setup)\n }\n }\n\n protected onUnsubscribe() {\n if (!this.hasListeners()) {\n this.#cleanup?.()\n this.#cleanup = undefined\n }\n }\n\n setEventListener(setup: SetupFn): void {\n this.#setup = setup\n this.#cleanup?.()\n this.#cleanup = setup((online?: boolean) => {\n if (typeof online === 'boolean') {\n this.setOnline(online)\n } else {\n this.onOnline()\n }\n })\n }\n\n setOnline(online?: boolean): void {\n this.#online = online\n\n if (online) {\n this.onOnline()\n }\n }\n\n onOnline(): void {\n this.listeners.forEach((listener) => {\n listener()\n })\n }\n\n isOnline(): boolean {\n if (typeof this.#online === 'boolean') {\n return this.#online\n }\n\n if (\n typeof navigator === 'undefined' ||\n typeof navigator.onLine === 'undefined'\n ) {\n return true\n }\n\n return navigator.onLine\n }\n}\n\nexport const onlineManager = new OnlineManager()\n"],"names":["OnlineManager","Subscribable","constructor","onOnline","isServer","window","addEventListener","listener","removeEventListener","onSubscribe","setEventListener","onUnsubscribe","hasListeners","undefined","setup","online","setOnline","listeners","forEach","isOnline","navigator","onLine","onlineManager"],"mappings":";;;;;AAOO,MAAMA,aAAa,SAASC,yBAAY,CAAC;AAC9C,EAAA,OAAO,CAAA;AACP,EAAA,QAAQ,CAAA;AAER,EAAA,MAAM,CAAA;AAENC,EAAAA,WAAW,GAAG;AACZ,IAAA,KAAK,EAAE,CAAA;AACP,IAAA,IAAI,CAAC,MAAM,GAAIC,QAAQ,IAAK;AAC1B;AACA;AACA,MAAA,IAAI,CAACC,cAAQ,IAAIC,MAAM,CAACC,gBAAgB,EAAE;AACxC,QAAA,MAAMC,QAAQ,GAAG,MAAMJ,QAAQ,EAAE,CAAA;AACjC;QACAE,MAAM,CAACC,gBAAgB,CAAC,QAAQ,EAAEC,QAAQ,EAAE,KAAK,CAAC,CAAA;QAClDF,MAAM,CAACC,gBAAgB,CA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AAQ,GAAS;AACf,IAAA,IAAI,CAACc,SAAS,CAACC,OAAO,CAAEX,QAAQ,IAAK;AACnCA,MAAAA,QAAQ,EAAE,CAAA;AACZ,KAAC,CAAC,CAAA;AACJ,GAAA;AAEAY,EAAAA,QAAQ,GAAY;AAClB,IAAA,IAAI,OAAO,IAAI,CAAC,OAAO,KAAK,SAAS,EAAE;MACrC,OAAO,IAAI,CAAC,OAAO,CAAA;AACrB,KAAA;IAEA,IACE,OAAOC,SAAS,KAAK,WAAW,IAChC,OAAOA,SAAS,CAACC,MAAM,KAAK,WAAW,EACvC;AACA,MAAA,OAAO,IAAI,CAAA;AACb,KAAA;IAEA,OAAOD,SAAS,CAACC,MAAM,CAAA;AACzB,GAAA;AACF,CAAA;AAEaC,MAAAA,aAAa,GAAG,IAAItB,aAAa;;;;;"}
|
|
@@ -2,16 +2,17 @@ import { Subscribable } from './subscribable.mjs';
|
|
|
2
2
|
import { isServer } from './utils.mjs';
|
|
3
3
|
|
|
4
4
|
class OnlineManager extends Subscribable {
|
|
5
|
+
#online;
|
|
6
|
+
#cleanup;
|
|
7
|
+
#setup;
|
|
5
8
|
constructor() {
|
|
6
9
|
super();
|
|
7
|
-
|
|
8
|
-
this.setup = onOnline => {
|
|
10
|
+
this.#setup = onOnline => {
|
|
9
11
|
// addEventListener does not exist in React Native, but window does
|
|
10
12
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
11
13
|
if (!isServer && window.addEventListener) {
|
|
12
|
-
const listener = () => onOnline();
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
const listener = () => onOnline();
|
|
15
|
+
// Listen to online
|
|
15
16
|
window.addEventListener('online', listener, false);
|
|
16
17
|
window.addEventListener('offline', listener, false);
|
|
17
18
|
return () => {
|
|
@@ -20,32 +21,24 @@ class OnlineManager extends Subscribable {
|
|
|
20
21
|
window.removeEventListener('offline', listener);
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
|
-
|
|
24
24
|
return;
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
|
|
28
27
|
onSubscribe() {
|
|
29
|
-
if (!this
|
|
30
|
-
this.setEventListener(this
|
|
28
|
+
if (!this.#cleanup) {
|
|
29
|
+
this.setEventListener(this.#setup);
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
|
-
|
|
34
32
|
onUnsubscribe() {
|
|
35
33
|
if (!this.hasListeners()) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
|
|
39
|
-
this.cleanup = undefined;
|
|
34
|
+
this.#cleanup?.();
|
|
35
|
+
this.#cleanup = undefined;
|
|
40
36
|
}
|
|
41
37
|
}
|
|
42
|
-
|
|
43
38
|
setEventListener(setup) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this
|
|
47
|
-
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
|
|
48
|
-
this.cleanup = setup(online => {
|
|
39
|
+
this.#setup = setup;
|
|
40
|
+
this.#cleanup?.();
|
|
41
|
+
this.#cleanup = setup(online => {
|
|
49
42
|
if (typeof online === 'boolean') {
|
|
50
43
|
this.setOnline(online);
|
|
51
44
|
} else {
|
|
@@ -53,33 +46,26 @@ class OnlineManager extends Subscribable {
|
|
|
53
46
|
}
|
|
54
47
|
});
|
|
55
48
|
}
|
|
56
|
-
|
|
57
49
|
setOnline(online) {
|
|
58
|
-
this
|
|
59
|
-
|
|
50
|
+
this.#online = online;
|
|
60
51
|
if (online) {
|
|
61
52
|
this.onOnline();
|
|
62
53
|
}
|
|
63
54
|
}
|
|
64
|
-
|
|
65
55
|
onOnline() {
|
|
66
56
|
this.listeners.forEach(listener => {
|
|
67
57
|
listener();
|
|
68
58
|
});
|
|
69
59
|
}
|
|
70
|
-
|
|
71
60
|
isOnline() {
|
|
72
|
-
if (typeof this
|
|
73
|
-
return this
|
|
61
|
+
if (typeof this.#online === 'boolean') {
|
|
62
|
+
return this.#online;
|
|
74
63
|
}
|
|
75
|
-
|
|
76
64
|
if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
|
|
77
65
|
return true;
|
|
78
66
|
}
|
|
79
|
-
|
|
80
67
|
return navigator.onLine;
|
|
81
68
|
}
|
|
82
|
-
|
|
83
69
|
}
|
|
84
70
|
const onlineManager = new OnlineManager();
|
|
85
71
|
|