@hanzogui/use-async 2.0.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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/errors.cjs +30 -0
  3. package/dist/cjs/errors.native.js +120 -0
  4. package/dist/cjs/errors.native.js.map +1 -0
  5. package/dist/cjs/idle.cjs +47 -0
  6. package/dist/cjs/idle.native.js +57 -0
  7. package/dist/cjs/idle.native.js.map +1 -0
  8. package/dist/cjs/index.cjs +21 -0
  9. package/dist/cjs/index.native.js +24 -0
  10. package/dist/cjs/index.native.js.map +1 -0
  11. package/dist/cjs/sleep.cjs +29 -0
  12. package/dist/cjs/sleep.native.js +34 -0
  13. package/dist/cjs/sleep.native.js.map +1 -0
  14. package/dist/cjs/useAsyncEffect.cjs +61 -0
  15. package/dist/cjs/useAsyncEffect.native.js +75 -0
  16. package/dist/cjs/useAsyncEffect.native.js.map +1 -0
  17. package/dist/esm/errors.mjs +7 -0
  18. package/dist/esm/errors.mjs.map +1 -0
  19. package/dist/esm/errors.native.js +94 -0
  20. package/dist/esm/errors.native.js.map +1 -0
  21. package/dist/esm/idle.mjs +24 -0
  22. package/dist/esm/idle.mjs.map +1 -0
  23. package/dist/esm/idle.native.js +31 -0
  24. package/dist/esm/idle.native.js.map +1 -0
  25. package/dist/esm/index.js +5 -0
  26. package/dist/esm/index.js.map +1 -0
  27. package/dist/esm/index.mjs +5 -0
  28. package/dist/esm/index.mjs.map +1 -0
  29. package/dist/esm/index.native.js +5 -0
  30. package/dist/esm/index.native.js.map +1 -0
  31. package/dist/esm/sleep.mjs +6 -0
  32. package/dist/esm/sleep.mjs.map +1 -0
  33. package/dist/esm/sleep.native.js +8 -0
  34. package/dist/esm/sleep.native.js.map +1 -0
  35. package/dist/esm/useAsyncEffect.mjs +36 -0
  36. package/dist/esm/useAsyncEffect.mjs.map +1 -0
  37. package/dist/esm/useAsyncEffect.native.js +47 -0
  38. package/dist/esm/useAsyncEffect.native.js.map +1 -0
  39. package/package.json +42 -0
  40. package/src/errors.ts +6 -0
  41. package/src/idle.ts +66 -0
  42. package/src/index.ts +4 -0
  43. package/src/sleep.ts +8 -0
  44. package/src/useAsyncEffect.ts +73 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Tamagui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,30 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: !0
