rask-ui 0.10.5 → 0.11.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/README.md CHANGED
@@ -484,9 +484,41 @@ function Timer() {
484
484
  }
485
485
  ```
486
486
 
487
+ **Effect with Disposal:**
488
+
489
+ The callback can optionally return a dispose function that runs before the effect executes again:
490
+
491
+ ```tsx
492
+ import { createEffect, createState } from "rask-ui";
493
+
494
+ function LiveData() {
495
+ const state = createState({ url: "/api/data", data: null });
496
+
497
+ createEffect(() => {
498
+ const eventSource = new EventSource(state.url);
499
+
500
+ eventSource.onmessage = (event) => {
501
+ state.data = JSON.parse(event.data);
502
+ };
503
+
504
+ // Dispose function runs before effect re-executes
505
+ return () => {
506
+ eventSource.close();
507
+ };
508
+ });
509
+
510
+ return () => (
511
+ <div>
512
+ <input value={state.url} onInput={(e) => state.url = e.target.value} />
513
+ <pre>{JSON.stringify(state.data, null, 2)}</pre>
514
+ </div>
515
+ );
516
+ }
517
+ ```
518
+
487
519
  **Parameters:**
488
520
 
489
- - `callback: () => void` - Function to run when dependencies change
521
+ - `callback: () => void | (() => void)` - Function to run when dependencies change. Can optionally return a dispose function that runs before the effect executes again.
490
522
 
491
523
  **Features:**
492
524
 
@@ -494,13 +526,16 @@ function Timer() {
494
526
  - Automatically tracks reactive dependencies accessed during execution
495
527
  - Re-runs on microtask when dependencies change (prevents synchronous cascades)
496
528
  - Automatically cleaned up when component unmounts
497
- - Can be used for side effects like logging, syncing to localStorage, or updating derived state
529
+ - Optional dispose function for cleaning up resources before re-execution
530
+ - Can be used for side effects like logging, syncing to localStorage, managing subscriptions, or updating derived state
498
531
 
499
532
  **Notes:**
500
533
 
501
534
  - Only call during component setup phase (not in render function)
502
535
  - Effects are queued on microtask to avoid synchronous execution from prop changes
503
536
  - Be careful with effects that modify state - can cause infinite loops if not careful
537
+ - Dispose functions run before the effect re-executes, not when the component unmounts
538
+ - For component unmount cleanup, use `createCleanup()` instead
504
539
 
505
540
  ---
506
541
 
@@ -1,2 +1,2 @@
1
- export declare function createEffect(cb: () => void): void;
1
+ export declare function createEffect(cb: () => void | (() => void)): void;
2
2
  //# sourceMappingURL=createEffect.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createEffect.d.ts","sourceRoot":"","sources":["../src/createEffect.ts"],"names":[],"mappings":"AAGA,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM,IAAI,QAwB1C"}
1
+ {"version":3,"file":"createEffect.d.ts","sourceRoot":"","sources":["../src/createEffect.ts"],"names":[],"mappings":"AAGA,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,QA6BzD"}
@@ -8,14 +8,21 @@ export function createEffect(cb) {
8
8
  catch {
9
9
  currentComponent = undefined;
10
10
  }
11
+ let disposer;
11
12
  const observer = new Observer(() => {
12
13
  // We trigger effects on micro task as synchronous observer notifications
13
14
  // (Like when components sets props) should not synchronously trigger effects
14
15
  queueMicrotask(runEffect);
15
16
  });
16
17
  const runEffect = () => {
18
+ try {
19
+ disposer?.();
20
+ }
21
+ catch (error) {
22
+ console.error("Error in effect dispose function:", error);
23
+ }
17
24
  const stopObserving = observer.observe();
18
- cb();
25
+ disposer = cb();
19
26
  stopObserving();
20
27
  };
21
28
  if (currentComponent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rask-ui",
3
- "version": "0.10.5",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",