Skip to content

Commit 5beff29

Browse files
committed
fixes: v0.1.3 - changes signature and exports
1 parent 07c306b commit 5beff29

File tree

8 files changed

+57
-21
lines changed

8 files changed

+57
-21
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ A repository of `React` components
1414

1515
# Hooks
1616

17-
## Async
17+
### Async hooks
1818

1919
| Name | Behaviour | todo |
2020
|-----------------------|----------------------------------------------------------------------|------|
@@ -28,3 +28,19 @@ A repository of `React` components
2828
| `useSharedAsync2` | Last call is the only one effective, all resolve with the last value | |
2929
| | | |
3030
| `useQuery` | Similar to task, but runs when dependencies changes | |
31+
32+
### Browser hooks
33+
34+
35+
### Dom hooks
36+
37+
38+
### Generic hooks
39+
40+
41+
### Navigation hooks
42+
43+
44+
### State hooks
45+
46+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Hook collection for React",
44
"author": "Borut Svara <[email protected]>",
55
"license": "ISC",
6-
"version": "0.1.1",
6+
"version": "0.1.3",
77
"main": "dist/index.js",
88
"types": "dist/index.d.ts",
99
"scripts": {

src/async/useAsync.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ export interface UseAsyncState<Res, Err> {
2020
error?: Err
2121
}
2222

23-
export interface UseAsyncReturn<Args extends any[] = [], Res = void, Err = any> extends UseAsyncState<Res, Err>{
23+
export interface UseAsyncReturn<Res = void, Args extends any[] = [], Err = any> extends UseAsyncState<Res, Err>{
2424
run: (...args: Args) => void
2525
runAsync: PromiseFn<Args, Res>
2626
}
2727

28-
export function useAsync<Args extends any[] = [], Res = void, Err = any>(
28+
export function useAsync<Res = void, Args extends any[] = [], Err = any>(
2929
fn: PromiseFn<Args, Res>,
30-
): UseAsyncReturn<Args, Res, Err> {
30+
): UseAsyncReturn<Res, Args, Err> {
3131

3232
// Unique call id to handle concurrency
3333
const callIdRef = useRef(0)

src/async/useExclusivePromise.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import { useLatest } from '../generic/useLatest';
1515
*
1616
* /B 2022-10-19
1717
*/
18-
export function useExclusivePromise<R, A extends any[] = []>(fn: PromiseFn<A, R>): PromiseFn<A, R> {
18+
export function useExclusivePromise<Res, Args extends any[] = []>(fn: PromiseFn<Args, Res>): PromiseFn<Args, Res> {
1919

2020
const lastFn = useLatest(fn)
2121
const isExecuting = useRef<boolean>(false)
2222

23-
return useCallback(async (...args: A): Promise<R> => {
23+
return useCallback(async (...args: Args): Promise<Res> => {
2424
if (isExecuting.current) {
2525
throw new ConcurrencyError(`Promise is already running`)
2626
} else {

src/async/usePromise.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { useLatest } from '../generic/useLatest';
1313
*
1414
* /B 2022-10-19
1515
*/
16-
export function usePromise<R, A extends any[] = []>(fn: PromiseFn<A, R>): DispatchFn<A> {
16+
export function usePromise<Res, Args extends any[] = []>(fn: PromiseFn<Args, Res>): DispatchFn<Args> {
1717

1818
const lastFn = useLatest(fn)
1919

20-
return useCallback((...args: A) => {
20+
return useCallback((...args: Args) => {
2121
lastFn.current.apply(undefined, ...args).catch((error: any) => {
2222
console.debug(`usePromise collected an error ${fn.name}`, error)
2323
})

src/async/useSharedAsync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { usePromise } from './usePromise';
99
* same or will throw if arguments have changed.
1010
*/
1111

12-
export function useSharedAsync<Args extends any[] = [], Res = void, Err = any>(
12+
export function useSharedAsync<Res = void, Args extends any[] = [], Err = any>(
1313
fn: PromiseFn<Args, Res>,
14-
): UseAsyncReturn<Args, Res, Err> {
14+
): UseAsyncReturn<Res, Args, Err> {
1515

1616
const { runAsync: oldRunAsync, run: oldRun, ...rest } = useAsync(fn);
1717
const runAsync = useSharedPromise(oldRunAsync)

src/async/useSharedPromise.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ import { areArgsEqual } from '../utils';
2121
*
2222
* /B 2022-10-19
2323
*/
24-
export function useSharedPromise<R, A extends any[] = []>(fn: PromiseFn<A, R>): PromiseFn<A, R> {
24+
export function useSharedPromise<Res, Args extends any[] = []>(fn: PromiseFn<Args, Res>): PromiseFn<Args, Res> {
2525

2626
const lastFn = useLatest(fn)
27-
const stack = useRef<{ args: A, res: ((v: R) => void)[], rej: ((e: any) => void)[] } | undefined>()
27+
const stack = useRef<{ args: Args, res: ((v: Res) => void)[], rej: ((e: any) => void)[] } | undefined>()
2828

29-
return useCallback(async (...args: A): Promise<R> => {
30-
return new Promise<R>((res, rej) => {
29+
return useCallback(async (...args: Args): Promise<Res> => {
30+
return new Promise<Res>((res, rej) => {
3131
if (!stack.current) {
3232
// First execution
3333
stack.current = { args, res: [res], rej: [rej] }
3434

3535
lastFn.current.apply(undefined, args)
36-
.then((r: R) => {
36+
.then((r: Res) => {
3737
stack.current?.res.forEach(f => f(r))
3838
stack.current = undefined
3939
})

src/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
export { useLatest } from './generic/useLatest';
2-
export { usePrevious } from './generic/usePrevious';
3-
export { useDefault } from './state/useDefault';
4-
export { usePatch } from './state/usePatch';
5-
export { useReset } from './state/useReset';
1+
2+
export * from './async/types';
3+
export * from './async/useAsync';
4+
export * from './async/useExclusivePromise';
5+
export * from './async/usePromise';
6+
export * from './async/useQuery';
7+
export * from './async/useSharedAsync';
8+
export * from './async/useSharedPromise';
9+
10+
export * from './browser/useIsMobile';
11+
12+
export * from './dom/useEvent';
13+
14+
export * from './generic/useLatest';
15+
export * from './generic/usePrevious';
16+
17+
export * from './navigation/useHref';
18+
export * from './navigation/useLocation';
19+
export * from './navigation/useSearchParam';
20+
21+
export * from './state/useDefault';
22+
export * from './state/usePatch';
23+
export * from './state/useReset';
24+
25+
export * from './utils';

0 commit comments

Comments
 (0)