@reactuses/core 5.0.22 → 5.0.23-beta.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/dist/index.cjs +245 -48
- package/dist/index.d.cts +159 -1
- package/dist/index.d.mts +159 -1
- package/dist/index.d.ts +159 -1
- package/dist/index.mjs +245 -49
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { isEqual, debounce, throttle } from 'lodash-es';
|
|
|
3
3
|
import Cookies from 'js-cookie';
|
|
4
4
|
import screenfull from 'screenfull';
|
|
5
5
|
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
|
|
6
|
+
import { fetchEventSource } from '@microsoft/fetch-event-source';
|
|
6
7
|
|
|
7
8
|
const useLatest = (value)=>{
|
|
8
9
|
const ref = useRef(value);
|
|
@@ -145,7 +146,7 @@ function useMountedState() {
|
|
|
145
146
|
return get;
|
|
146
147
|
}
|
|
147
148
|
|
|
148
|
-
function asyncGeneratorStep$
|
|
149
|
+
function asyncGeneratorStep$7(gen, resolve, reject, _next, _throw, key, arg) {
|
|
149
150
|
try {
|
|
150
151
|
var info = gen[key](arg);
|
|
151
152
|
var value = info.value;
|
|
@@ -159,16 +160,16 @@ function asyncGeneratorStep$6(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
159
160
|
Promise.resolve(value).then(_next, _throw);
|
|
160
161
|
}
|
|
161
162
|
}
|
|
162
|
-
function _async_to_generator$
|
|
163
|
+
function _async_to_generator$7(fn) {
|
|
163
164
|
return function() {
|
|
164
165
|
var self = this, args = arguments;
|
|
165
166
|
return new Promise(function(resolve, reject) {
|
|
166
167
|
var gen = fn.apply(self, args);
|
|
167
168
|
function _next(value) {
|
|
168
|
-
asyncGeneratorStep$
|
|
169
|
+
asyncGeneratorStep$7(gen, resolve, reject, _next, _throw, "next", value);
|
|
169
170
|
}
|
|
170
171
|
function _throw(err) {
|
|
171
|
-
asyncGeneratorStep$
|
|
172
|
+
asyncGeneratorStep$7(gen, resolve, reject, _next, _throw, "throw", err);
|
|
172
173
|
}
|
|
173
174
|
_next(undefined);
|
|
174
175
|
});
|
|
@@ -177,7 +178,7 @@ function _async_to_generator$6(fn) {
|
|
|
177
178
|
const useAsyncEffect = (effect, cleanup = noop, deps)=>{
|
|
178
179
|
const mounted = useMountedState();
|
|
179
180
|
useEffect(()=>{
|
|
180
|
-
const execute = /*#__PURE__*/ _async_to_generator$
|
|
181
|
+
const execute = /*#__PURE__*/ _async_to_generator$7(function*() {
|
|
181
182
|
if (!mounted()) {
|
|
182
183
|
return;
|
|
183
184
|
}
|
|
@@ -1196,7 +1197,7 @@ function useSupported(callback, sync = false) {
|
|
|
1196
1197
|
return supported;
|
|
1197
1198
|
}
|
|
1198
1199
|
|
|
1199
|
-
function asyncGeneratorStep$
|
|
1200
|
+
function asyncGeneratorStep$6(gen, resolve, reject, _next, _throw, key, arg) {
|
|
1200
1201
|
try {
|
|
1201
1202
|
var info = gen[key](arg);
|
|
1202
1203
|
var value = info.value;
|
|
@@ -1210,16 +1211,16 @@ function asyncGeneratorStep$5(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
1210
1211
|
Promise.resolve(value).then(_next, _throw);
|
|
1211
1212
|
}
|
|
1212
1213
|
}
|
|
1213
|
-
function _async_to_generator$
|
|
1214
|
+
function _async_to_generator$6(fn) {
|
|
1214
1215
|
return function() {
|
|
1215
1216
|
var self = this, args = arguments;
|
|
1216
1217
|
return new Promise(function(resolve, reject) {
|
|
1217
1218
|
var gen = fn.apply(self, args);
|
|
1218
1219
|
function _next(value) {
|
|
1219
|
-
asyncGeneratorStep$
|
|
1220
|
+
asyncGeneratorStep$6(gen, resolve, reject, _next, _throw, "next", value);
|
|
1220
1221
|
}
|
|
1221
1222
|
function _throw(err) {
|
|
1222
|
-
asyncGeneratorStep$
|
|
1223
|
+
asyncGeneratorStep$6(gen, resolve, reject, _next, _throw, "throw", err);
|
|
1223
1224
|
}
|
|
1224
1225
|
_next(undefined);
|
|
1225
1226
|
});
|
|
@@ -1227,7 +1228,7 @@ function _async_to_generator$5(fn) {
|
|
|
1227
1228
|
}
|
|
1228
1229
|
const useEyeDropper = ()=>{
|
|
1229
1230
|
const isSupported = useSupported(()=>typeof window !== 'undefined' && 'EyeDropper' in window, true);
|
|
1230
|
-
const open = useCallback(/*#__PURE__*/ _async_to_generator$
|
|
1231
|
+
const open = useCallback(/*#__PURE__*/ _async_to_generator$6(function*(options = {}) {
|
|
1231
1232
|
if (!isSupported) {
|
|
1232
1233
|
return {
|
|
1233
1234
|
sRGBHex: ''
|
|
@@ -1262,7 +1263,7 @@ function useFavicon(href, baseUrl = '', rel = 'icon') {
|
|
|
1262
1263
|
]);
|
|
1263
1264
|
}
|
|
1264
1265
|
|
|
1265
|
-
function asyncGeneratorStep$
|
|
1266
|
+
function asyncGeneratorStep$5(gen, resolve, reject, _next, _throw, key, arg) {
|
|
1266
1267
|
try {
|
|
1267
1268
|
var info = gen[key](arg);
|
|
1268
1269
|
var value = info.value;
|
|
@@ -1276,23 +1277,23 @@ function asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
1276
1277
|
Promise.resolve(value).then(_next, _throw);
|
|
1277
1278
|
}
|
|
1278
1279
|
}
|
|
1279
|
-
function _async_to_generator$
|
|
1280
|
+
function _async_to_generator$5(fn) {
|
|
1280
1281
|
return function() {
|
|
1281
1282
|
var self = this, args = arguments;
|
|
1282
1283
|
return new Promise(function(resolve, reject) {
|
|
1283
1284
|
var gen = fn.apply(self, args);
|
|
1284
1285
|
function _next(value) {
|
|
1285
|
-
asyncGeneratorStep$
|
|
1286
|
+
asyncGeneratorStep$5(gen, resolve, reject, _next, _throw, "next", value);
|
|
1286
1287
|
}
|
|
1287
1288
|
function _throw(err) {
|
|
1288
|
-
asyncGeneratorStep$
|
|
1289
|
+
asyncGeneratorStep$5(gen, resolve, reject, _next, _throw, "throw", err);
|
|
1289
1290
|
}
|
|
1290
1291
|
_next(undefined);
|
|
1291
1292
|
});
|
|
1292
1293
|
};
|
|
1293
1294
|
}
|
|
1294
|
-
function _extends$
|
|
1295
|
-
_extends$
|
|
1295
|
+
function _extends$3() {
|
|
1296
|
+
_extends$3 = Object.assign || function(target) {
|
|
1296
1297
|
for(var i = 1; i < arguments.length; i++){
|
|
1297
1298
|
var source = arguments[i];
|
|
1298
1299
|
for(var key in source){
|
|
@@ -1303,7 +1304,7 @@ function _extends$2() {
|
|
|
1303
1304
|
}
|
|
1304
1305
|
return target;
|
|
1305
1306
|
};
|
|
1306
|
-
return _extends$
|
|
1307
|
+
return _extends$3.apply(this, arguments);
|
|
1307
1308
|
}
|
|
1308
1309
|
const DEFAULT_OPTIONS = {
|
|
1309
1310
|
multiple: true,
|
|
@@ -1328,11 +1329,11 @@ const useFileDialog = (options = defaultOptions$1)=>{
|
|
|
1328
1329
|
return input;
|
|
1329
1330
|
}, []);
|
|
1330
1331
|
inputRef.current = initFn();
|
|
1331
|
-
const open = /*#__PURE__*/ _async_to_generator$
|
|
1332
|
+
const open = /*#__PURE__*/ _async_to_generator$5(function*(localOptions) {
|
|
1332
1333
|
if (!inputRef.current) {
|
|
1333
1334
|
return;
|
|
1334
1335
|
}
|
|
1335
|
-
const _options = _extends$
|
|
1336
|
+
const _options = _extends$3({}, DEFAULT_OPTIONS, options, localOptions);
|
|
1336
1337
|
inputRef.current.multiple = _options.multiple;
|
|
1337
1338
|
inputRef.current.accept = _options.accept;
|
|
1338
1339
|
inputRef.current.capture = _options.capture;
|
|
@@ -1738,7 +1739,7 @@ const createUpdateEffect = (hook)=>(effect, deps)=>{
|
|
|
1738
1739
|
|
|
1739
1740
|
const useUpdateEffect = createUpdateEffect(useEffect);
|
|
1740
1741
|
|
|
1741
|
-
function asyncGeneratorStep$
|
|
1742
|
+
function asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, key, arg) {
|
|
1742
1743
|
try {
|
|
1743
1744
|
var info = gen[key](arg);
|
|
1744
1745
|
var value = info.value;
|
|
@@ -1752,23 +1753,23 @@ function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
1752
1753
|
Promise.resolve(value).then(_next, _throw);
|
|
1753
1754
|
}
|
|
1754
1755
|
}
|
|
1755
|
-
function _async_to_generator$
|
|
1756
|
+
function _async_to_generator$4(fn) {
|
|
1756
1757
|
return function() {
|
|
1757
1758
|
var self = this, args = arguments;
|
|
1758
1759
|
return new Promise(function(resolve, reject) {
|
|
1759
1760
|
var gen = fn.apply(self, args);
|
|
1760
1761
|
function _next(value) {
|
|
1761
|
-
asyncGeneratorStep$
|
|
1762
|
+
asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, "next", value);
|
|
1762
1763
|
}
|
|
1763
1764
|
function _throw(err) {
|
|
1764
|
-
asyncGeneratorStep$
|
|
1765
|
+
asyncGeneratorStep$4(gen, resolve, reject, _next, _throw, "throw", err);
|
|
1765
1766
|
}
|
|
1766
1767
|
_next(undefined);
|
|
1767
1768
|
});
|
|
1768
1769
|
};
|
|
1769
1770
|
}
|
|
1770
|
-
function _extends$
|
|
1771
|
-
_extends$
|
|
1771
|
+
function _extends$2() {
|
|
1772
|
+
_extends$2 = Object.assign || function(target) {
|
|
1772
1773
|
for(var i = 1; i < arguments.length; i++){
|
|
1773
1774
|
var source = arguments[i];
|
|
1774
1775
|
for(var key in source){
|
|
@@ -1779,22 +1780,22 @@ function _extends$1() {
|
|
|
1779
1780
|
}
|
|
1780
1781
|
return target;
|
|
1781
1782
|
};
|
|
1782
|
-
return _extends$
|
|
1783
|
+
return _extends$2.apply(this, arguments);
|
|
1783
1784
|
}
|
|
1784
1785
|
const useInfiniteScroll = (target, onLoadMore, options = defaultOptions$1)=>{
|
|
1785
1786
|
const savedLoadMore = useLatest(onLoadMore);
|
|
1786
1787
|
var _options_direction;
|
|
1787
1788
|
const direction = (_options_direction = options.direction) != null ? _options_direction : 'bottom';
|
|
1788
1789
|
var _options_distance;
|
|
1789
|
-
const state = useScroll(target, _extends$
|
|
1790
|
-
offset: _extends$
|
|
1790
|
+
const state = useScroll(target, _extends$2({}, options, {
|
|
1791
|
+
offset: _extends$2({
|
|
1791
1792
|
[direction]: (_options_distance = options.distance) != null ? _options_distance : 0
|
|
1792
1793
|
}, options.offset)
|
|
1793
1794
|
}));
|
|
1794
1795
|
const di = state[3][direction];
|
|
1795
1796
|
useUpdateEffect(()=>{
|
|
1796
1797
|
const element = getTargetElement(target);
|
|
1797
|
-
const fn = /*#__PURE__*/ _async_to_generator$
|
|
1798
|
+
const fn = /*#__PURE__*/ _async_to_generator$4(function*() {
|
|
1798
1799
|
var _element_scrollHeight, _element_scrollWidth;
|
|
1799
1800
|
const previous = {
|
|
1800
1801
|
height: (_element_scrollHeight = element == null ? void 0 : element.scrollHeight) != null ? _element_scrollHeight : 0,
|
|
@@ -1944,7 +1945,7 @@ const useMeasure = (target, options = defaultOptions$1)=>{
|
|
|
1944
1945
|
];
|
|
1945
1946
|
};
|
|
1946
1947
|
|
|
1947
|
-
function asyncGeneratorStep$
|
|
1948
|
+
function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
|
|
1948
1949
|
try {
|
|
1949
1950
|
var info = gen[key](arg);
|
|
1950
1951
|
var value = info.value;
|
|
@@ -1958,16 +1959,16 @@ function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
1958
1959
|
Promise.resolve(value).then(_next, _throw);
|
|
1959
1960
|
}
|
|
1960
1961
|
}
|
|
1961
|
-
function _async_to_generator$
|
|
1962
|
+
function _async_to_generator$3(fn) {
|
|
1962
1963
|
return function() {
|
|
1963
1964
|
var self = this, args = arguments;
|
|
1964
1965
|
return new Promise(function(resolve, reject) {
|
|
1965
1966
|
var gen = fn.apply(self, args);
|
|
1966
1967
|
function _next(value) {
|
|
1967
|
-
asyncGeneratorStep$
|
|
1968
|
+
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value);
|
|
1968
1969
|
}
|
|
1969
1970
|
function _throw(err) {
|
|
1970
|
-
asyncGeneratorStep$
|
|
1971
|
+
asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err);
|
|
1971
1972
|
}
|
|
1972
1973
|
_next(undefined);
|
|
1973
1974
|
});
|
|
@@ -2001,7 +2002,7 @@ const useMediaDevices = (options = {})=>{
|
|
|
2001
2002
|
});
|
|
2002
2003
|
}).catch(noop);
|
|
2003
2004
|
}, []);
|
|
2004
|
-
const ensurePermissions = useCallback(/*#__PURE__*/ _async_to_generator$
|
|
2005
|
+
const ensurePermissions = useCallback(/*#__PURE__*/ _async_to_generator$3(function*() {
|
|
2005
2006
|
if (!isSupported) {
|
|
2006
2007
|
return false;
|
|
2007
2008
|
}
|
|
@@ -2884,8 +2885,8 @@ function useSessionStorage(key, defaultValue, options = defaultOptions$1) {
|
|
|
2884
2885
|
return useStorage(key, defaultValue, ()=>isBrowser ? sessionStorage : undefined, options);
|
|
2885
2886
|
}
|
|
2886
2887
|
|
|
2887
|
-
function _extends() {
|
|
2888
|
-
_extends = Object.assign || function(target) {
|
|
2888
|
+
function _extends$1() {
|
|
2889
|
+
_extends$1 = Object.assign || function(target) {
|
|
2889
2890
|
for(var i = 1; i < arguments.length; i++){
|
|
2890
2891
|
var source = arguments[i];
|
|
2891
2892
|
for(var key in source){
|
|
@@ -2896,11 +2897,11 @@ function _extends() {
|
|
|
2896
2897
|
}
|
|
2897
2898
|
return target;
|
|
2898
2899
|
};
|
|
2899
|
-
return _extends.apply(this, arguments);
|
|
2900
|
+
return _extends$1.apply(this, arguments);
|
|
2900
2901
|
}
|
|
2901
2902
|
const useSetState = (initialState)=>{
|
|
2902
2903
|
const [state, _setState] = useState(initialState);
|
|
2903
|
-
const setState = useCallback((statePartial)=>_setState((current)=>_extends({}, current, typeof statePartial === 'function' ? statePartial(current) : statePartial)), []);
|
|
2904
|
+
const setState = useCallback((statePartial)=>_setState((current)=>_extends$1({}, current, typeof statePartial === 'function' ? statePartial(current) : statePartial)), []);
|
|
2904
2905
|
return [
|
|
2905
2906
|
state,
|
|
2906
2907
|
setState
|
|
@@ -3086,7 +3087,7 @@ const useToggle = (initialValue)=>{
|
|
|
3086
3087
|
|
|
3087
3088
|
const useUpdateLayoutEffect = createUpdateEffect(useLayoutEffect);
|
|
3088
3089
|
|
|
3089
|
-
function asyncGeneratorStep$
|
|
3090
|
+
function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
|
|
3090
3091
|
try {
|
|
3091
3092
|
var info = gen[key](arg);
|
|
3092
3093
|
var value = info.value;
|
|
@@ -3100,16 +3101,16 @@ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
3100
3101
|
Promise.resolve(value).then(_next, _throw);
|
|
3101
3102
|
}
|
|
3102
3103
|
}
|
|
3103
|
-
function _async_to_generator$
|
|
3104
|
+
function _async_to_generator$2(fn) {
|
|
3104
3105
|
return function() {
|
|
3105
3106
|
var self = this, args = arguments;
|
|
3106
3107
|
return new Promise(function(resolve, reject) {
|
|
3107
3108
|
var gen = fn.apply(self, args);
|
|
3108
3109
|
function _next(value) {
|
|
3109
|
-
asyncGeneratorStep$
|
|
3110
|
+
asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value);
|
|
3110
3111
|
}
|
|
3111
3112
|
function _throw(err) {
|
|
3112
|
-
asyncGeneratorStep$
|
|
3113
|
+
asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err);
|
|
3113
3114
|
}
|
|
3114
3115
|
_next(undefined);
|
|
3115
3116
|
});
|
|
@@ -3139,7 +3140,7 @@ const useWebNotification = (requestPermissions = false)=>{
|
|
|
3139
3140
|
}, [
|
|
3140
3141
|
isSupported
|
|
3141
3142
|
]);
|
|
3142
|
-
const ensurePermissions = useCallback(/*#__PURE__*/ _async_to_generator$
|
|
3143
|
+
const ensurePermissions = useCallback(/*#__PURE__*/ _async_to_generator$2(function*() {
|
|
3143
3144
|
if (!isSupported) return;
|
|
3144
3145
|
if (!permissionGranted.current && Notification.permission !== 'denied') {
|
|
3145
3146
|
const result = yield Notification.requestPermission();
|
|
@@ -3250,7 +3251,7 @@ const useWindowSize = ()=>{
|
|
|
3250
3251
|
};
|
|
3251
3252
|
};
|
|
3252
3253
|
|
|
3253
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
3254
|
+
function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
|
|
3254
3255
|
try {
|
|
3255
3256
|
var info = gen[key](arg);
|
|
3256
3257
|
var value = info.value;
|
|
@@ -3264,16 +3265,16 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
|
3264
3265
|
Promise.resolve(value).then(_next, _throw);
|
|
3265
3266
|
}
|
|
3266
3267
|
}
|
|
3267
|
-
function _async_to_generator(fn) {
|
|
3268
|
+
function _async_to_generator$1(fn) {
|
|
3268
3269
|
return function() {
|
|
3269
3270
|
var self = this, args = arguments;
|
|
3270
3271
|
return new Promise(function(resolve, reject) {
|
|
3271
3272
|
var gen = fn.apply(self, args);
|
|
3272
3273
|
function _next(value) {
|
|
3273
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
3274
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
|
|
3274
3275
|
}
|
|
3275
3276
|
function _throw(err) {
|
|
3276
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
3277
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
|
|
3277
3278
|
}
|
|
3278
3279
|
_next(undefined);
|
|
3279
3280
|
});
|
|
@@ -3288,7 +3289,7 @@ const useClipboard = ()=>{
|
|
|
3288
3289
|
}, []);
|
|
3289
3290
|
useEventListener('copy', updateText);
|
|
3290
3291
|
useEventListener('cut', updateText);
|
|
3291
|
-
const copy = useCallback(/*#__PURE__*/ _async_to_generator(function*(txt) {
|
|
3292
|
+
const copy = useCallback(/*#__PURE__*/ _async_to_generator$1(function*(txt) {
|
|
3292
3293
|
setText(txt);
|
|
3293
3294
|
yield window.navigator.clipboard.writeText(txt);
|
|
3294
3295
|
}), []);
|
|
@@ -3777,4 +3778,199 @@ const useElementByPoint = (options)=>{
|
|
|
3777
3778
|
};
|
|
3778
3779
|
};
|
|
3779
3780
|
|
|
3780
|
-
|
|
3781
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
3782
|
+
try {
|
|
3783
|
+
var info = gen[key](arg);
|
|
3784
|
+
var value = info.value;
|
|
3785
|
+
} catch (error) {
|
|
3786
|
+
reject(error);
|
|
3787
|
+
return;
|
|
3788
|
+
}
|
|
3789
|
+
if (info.done) {
|
|
3790
|
+
resolve(value);
|
|
3791
|
+
} else {
|
|
3792
|
+
Promise.resolve(value).then(_next, _throw);
|
|
3793
|
+
}
|
|
3794
|
+
}
|
|
3795
|
+
function _async_to_generator(fn) {
|
|
3796
|
+
return function() {
|
|
3797
|
+
var self = this, args = arguments;
|
|
3798
|
+
return new Promise(function(resolve, reject) {
|
|
3799
|
+
var gen = fn.apply(self, args);
|
|
3800
|
+
function _next(value) {
|
|
3801
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
3802
|
+
}
|
|
3803
|
+
function _throw(err) {
|
|
3804
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
3805
|
+
}
|
|
3806
|
+
_next(undefined);
|
|
3807
|
+
});
|
|
3808
|
+
};
|
|
3809
|
+
}
|
|
3810
|
+
function _extends() {
|
|
3811
|
+
_extends = Object.assign || function(target) {
|
|
3812
|
+
for(var i = 1; i < arguments.length; i++){
|
|
3813
|
+
var source = arguments[i];
|
|
3814
|
+
for(var key in source){
|
|
3815
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
3816
|
+
target[key] = source[key];
|
|
3817
|
+
}
|
|
3818
|
+
}
|
|
3819
|
+
}
|
|
3820
|
+
return target;
|
|
3821
|
+
};
|
|
3822
|
+
return _extends.apply(this, arguments);
|
|
3823
|
+
}
|
|
3824
|
+
function _object_without_properties_loose(source, excluded) {
|
|
3825
|
+
if (source == null) return {};
|
|
3826
|
+
var target = {};
|
|
3827
|
+
var sourceKeys = Object.keys(source);
|
|
3828
|
+
var key, i;
|
|
3829
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
3830
|
+
key = sourceKeys[i];
|
|
3831
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
3832
|
+
target[key] = source[key];
|
|
3833
|
+
}
|
|
3834
|
+
return target;
|
|
3835
|
+
}
|
|
3836
|
+
const useFetchEventSource = (url, options = {})=>{
|
|
3837
|
+
const [data, setData] = useState(null);
|
|
3838
|
+
const [error, setError] = useState(null);
|
|
3839
|
+
const [status, setStatus] = useState("DISCONNECTED");
|
|
3840
|
+
const [event, setEvent] = useState(null);
|
|
3841
|
+
const [lastEventId, setLastEventId] = useState(null);
|
|
3842
|
+
const retries = useRef(0);
|
|
3843
|
+
const abortController = useRef(null);
|
|
3844
|
+
const explicitlyClosed = useRef(false);
|
|
3845
|
+
const close = useEvent(()=>{
|
|
3846
|
+
if (!explicitlyClosed.current) {
|
|
3847
|
+
var _abortController_current;
|
|
3848
|
+
explicitlyClosed.current = true;
|
|
3849
|
+
(_abortController_current = abortController.current) == null ? void 0 : _abortController_current.abort();
|
|
3850
|
+
abortController.current = null;
|
|
3851
|
+
setStatus("DISCONNECTED");
|
|
3852
|
+
options.onClose == null ? void 0 : options.onClose.call(options);
|
|
3853
|
+
}
|
|
3854
|
+
});
|
|
3855
|
+
const open = useEvent(/*#__PURE__*/ _async_to_generator(function*() {
|
|
3856
|
+
close();
|
|
3857
|
+
setStatus("CONNECTING");
|
|
3858
|
+
explicitlyClosed.current = false;
|
|
3859
|
+
retries.current = 0;
|
|
3860
|
+
// 创建新的 AbortController
|
|
3861
|
+
abortController.current = new AbortController();
|
|
3862
|
+
try {
|
|
3863
|
+
// 从选项中提取 FetchEventSourceInit 相关的选项
|
|
3864
|
+
const { immediate, autoReconnect, onOpen, onMessage, onError, onClose, withCredentials, body } = options, fetchOptions = _object_without_properties_loose(options, [
|
|
3865
|
+
"immediate",
|
|
3866
|
+
"autoReconnect",
|
|
3867
|
+
"onOpen",
|
|
3868
|
+
"onMessage",
|
|
3869
|
+
"onError",
|
|
3870
|
+
"onClose",
|
|
3871
|
+
"withCredentials",
|
|
3872
|
+
"body"
|
|
3873
|
+
]);
|
|
3874
|
+
// 构建请求配置
|
|
3875
|
+
const finalOptions = _extends({
|
|
3876
|
+
method: options.method || "GET",
|
|
3877
|
+
headers: _extends({
|
|
3878
|
+
Accept: "text/event-stream",
|
|
3879
|
+
"Cache-Control": "no-cache",
|
|
3880
|
+
Connection: "keep-alive"
|
|
3881
|
+
}, options.headers),
|
|
3882
|
+
signal: abortController.current.signal,
|
|
3883
|
+
credentials: withCredentials ? "include" : "same-origin"
|
|
3884
|
+
}, fetchOptions);
|
|
3885
|
+
// 只在 POST 请求时添加 body
|
|
3886
|
+
if (options.method === "POST" && body) {
|
|
3887
|
+
finalOptions.body = body;
|
|
3888
|
+
finalOptions.headers = _extends({}, finalOptions.headers, {
|
|
3889
|
+
"Content-Type": "application/json"
|
|
3890
|
+
});
|
|
3891
|
+
}
|
|
3892
|
+
yield fetchEventSource(url.toString(), _extends({}, finalOptions, {
|
|
3893
|
+
openWhenHidden: false,
|
|
3894
|
+
onopen (response) {
|
|
3895
|
+
return _async_to_generator(function*() {
|
|
3896
|
+
if (response.ok) {
|
|
3897
|
+
setStatus("CONNECTED");
|
|
3898
|
+
setError(null);
|
|
3899
|
+
options.onOpen == null ? void 0 : options.onOpen.call(options);
|
|
3900
|
+
} else {
|
|
3901
|
+
const error = new Error(`Failed to connect: ${response.status} ${response.statusText}`);
|
|
3902
|
+
setError(error);
|
|
3903
|
+
throw error;
|
|
3904
|
+
}
|
|
3905
|
+
})();
|
|
3906
|
+
},
|
|
3907
|
+
onmessage (msg) {
|
|
3908
|
+
if (!explicitlyClosed.current) {
|
|
3909
|
+
setData(msg.data);
|
|
3910
|
+
var _msg_id;
|
|
3911
|
+
setLastEventId((_msg_id = msg.id) != null ? _msg_id : null);
|
|
3912
|
+
setEvent(msg.event || null);
|
|
3913
|
+
options.onMessage == null ? void 0 : options.onMessage.call(options, msg);
|
|
3914
|
+
}
|
|
3915
|
+
},
|
|
3916
|
+
onerror (err) {
|
|
3917
|
+
setError(err);
|
|
3918
|
+
setStatus("DISCONNECTED");
|
|
3919
|
+
const retryDelay = options.onError == null ? void 0 : options.onError.call(options, err);
|
|
3920
|
+
if (options.autoReconnect && !explicitlyClosed.current) {
|
|
3921
|
+
const { retries: maxRetries = -1, delay = 1000, onFailed } = options.autoReconnect;
|
|
3922
|
+
retries.current += 1;
|
|
3923
|
+
if (typeof maxRetries === "number" && (maxRetries < 0 || retries.current < maxRetries) || typeof maxRetries === "function" && maxRetries()) {
|
|
3924
|
+
return retryDelay != null ? retryDelay : delay;
|
|
3925
|
+
} else {
|
|
3926
|
+
onFailed == null ? void 0 : onFailed();
|
|
3927
|
+
throw err;
|
|
3928
|
+
}
|
|
3929
|
+
}
|
|
3930
|
+
throw err;
|
|
3931
|
+
},
|
|
3932
|
+
onclose () {
|
|
3933
|
+
if (!explicitlyClosed.current) {
|
|
3934
|
+
setStatus("DISCONNECTED");
|
|
3935
|
+
options.onClose == null ? void 0 : options.onClose.call(options);
|
|
3936
|
+
}
|
|
3937
|
+
}
|
|
3938
|
+
}));
|
|
3939
|
+
} catch (err) {
|
|
3940
|
+
// 只处理非主动关闭导致的错误
|
|
3941
|
+
if (!explicitlyClosed.current) {
|
|
3942
|
+
console.error("EventSource Error:", err);
|
|
3943
|
+
setError(err);
|
|
3944
|
+
setStatus("DISCONNECTED");
|
|
3945
|
+
}
|
|
3946
|
+
}
|
|
3947
|
+
}));
|
|
3948
|
+
useEffect(()=>{
|
|
3949
|
+
if (options.immediate !== false) {
|
|
3950
|
+
open();
|
|
3951
|
+
}
|
|
3952
|
+
return ()=>{
|
|
3953
|
+
// 组件卸载时关闭连接
|
|
3954
|
+
close();
|
|
3955
|
+
};
|
|
3956
|
+
}, [
|
|
3957
|
+
open,
|
|
3958
|
+
close,
|
|
3959
|
+
options.immediate
|
|
3960
|
+
]);
|
|
3961
|
+
// 组件卸载时确保连接关闭
|
|
3962
|
+
useUnmount(()=>{
|
|
3963
|
+
close();
|
|
3964
|
+
});
|
|
3965
|
+
return {
|
|
3966
|
+
data,
|
|
3967
|
+
error,
|
|
3968
|
+
status,
|
|
3969
|
+
lastEventId,
|
|
3970
|
+
event,
|
|
3971
|
+
close,
|
|
3972
|
+
open
|
|
3973
|
+
};
|
|
3974
|
+
};
|
|
3975
|
+
|
|
3976
|
+
export { assignRef, defaultOptions, mergeRefs, use, useActiveElement, useAsyncEffect, useBroadcastChannel, useClickOutside, useClipboard, useControlled, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDevicePixelRatio, useDisclosure, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementByPoint, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEventSource, useEyeDropper, useFavicon, useFetchEventSource, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMergedRefs, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePreferredLanguages, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactuses/core",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.23-beta.1",
|
|
4
4
|
"license": "Unlicense",
|
|
5
5
|
"homepage": "https://www.reactuse.com/",
|
|
6
6
|
"repository": {
|
|
@@ -74,6 +74,7 @@
|
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
|
+
"@microsoft/fetch-event-source": "^2.0.1",
|
|
77
78
|
"js-cookie": "^3.0.5",
|
|
78
79
|
"lodash-es": "^4.17.21",
|
|
79
80
|
"screenfull": "^5.0.0",
|