extra-react-hooks 0.6.6 → 0.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
|
-
import { go } from '@blackglory/prelude';
|
|
2
|
+
import { go, pass } from '@blackglory/prelude';
|
|
3
3
|
import { AbortController } from 'extra-abort';
|
|
4
4
|
import { useMemoWithCleanup } from './use-memo-with-cleanup.js';
|
|
5
|
-
|
|
5
|
+
import { CustomError } from '@blackglory/errors';
|
|
6
|
+
class InternalAbortError extends CustomError {
|
|
7
|
+
}
|
|
6
8
|
export function useCallbackAsync(callback, deps) {
|
|
7
|
-
const controller = useMemoWithCleanup(() => new AbortController(), controller => controller.abort(
|
|
9
|
+
const controller = useMemoWithCleanup(() => new AbortController(), controller => controller.abort(new InternalAbortError()), deps);
|
|
8
10
|
return useCallback((...args) => {
|
|
9
11
|
go(async () => {
|
|
10
12
|
try {
|
|
11
13
|
await callback(...args, controller.signal);
|
|
12
14
|
}
|
|
13
15
|
catch (err) {
|
|
14
|
-
if (err
|
|
16
|
+
if (err instanceof InternalAbortError) {
|
|
17
|
+
pass();
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
15
20
|
throw err;
|
|
21
|
+
}
|
|
16
22
|
}
|
|
17
23
|
});
|
|
18
24
|
}, deps);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-callback-async.js","sourceRoot":"","sources":["../src/use-callback-async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAkB,MAAM,OAAO,CAAA;AACnD,OAAO,EAAE,EAAE,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"use-callback-async.js","sourceRoot":"","sources":["../src/use-callback-async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAkB,MAAM,OAAO,CAAA;AACnD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAA;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,MAAM,kBAAmB,SAAQ,WAAW;CAAG;AAE/C,MAAM,UAAU,gBAAgB,CAC9B,QAA0E,EAC1E,IAAoB;IAEpB,MAAM,UAAU,GAAG,kBAAkB,CACnC,GAAG,EAAE,CAAC,IAAI,eAAe,EAAE,EAC3B,UAAU,CAAC,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,kBAAkB,EAAE,CAAC,EACxD,IAAI,CACL,CAAA;IAED,OAAO,WAAW,CAAC,CAAC,GAAG,IAAU,EAAE,EAAE;QACnC,EAAE,CAAC,KAAK,IAAI,EAAE;YACZ,IAAI;gBACF,MAAM,QAAQ,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,CAAA;aAC3C;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,GAAG,YAAY,kBAAkB,EAAE;oBACrC,IAAI,EAAE,CAAA;iBACP;qBAAM;oBACL,MAAM,GAAG,CAAA;iBACV;aACF;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,IAAI,CAAC,CAAA;AACV,CAAC"}
|
package/lib/use-effect-async.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
-
import { go } from '@blackglory/prelude';
|
|
2
|
+
import { go, pass } from '@blackglory/prelude';
|
|
3
3
|
import { AbortController } from 'extra-abort';
|
|
4
|
-
|
|
4
|
+
import { CustomError } from '@blackglory/errors';
|
|
5
|
+
class InternalAbortError extends CustomError {
|
|
6
|
+
}
|
|
5
7
|
export function useEffectAsync(effect, deps) {
|
|
6
8
|
useEffect(() => {
|
|
7
9
|
const controller = new AbortController();
|
|
@@ -10,11 +12,15 @@ export function useEffectAsync(effect, deps) {
|
|
|
10
12
|
await effect(controller.signal);
|
|
11
13
|
}
|
|
12
14
|
catch (err) {
|
|
13
|
-
if (err
|
|
15
|
+
if (err instanceof InternalAbortError) {
|
|
16
|
+
pass();
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
14
19
|
throw err;
|
|
20
|
+
}
|
|
15
21
|
}
|
|
16
22
|
});
|
|
17
|
-
return () => controller.abort(
|
|
23
|
+
return () => controller.abort(new InternalAbortError());
|
|
18
24
|
}, deps);
|
|
19
25
|
}
|
|
20
26
|
//# sourceMappingURL=use-effect-async.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-effect-async.js","sourceRoot":"","sources":["../src/use-effect-async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkB,MAAM,OAAO,CAAA;AACjD,OAAO,EAAE,EAAE,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"use-effect-async.js","sourceRoot":"","sources":["../src/use-effect-async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkB,MAAM,OAAO,CAAA;AACjD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAA;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,MAAM,kBAAmB,SAAQ,WAAW;CAAG;AAE/C,MAAM,UAAU,cAAc,CAC5B,MAA8C,EAC9C,IAAqB;IAErB,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;QAExC,EAAE,CAAC,KAAK,IAAI,EAAE;YACZ,IAAI;gBACF,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;aAChC;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,GAAG,YAAY,kBAAkB,EAAE;oBACrC,IAAI,EAAE,CAAA;iBACP;qBAAM;oBACL,MAAM,GAAG,CAAA;iBACV;aACF;QACH,CAAC,CAAC,CAAA;QAEF,OAAO,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,kBAAkB,EAAE,CAAC,CAAA;IACzD,CAAC,EAAE,IAAI,CAAC,CAAA;AACV,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-react-hooks",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"files": [
|
|
@@ -65,6 +65,7 @@
|
|
|
65
65
|
"webpack": "^5.86.0"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
+
"@blackglory/errors": "^3.0.3",
|
|
68
69
|
"@blackglory/prelude": "^0.3.3",
|
|
69
70
|
"extra-abort": "^0.3.7",
|
|
70
71
|
"extra-fsm": "^0.2.1",
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { useCallback, DependencyList } from 'react'
|
|
2
|
-
import { go } from '@blackglory/prelude'
|
|
2
|
+
import { go, pass } from '@blackglory/prelude'
|
|
3
3
|
import { AbortController } from 'extra-abort'
|
|
4
4
|
import { useMemoWithCleanup } from './use-memo-with-cleanup.js'
|
|
5
|
+
import { CustomError } from '@blackglory/errors'
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
class InternalAbortError extends CustomError {}
|
|
7
8
|
|
|
8
9
|
export function useCallbackAsync<Args extends unknown[]>(
|
|
9
10
|
callback: (...args: [...args: Args, signal: AbortSignal]) => Promise<void>
|
|
@@ -11,7 +12,7 @@ export function useCallbackAsync<Args extends unknown[]>(
|
|
|
11
12
|
): (...args: Args) => void {
|
|
12
13
|
const controller = useMemoWithCleanup(
|
|
13
14
|
() => new AbortController()
|
|
14
|
-
, controller => controller.abort(
|
|
15
|
+
, controller => controller.abort(new InternalAbortError())
|
|
15
16
|
, deps
|
|
16
17
|
)
|
|
17
18
|
|
|
@@ -20,7 +21,11 @@ export function useCallbackAsync<Args extends unknown[]>(
|
|
|
20
21
|
try {
|
|
21
22
|
await callback(...args, controller.signal)
|
|
22
23
|
} catch (err) {
|
|
23
|
-
if (err
|
|
24
|
+
if (err instanceof InternalAbortError) {
|
|
25
|
+
pass()
|
|
26
|
+
} else {
|
|
27
|
+
throw err
|
|
28
|
+
}
|
|
24
29
|
}
|
|
25
30
|
})
|
|
26
31
|
}, deps)
|
package/src/use-effect-async.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { useEffect, DependencyList } from 'react'
|
|
2
|
-
import { go } from '@blackglory/prelude'
|
|
2
|
+
import { go, pass } from '@blackglory/prelude'
|
|
3
3
|
import { AbortController } from 'extra-abort'
|
|
4
|
+
import { CustomError } from '@blackglory/errors'
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
class InternalAbortError extends CustomError {}
|
|
6
7
|
|
|
7
8
|
export function useEffectAsync(
|
|
8
9
|
effect: (signal: AbortSignal) => Promise<void>
|
|
@@ -15,10 +16,14 @@ export function useEffectAsync(
|
|
|
15
16
|
try {
|
|
16
17
|
await effect(controller.signal)
|
|
17
18
|
} catch (err) {
|
|
18
|
-
if (err
|
|
19
|
+
if (err instanceof InternalAbortError) {
|
|
20
|
+
pass()
|
|
21
|
+
} else {
|
|
22
|
+
throw err
|
|
23
|
+
}
|
|
19
24
|
}
|
|
20
25
|
})
|
|
21
26
|
|
|
22
|
-
return () => controller.abort(
|
|
27
|
+
return () => controller.abort(new InternalAbortError())
|
|
23
28
|
}, deps)
|
|
24
29
|
}
|