rask-ui 0.22.0 → 0.24.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 (121) hide show
  1. package/dist/compiler.d.ts +3 -1
  2. package/dist/compiler.d.ts.map +1 -1
  3. package/dist/compiler.js +7 -1
  4. package/dist/component.d.ts +4 -18
  5. package/dist/component.d.ts.map +1 -1
  6. package/dist/component.js +14 -67
  7. package/dist/createComputed.d.ts +4 -0
  8. package/dist/createComputed.d.ts.map +1 -0
  9. package/dist/createComputed.js +69 -0
  10. package/dist/createContext.d.ts +26 -9
  11. package/dist/createContext.d.ts.map +1 -1
  12. package/dist/createContext.js +47 -30
  13. package/dist/createEffect.d.ts +2 -0
  14. package/dist/createEffect.d.ts.map +1 -0
  15. package/dist/createEffect.js +29 -0
  16. package/dist/createRouter.d.ts +8 -0
  17. package/dist/createRouter.d.ts.map +1 -0
  18. package/dist/createRouter.js +27 -0
  19. package/dist/createState.d.ts +2 -0
  20. package/dist/createState.d.ts.map +1 -1
  21. package/dist/createState.js +40 -5
  22. package/dist/createTask.d.ts +31 -0
  23. package/dist/createTask.d.ts.map +1 -0
  24. package/dist/createTask.js +79 -0
  25. package/dist/createView.d.ts +18 -44
  26. package/dist/createView.d.ts.map +1 -1
  27. package/dist/createView.js +57 -48
  28. package/dist/error.d.ts +3 -14
  29. package/dist/error.d.ts.map +1 -1
  30. package/dist/error.js +14 -15
  31. package/dist/index.d.ts +2 -2
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +2 -2
  34. package/dist/jsx.d.ts +10 -256
  35. package/dist/observation.d.ts +1 -1
  36. package/dist/observation.d.ts.map +1 -1
  37. package/dist/patchInferno.d.ts +6 -0
  38. package/dist/patchInferno.d.ts.map +1 -0
  39. package/dist/patchInferno.js +53 -0
  40. package/dist/plugin.d.ts +1 -1
  41. package/dist/plugin.d.ts.map +1 -1
  42. package/dist/plugin.js +14 -26
  43. package/dist/scheduler.d.ts +4 -0
  44. package/dist/scheduler.d.ts.map +1 -0
  45. package/dist/scheduler.js +107 -0
  46. package/dist/tests/batch.test.d.ts +2 -0
  47. package/dist/tests/batch.test.d.ts.map +1 -0
  48. package/dist/tests/batch.test.js +244 -0
  49. package/dist/tests/createComputed.test.d.ts +2 -0
  50. package/dist/tests/createComputed.test.d.ts.map +1 -0
  51. package/dist/tests/createComputed.test.js +257 -0
  52. package/dist/tests/createContext.test.d.ts +2 -0
  53. package/dist/tests/createContext.test.d.ts.map +1 -0
  54. package/dist/tests/createContext.test.js +136 -0
  55. package/dist/tests/createEffect.test.d.ts +2 -0
  56. package/dist/tests/createEffect.test.d.ts.map +1 -0
  57. package/dist/tests/createEffect.test.js +467 -0
  58. package/dist/tests/createState.test.d.ts.map +1 -0
  59. package/dist/tests/createState.test.js +144 -0
  60. package/dist/tests/createTask.test.d.ts +2 -0
  61. package/dist/tests/createTask.test.d.ts.map +1 -0
  62. package/dist/tests/createTask.test.js +322 -0
  63. package/dist/tests/createView.test.d.ts.map +1 -0
  64. package/dist/{createView.test.js → tests/createView.test.js} +40 -40
  65. package/dist/tests/error.test.d.ts +2 -0
  66. package/dist/tests/error.test.d.ts.map +1 -0
  67. package/dist/tests/error.test.js +168 -0
  68. package/dist/tests/observation.test.d.ts.map +1 -0
  69. package/dist/tests/observation.test.js +341 -0
  70. package/dist/useCatchError.d.ts +6 -3
  71. package/dist/useCatchError.d.ts.map +1 -1
  72. package/dist/useCatchError.js +2 -2
  73. package/dist/useComputed.d.ts +5 -0
  74. package/dist/useComputed.d.ts.map +1 -0
  75. package/dist/useComputed.js +69 -0
  76. package/dist/useQuery.d.ts +25 -0
  77. package/dist/useQuery.d.ts.map +1 -0
  78. package/dist/useQuery.js +25 -0
  79. package/dist/useSuspendAsync.d.ts +18 -0
  80. package/dist/useSuspendAsync.d.ts.map +1 -0
  81. package/dist/useSuspendAsync.js +37 -0
  82. package/dist/useTask.d.ts +25 -0
  83. package/dist/useTask.d.ts.map +1 -0
  84. package/dist/useTask.js +70 -0
  85. package/package.json +1 -1
  86. package/swc-plugin/target/wasm32-wasip1/release/swc_plugin_rask_component.wasm +0 -0
  87. package/dist/asyncState.d.ts +0 -16
  88. package/dist/asyncState.d.ts.map +0 -1
  89. package/dist/asyncState.js +0 -24
  90. package/dist/context.d.ts +0 -5
  91. package/dist/context.d.ts.map +0 -1
  92. package/dist/context.js +0 -29
  93. package/dist/createAsync.test.d.ts +0 -2
  94. package/dist/createAsync.test.d.ts.map +0 -1
  95. package/dist/createAsync.test.js +0 -110
  96. package/dist/createMutation.test.d.ts +0 -2
  97. package/dist/createMutation.test.d.ts.map +0 -1
  98. package/dist/createMutation.test.js +0 -168
  99. package/dist/createQuery.test.d.ts +0 -2
  100. package/dist/createQuery.test.d.ts.map +0 -1
  101. package/dist/createQuery.test.js +0 -156
  102. package/dist/createRef.d.ts +0 -6
  103. package/dist/createRef.d.ts.map +0 -1
  104. package/dist/createRef.js +0 -8
  105. package/dist/createState.test.d.ts.map +0 -1
  106. package/dist/createState.test.js +0 -111
  107. package/dist/createView.test.d.ts.map +0 -1
  108. package/dist/observation.test.d.ts.map +0 -1
  109. package/dist/observation.test.js +0 -150
  110. package/dist/suspense.d.ts +0 -25
  111. package/dist/suspense.d.ts.map +0 -1
  112. package/dist/suspense.js +0 -97
  113. package/dist/test-setup.d.ts +0 -16
  114. package/dist/test-setup.d.ts.map +0 -1
  115. package/dist/test-setup.js +0 -40
  116. package/dist/test.d.ts +0 -2
  117. package/dist/test.d.ts.map +0 -1
  118. package/dist/test.js +0 -24
  119. /package/dist/{createState.test.d.ts → tests/createState.test.d.ts} +0 -0
  120. /package/dist/{createView.test.d.ts → tests/createView.test.d.ts} +0 -0
  121. /package/dist/{observation.test.d.ts → tests/observation.test.d.ts} +0 -0