9
+ });
10
+ },
11
+ __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ return to;
17
+ };
18
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
19
+ value: !0
20
+ }), mod);
21
+ var errors_exports = {};
22
+ __export(errors_exports, {
23
+ AbortError: () => AbortError
24
+ });
25
+ module.exports = __toCommonJS(errors_exports);
26
+ class AbortError extends Error {
27
+ constructor(message = "") {
28
+ super(message), this.name = "AbortError";
29
+ }
30
+ }
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
+ value: !0
22
+ }), mod);
23
+ var errors_exports = {};
24
+ __export(errors_exports, {
25
+ AbortError: () => AbortError
26
+ });
27
+ module.exports = __toCommonJS(errors_exports);
28
+ function _assert_this_initialized(self) {
29
+ if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
30
+ return self;
31
+ }
32
+ function _call_super(_this, derived, args) {
33
+ return derived = _get_prototype_of(derived), _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
34
+ }
35
+ function _class_call_check(instance, Constructor) {
36
+ if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
37
+ }
38
+ function _construct(Parent, args, Class) {
39
+ return _is_native_reflect_construct() ? _construct = Reflect.construct : _construct = function (Parent2, args2, Class2) {
40
+ var a = [null];
41
+ a.push.apply(a, args2);
42
+ var Constructor = Function.bind.apply(Parent2, a),
43
+ instance = new Constructor();
44
+ return Class2 && _set_prototype_of(instance, Class2.prototype), instance;
45
+ }, _construct.apply(null, arguments);
46
+ }
47
+ function _get_prototype_of(o) {
48
+ return _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function (o2) {
49
+ return o2.__proto__ || Object.getPrototypeOf(o2);
50
+ }, _get_prototype_of(o);
51
+ }
52
+ function _inherits(subClass, superClass) {
53
+ if (typeof superClass != "function" && superClass !== null) throw new TypeError("Super expression must either be null or a function");
54
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
55
+ constructor: {
56
+ value: subClass,
57
+ writable: !0,
58
+ configurable: !0
59
+ }
60
+ }), superClass && _set_prototype_of(subClass, superClass);
61
+ }
62
+ function _is_native_function(fn) {
63
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
64
+ }
65
+ function _possible_constructor_return(self, call) {
66
+ return call && (_type_of(call) === "object" || typeof call == "function") ? call : _assert_this_initialized(self);
67
+ }
68
+ function _set_prototype_of(o, p) {
69
+ return _set_prototype_of = Object.setPrototypeOf || function (o2, p2) {
70
+ return o2.__proto__ = p2, o2;
71
+ }, _set_prototype_of(o, p);
72
+ }
73
+ function _type_of(obj) {
74
+ "@swc/helpers - typeof";
75
+
76
+ return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
77
+ }
78
+ function _wrap_native_super(Class) {
79
+ var _cache = typeof Map == "function" ? /* @__PURE__ */new Map() : void 0;
80
+ return _wrap_native_super = function (Class2) {
81
+ if (Class2 === null || !_is_native_function(Class2)) return Class2;
82
+ if (typeof Class2 != "function") throw new TypeError("Super expression must either be null or a function");
83
+ if (typeof _cache < "u") {
84
+ if (_cache.has(Class2)) return _cache.get(Class2);
85
+ _cache.set(Class2, Wrapper);
86
+ }
87
+ function Wrapper() {
88
+ return _construct(Class2, arguments, _get_prototype_of(this).constructor);
89
+ }
90
+ return Wrapper.prototype = Object.create(Class2.prototype, {
91
+ constructor: {
92
+ value: Wrapper,
93
+ enumerable: !1,
94
+ writable: !0,
95
+ configurable: !0
96
+ }
97
+ }), _set_prototype_of(Wrapper, Class2);
98
+ }, _wrap_native_super(Class);
99
+ }
100
+ function _is_native_reflect_construct() {
101
+ try {
102
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
103
+ } catch {}
104
+ return (_is_native_reflect_construct = function () {
105
+ return !!result;
106
+ })();
107
+ }
108
+ var AbortError = /* @__PURE__ */function (Error1) {
109
+ "use strict";
110
+
111
+ _inherits(AbortError2, Error1);
112
+ function AbortError2() {
113
+ var message = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "";
114
+ _class_call_check(this, AbortError2);
115
+ var _this;
116
+ return _this = _call_super(this, AbortError2, [message]), _this.name = "AbortError", _this;
117
+ }
118
+ return AbortError2;
119
+ }(_wrap_native_super(Error));
120
+ //# sourceMappingURL=errors.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","errors_exports","__export","AbortError","module","exports","_assert_this_initialized","self","ReferenceError"],"sources":["../../src/errors.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,cAAA;AAAAC,QAAA,CAAAD,cAAA;EAAAE,UAAA,EAAAA,CAAA,KAAAA;AAAA;AAAOC,MAAM,CAAAC,OAAA,GAAAT,YAAmB,CAAAK,cAAM;AAAA,SACpCK,wBAA0BA,CAAAC,IAAA;EACxB,IAAAA,IAAM,UACN,GACF,UAAAC,cAAA;EACF,OAAAD,IAAA","ignoreList":[]}
@@ -0,0 +1,47 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: !0
9
+ });
10
+ },
11
+ __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ return to;
17
+ };
18
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
19
+ value: !0
20
+ }), mod);
21
+ var idle_exports = {};
22
+ __export(idle_exports, {
23
+ idle: () => idle
24
+ });
25
+ module.exports = __toCommonJS(idle_exports);
26
+ var import_errors = require("./errors.cjs"),
27
+ import_sleep = require("./sleep.cjs");
28
+ const idleCb = typeof requestIdleCallback > "u" ? cb => setTimeout(cb, 1) : requestIdleCallback,
29
+ idleAsync = () => new Promise(res => {
30
+ idleCb(res);
31
+ }),
32
+ idle = async (signal, options) => {
33
+ const {
34
+ max,
35
+ min,
36
+ fully
37
+ } = options || {},
38
+ idleFn = fully ? fullyIdle : idleAsync;
39
+ if (max && min && min < max ? await Promise.race([Promise.all([idleFn(), (0, import_sleep.sleep)(min)]), (0, import_sleep.sleep)(max)]) : max ? await Promise.race([idleFn(), (0, import_sleep.sleep)(max)]) : min ? await Promise.all([idleFn(), (0, import_sleep.sleep)(min)]) : await idleFn(), signal?.aborted) throw new import_errors.AbortError();
40
+ },
41
+ fullyIdle = async signal => {
42
+ for (;;) {
43
+ const startTime = Date.now();
44
+ if (await idle(signal), Date.now() - startTime < 15) break;
45
+ if (signal?.aborted) throw new import_errors.AbortError();
46
+ }
47
+ };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
+ value: !0
22
+ }), mod);
23
+ var idle_exports = {};
24
+ __export(idle_exports, {
25
+ idle: () => idle
26
+ });
27
+ module.exports = __toCommonJS(idle_exports);
28
+ var import_errors = require("./errors.native.js"),
29
+ import_sleep = require("./sleep.native.js"),
30
+ idleCb = typeof requestIdleCallback > "u" ? function (cb) {
31
+ return setTimeout(cb, 1);
32
+ } : requestIdleCallback,
33
+ idleAsync = function () {
34
+ return new Promise(function (res) {
35
+ idleCb(res);
36
+ });
37
+ },
38
+ idle = async function (signal, options) {
39
+ var {
40
+ max,
41
+ min,
42
+ fully
43
+ } = options || {},
44
+ idleFn = fully ? fullyIdle : idleAsync;
45
+ if (max && min && min < max ? await Promise.race([Promise.all([idleFn(), (0, import_sleep.sleep)(min)]), (0, import_sleep.sleep)(max)]) : max ? await Promise.race([idleFn(), (0, import_sleep.sleep)(max)]) : min ? await Promise.all([idleFn(), (0, import_sleep.sleep)(min)]) : await idleFn(), signal?.aborted) throw new import_errors.AbortError();
46
+ },
47
+ fullyIdle = async function (signal) {
48
+ for (;;) {
49
+ var startTime = Date.now();
50
+ await idle(signal);
51
+ var endTime = Date.now(),
52
+ duration = endTime - startTime;
53
+ if (duration < 15) break;
54
+ if (signal?.aborted) throw new import_errors.AbortError();
55
+ }
56
+ };
57
+ //# sourceMappingURL=idle.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","idle_exports","__export","idle","module","exports","import_errors","require","import_sleep","idleCb","requestIdleCallback","cb","setTimeout","idleAsync","Promise","res","signal","options","max","min","fully","idleFn","fullyIdle","race","all","sleep"],"sources":["../../src/idle.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,YAAA;AAAAC,QAAA,CAAAD,YAAA;EAAAE,IAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAT,YAA2B,CAAAK,YAC3B;AASA,IAAAK,aACE,GAAAC,OAAO;EAAAC,YACF,GAAAD,OAAmB,oBAAe,CAAC;EAAAE,MACpC,UAAAC,mBAEY,MACT,aAAYC,EAAC;IAClB,OAAOC,UAAG,CAAAD,EAAA;EACZ,CAAC,GAGUD,mBACX;EAAAG,SACA,YAAAA,CAAA,EAKkB;IAClB,OAAM,IAAEC,OAAU,WAAUC,GAAA;MAc5BN,MAVI,CAAAM,GAAO;IAWT;EAEJ;EAEMZ,IAAA,kBAAAA,CAAmBa,MAAA,EAAwCC,OAAA;IAC/D;QAAAC,GAAA;QAAaC,GAAA;QAAAC;MAAA,IAAAH,OAAA;MAAAI,MAAA,GAAAD,KAAA,GAAAE,SAAA,GAAAT,SAAA;IACX,IAAAK,GAAA,IAAMC,GAAA,IAAAA,GAAY,GAAAD,GAAK,GAAI,MAAAJ,OAAA,CAAAS,IAAA,EAM3BT,OALA,CAAAU,GAAM,EAMJH,MAAA,IAIF,EAAI,EAAAb,YAAQ,CAAAiB,KAAA,EAAAN,GAAA,EACV,GAEJ,IAAAX,YAAA,CAAAiB,KAAA,EAAAP,GAAA,EACF,IAAAA,GAAA,SAAAJ,OAAA,CAAAS,IAAA,E","ignoreList":[]}
@@ -0,0 +1,21 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
7
+ get: () => from[key],
8
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
9
+ });
10
+ return to;
11
+ },
12
+ __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
13
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
14
+ value: !0
15
+ }), mod);
16
+ var index_exports = {};
17
+ module.exports = __toCommonJS(index_exports);
18
+ __reExport(index_exports, require("./useAsyncEffect.cjs"), module.exports);
19
+ __reExport(index_exports, require("./sleep.cjs"), module.exports);
20
+ __reExport(index_exports, require("./idle.cjs"), module.exports);
21
+ __reExport(index_exports, require("./errors.cjs"), module.exports);
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
9
+ get: () => from[key],
10
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
11
+ });
12
+ return to;
13
+ },
14
+ __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
16
+ value: !0
17
+ }), mod);
18
+ var index_exports = {};
19
+ module.exports = __toCommonJS(index_exports);
20
+ __reExport(index_exports, require("./useAsyncEffect.native.js"), module.exports);
21
+ __reExport(index_exports, require("./sleep.native.js"), module.exports);
22
+ __reExport(index_exports, require("./idle.native.js"), module.exports);
23
+ __reExport(index_exports, require("./errors.native.js"), module.exports);
24
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","module","exports","__reExport","require"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAP,YAAc,CAAAK,aAAA;AACdG,UAAA,CAAAH,aAAA,EAAcI,OAAA,6BADd,GAAAH,MAAA,CAAAC,OAAA;AAEAC,UAAA,CAAAH,aAAA,EAAcI,OAAA,uBAFdH,MAAA,CAAAC,OAAA;AAGAC,UAAA,CAAAH,aAAA,EAAcI,OAAA,sBAAAH,MAHd,CAAAC,OAAA","ignoreList":[]}
@@ -0,0 +1,29 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: !0
9
+ });
10
+ },
11
+ __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ return to;
17
+ };
18
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
19
+ value: !0
20
+ }), mod);
21
+ var sleep_exports = {};
22
+ __export(sleep_exports, {
23
+ sleep: () => sleep
24
+ });
25
+ module.exports = __toCommonJS(sleep_exports);
26
+ var import_errors = require("./errors.cjs");
27
+ const sleep = async (ms, signal) => {
28
+ if (await new Promise(res => setTimeout(res, ms)), signal?.aborted) throw new import_errors.AbortError();
29
+ };
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
+ value: !0
22
+ }), mod);
23
+ var sleep_exports = {};
24
+ __export(sleep_exports, {
25
+ sleep: () => sleep
26
+ });
27
+ module.exports = __toCommonJS(sleep_exports);
28
+ var import_errors = require("./errors.native.js"),
29
+ sleep = async function (ms, signal) {
30
+ if (await new Promise(function (res) {
31
+ return setTimeout(res, ms);
32
+ }), signal?.aborted) throw new import_errors.AbortError();
33
+ };
34
+ //# sourceMappingURL=sleep.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","sleep_exports","__export","sleep","module","exports","import_errors","require","ms","signal","Promise","res","setTimeout","aborted"],"sources":["../../src/sleep.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,QAAA,CAAAD,aAAA;EAAAE,KAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAT,YAA2B,CAAAK,aAAA;AAEpB,IAAAK,aAAc,GAAAC,OAAmB,qBAAwC;EAAAJ,KAAA,kBAAAA,CAAAK,EAAA,EAAAC,MAAA;IAE9E,IADA,MAAM,IAAIC,OAAA,CAAQ,UAASC,GAAA;MAEzB,OAAMC,UAAI,CAAAD,GAAA,EAAAH,EAAA;IAEd,IAAAC,MAAA,EAAAI,OAAA,E","ignoreList":[]}
@@ -0,0 +1,61 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: !0
9
+ });
10
+ },
11
+ __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ return to;
17
+ };
18
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
19
+ value: !0
20
+ }), mod);
21
+ var useAsyncEffect_exports = {};
22
+ __export(useAsyncEffect_exports, {
23
+ useAsyncEffect: () => useAsyncEffect,
24
+ useAsyncEffectOfType: () => useAsyncEffectOfType,
25
+ useAsyncLayoutEffect: () => useAsyncLayoutEffect
26
+ });
27
+ module.exports = __toCommonJS(useAsyncEffect_exports);
28
+ var import_react = require("react"),
29
+ import_errors = require("./errors.cjs");
30
+ const DEBUG_LEVEL = 0;
31
+ function useAsyncEffect(cb, deps = []) {
32
+ useAsyncEffectOfType(import_react.useEffect, cb, deps);
33
+ }
34
+ function useAsyncLayoutEffect(cb, deps = []) {
35
+ useAsyncEffectOfType(import_react.useLayoutEffect, cb, deps);
36
+ }
37
+ function useAsyncEffectOfType(type, cb, deps = []) {
38
+ type(() => {
39
+ const controller = new AbortController(),
40
+ signal = controller.signal;
41
+ try {
42
+ const value = cb(signal, ...deps);
43
+ Promise.resolve(value).then(async res => {
44
+ if (res && typeof res == "function") {
45
+ if (signal.aborted) return res();
46
+ signal.addEventListener("abort", res);
47
+ }
48
+ }).catch(handleError);
49
+ } catch (error) {
50
+ handleError(error);
51
+ }
52
+ function handleError(error) {
53
+ if (error instanceof import_errors.AbortError) return DEBUG_LEVEL > 2 && console.info(`\u{1F41B} useAsyncEffect aborted: ${error.message}`), null;
54
+ if (typeof error == "object" && error.name === "AbortError") return null;
55
+ throw error;
56
+ }
57
+ return () => {
58
+ signal.aborted || controller.abort();
59
+ };
60
+ }, deps);
61
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
+ value: !0
22
+ }), mod);
23
+ var useAsyncEffect_exports = {};
24
+ __export(useAsyncEffect_exports, {
25
+ useAsyncEffect: () => useAsyncEffect,
26
+ useAsyncEffectOfType: () => useAsyncEffectOfType,
27
+ useAsyncLayoutEffect: () => useAsyncLayoutEffect
28
+ });
29
+ module.exports = __toCommonJS(useAsyncEffect_exports);
30
+ var import_react = require("react"),
31
+ import_errors = require("./errors.native.js");
32
+ function _instanceof(left, right) {
33
+ return right != null && typeof Symbol < "u" && right[Symbol.hasInstance] ? !!right[Symbol.hasInstance](left) : left instanceof right;
34
+ }
35
+ function _type_of(obj) {
36
+ "@swc/helpers - typeof";
37
+
38
+ return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
39
+ }
40
+ var DEBUG_LEVEL = 0;
41
+ function useAsyncEffect(cb) {
42
+ var deps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
43
+ useAsyncEffectOfType(import_react.useEffect, cb, deps);
44
+ }
45
+ function useAsyncLayoutEffect(cb) {
46
+ var deps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
47
+ useAsyncEffectOfType(import_react.useLayoutEffect, cb, deps);
48
+ }
49
+ function useAsyncEffectOfType(type, cb) {
50
+ var deps = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : [];
51
+ type(function () {
52
+ var controller = new AbortController(),
53
+ signal = controller.signal;
54
+ try {
55
+ var value = cb(signal, ...deps);
56
+ Promise.resolve(value).then(async function (res) {
57
+ if (res && typeof res == "function") {
58
+ if (signal.aborted) return res();
59
+ signal.addEventListener("abort", res);
60
+ }
61
+ }).catch(handleError);
62
+ } catch (error) {
63
+ handleError(error);
64
+ }
65
+ function handleError(error) {
66
+ if (_instanceof(error, import_errors.AbortError)) return DEBUG_LEVEL > 2 && console.info(`\u{1F41B} useAsyncEffect aborted: ${error.message}`), null;
67
+ if ((typeof error > "u" ? "undefined" : _type_of(error)) === "object" && error.name === "AbortError") return null;
68
+ throw error;
69
+ }
70
+ return function () {
71
+ signal.aborted || controller.abort();
72
+ };
73
+ }, deps);
74
+ }
75
+ //# sourceMappingURL=useAsyncEffect.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","useAsyncEffect_exports","__export","useAsyncEffect","useAsyncEffectOfType","useAsyncLayoutEffect","module","exports","import_react","require","import_errors","_instanceof","left","right","Symbol","hasInstance","_type_of","obj","constructor","DEBUG_LEVEL","cb","deps","arguments","length","useEffect","useLayoutEffect","type","controller","AbortController","signal","Promise","resolve","then","res","aborted","addEventListener","catch","handleError","error"],"sources":["../../src/useAsyncEffect.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,sBAAA;AAAAC,QAAA,CAAAD,sBAAA;EAAAE,cAAA,EAAAA,CAAA,KAAAA,cAAA;EAAAC,oBAAA,EAAAA,CAAA,KAAAA,oBAAA;EAAAC,oBAAA,EAAAA,CAAA,KAAAA;AAAA;AAEAC,MAAA,CAAAC,OAAA,GAAAX,YAA2C,CAAAK,sBAC3C,CAA2B;AAE3B,IAAAO,YAAM,GAAAC,OAAc;EAAAC,aAAA,GAAAD,OAAA;AASb,SAASE,YAAAC,IAAe,EAAAC,KAAyB;EACtD,OAAAA,KAAA,YAAqB,OAAAC,MAAA,UAAWD,KAAI,CAAIC,MAAA,CAAAC,WAAA,MAAAF,KAAA,CAAAC,MAAA,CAAAC,WAAA,EAAAH,IAAA,IAAAA,IAAA,YAAAC,KAAA;AAC1C;AAEO,SAASG,SAAAC,GAAA;EACd,uBAAqB;;EACvB,OAAAA,GAAA,WAAAH,MAAA,UAAAG,GAAA,CAAAC,WAAA,KAAAJ,MAAA,qBAAAG,GAAA;AAEO;AAKL,IAAAE,WAAW;AACT,SAAAhB,cAAmBA,CAAAiB,EAAA,EAAI;EAIvB,IAAAC,IAAI,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,iBAAAA,SAAA;EACFlB,oBAAc,CAAGI,YAAW,CAAAgB,SAAI,EAAAJ,EAAA,EAAAC,IAAA;AAEhC;AAEI,SAAAhB,oBAAkBA,CAAAe,EAAA,EAAQ;EACxB,IAAAC,IAAA,GAAAC,SAAW,CAAAC,MAAS,QAAOD,SAAI,iBAAAA,SAAA;EAC/BlB,oBAAO,CAAAI,YAAiB,CAAAiB,eAAY,EAAAL,EAAA,EAAAC,IAAA;AAAA;AACtC,SAEDjB,oBAAiBA,CAAAsB,IAAA,EAAAN,EAAA;EAAA,IACtBC,IAAA,GAASC,SAAO,CAAAC,MAAA,QAAAD,SAAA,iBAAAA,SAAA;EACdI,IAAA,aAAY;IACd,IAAAC,UAAA,OAAAC,eAAA;MAAAC,MAAA,GAAAF,UAAA,CAAAE,MAAA;IAEA;MACE,IAAI7B,KAAA,GAAAoB,EAAA,CAAAS,MAAiB,KAAAR,IAAA;MACnBS,OAAA,CAAAC,OAAI,CAAA/B,KAAA,EAAcgC,IAAA,CAChB,gBAAaC,GAAA;QAOjB,IAAIA,GAAA,IAAO,OAAUA,GAAA,cAAY,EAAM;UACrC,IAAAJ,MAAO,CAAAK,OAAA,SAAAD,GAAA;UAMTJ,MAAM,CAAAM,gBAAA,UAAAF,GAAA;QACR;MAEA,GAAAG,KAAO,CAAAC,WAAM;IACX,EAAI,OAAOC,KAAA;MAEbD,WAAA,CAAAC,KAAA;IACF;IACF,SAAAD,YAAAC,KAAA","ignoreList":[]}
@@ -0,0 +1,7 @@
1
+ class AbortError extends Error {
2
+ constructor(message = "") {
3
+ super(message), this.name = "AbortError";
4
+ }
5
+ }
6
+ export { AbortError };
7
+ //# sourceMappingURL=errors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["AbortError","Error","constructor","message","name"],"sources":["../../src/errors.ts"],"sourcesContent":[null],"mappings":"AAAO,MAAMA,UAAA,SAAmBC,KAAA,CAAM;EACpCC,YAAYC,OAAA,GAAU,IAAI;IACxB,MAAMA,OAAO,GACb,KAAKC,IAAA,GAAO;EACd;AACF","ignoreList":[]}
@@ -0,0 +1,94 @@
1
+ function _assert_this_initialized(self) {
2
+ if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
3
+ return self;
4
+ }
5
+ function _call_super(_this, derived, args) {
6
+ return derived = _get_prototype_of(derived), _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
7
+ }
8
+ function _class_call_check(instance, Constructor) {
9
+ if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
10
+ }
11
+ function _construct(Parent, args, Class) {
12
+ return _is_native_reflect_construct() ? _construct = Reflect.construct : _construct = function (Parent2, args2, Class2) {
13
+ var a = [null];
14
+ a.push.apply(a, args2);
15
+ var Constructor = Function.bind.apply(Parent2, a),
16
+ instance = new Constructor();
17
+ return Class2 && _set_prototype_of(instance, Class2.prototype), instance;
18
+ }, _construct.apply(null, arguments);
19
+ }
20
+ function _get_prototype_of(o) {
21
+ return _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function (o2) {
22
+ return o2.__proto__ || Object.getPrototypeOf(o2);
23
+ }, _get_prototype_of(o);
24
+ }
25
+ function _inherits(subClass, superClass) {
26
+ if (typeof superClass != "function" && superClass !== null) throw new TypeError("Super expression must either be null or a function");
27
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
28
+ constructor: {
29
+ value: subClass,
30
+ writable: !0,
31
+ configurable: !0
32
+ }
33
+ }), superClass && _set_prototype_of(subClass, superClass);
34
+ }
35
+ function _is_native_function(fn) {
36
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
37
+ }
38
+ function _possible_constructor_return(self, call) {
39
+ return call && (_type_of(call) === "object" || typeof call == "function") ? call : _assert_this_initialized(self);
40
+ }
41
+ function _set_prototype_of(o, p) {
42
+ return _set_prototype_of = Object.setPrototypeOf || function (o2, p2) {
43
+ return o2.__proto__ = p2, o2;
44
+ }, _set_prototype_of(o, p);
45
+ }
46
+ function _type_of(obj) {
47
+ "@swc/helpers - typeof";
48
+
49
+ return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
50
+ }
51
+ function _wrap_native_super(Class) {
52
+ var _cache = typeof Map == "function" ? /* @__PURE__ */new Map() : void 0;
53
+ return _wrap_native_super = function (Class2) {
54
+ if (Class2 === null || !_is_native_function(Class2)) return Class2;
55
+ if (typeof Class2 != "function") throw new TypeError("Super expression must either be null or a function");
56
+ if (typeof _cache < "u") {
57
+ if (_cache.has(Class2)) return _cache.get(Class2);
58
+ _cache.set(Class2, Wrapper);
59
+ }
60
+ function Wrapper() {
61
+ return _construct(Class2, arguments, _get_prototype_of(this).constructor);
62
+ }
63
+ return Wrapper.prototype = Object.create(Class2.prototype, {
64
+ constructor: {
65
+ value: Wrapper,
66
+ enumerable: !1,
67
+ writable: !0,
68
+ configurable: !0
69
+ }
70
+ }), _set_prototype_of(Wrapper, Class2);
71
+ }, _wrap_native_super(Class);
72
+ }
73
+ function _is_native_reflect_construct() {
74
+ try {
75
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
76
+ } catch {}
77
+ return (_is_native_reflect_construct = function () {
78
+ return !!result;
79
+ })();
80
+ }
81
+ var AbortError = /* @__PURE__ */function (Error1) {
82
+ "use strict";
83
+
84
+ _inherits(AbortError2, Error1);
85
+ function AbortError2() {
86
+ var message = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "";
87
+ _class_call_check(this, AbortError2);
88
+ var _this;
89
+ return _this = _call_super(this, AbortError2, [message]), _this.name = "AbortError", _this;
90
+ }
91
+ return AbortError2;
92
+ }(_wrap_native_super(Error));
93
+ export { AbortError };
94
+ //# sourceMappingURL=errors.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_assert_this_initialized","self","ReferenceError"],"sources":["../../src/errors.ts"],"sourcesContent":[null],"mappings":"AAAO,SAAMA,wBAAyBA,CAAAC,IAAA;EACpC,IAAAA,IAAA,KAAY,QACV,MAAM,IAAAC,cACD,4DAAO;EACd,OAAAD,IAAA;AACF","ignoreList":[]}
@@ -0,0 +1,24 @@
1
+ import { AbortError } from "./errors.mjs";
2
+ import { sleep } from "./sleep.mjs";
3
+ const idleCb = typeof requestIdleCallback > "u" ? cb => setTimeout(cb, 1) : requestIdleCallback,
4
+ idleAsync = () => new Promise(res => {
5
+ idleCb(res);
6
+ }),
7
+ idle = async (signal, options) => {
8
+ const {
9
+ max,
10
+ min,
11
+ fully
12
+ } = options || {},
13
+ idleFn = fully ? fullyIdle : idleAsync;
14
+ if (max && min && min < max ? await Promise.race([Promise.all([idleFn(), sleep(min)]), sleep(max)]) : max ? await Promise.race([idleFn(), sleep(max)]) : min ? await Promise.all([idleFn(), sleep(min)]) : await idleFn(), signal?.aborted) throw new AbortError();
15
+ },
16
+ fullyIdle = async signal => {
17
+ for (;;) {
18
+ const startTime = Date.now();
19
+ if (await idle(signal), Date.now() - startTime < 15) break;
20
+ if (signal?.aborted) throw new AbortError();
21
+ }
22
+ };
23
+ export { idle };
24
+ //# sourceMappingURL=idle.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["AbortError","sleep","idleCb","requestIdleCallback","cb","setTimeout","idleAsync","Promise","res","idle","signal","options","max","min","fully","idleFn","fullyIdle","race","all","aborted","startTime","Date","now"],"sources":["../../src/idle.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,UAAA,QAAkB;AAC3B,SAASC,KAAA,QAAa;AAStB,MAAMC,MAAA,GACJ,OAAOC,mBAAA,GAAwB,MAC1BC,EAAA,IAAmBC,UAAA,CAAWD,EAAA,EAAI,CAAC,IACpCD,mBAAA;EAEAG,SAAA,GAAYA,CAAA,KACT,IAAIC,OAAA,CAASC,GAAA,IAAQ;IAC1BN,MAAA,CAAOM,GAAG;EACZ,CAAC;EAGUC,IAAA,GAAO,MAAAA,CAClBC,MAAA,EACAC,OAAA,KAKkB;IAClB,MAAM;QAAEC,GAAA;QAAKC,GAAA;QAAKC;MAAM,IAAIH,OAAA,IAAW,CAAC;MAElCI,MAAA,GAASD,KAAA,GAAQE,SAAA,GAAYV,SAAA;IAYnC,IAVIM,GAAA,IAAOC,GAAA,IAAOA,GAAA,GAAMD,GAAA,GACtB,MAAML,OAAA,CAAQU,IAAA,CAAK,CAACV,OAAA,CAAQW,GAAA,CAAI,CAACH,MAAA,CAAO,GAAGd,KAAA,CAAMY,GAAG,CAAC,CAAC,GAAGZ,KAAA,CAAMW,GAAG,CAAC,CAAC,IAC3DA,GAAA,GACT,MAAML,OAAA,CAAQU,IAAA,CAAK,CAACF,MAAA,CAAO,GAAGd,KAAA,CAAMW,GAAG,CAAC,CAAC,IAChCC,GAAA,GACT,MAAMN,OAAA,CAAQW,GAAA,CAAI,CAACH,MAAA,CAAO,GAAGd,KAAA,CAAMY,GAAG,CAAC,CAAC,IAExC,MAAME,MAAA,CAAO,GAGXL,MAAA,EAAQS,OAAA,EACV,MAAM,IAAInB,UAAA,CAAW;EAEzB;EAEMgB,SAAA,GAAY,MAAON,MAAA,IAAwC;IAC/D,SAAa;MACX,MAAMU,SAAA,GAAYC,IAAA,CAAKC,GAAA,CAAI;MAM3B,IALA,MAAMb,IAAA,CAAKC,MAAM,GACDW,IAAA,CAAKC,GAAA,CAAI,IACEF,SAAA,GAGZ,IACb;MAIF,IAAIV,MAAA,EAAQS,OAAA,EACV,MAAM,IAAInB,UAAA,CAAW;IAEzB;EACF","ignoreList":[]}
@@ -0,0 +1,31 @@
1
+ import { AbortError } from "./errors.native.js";
2
+ import { sleep } from "./sleep.native.js";
3
+ var idleCb = typeof requestIdleCallback > "u" ? function (cb) {
4
+ return setTimeout(cb, 1);
5
+ } : requestIdleCallback,
6
+ idleAsync = function () {
7
+ return new Promise(function (res) {
8
+ idleCb(res);
9
+ });
10
+ },
11
+ idle = async function (signal, options) {
12
+ var {
13
+ max,
14
+ min,
15
+ fully
16
+ } = options || {},
17
+ idleFn = fully ? fullyIdle : idleAsync;
18
+ if (max && min && min < max ? await Promise.race([Promise.all([idleFn(), sleep(min)]), sleep(max)]) : max ? await Promise.race([idleFn(), sleep(max)]) : min ? await Promise.all([idleFn(), sleep(min)]) : await idleFn(), signal?.aborted) throw new AbortError();
19
+ },
20
+ fullyIdle = async function (signal) {
21
+ for (;;) {
22
+ var startTime = Date.now();
23
+ await idle(signal);
24
+ var endTime = Date.now(),
25
+ duration = endTime - startTime;
26
+ if (duration < 15) break;
27
+ if (signal?.aborted) throw new AbortError();
28
+ }
29
+ };
30
+ export { idle };
31
+ //# sourceMappingURL=idle.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["AbortError","sleep","idleCb","requestIdleCallback","cb","setTimeout","idleAsync","Promise","res","idle","signal","options","max","min","fully","idleFn","fullyIdle","race","all"],"sources":["../../src/idle.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,UAAA,QAAkB;AAC3B,SAASC,KAAA,QAAa;AAStB,IAAAC,MAAM,UACJC,mBAAO,MAAwB,aAC1BC,EAAmB;IAKtB,OAAOC,UAAG,CAAAD,EAAA;EACZ,CAAC,GAGUD,mBACX;EAAAG,SACA,YAAAA,CAAA,EAKkB;IAClB,OAAM,IAAEC,OAAU,WAAUC,GAAA;MAc5BN,MAVI,CAAAM,GAAO;IAWT;EAEJ;EAEMC,IAAA,kBAAAA,CAAmBC,MAAA,EAAwCC,OAAA;IAC/D;QAAAC,GAAA;QAAaC,GAAA;QAAAC;MAAA,IAAAH,OAAA;MAAAI,MAAA,GAAAD,KAAA,GAAAE,SAAA,GAAAV,SAAA;IACX,IAAAM,GAAA,IAAMC,GAAA,IAAAA,GAAY,GAAAD,GAAK,GAAI,MAAAL,OAAA,CAAAU,IAAA,EAM3BV,OALA,CAAAW,GAAM,EAMJH,MAAA,IAIFd,KAAI,CAAAY,GAAA,CAAQ,CACV,GAEJZ,KAAA,CAAAW,GAAA,EACF,IAAAA,GAAA,SAAAL,OAAA,CAAAU,IAAA,E","ignoreList":[]}
@@ -0,0 +1,5 @@
1
+ export * from "./useAsyncEffect.mjs";
2
+ export * from "./sleep.mjs";
3
+ export * from "./idle.mjs";
4
+ export * from "./errors.mjs";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","ignoreList":[]}
@@ -0,0 +1,5 @@
1
+ export * from "./useAsyncEffect.mjs";
2
+ export * from "./sleep.mjs";
3
+ export * from "./idle.mjs";
4
+ export * from "./errors.mjs";
5
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","ignoreList":[]}
@@ -0,0 +1,5 @@
1
+ export * from "./useAsyncEffect.native.js";
2
+ export * from "./sleep.native.js";
3
+ export * from "./idle.native.js";
4
+ export * from "./errors.native.js";
5
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","ignoreList":[]}
@@ -0,0 +1,6 @@
1
+ import { AbortError } from "./errors.mjs";
2
+ const sleep = async (ms, signal) => {
3
+ if (await new Promise(res => setTimeout(res, ms)), signal?.aborted) throw new AbortError();
4
+ };
5
+ export { sleep };
6
+ //# sourceMappingURL=sleep.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["AbortError","sleep","ms","signal","Promise","res","setTimeout","aborted"],"sources":["../../src/sleep.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,UAAA,QAAkB;AAEpB,MAAMC,KAAA,GAAQ,MAAAA,CAAOC,EAAA,EAAYC,MAAA,KAAwC;EAE9E,IADA,MAAM,IAAIC,OAAA,CAASC,GAAA,IAAQC,UAAA,CAAWD,GAAA,EAAKH,EAAE,CAAC,GAC1CC,MAAA,EAAQI,OAAA,EACV,MAAM,IAAIP,UAAA,CAAW;AAEzB","ignoreList":[]}
@@ -0,0 +1,8 @@
1
+ import { AbortError } from "./errors.native.js";
2
+ var sleep = async function (ms, signal) {
3
+ if (await new Promise(function (res) {
4
+ return setTimeout(res, ms);
5
+ }), signal?.aborted) throw new AbortError();
6
+ };
7
+ export { sleep };
8
+ //# sourceMappingURL=sleep.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["AbortError","sleep","ms","signal","Promise","res","setTimeout","aborted"],"sources":["../../src/sleep.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,UAAA,QAAkB;AAEpB,IAAAC,KAAM,kBAAAA,CAA2BC,EAAA,EAAAC,MAAwC;EAE9E,IADA,MAAM,IAAIC,OAAA,CAAQ,UAASC,GAAA;IAEzB,OAAMC,UAAI,CAAAD,GAAW,EAAAH,EAAA;EAEzB,IAAAC,MAAA,EAAAI,OAAA,E","ignoreList":[]}
@@ -0,0 +1,36 @@
1
+ import { useEffect, useLayoutEffect } from "react";
2
+ import { AbortError } from "./errors.mjs";
3
+ const DEBUG_LEVEL = 0;
4
+ function useAsyncEffect(cb, deps = []) {
5
+ useAsyncEffectOfType(useEffect, cb, deps);
6
+ }
7
+ function useAsyncLayoutEffect(cb, deps = []) {
8
+ useAsyncEffectOfType(useLayoutEffect, cb, deps);
9
+ }
10
+ function useAsyncEffectOfType(type, cb, deps = []) {
11
+ type(() => {
12
+ const controller = new AbortController(),
13
+ signal = controller.signal;
14
+ try {
15
+ const value = cb(signal, ...deps);
16
+ Promise.resolve(value).then(async res => {
17
+ if (res && typeof res == "function") {
18
+ if (signal.aborted) return res();
19
+ signal.addEventListener("abort", res);
20
+ }
21
+ }).catch(handleError);
22
+ } catch (error) {
23
+ handleError(error);
24
+ }
25
+ function handleError(error) {
26
+ if (error instanceof AbortError) return DEBUG_LEVEL > 2 && console.info(`\u{1F41B} useAsyncEffect aborted: ${error.message}`), null;
27
+ if (typeof error == "object" && error.name === "AbortError") return null;
28
+ throw error;
29
+ }
30
+ return () => {
31
+ signal.aborted || controller.abort();
32
+ };
33
+ }, deps);
34
+ }
35
+ export { useAsyncEffect, useAsyncEffectOfType, useAsyncLayoutEffect };
36
+ //# sourceMappingURL=useAsyncEffect.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["useEffect","useLayoutEffect","AbortError","DEBUG_LEVEL","useAsyncEffect","cb","deps","useAsyncEffectOfType","useAsyncLayoutEffect","type","controller","AbortController","signal","value","Promise","resolve","then","res","aborted","addEventListener","catch","handleError","error","console","info","message","name","abort"],"sources":["../../src/useAsyncEffect.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,SAAA,EAAWC,eAAA,QAAuB;AAC3C,SAASC,UAAA,QAAkB;AAE3B,MAAMC,WAAA,GAAc;AASb,SAASC,eAAeC,EAAA,EAAyBC,IAAA,GAAc,EAAC,EAAS;EAC9EC,oBAAA,CAAqBP,SAAA,EAAWK,EAAA,EAAIC,IAAI;AAC1C;AAEO,SAASE,qBAAqBH,EAAA,EAAyBC,IAAA,GAAc,EAAC,EAAS;EACpFC,oBAAA,CAAqBN,eAAA,EAAiBI,EAAA,EAAIC,IAAI;AAChD;AAEO,SAASC,qBACdE,IAAA,EACAJ,EAAA,EACAC,IAAA,GAAc,EAAC,EACT;EACNG,IAAA,CAAK,MAAM;IACT,MAAMC,UAAA,GAAa,IAAIC,eAAA,CAAgB;MACjCC,MAAA,GAASF,UAAA,CAAWE,MAAA;IAG1B,IAAI;MACF,MAAMC,KAAA,GAAQR,EAAA,CAAGO,MAAA,EAAQ,GAAGN,IAAI;MAEhCQ,OAAA,CAAQC,OAAA,CAAQF,KAAK,EAClBG,IAAA,CAAK,MAAOC,GAAA,IAAQ;QACnB,IAAIA,GAAA,IAAO,OAAOA,GAAA,IAAQ,YAAY;UACpC,IAAIL,MAAA,CAAOM,OAAA,EAAS,OAAOD,GAAA,CAAI;UAC/BL,MAAA,CAAOO,gBAAA,CAAiB,SAASF,GAAG;QACtC;MACF,CAAC,EACAG,KAAA,CAAMC,WAAW;IACtB,SAASC,KAAA,EAAO;MACdD,WAAA,CAAYC,KAAK;IACnB;IAEA,SAASD,YAAYC,KAAA,EAAY;MAC/B,IAAIA,KAAA,YAAiBpB,UAAA,EACnB,OAAIC,WAAA,GAAc,KAChBoB,OAAA,CAAQC,IAAA,CAAK,qCAA8BF,KAAA,CAAMG,OAAO,EAAE,GAErD;MAKT,IAAI,OAAOH,KAAA,IAAU,YAAYA,KAAA,CAAMI,IAAA,KAAS,cAC9C,OAAO;MAMT,MAAMJ,KAAA;IACR;IAEA,OAAO,MAAM;MACPV,MAAA,CAAOM,OAAA,IACXR,UAAA,CAAWiB,KAAA,CAAM;IACnB;EACF,GAAGrB,IAAI;AACT","ignoreList":[]}
@@ -0,0 +1,47 @@
1
+ import { useEffect, useLayoutEffect } from "react";
2
+ import { AbortError } from "./errors.native.js";
3
+ function _instanceof(left, right) {
4
+ return right != null && typeof Symbol < "u" && right[Symbol.hasInstance] ? !!right[Symbol.hasInstance](left) : left instanceof right;
5
+ }
6
+ function _type_of(obj) {
7
+ "@swc/helpers - typeof";
8
+
9
+ return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
10
+ }
11
+ var DEBUG_LEVEL = 0;
12
+ function useAsyncEffect(cb) {
13
+ var deps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
14
+ useAsyncEffectOfType(useEffect, cb, deps);
15
+ }
16
+ function useAsyncLayoutEffect(cb) {
17
+ var deps = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
18
+ useAsyncEffectOfType(useLayoutEffect, cb, deps);
19
+ }
20
+ function useAsyncEffectOfType(type, cb) {
21
+ var deps = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : [];
22
+ type(function () {
23
+ var controller = new AbortController(),
24
+ signal = controller.signal;
25
+ try {
26
+ var value = cb(signal, ...deps);
27
+ Promise.resolve(value).then(async function (res) {
28
+ if (res && typeof res == "function") {
29
+ if (signal.aborted) return res();
30
+ signal.addEventListener("abort", res);
31
+ }
32
+ }).catch(handleError);
33
+ } catch (error) {
34
+ handleError(error);
35
+ }
36
+ function handleError(error) {
37
+ if (_instanceof(error, AbortError)) return DEBUG_LEVEL > 2 && console.info(`\u{1F41B} useAsyncEffect aborted: ${error.message}`), null;
38
+ if ((typeof error > "u" ? "undefined" : _type_of(error)) === "object" && error.name === "AbortError") return null;
39
+ throw error;
40
+ }
41
+ return function () {
42
+ signal.aborted || controller.abort();
43
+ };
44
+ }, deps);
45
+ }
46
+ export { useAsyncEffect, useAsyncEffectOfType, useAsyncLayoutEffect };
47
+ //# sourceMappingURL=useAsyncEffect.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["useEffect","useLayoutEffect","AbortError","_instanceof","left","right","Symbol","hasInstance","_type_of","obj","constructor","DEBUG_LEVEL","useAsyncEffect","cb","deps","arguments","length","useAsyncEffectOfType","useAsyncLayoutEffect","type","controller","AbortController","signal","value","Promise","resolve","then","res","aborted","addEventListener","catch","handleError","error"],"sources":["../../src/useAsyncEffect.ts"],"sourcesContent":[null],"mappings":"AAEA,SAASA,SAAA,EAAWC,eAAA,QAAuB;AAC3C,SAASC,UAAA,QAAkB;AAE3B,SAAMC,WAAcA,CAAAC,IAAA,EAAAC,KAAA;EASb,OAASA,KAAA,YAAe,OAAyBC,MAAwB,UAAAD,KAAA,CAAAC,MAAA,CAAAC,WAAA,MAAAF,KAAA,CAAAC,MAAA,CAAAC,WAAA,EAAAH,IAAA,IAAAA,IAAA,YAAAC,KAAA;AAC9E;AACF,SAAAG,SAAAC,GAAA;EAEO,uBAAS;;EACd,OAAAA,GAAA,WAAqBH,MAAA,UAAAG,GAAiB,CAAAC,WAAQ,KAAAJ,MAAA,qBAAAG,GAAA;AAChD;AAEO,IAAAE,WAAS;AAKd,SAAKC,cAAMA,CAAAC,EAAA;EACT,IAAAC,IAAM,GAAAC,SAAA,CAAaC,MAAI,QAAAD,SACjB,aAAS,IAAAA,SAAW;EAG1BE,oBAAI,CAAAjB,SAAA,EAAAa,EAAA,EAAAC,IAAA;AACF;AAEA,SAAAI,oBACGA,CAAAL,EAAA,EAAK;EACJ,IAAAC,IAAA,GAAIC,SAAO,CAAAC,MAAO,IAAQ,IAAAD,SAAY,iBAAAA,SAAA;EACpCE,oBAAW,CAAAhB,eAAgB,EAAAY,EAAI,EAAAC,IAAA;AAC/B;AAAoC,SACtCG,qBAAAE,IAAA,EAAAN,EAAA;EAAA,IACFC,IACC,GAAAC,SAAM,CAAAC,MAAW,QAAAD,SAAA,iBAAAA,SAAA;EAAAI,IACtB,aAAS;IACP,IAAAC,UAAY,OAAKC,eAAA;MAAAC,MAAA,GAAAF,UAAA,CAAAE,MAAA;IACnB;MAEA,IAAAC,KAAS,GAAAV,EAAA,CAAAS,MAAY,KAAYR,IAAA;MAC/BU,OAAI,CAAAC,OAAA,CAAAF,KAAiB,EAAAG,IAAA,iBAAAC,GAAA;QACnB,IAAAA,GAAI,WAAAA,GAAc,cACR;UAOR,IAAAL,MAAO,CAAAM,OAAU,SAAAD,GAAY;UAC/BL,MAAO,CAAAO,gBAAA,UAAAF,GAAA;QAMT;MACF,GAAAG,KAAA,CAAAC,WAAA;IAEA,SAAOC,KAAM;MACPD,WAAO,CAAAC,KAAA,CACX;IACF;IACF,SAAOD,YAAAC,KAAA;MACT,IAAA7B,WAAA,CAAA6B,KAAA,EAAA9B,UAAA,G","ignoreList":[]}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@hanzogui/use-async",
3
+ "version": "2.0.0",
4
+ "files": [
5
+ "src",
6
+ "types",
7
+ "dist"
8
+ ],
9
+ "type": "module",
10
+ "sideEffects": false,
11
+ "main": "dist/cjs",
12
+ "module": "dist/esm",
13
+ "types": "./src",
14
+ "exports": {
15
+ "./package.json": "./package.json",
16
+ ".": {
17
+ "types": "./src/index.ts",
18
+ "react-native": "./dist/esm/index.native.js",
19
+ "browser": "./dist/esm/index.mjs",
20
+ "module": "./dist/esm/index.mjs",
21
+ "import": "./dist/esm/index.mjs",
22
+ "require": "./dist/cjs/index.cjs",
23
+ "default": "./dist/esm/index.mjs"
24
+ }
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "hanzo-gui-build --skip-types",
31
+ "watch": "hanzo-gui-build --skip-types --watch",
32
+ "clean": "hanzo-gui-build clean",
33
+ "clean:build": "hanzo-gui-build clean:build"
34
+ },
35
+ "devDependencies": {
36
+ "@hanzogui/build": "workspace:*",
37
+ "react": ">=19"
38
+ },
39
+ "peerDependencies": {
40
+ "react": ">=19"
41
+ }
42
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,6 @@
1
+ export class AbortError extends Error {
2
+ constructor(message = '') {
3
+ super(message)
4
+ this.name = 'AbortError'
5
+ }
6
+ }
package/src/idle.ts ADDED
@@ -0,0 +1,66 @@
1
+ import { AbortError } from './errors'
2
+ import { sleep } from './sleep'
3
+
4
+ /**
5
+ * Two helpers wrapping requestIdleCallback in async, abortable.
6
+ */
7
+
8
+ type CallbackFn = (...args: any[]) => any | (() => any)
9
+ type CallbackerFn = (cb: CallbackFn) => void
10
+
11
+ const idleCb: CallbackerFn =
12
+ typeof requestIdleCallback === 'undefined'
13
+ ? (cb: CallbackFn) => setTimeout(cb, 1)
14
+ : requestIdleCallback
15
+
16
+ const idleAsync = () => {
17
+ return new Promise((res) => {
18
+ idleCb(res)
19
+ })
20
+ }
21
+
22
+ export const idle = async (
23
+ signal?: AbortSignal,
24
+ options?: {
25
+ max?: number
26
+ min?: number
27
+ fully?: boolean
28
+ }
29
+ ): Promise<void> => {
30
+ const { max, min, fully } = options || {}
31
+
32
+ const idleFn = fully ? fullyIdle : idleAsync
33
+
34
+ if (max && min && min < max) {
35
+ await Promise.race([Promise.all([idleFn(), sleep(min)]), sleep(max)])
36
+ } else if (max) {
37
+ await Promise.race([idleFn(), sleep(max)])
38
+ } else if (min) {
39
+ await Promise.all([idleFn(), sleep(min)])
40
+ } else {
41
+ await idleFn()
42
+ }
43
+
44
+ if (signal?.aborted) {
45
+ throw new AbortError()
46
+ }
47
+ }
48
+
49
+ const fullyIdle = async (signal?: AbortSignal): Promise<void> => {
50
+ while (true) {
51
+ const startTime = Date.now()
52
+ await idle(signal)
53
+ const endTime = Date.now()
54
+ const duration = endTime - startTime
55
+
56
+ // If idle callback took less than 15ms, consider it truly idle
57
+ if (duration < 15) {
58
+ break
59
+ }
60
+
61
+ // Check for abort signal after each iteration
62
+ if (signal?.aborted) {
63
+ throw new AbortError()
64
+ }
65
+ }
66
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './useAsyncEffect'
2
+ export * from './sleep'
3
+ export * from './idle'
4
+ export * from './errors'
package/src/sleep.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { AbortError } from './errors'
2
+
3
+ export const sleep = async (ms: number, signal?: AbortSignal): Promise<void> => {
4
+ await new Promise((res) => setTimeout(res, ms))
5
+ if (signal?.aborted) {
6
+ throw new AbortError()
7
+ }
8
+ }
@@ -0,0 +1,73 @@
1
+ // adopted from https://github.com/franciscop/use-async/blob/master/src/index.js
2
+
3
+ import { useEffect, useLayoutEffect } from 'react'
4
+ import { AbortError } from './errors'
5
+
6
+ const DEBUG_LEVEL = 0 // You can adjust this based on your needs
7
+
8
+ type Cleanup = () => void
9
+
10
+ type AsyncEffectCallback = (
11
+ signal: AbortSignal,
12
+ ...deps: any[]
13
+ ) => Promise<Cleanup | void> | void
14
+
15
+ export function useAsyncEffect(cb: AsyncEffectCallback, deps: any[] = []): void {
16
+ useAsyncEffectOfType(useEffect, cb, deps)
17
+ }
18
+
19
+ export function useAsyncLayoutEffect(cb: AsyncEffectCallback, deps: any[] = []): void {
20
+ useAsyncEffectOfType(useLayoutEffect, cb, deps)
21
+ }
22
+
23
+ export function useAsyncEffectOfType(
24
+ type: Function,
25
+ cb: AsyncEffectCallback,
26
+ deps: any[] = []
27
+ ): void {
28
+ type(() => {
29
+ const controller = new AbortController()
30
+ const signal = controller.signal
31
+
32
+ // wrap in try in case its not async (for simple use cases)
33
+ try {
34
+ const value = cb(signal, ...deps)
35
+
36
+ Promise.resolve(value)
37
+ .then(async (res) => {
38
+ if (res && typeof res === 'function') {
39
+ if (signal.aborted) return res()
40
+ signal.addEventListener('abort', res)
41
+ }
42
+ })
43
+ .catch(handleError)
44
+ } catch (error) {
45
+ handleError(error)
46
+ }
47
+
48
+ function handleError(error: any) {
49
+ if (error instanceof AbortError) {
50
+ if (DEBUG_LEVEL > 2) {
51
+ console.info(`🐛 useAsyncEffect aborted: ${error.message}`)
52
+ }
53
+ return null
54
+ }
55
+
56
+ // JS handles aborting a promise as an error. Thus, ignore them since they're a normal part
57
+ // of the expected async workflow
58
+ if (typeof error === 'object' && error.name === 'AbortError') {
59
+ return null
60
+ }
61
+
62
+ // Real errors, bubble it up since the CB is expected to handle the
63
+ // errors by itself, so this is like a "fatal error" that should've
64
+ // been handled by the devs
65
+ throw error
66
+ }
67
+
68
+ return () => {
69
+ if (signal.aborted) return
70
+ controller.abort()
71
+ }
72
+ }, deps)
73
+ }