rask-ui 0.25.2 → 0.26.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 (67) hide show
  1. package/dist/component.d.ts +1 -0
  2. package/dist/component.d.ts.map +1 -1
  3. package/dist/component.js +5 -4
  4. package/dist/createContext.d.ts.map +1 -1
  5. package/dist/createContext.js +1 -4
  6. package/dist/useRef.d.ts +2 -0
  7. package/dist/useRef.d.ts.map +1 -1
  8. package/dist/useRef.js +3 -0
  9. package/package.json +1 -1
  10. package/dist/asyncState.d.ts +0 -16
  11. package/dist/asyncState.d.ts.map +0 -1
  12. package/dist/asyncState.js +0 -24
  13. package/dist/compiler.d.ts +0 -2
  14. package/dist/compiler.d.ts.map +0 -1
  15. package/dist/compiler.js +0 -1
  16. package/dist/context.d.ts +0 -5
  17. package/dist/context.d.ts.map +0 -1
  18. package/dist/context.js +0 -29
  19. package/dist/createAsync.d.ts +0 -39
  20. package/dist/createAsync.d.ts.map +0 -1
  21. package/dist/createAsync.js +0 -47
  22. package/dist/createAsync.test.d.ts +0 -2
  23. package/dist/createAsync.test.d.ts.map +0 -1
  24. package/dist/createAsync.test.js +0 -110
  25. package/dist/createMutation.d.ts +0 -43
  26. package/dist/createMutation.d.ts.map +0 -1
  27. package/dist/createMutation.js +0 -76
  28. package/dist/createMutation.test.d.ts +0 -2
  29. package/dist/createMutation.test.d.ts.map +0 -1
  30. package/dist/createMutation.test.js +0 -168
  31. package/dist/createQuery.d.ts +0 -42
  32. package/dist/createQuery.d.ts.map +0 -1
  33. package/dist/createQuery.js +0 -80
  34. package/dist/createQuery.test.d.ts +0 -2
  35. package/dist/createQuery.test.d.ts.map +0 -1
  36. package/dist/createQuery.test.js +0 -156
  37. package/dist/createRef.d.ts +0 -6
  38. package/dist/createRef.d.ts.map +0 -1
  39. package/dist/createRef.js +0 -8
  40. package/dist/createState.d.ts +0 -26
  41. package/dist/createState.d.ts.map +0 -1
  42. package/dist/createState.js +0 -94
  43. package/dist/createState.test.d.ts +0 -2
  44. package/dist/createState.test.d.ts.map +0 -1
  45. package/dist/createState.test.js +0 -111
  46. package/dist/createView.d.ts +0 -54
  47. package/dist/createView.d.ts.map +0 -1
  48. package/dist/createView.js +0 -68
  49. package/dist/createView.test.d.ts +0 -2
  50. package/dist/createView.test.d.ts.map +0 -1
  51. package/dist/createView.test.js +0 -203
  52. package/dist/error.d.ts +0 -16
  53. package/dist/error.d.ts.map +0 -1
  54. package/dist/error.js +0 -17
  55. package/dist/jsx.d.ts +0 -257
  56. package/dist/observation.test.d.ts +0 -2
  57. package/dist/observation.test.d.ts.map +0 -1
  58. package/dist/observation.test.js +0 -150
  59. package/dist/suspense.d.ts +0 -25
  60. package/dist/suspense.d.ts.map +0 -1
  61. package/dist/suspense.js +0 -97
  62. package/dist/test-setup.d.ts +0 -16
  63. package/dist/test-setup.d.ts.map +0 -1
  64. package/dist/test-setup.js +0 -40
  65. package/dist/test.d.ts +0 -2
  66. package/dist/test.d.ts.map +0 -1
  67. package/dist/test.js +0 -24
@@ -18,6 +18,7 @@ export declare class RaskComponent<P extends Props<any>> extends Component<P> {
18
18
  run: () => void;
19
19
  }>;
