bunja 0.0.0 → 0.0.1
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 +53 -2
- package/bunja.ts +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,59 @@
|
|
|
1
1
|
# Bunja
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Bunja is State Lifetime Manager for React. (Minified & gzipped size < 1kB)\
|
|
4
4
|
Heavily inspired by [Bunshi](https://github.com/saasquatch/bunshi).
|
|
5
5
|
|
|
6
6
|
> Definition: Bunja (分子 / 분자) - Korean for molecule, member or element.
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Why is managing the lifetime of state necessary?
|
|
9
|
+
|
|
10
|
+
Global state managers like jotai or signals offer the advantage of declaratively describing state and effectively reducing render counts,
|
|
11
|
+
but they lack suitable methods for managing resources with a defined start and end.\
|
|
12
|
+
For example, consider establishing and closing a WebSocket connection or a modal form UI that appears temporarily and then disappears.
|
|
13
|
+
|
|
14
|
+
Bunja is a library designed to address these weaknesses.\
|
|
15
|
+
Each state defined with Bunja has a lifetime that begins when it is first depended on somewhere in the render tree and ends when all dependencies disappear.
|
|
16
|
+
|
|
17
|
+
Therefore, when writing a state to manage a WebSocket,
|
|
18
|
+
you only need to create a mount handler that establishes the WebSocket connection and an unmount handler that terminates the connection.\
|
|
19
|
+
The library automatically tracks the actual usage period and calls the mount and unmount handlers as needed.
|
|
20
|
+
|
|
21
|
+
## So, do I no longer need jotai or other state management libraries?
|
|
22
|
+
|
|
23
|
+
No. Bunja focuses solely on managing the lifetime of state, so jotai and other state management libraries are still valuable.\
|
|
24
|
+
You can typically use jotai or something, and when lifetime management becomes necessary, you can wrap those states with bunja.
|
|
25
|
+
|
|
26
|
+
## How to use
|
|
27
|
+
|
|
28
|
+
Bunja provides two functions: `bunja` and `useBunja`.\
|
|
29
|
+
You can use `bunja` to define a state with a finite lifetime and use the `useBunja` hook to access that state.
|
|
30
|
+
|
|
31
|
+
### Defining a Bunja
|
|
32
|
+
|
|
33
|
+
You can define a bunja using the `bunja` function. When you access the defined bunja with the `useBunja` hook, a bunja instance is created.\
|
|
34
|
+
If all components in the render tree that refer to the bunja disappear, the bunja instance is automatically destroyed.
|
|
35
|
+
|
|
36
|
+
You can also register functions to be called when the dependency on the bunja starts and ends by using the `mount` and `unmount` fields in the init function's return value.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const countBunja = bunja([], () => {
|
|
40
|
+
const countAtom = atom(0);
|
|
41
|
+
return {
|
|
42
|
+
value: countAtom,
|
|
43
|
+
mount: () => console.log("mounted"),
|
|
44
|
+
unmount: () => console.log("unmounted"),
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
function MyComponent() {
|
|
49
|
+
const countAtom = useBunja(countBunja);
|
|
50
|
+
const [count, setCount] = useAtom(countAtom);
|
|
51
|
+
// Your component logic here
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This code snippet defines a bunja that creates a `countAtom`.\
|
|
56
|
+
The `mount` function is logged when the bunja instance is first accessed,
|
|
57
|
+
and the `unmount` function is logged when it is no longer referenced by any component in the render tree.
|
|
58
|
+
|
|
59
|
+
TODO: context
|
package/bunja.ts
CHANGED
|
@@ -72,7 +72,7 @@ export function useBunja<T>(bunja: Bunja<T>): T {
|
|
|
72
72
|
React.useEffect(() => {
|
|
73
73
|
scopes.forEach((scope) => scope.reg(rid));
|
|
74
74
|
return () => scopes.forEach((scope) => scope.dereg(rid));
|
|
75
|
-
}, [rid]);
|
|
75
|
+
}, [rid, ...scopes]);
|
|
76
76
|
return bunjaInstance.value;
|
|
77
77
|
}
|
|
78
78
|
|
package/package.json
CHANGED