rask-ui 0.20.5 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/createComputed.d.ts +4 -0
- package/dist/createComputed.d.ts.map +1 -0
- package/dist/createComputed.js +69 -0
- package/dist/createEffect.d.ts +2 -0
- package/dist/createEffect.d.ts.map +1 -0
- package/dist/createEffect.js +29 -0
- package/dist/createRouter.d.ts +8 -0
- package/dist/createRouter.d.ts.map +1 -0
- package/dist/createRouter.js +27 -0
- package/dist/createState.d.ts +2 -0
- package/dist/createState.d.ts.map +1 -1
- package/dist/createState.js +40 -5
- package/dist/createTask.d.ts +31 -0
- package/dist/createTask.d.ts.map +1 -0
- package/dist/createTask.js +79 -0
- package/dist/createView.d.ts +18 -44
- package/dist/createView.d.ts.map +1 -1
- package/dist/createView.js +57 -48
- package/dist/error.d.ts +3 -14
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +14 -15
- package/dist/jsx.d.ts +10 -256
- package/dist/patchInferno.d.ts +6 -0
- package/dist/patchInferno.d.ts.map +1 -0
- package/dist/patchInferno.js +53 -0
- package/dist/scheduler.d.ts +4 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +107 -0
- package/dist/tests/batch.test.d.ts +2 -0
- package/dist/tests/batch.test.d.ts.map +1 -0
- package/dist/tests/batch.test.js +244 -0
- package/dist/tests/createComputed.test.d.ts +2 -0
- package/dist/tests/createComputed.test.d.ts.map +1 -0
- package/dist/tests/createComputed.test.js +257 -0
- package/dist/tests/createContext.test.d.ts +2 -0
- package/dist/tests/createContext.test.d.ts.map +1 -0
- package/dist/tests/createContext.test.js +136 -0
- package/dist/tests/createEffect.test.d.ts +2 -0
- package/dist/tests/createEffect.test.d.ts.map +1 -0
- package/dist/tests/createEffect.test.js +467 -0
- package/dist/tests/createState.test.d.ts.map +1 -0
- package/dist/tests/createState.test.js +144 -0
- package/dist/tests/createTask.test.d.ts +2 -0
- package/dist/tests/createTask.test.d.ts.map +1 -0
- package/dist/tests/createTask.test.js +322 -0
- package/dist/tests/createView.test.d.ts.map +1 -0
- package/dist/{createView.test.js → tests/createView.test.js} +40 -40
- package/dist/tests/error.test.d.ts +2 -0
- package/dist/tests/error.test.d.ts.map +1 -0
- package/dist/tests/error.test.js +168 -0
- package/dist/tests/observation.test.d.ts.map +1 -0
- package/dist/tests/observation.test.js +341 -0
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useComputed.d.ts +5 -0
- package/dist/useComputed.d.ts.map +1 -0
- package/dist/useComputed.js +69 -0
- package/dist/useQuery.d.ts +25 -0
- package/dist/useQuery.d.ts.map +1 -0
- package/dist/useQuery.js +25 -0
- package/dist/useSuspendAsync.d.ts +18 -0
- package/dist/useSuspendAsync.d.ts.map +1 -0
- package/dist/useSuspendAsync.js +37 -0
- package/dist/useTask.d.ts +25 -0
- package/dist/useTask.d.ts.map +1 -0
- package/dist/useTask.js +70 -0
- package/package.json +1 -1
- package/swc-plugin/target/wasm32-wasip1/release/swc_plugin_rask_component.wasm +0 -0
- package/dist/asyncState.d.ts +0 -16
- package/dist/asyncState.d.ts.map +0 -1
- package/dist/asyncState.js +0 -24
- package/dist/context.d.ts +0 -5
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -29
- package/dist/createAsync.test.d.ts +0 -2
- package/dist/createAsync.test.d.ts.map +0 -1
- package/dist/createAsync.test.js +0 -110
- package/dist/createMutation.test.d.ts +0 -2
- package/dist/createMutation.test.d.ts.map +0 -1
- package/dist/createMutation.test.js +0 -168
- package/dist/createQuery.test.d.ts +0 -2
- package/dist/createQuery.test.d.ts.map +0 -1
- package/dist/createQuery.test.js +0 -156
- package/dist/createRef.d.ts +0 -6
- package/dist/createRef.d.ts.map +0 -1
- package/dist/createRef.js +0 -8
- package/dist/createState.test.d.ts.map +0 -1
- package/dist/createState.test.js +0 -111
- package/dist/createView.test.d.ts.map +0 -1
- package/dist/observation.test.d.ts.map +0 -1
- package/dist/observation.test.js +0 -150
- package/dist/suspense.d.ts +0 -25
- package/dist/suspense.d.ts.map +0 -1
- package/dist/suspense.js +0 -97
- package/dist/test-setup.d.ts +0 -16
- package/dist/test-setup.d.ts.map +0 -1
- package/dist/test-setup.js +0 -40
- package/dist/test.d.ts +0 -2
- package/dist/test.d.ts.map +0 -1
- package/dist/test.js +0 -24
- /package/dist/{createState.test.d.ts → tests/createState.test.d.ts} +0 -0
- /package/dist/{createView.test.d.ts → tests/createView.test.d.ts} +0 -0
- /package/dist/{observation.test.d.ts → tests/observation.test.d.ts} +0 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AsyncState } from "./useAsync";
|
|
2
|
+
type SuspendAsyncState<T extends Record<string, AsyncState<any>>> = {
|
|
3
|
+
values: {
|
|
4
|
+
[K in keyof T]: T[K]["value"];
|
|
5
|
+
};
|
|
6
|
+
} & ({
|
|
7
|
+
isPending: true;
|
|
8
|
+
error: null;
|
|
9
|
+
} | {
|
|
10
|
+
isPending: false;
|
|
11
|
+
error: string;
|
|
12
|
+
} | {
|
|
13
|
+
isPending: false;
|
|
14
|
+
error: null;
|
|
15
|
+
});
|
|
16
|
+
export declare function useSuspendAsync<T extends Record<string, AsyncState<any>>>(asyncs: T): SuspendAsyncState<T>;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=useSuspendAsync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSuspendAsync.d.ts","sourceRoot":"","sources":["../src/useSuspendAsync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC,KAAK,iBAAiB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI;IAClE,MAAM,EAAE;SACL,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;KAC9B,CAAC;CACH,GAAG,CACA;IACE,SAAS,EAAE,IAAI,CAAC;IAChB,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,KAAK,EAAE,IAAI,CAAC;CACb,CACJ,CAAC;AAEF,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,EACvE,MAAM,EAAE,CAAC,wBAyCV"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useEffect } from "./useEffect";
|
|
2
|
+
import { useState } from "./useState";
|
|
3
|
+
export function useSuspendAsync(asyncs) {
|
|
4
|
+
const state = useState({
|
|
5
|
+
isPending: false,
|
|
6
|
+
error: null,
|
|
7
|
+
values: Object.keys(asyncs).reduce((aggr, key) => ({ ...aggr, [key]: asyncs[key].value }), {}),
|
|
8
|
+
});
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
let isPending = false;
|
|
11
|
+
let error = "";
|
|
12
|
+
for (const key in asyncs) {
|
|
13
|
+
if (asyncs[key].error) {
|
|
14
|
+
error += asyncs[key].error + "\n";
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (asyncs[key].isPending) {
|
|
18
|
+
isPending = true;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (error) {
|
|
23
|
+
state.isPending = false;
|
|
24
|
+
state.error = error;
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (isPending) {
|
|
28
|
+
state.isPending = false;
|
|
29
|
+
state.error = null;
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
for (const key in asyncs) {
|
|
33
|
+
state.values[key] = asyncs[key].value;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return state;
|
|
37
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type TaskState<P, T> = {
|
|
2
|
+
isPending: false;
|
|
3
|
+
params: null;
|
|
4
|
+
result: null;
|
|
5
|
+
error: null;
|
|
6
|
+
} | {
|
|
7
|
+
isPending: true;
|
|
8
|
+
result: T | null;
|
|
9
|
+
params: P;
|
|
10
|
+
error: null;
|
|
11
|
+
} | {
|
|
12
|
+
isPending: false;
|
|
13
|
+
params: null;
|
|
14
|
+
result: T;
|
|
15
|
+
error: null;
|
|
16
|
+
} | {
|
|
17
|
+
isPending: false;
|
|
18
|
+
params: null;
|
|
19
|
+
result: null;
|
|
20
|
+
error: string;
|
|
21
|
+
};
|
|
22
|
+
export type Task<A, B = never> = [B] extends [never] ? [TaskState<null, A>, () => void] : [TaskState<A, B>, (params: A) => void];
|
|
23
|
+
export declare function useTask<T>(task: (params: undefined, signal: AbortSignal) => Promise<T>): Task<T>;
|
|
24
|
+
export declare function useTask<P, T>(task: (params: P, signal: AbortSignal) => Promise<T>): Task<P, T>;
|
|
25
|
+
//# sourceMappingURL=useTask.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTask.d.ts","sourceRoot":"","sources":["../src/useTask.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,CAAC,CAAC,EAAE,CAAC,IACtB;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,MAAM,EAAE,IAAI,CAAC;IACb,MAAM,EAAE,IAAI,CAAC;IACb,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;IACjB,MAAM,EAAE,CAAC,CAAC;IACV,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,MAAM,EAAE,IAAI,CAAC;IACb,MAAM,EAAE,CAAC,CAAC;IACV,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,MAAM,EAAE,IAAI,CAAC;IACb,MAAM,EAAE,IAAI,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEN,MAAM,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAChD,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,IAAI,CAAC,GAChC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,CAAC,CAAC;AAE3C,wBAAgB,OAAO,CAAC,CAAC,EACvB,IAAI,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAC3D,IAAI,CAAC,CAAC,CAAC,CAAC;AACX,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAC1B,IAAI,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GACnD,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
package/dist/useTask.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { useCleanup, getCurrentComponent } from "./component";
|
|
2
|
+
import { assignState, useState } from "./useState";
|
|
3
|
+
export function useTask(task) {
|
|
4
|
+
const currentComponent = getCurrentComponent();
|
|
5
|
+
if (!currentComponent || currentComponent.isRendering) {
|
|
6
|
+
throw new Error("Only use createTask in component setup");
|
|
7
|
+
}
|
|
8
|
+
const state = useState({
|
|
9
|
+
isPending: false,
|
|
10
|
+
result: null,
|
|
11
|
+
error: null,
|
|
12
|
+
params: null,
|
|
13
|
+
});
|
|
14
|
+
let currentAbortController;
|
|
15
|
+
const fetch = (params) => {
|
|
16
|
+
currentAbortController?.abort();
|
|
17
|
+
const abortController = (currentAbortController = new AbortController());
|
|
18
|
+
const promise = task(params, abortController.signal);
|
|
19
|
+
promise
|
|
20
|
+
.then((result) => {
|
|
21
|
+
if (abortController.signal.aborted) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
assignState(state, {
|
|
25
|
+
isPending: false,
|
|
26
|
+
result,
|
|
27
|
+
error: null,
|
|
28
|
+
params: null,
|
|
29
|
+
});
|
|
30
|
+
})
|
|
31
|
+
.catch((error) => {
|
|
32
|
+
if (abortController.signal.aborted) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
assignState(state, {
|
|
36
|
+
isPending: false,
|
|
37
|
+
result: null,
|
|
38
|
+
error: String(error),
|
|
39
|
+
params: null,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
return promise;
|
|
43
|
+
};
|
|
44
|
+
useCleanup(() => currentAbortController?.abort());
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
get isPending() {
|
|
48
|
+
return state.isPending;
|
|
49
|
+
},
|
|
50
|
+
get result() {
|
|
51
|
+
return state.result;
|
|
52
|
+
},
|
|
53
|
+
get error() {
|
|
54
|
+
return state.error;
|
|
55
|
+
},
|
|
56
|
+
get params() {
|
|
57
|
+
return state.params;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
(params) => {
|
|
61
|
+
fetch(params);
|
|
62
|
+
assignState(state, {
|
|
63
|
+
isPending: true,
|
|
64
|
+
result: null,
|
|
65
|
+
error: null,
|
|
66
|
+
params: (params || null),
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
];
|
|
70
|
+
}
|
package/package.json
CHANGED
|
Binary file
|
package/dist/asyncState.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
type AsyncState<T> = {
|
|
2
|
-
isPending: true;
|
|
3
|
-
value: null;
|
|
4
|
-
error: null;
|
|
5
|
-
} | {
|
|
6
|
-
isPending: false;
|
|
7
|
-
value: T;
|
|
8
|
-
error: null;
|
|
9
|
-
} | {
|
|
10
|
-
isPending: false;
|
|
11
|
-
value: null;
|
|
12
|
-
error: string;
|
|
13
|
-
};
|
|
14
|
-
export declare function createAsyncState<T>(promise: Promise<T>): AsyncState<T>;
|
|
15
|
-
export {};
|
|
16
|
-
//# sourceMappingURL=asyncState.d.ts.map
|
package/dist/asyncState.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"asyncState.d.ts","sourceRoot":"","sources":["../src/asyncState.ts"],"names":[],"mappings":"AAEA,KAAK,UAAU,CAAC,CAAC,IACb;IACE,SAAS,EAAE,IAAI,CAAC;IAChB,KAAK,EAAE,IAAI,CAAC;IACZ,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,KAAK,EAAE,CAAC,CAAC;IACT,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,KAAK,EAAE,IAAI,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEN,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,iBAwBtD"}
|
package/dist/asyncState.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { createState } from "./createState";
|
|
2
|
-
export function createAsyncState(promise) {
|
|
3
|
-
const state = createState({
|
|
4
|
-
isPending: true,
|
|
5
|
-
error: null,
|
|
6
|
-
value: null,
|
|
7
|
-
});
|
|
8
|
-
promise
|
|
9
|
-
.then((value) => {
|
|
10
|
-
Object.assign(state, {
|
|
11
|
-
value,
|
|
12
|
-
error: null,
|
|
13
|
-
isPending: false,
|
|
14
|
-
});
|
|
15
|
-
})
|
|
16
|
-
.catch((error) => {
|
|
17
|
-
Object.assign(state, {
|
|
18
|
-
value: null,
|
|
19
|
-
error: String(error),
|
|
20
|
-
isPending: false,
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
return state;
|
|
24
|
-
}
|
package/dist/context.d.ts
DELETED
package/dist/context.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAEA,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM;eAE/B,CAAC;WAaL,CAAC;EAmBX"}
|
package/dist/context.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { getCurrentComponent } from "./component";
|
|
2
|
-
export function createContext() {
|
|
3
|
-
const context = {
|
|
4
|
-
set(value) {
|
|
5
|
-
const currentComponent = getCurrentComponent();
|
|
6
|
-
if (!currentComponent) {
|
|
7
|
-
throw new Error("You can not set context out component setup");
|
|
8
|
-
}
|
|
9
|
-
if (!currentComponent.contexts) {
|
|
10
|
-
currentComponent.contexts = new Map();
|
|
11
|
-
}
|
|
12
|
-
currentComponent.contexts.set(context, value);
|
|
13
|
-
},
|
|
14
|
-
get() {
|
|
15
|
-
let currentComponent = getCurrentComponent();
|
|
16
|
-
if (!currentComponent) {
|
|
17
|
-
throw new Error("You can not set context out component setup");
|
|
18
|
-
}
|
|
19
|
-
while (currentComponent) {
|
|
20
|
-
if (currentComponent.contexts?.has(context)) {
|
|
21
|
-
return currentComponent.contexts.get(context);
|
|
22
|
-
}
|
|
23
|
-
currentComponent = currentComponent.parent;
|
|
24
|
-
}
|
|
25
|
-
throw new Error("Could not find context in parent components");
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
return context;
|
|
29
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createAsync.test.d.ts","sourceRoot":"","sources":["../src/createAsync.test.ts"],"names":[],"mappings":""}
|
package/dist/createAsync.test.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { createAsync } from './createAsync';
|
|
3
|
-
describe('createAsync', () => {
|
|
4
|
-
it('should start in pending state', () => {
|
|
5
|
-
const promise = new Promise(() => { });
|
|
6
|
-
const async = createAsync(promise);
|
|
7
|
-
expect(async.isPending).toBe(true);
|
|
8
|
-
expect(async.value).toBeNull();
|
|
9
|
-
expect(async.error).toBeNull();
|
|
10
|
-
});
|
|
11
|
-
it('should resolve to value state on success', async () => {
|
|
12
|
-
const promise = Promise.resolve('success');
|
|
13
|
-
const async = createAsync(promise);
|
|
14
|
-
expect(async.isPending).toBe(true);
|
|
15
|
-
await promise;
|
|
16
|
-
// Wait for state update
|
|
17
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
18
|
-
expect(async.isPending).toBe(false);
|
|
19
|
-
expect(async.value).toBe('success');
|
|
20
|
-
expect(async.error).toBeNull();
|
|
21
|
-
});
|
|
22
|
-
it('should resolve to error state on rejection', async () => {
|
|
23
|
-
const promise = Promise.reject(new Error('failed'));
|
|
24
|
-
const async = createAsync(promise);
|
|
25
|
-
expect(async.isPending).toBe(true);
|
|
26
|
-
try {
|
|
27
|
-
await promise;
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
// Ignore
|
|
31
|
-
}
|
|
32
|
-
// Wait for state update
|
|
33
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
34
|
-
expect(async.isPending).toBe(false);
|
|
35
|
-
expect(async.value).toBeNull();
|
|
36
|
-
expect(async.error).toContain('failed');
|
|
37
|
-
});
|
|
38
|
-
it('should handle numeric values', async () => {
|
|
39
|
-
const promise = Promise.resolve(42);
|
|
40
|
-
const async = createAsync(promise);
|
|
41
|
-
await promise;
|
|
42
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
43
|
-
expect(async.value).toBe(42);
|
|
44
|
-
});
|
|
45
|
-
it('should handle object values', async () => {
|
|
46
|
-
const data = { id: 1, name: 'Test' };
|
|
47
|
-
const promise = Promise.resolve(data);
|
|
48
|
-
const async = createAsync(promise);
|
|
49
|
-
await promise;
|
|
50
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
51
|
-
expect(async.value).toEqual(data);
|
|
52
|
-
});
|
|
53
|
-
it('should handle array values', async () => {
|
|
54
|
-
const data = [1, 2, 3, 4, 5];
|
|
55
|
-
const promise = Promise.resolve(data);
|
|
56
|
-
const async = createAsync(promise);
|
|
57
|
-
await promise;
|
|
58
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
59
|
-
expect(async.value).toEqual(data);
|
|
60
|
-
});
|
|
61
|
-
it('should convert error to string', async () => {
|
|
62
|
-
const promise = Promise.reject('string error');
|
|
63
|
-
const async = createAsync(promise);
|
|
64
|
-
try {
|
|
65
|
-
await promise;
|
|
66
|
-
}
|
|
67
|
-
catch {
|
|
68
|
-
// Ignore
|
|
69
|
-
}
|
|
70
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
71
|
-
expect(typeof async.error).toBe('string');
|
|
72
|
-
expect(async.error).toBe('string error');
|
|
73
|
-
});
|
|
74
|
-
it('should handle error objects', async () => {
|
|
75
|
-
const error = new Error('Something went wrong');
|
|
76
|
-
const promise = Promise.reject(error);
|
|
77
|
-
const async = createAsync(promise);
|
|
78
|
-
try {
|
|
79
|
-
await promise;
|
|
80
|
-
}
|
|
81
|
-
catch {
|
|
82
|
-
// Ignore
|
|
83
|
-
}
|
|
84
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
85
|
-
expect(async.error).toContain('Something went wrong');
|
|
86
|
-
});
|
|
87
|
-
it('should create reactive state', async () => {
|
|
88
|
-
const promise = new Promise((resolve) => {
|
|
89
|
-
setTimeout(() => resolve('delayed'), 10);
|
|
90
|
-
});
|
|
91
|
-
const async = createAsync(promise);
|
|
92
|
-
expect(async.isPending).toBe(true);
|
|
93
|
-
await promise;
|
|
94
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
95
|
-
expect(async.isPending).toBe(false);
|
|
96
|
-
expect(async.value).toBe('delayed');
|
|
97
|
-
});
|
|
98
|
-
it('should handle immediate resolution', async () => {
|
|
99
|
-
const async = createAsync(Promise.resolve('immediate'));
|
|
100
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
101
|
-
expect(async.isPending).toBe(false);
|
|
102
|
-
expect(async.value).toBe('immediate');
|
|
103
|
-
});
|
|
104
|
-
it('should handle immediate rejection', async () => {
|
|
105
|
-
const async = createAsync(Promise.reject('immediate error'));
|
|
106
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
107
|
-
expect(async.isPending).toBe(false);
|
|
108
|
-
expect(async.error).toBe('immediate error');
|
|
109
|
-
});
|
|
110
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createMutation.test.d.ts","sourceRoot":"","sources":["../src/createMutation.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
-
import { createMutation } from './createMutation';
|
|
3
|
-
describe('createMutation', () => {
|
|
4
|
-
it('should start in idle state', () => {
|
|
5
|
-
const mutator = vi.fn(() => Promise.resolve(null));
|
|
6
|
-
const mutation = createMutation(mutator);
|
|
7
|
-
expect(mutation.isPending).toBe(false);
|
|
8
|
-
expect(mutation.params).toBeNull();
|
|
9
|
-
expect(mutation.error).toBeNull();
|
|
10
|
-
expect(mutator).not.toHaveBeenCalled();
|
|
11
|
-
});
|
|
12
|
-
it('should execute mutator when mutate is called', async () => {
|
|
13
|
-
const mutator = vi.fn((params) => Promise.resolve(params));
|
|
14
|
-
const mutation = createMutation(mutator);
|
|
15
|
-
mutation.mutate('test');
|
|
16
|
-
expect(mutation.isPending).toBe(true);
|
|
17
|
-
expect(mutation.params).toBe('test');
|
|
18
|
-
expect(mutator).toHaveBeenCalledWith('test');
|
|
19
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
20
|
-
expect(mutation.isPending).toBe(false);
|
|
21
|
-
expect(mutation.params).toBeNull();
|
|
22
|
-
});
|
|
23
|
-
it('should handle successful mutations', async () => {
|
|
24
|
-
const mutator = (params) => Promise.resolve(params);
|
|
25
|
-
const mutation = createMutation(mutator);
|
|
26
|
-
mutation.mutate({ id: 1 });
|
|
27
|
-
expect(mutation.isPending).toBe(true);
|
|
28
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
29
|
-
expect(mutation.isPending).toBe(false);
|
|
30
|
-
expect(mutation.error).toBeNull();
|
|
31
|
-
expect(mutation.params).toBeNull();
|
|
32
|
-
});
|
|
33
|
-
it('should handle mutation errors', async () => {
|
|
34
|
-
const mutator = (params) => Promise.reject(new Error('Mutation failed'));
|
|
35
|
-
const mutation = createMutation(mutator);
|
|
36
|
-
mutation.mutate('test');
|
|
37
|
-
expect(mutation.isPending).toBe(true);
|
|
38
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
39
|
-
expect(mutation.isPending).toBe(false);
|
|
40
|
-
expect(mutation.error).toContain('Mutation failed');
|
|
41
|
-
expect(mutation.params).toBeNull();
|
|
42
|
-
});
|
|
43
|
-
it('should cancel previous mutation on new mutate call', async () => {
|
|
44
|
-
let resolveFirst;
|
|
45
|
-
let resolveSecond;
|
|
46
|
-
const firstPromise = new Promise((resolve) => {
|
|
47
|
-
resolveFirst = resolve;
|
|
48
|
-
});
|
|
49
|
-
const secondPromise = new Promise((resolve) => {
|
|
50
|
-
resolveSecond = resolve;
|
|
51
|
-
});
|
|
52
|
-
const mutator = vi
|
|
53
|
-
.fn()
|
|
54
|
-
.mockReturnValueOnce(firstPromise)
|
|
55
|
-
.mockReturnValueOnce(secondPromise);
|
|
56
|
-
const mutation = createMutation(mutator);
|
|
57
|
-
mutation.mutate('first');
|
|
58
|
-
expect(mutation.params).toBe('first');
|
|
59
|
-
// Trigger second mutation before first completes
|
|
60
|
-
mutation.mutate('second');
|
|
61
|
-
expect(mutation.params).toBe('second');
|
|
62
|
-
// Resolve first (should be ignored due to cancellation)
|
|
63
|
-
resolveFirst('first');
|
|
64
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
65
|
-
expect(mutation.isPending).toBe(true); // Still pending second
|
|
66
|
-
// Resolve second
|
|
67
|
-
resolveSecond('second');
|
|
68
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
69
|
-
expect(mutation.isPending).toBe(false);
|
|
70
|
-
expect(mutation.params).toBeNull();
|
|
71
|
-
});
|
|
72
|
-
it('should handle rapid successive mutations', async () => {
|
|
73
|
-
let counter = 0;
|
|
74
|
-
const mutator = vi.fn(() => Promise.resolve(++counter));
|
|
75
|
-
const mutation = createMutation(mutator);
|
|
76
|
-
// Rapid mutations
|
|
77
|
-
mutation.mutate('1');
|
|
78
|
-
mutation.mutate('2');
|
|
79
|
-
mutation.mutate('3');
|
|
80
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
81
|
-
// Only the last mutation should complete
|
|
82
|
-
expect(mutator).toHaveBeenCalledTimes(3);
|
|
83
|
-
expect(mutation.isPending).toBe(false);
|
|
84
|
-
expect(mutation.params).toBeNull();
|
|
85
|
-
});
|
|
86
|
-
it('should clear error on successful retry', async () => {
|
|
87
|
-
const mutator = vi
|
|
88
|
-
.fn()
|
|
89
|
-
.mockRejectedValueOnce(new Error('First error'))
|
|
90
|
-
.mockResolvedValueOnce('success');
|
|
91
|
-
const mutation = createMutation(mutator);
|
|
92
|
-
mutation.mutate('attempt1');
|
|
93
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
94
|
-
expect(mutation.error).toContain('First error');
|
|
95
|
-
mutation.mutate('attempt2');
|
|
96
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
97
|
-
expect(mutation.error).toBeNull();
|
|
98
|
-
expect(mutation.isPending).toBe(false);
|
|
99
|
-
});
|
|
100
|
-
it('should handle different parameter types', async () => {
|
|
101
|
-
const mutator = vi.fn((params) => Promise.resolve(params));
|
|
102
|
-
const mutation = createMutation(mutator);
|
|
103
|
-
// Object params
|
|
104
|
-
mutation.mutate({ id: 1, name: 'test' });
|
|
105
|
-
expect(mutation.params).toEqual({ id: 1, name: 'test' });
|
|
106
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
107
|
-
// Array params
|
|
108
|
-
const mutation2 = createMutation(mutator);
|
|
109
|
-
mutation2.mutate([1, 2, 3]);
|
|
110
|
-
expect(mutation2.params).toEqual([1, 2, 3]);
|
|
111
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
112
|
-
// String params
|
|
113
|
-
const mutation3 = createMutation(mutator);
|
|
114
|
-
mutation3.mutate('string');
|
|
115
|
-
expect(mutation3.params).toBe('string');
|
|
116
|
-
});
|
|
117
|
-
it('should convert errors to strings', async () => {
|
|
118
|
-
const mutator = (params) => Promise.reject('string error');
|
|
119
|
-
const mutation = createMutation(mutator);
|
|
120
|
-
mutation.mutate('test');
|
|
121
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
122
|
-
expect(typeof mutation.error).toBe('string');
|
|
123
|
-
expect(mutation.error).toBe('string error');
|
|
124
|
-
});
|
|
125
|
-
it('should handle AbortController cancellation correctly', async () => {
|
|
126
|
-
const abortedPromise = new Promise((_, reject) => {
|
|
127
|
-
const error = new Error('Aborted');
|
|
128
|
-
error.name = 'AbortError';
|
|
129
|
-
setTimeout(() => reject(error), 5);
|
|
130
|
-
});
|
|
131
|
-
const successPromise = Promise.resolve('success');
|
|
132
|
-
const mutator = vi
|
|
133
|
-
.fn()
|
|
134
|
-
.mockReturnValueOnce(abortedPromise)
|
|
135
|
-
.mockReturnValueOnce(successPromise);
|
|
136
|
-
const mutation = createMutation(mutator);
|
|
137
|
-
mutation.mutate('first');
|
|
138
|
-
// Immediately trigger second mutation to abort first
|
|
139
|
-
mutation.mutate('second');
|
|
140
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
141
|
-
expect(mutation.isPending).toBe(false);
|
|
142
|
-
expect(mutation.error).toBeNull();
|
|
143
|
-
});
|
|
144
|
-
it('should track params during pending state', () => {
|
|
145
|
-
const mutator = () => new Promise((resolve) => setTimeout(() => resolve(null), 100));
|
|
146
|
-
const mutation = createMutation(mutator);
|
|
147
|
-
const params = { id: 123, action: 'update' };
|
|
148
|
-
mutation.mutate(params);
|
|
149
|
-
expect(mutation.isPending).toBe(true);
|
|
150
|
-
expect(mutation.params).toEqual(params);
|
|
151
|
-
});
|
|
152
|
-
it('should expose reactive getters', async () => {
|
|
153
|
-
const mutator = () => Promise.resolve('data');
|
|
154
|
-
const mutation = createMutation(mutator);
|
|
155
|
-
// Access getters before mutation
|
|
156
|
-
expect(mutation.isPending).toBe(false);
|
|
157
|
-
expect(mutation.params).toBeNull();
|
|
158
|
-
expect(mutation.error).toBeNull();
|
|
159
|
-
mutation.mutate('test');
|
|
160
|
-
// Access getters during mutation
|
|
161
|
-
expect(mutation.isPending).toBe(true);
|
|
162
|
-
expect(mutation.params).toBe('test');
|
|
163
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
164
|
-
// Access getters after mutation
|
|
165
|
-
expect(mutation.isPending).toBe(false);
|
|
166
|
-
expect(mutation.params).toBeNull();
|
|
167
|
-
});
|
|
168
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createQuery.test.d.ts","sourceRoot":"","sources":["../src/createQuery.test.ts"],"names":[],"mappings":""}
|