20
20
  contexts: Map<any, any>;
21
+ getContext(context: unknown): any;
21
22
  getChildContext(): any;
22
23
  onMounts: Array<() => void>;
23
24
  onCleanups: Array<() => void>;
@@ -1 +1 @@
1
- {"version":3,"file":"component.d.ts","sourceRoot":"","sources":["../src/component.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAsB,QAAQ,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAMrE,wBAAgB,mBAAmB,mCAElC;AAED,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,IAAI,QAM5C;AAED,wBAAgB,UAAU,CAAC,EAAE,EAAE,MAAM,IAAI,QAMxC;AAED,MAAM,MAAM,8BAA8B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;IAEnC,iBAAiB,IAAI,IAAI;IAGzB,oBAAoB,IAAI,IAAI;IAI5B,yBAAyB,CACvB,SAAS,EAAE,QAAQ,CAAC;QAAE,QAAQ,CAAC,EAAE,WAAW,CAAA;KAAE,GAAG,CAAC,CAAC,GAClD,IAAI;IAcP,qBAAqB,IAAI,OAAO;IAMhC,MAAM;CA4CP"}
package/dist/component.js CHANGED
@@ -56,16 +56,17 @@ export class RaskComponent extends Component {
56
56
  isRendering = false;
57
57
  effects = [];
58
58
  contexts = new Map();
59
- getChildContext() {
59
+ getContext(context) {
60
60
  const parentGetContext = this.context.getContext ||
61
61
  (() => {
62
62
  throw new Error("No context available");
63
63
  });
64
+ return this.contexts.get(context) || parentGetContext(context);
65
+ }
66
+ getChildContext() {
64
67
  return {
65
68
  ...this.context,
66
- getContext: (context) => {
67
- return this.contexts.get(context) || parentGetContext(context);
68
- },
69
+ getContext: this.getContext.bind(this),
69
70
  };
70
71
  }
71
72
  onMounts = [];
@@ -1 +1 @@
1
- {"version":3,"file":"createContext.d.ts","sourceRoot":"","sources":["../src/createContext.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAIH,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC;IAEvE;;;;;;OAMG;WACI,CAAC;IAqBR;;;;;;;OAOG;sBACe,CAAC,GAAG,CAAC;EAc1B"}
1
+ {"version":3,"file":"createContext.d.ts","sourceRoot":"","sources":["../src/createContext.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAIH,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC;IAEvE;;;;;;OAMG;WACI,CAAC;IAiBR;;;;;;;OAOG;sBACe,CAAC,GAAG,CAAC;EAc1B"}
@@ -42,10 +42,7 @@ export function createContext(hook) {
42
42
  if (!currentComponent) {
43
43
  throw new Error("Only use useContext in component setup");
44
44
  }
45
- if (typeof currentComponent.context.getContext !== "function") {
46
- throw new Error("There is no parent context");
47
- }
48
- const contextValue = currentComponent.context.getContext(hook);
45
+ const contextValue = currentComponent.getContext(hook);
49
46
  if (!contextValue) {
50
47
  throw new Error("There is a parent context, but not the one you are using");
51
48
  }
package/dist/useRef.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  import { RefObject } from "inferno";
2
+ export type Ref<T> = RefObject<T>;
3
+ export declare function assignRef<T>(ref: RefObject<T>, refValue: T): void;
2
4
  export declare function useRef<T>(): RefObject<T>;
3
5
  //# sourceMappingURL=useRef.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useRef.d.ts","sourceRoot":"","sources":["../src/useRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAIpC,wBAAgB,MAAM,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,CAoBxC"}
1
+ {"version":3,"file":"useRef.d.ts","sourceRoot":"","sources":["../src/useRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAIpC,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC;AAElC,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,QAE1D;AAED,wBAAgB,MAAM,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,CAoBxC"}
package/dist/useRef.js CHANGED
@@ -1,5 +1,8 @@
1
1
  import { getCurrentObserver, Signal } from "./observation";
2
2
  import { getCurrentComponent } from "./component";
3
+ export function assignRef(ref, refValue) {
4
+ ref.current = refValue;
5
+ }
3
6
  export function useRef() {
4
7
  let value = null;
5
8
  const currentComponent = getCurrentComponent();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rask-ui",
3
- "version": "0.25.2",
3
+ "version": "0.26.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
- }
@@ -1,2 +0,0 @@
1
- export { createVNode, createComponentVNode, createFragment, createTextVNode, normalizeProps, Component, } from "inferno";
2
- //# sourceMappingURL=compiler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,oBAAoB,EACpB,cAAc,EACd,eAAe,EACf,cAAc,EACd,SAAS,GACV,MAAM,SAAS,CAAC"}
package/dist/compiler.js DELETED
@@ -1 +0,0 @@
1
- export { createVNode, createComponentVNode, createFragment, createTextVNode, normalizeProps, Component, } from "inferno";
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,39 +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
- /**
15
- * Creates a reactive async state that tracks the lifecycle of a promise.
16
- *
17
- * @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity.
18
- * Access properties directly in your render function instead.
19
- *
20
- * @example
21
- * // ❌ Bad - destructuring loses reactivity
22
- * function Component() {
23
- * const async = createAsync(fetchData());
24
- * const { isPending, value, error } = async; // Don't do this!
25
- * return () => <div>{isPending ? "Loading..." : value}</div>; // Won't update!
26
- * }
27
- *
28
- * // ✅ Good - access properties directly
29
- * function Component() {
30
- * const async = createAsync(fetchData());
31
- * return () => <div>{async.isPending ? "Loading..." : async.value}</div>;
32
- * }
33
- *
34
- * @param promise - The promise to track
35
- * @returns Reactive state with isPending, value, and error properties
36
- */
37
- export declare function createAsync<T>(promise: Promise<T>): AsyncState<T>;
38
- export {};
39
- //# sourceMappingURL=createAsync.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createAsync.d.ts","sourceRoot":"","sources":["../src/createAsync.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;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,iBAwBjD"}
@@ -1,47 +0,0 @@
1
- import { createState } from "./createState";
2
- /**
3
- * Creates a reactive async state that tracks the lifecycle of a promise.
4
- *
5
- * @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity.
6
- * Access properties directly in your render function instead.
7
- *
8
- * @example
9
- * // ❌ Bad - destructuring loses reactivity
10
- * function Component() {
11
- * const async = createAsync(fetchData());
12
- * const { isPending, value, error } = async; // Don't do this!
13
- * return () => <div>{isPending ? "Loading..." : value}</div>; // Won't update!
14
- * }
15
- *
16
- * // ✅ Good - access properties directly
17
- * function Component() {
18
- * const async = createAsync(fetchData());
19
- * return () => <div>{async.isPending ? "Loading..." : async.value}</div>;
20
- * }
21
- *
22
- * @param promise - The promise to track
23
- * @returns Reactive state with isPending, value, and error properties
24
- */
25
- export function createAsync(promise) {
26
- const state = createState({
27
- isPending: true,
28
- error: null,
29
- value: null,
30
- });
31
- promise
32
- .then((value) => {
33
- Object.assign(state, {
34
- value,
35
- error: null,
36
- isPending: false,
37
- });
38
- })
39
- .catch((error) => {
40
- Object.assign(state, {
41
- value: null,
42
- error: String(error),
43
- isPending: false,
44
- });
45
- });
46
- return state;
47
- }
@@ -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,43 +0,0 @@
1
- type MutationState<T> = {
2
- isPending: true;
3
- params: T;
4
- error: null;
5
- } | {
6
- isPending: false;
7
- params: null;
8
- error: null;
9
- } | {
10
- isPending: false;
11
- params: null;
12
- error: string;
13
- };
14
- export type Mutation<T> = MutationState<T> & {
15
- mutate(): void;
16
- mutate(params: T): void;
17
- };
18
- /**
19
- * Creates a reactive mutation that manages async mutations with loading and error states.
20
- *
21
- * @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity.
22
- * Access properties directly in your render function instead.
23
- *
24
- * @example
25
- * // ❌ Bad - destructuring loses reactivity
26
- * function Component() {
27
- * const mutation = createMutation((params) => updateUser(params));
28
- * const { isPending, error } = mutation; // Don't do this!
29
- * return () => <button disabled={isPending}>Save</button>; // Won't update!
30
- * }
31
- *
32
- * // ✅ Good - access properties directly
33
- * function Component() {
34
- * const mutation = createMutation((params) => updateUser(params));
35
- * return () => <button disabled={mutation.isPending}>Save</button>;
36
- * }
37
- *
38
- * @param mutator - Function that performs the mutation and returns a promise
39
- * @returns Reactive mutation object with isPending, params, error properties and mutate method
40
- */
41
- export declare function createMutation<T>(mutator: (params: T) => Promise<T>): Mutation<T>;
42
- export {};
43
- //# sourceMappingURL=createMutation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createMutation.d.ts","sourceRoot":"","sources":["../src/createMutation.ts"],"names":[],"mappings":"AAEA,KAAK,aAAa,CAAC,CAAC,IAChB;IACE,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,CAAC,CAAC;IACV,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,MAAM,EAAE,IAAI,CAAC;IACb,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,MAAM,EAAE,IAAI,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEN,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG;IAC3C,MAAM,IAAI,IAAI,CAAC;IACf,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC9B,OAAO,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GACjC,QAAQ,CAAC,CAAC,CAAC,CA0Db"}
@@ -1,76 +0,0 @@
1
- import { createState } from "./createState";
2
- /**
3
- * Creates a reactive mutation that manages async mutations with loading and error states.
4
- *
5
- * @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity.
6
- * Access properties directly in your render function instead.
7
- *
8
- * @example
9
- * // ❌ Bad - destructuring loses reactivity
10
- * function Component() {
11
- * const mutation = createMutation((params) => updateUser(params));
12
- * const { isPending, error } = mutation; // Don't do this!
13
- * return () => <button disabled={isPending}>Save</button>; // Won't update!
14
- * }
15
- *
16
- * // ✅ Good - access properties directly
17
- * function Component() {
18
- * const mutation = createMutation((params) => updateUser(params));
19
- * return () => <button disabled={mutation.isPending}>Save</button>;
20
- * }
21
- *
22
- * @param mutator - Function that performs the mutation and returns a promise
23
- * @returns Reactive mutation object with isPending, params, error properties and mutate method
24
- */
25
- export function createMutation(mutator) {
26
- const state = createState({
27
- isPending: false,
28
- params: null,
29
- error: null,
30
- });
31
- const assign = (newState) => {
32
- Object.assign(state, newState);
33
- };
34
- let currentAbortController;
35
- return {
36
- get isPending() {
37
- return state.isPending;
38
- },
39
- get params() {
40
- return state.params;
41
- },
42
- get error() {
43
- return state.error;
44
- },
45
- mutate(params) {
46
- currentAbortController?.abort();
47
- const abortController = (currentAbortController = new AbortController());
48
- assign({
49
- isPending: true,
50
- params,
51
- error: null,
52
- });
53
- mutator(params)
54
- .then(() => {
55
- if (abortController.signal.aborted) {
56
- return;
57
- }
58
- assign({
59
- isPending: false,
60
- params: null,
61
- error: null,
62
- });
63
- })
64
- .catch((error) => {
65
- if (abortController.signal.aborted) {
66
- return;
67
- }
68
- assign({
69
- isPending: false,
70
- params: null,
71
- error: String(error),
72
- });
73
- });
74
- },
75
- };
76
- }
@@ -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":""}