@@ -0,0 +1,25 @@
1
+ import { useEffect } from "./useEffect";
2
+ import { useTask } from "./useTask";
3
+ export function useQuery(query, initialValue) {
4
+ const [taskState, runTask] = useTask(query);
5
+ let lastValue = initialValue === undefined ? null : initialValue;
6
+ useEffect(() => {
7
+ if (taskState.result) {
8
+ lastValue = taskState.result;
9
+ }
10
+ });
11
+ return [
12
+ {
13
+ get value() {
14
+ return taskState.result || lastValue;
15
+ },
16
+ get isPending() {
17
+ return taskState.isPending;
18
+ },
19
+ get error() {
20
+ return taskState.error;
21
+ },
22
+ },
23
+ runTask,
24
+ ];
25
+ }
@@ -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"}
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rask-ui",
3
- "version": "0.22.0",
3
+ "version": "0.24.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -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
@@ -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"}
@@ -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
@@ -1,5 +0,0 @@
1
- export declare function createContext<T extends object>(): {
2
- set(value: T): void;
3
- get(): T;
4
- };
5
- //# sourceMappingURL=context.d.ts.map
@@ -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,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=createAsync.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createAsync.test.d.ts","sourceRoot":"","sources":["../src/createAsync.test.ts"],"names":[],"mappings":""}
@@ -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,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=createMutation.test.d.ts.map
@@ -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,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=createQuery.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createQuery.test.d.ts","sourceRoot":"","sources":["../src/createQuery.test.ts"],"names":[],"mappings":""}