bunja 1.0.0 → 2.0.0-alpha.2
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 -40
- package/bunja.ts +300 -182
- package/codemod/bunja-v1-to-v2.js +118 -0
- package/compat/bunja-v1.ts +48 -0
- package/deno.json +1 -1
- package/deno.lock +174 -83
- package/dist/bunja-bUA1rGXy.cjs +305 -0
- package/dist/bunja-wcx846sL.js +274 -0
- package/dist/bunja.cjs +1 -1
- package/dist/bunja.d.cts +51 -42
- package/dist/bunja.d.ts +51 -42
- package/dist/bunja.js +1 -1
- package/dist/react.cjs +14 -5
- package/dist/react.d.cts +5 -4
- package/dist/react.d.ts +5 -4
- package/dist/react.js +14 -6
- package/package.json +4 -4
- package/react.ts +32 -14
- package/test.ts +53 -32
- package/dist/bunja-Q0ZusYIM.cjs +0 -189
- package/dist/bunja-fHIhQAuL.js +0 -158
package/README.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
> [!WARNING]
|
|
2
|
+
> You are viewing the `v2` branch.
|
|
3
|
+
>
|
|
4
|
+
> The current stable version is `1.x.x`.\
|
|
5
|
+
> If you want to view the code for that version,
|
|
6
|
+
> please [switch to the `v1` branch](https://github.com/disjukr/bunja/tree/v1).
|
|
7
|
+
|
|
1
8
|
# Bunja
|
|
2
9
|
|
|
3
10
|
Bunja is lightweight State Lifetime Manager.\
|
|
@@ -33,21 +40,21 @@ You can use `bunja` to define a state with a finite lifetime and use the `useBun
|
|
|
33
40
|
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
41
|
If all components in the render tree that refer to the bunja disappear, the bunja instance is automatically destroyed.
|
|
35
42
|
|
|
36
|
-
If you want to trigger effects when the lifetime of a bunja starts and ends, you can use the `bunja.effect`
|
|
43
|
+
If you want to trigger effects when the lifetime of a bunja starts and ends, you can use the `bunja.effect` function.
|
|
37
44
|
|
|
38
45
|
```ts
|
|
39
46
|
import { bunja } from "bunja";
|
|
40
47
|
import { useBunja } from "bunja/react";
|
|
41
48
|
|
|
42
|
-
const countBunja = bunja(
|
|
49
|
+
const countBunja = bunja(() => {
|
|
43
50
|
const countAtom = atom(0);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
};
|
|
51
|
+
|
|
52
|
+
bunja.effect(() => {
|
|
53
|
+
console.log("mounted");
|
|
54
|
+
return () => console.log("unmounted");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return { countAtom };
|
|
51
58
|
});
|
|
52
59
|
|
|
53
60
|
function MyComponent() {
|
|
@@ -67,7 +74,7 @@ In such a case, you can write the following code.
|
|
|
67
74
|
|
|
68
75
|
```ts
|
|
69
76
|
// To simplify the example, code for buffering and reconnection has been omitted.
|
|
70
|
-
const websocketBunja = bunja(
|
|
77
|
+
const websocketBunja = bunja(() => {
|
|
71
78
|
let socket;
|
|
72
79
|
const send = (message) => socket.send(JSON.stringify(message));
|
|
73
80
|
|
|
@@ -77,35 +84,35 @@ const websocketBunja = bunja([], () => {
|
|
|
77
84
|
return () => emitter.off("message", handler);
|
|
78
85
|
};
|
|
79
86
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
},
|
|
88
|
-
};
|
|
87
|
+
bunja.effect(() => {
|
|
88
|
+
socket = new WebSocket("...");
|
|
89
|
+
socket.onmessage = (e) => emitter.emit("message", JSON.parse(e.data));
|
|
90
|
+
return () => socket.close();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return { send, on };
|
|
89
94
|
});
|
|
90
95
|
|
|
91
|
-
const resourceFooBunja = bunja(
|
|
96
|
+
const resourceFooBunja = bunja(() => {
|
|
97
|
+
const { send, on } = bunja.use(websocketBunja);
|
|
92
98
|
const resourceFooAtom = atom();
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
};
|
|
99
|
+
|
|
100
|
+
bunja.effect(() => {
|
|
101
|
+
const off = on((message) => {
|
|
102
|
+
if (message.type === "foo") store.set(resourceAtom, message.value);
|
|
103
|
+
});
|
|
104
|
+
send("subscribe-foo");
|
|
105
|
+
return () => {
|
|
106
|
+
send("unsubscribe-foo");
|
|
107
|
+
off();
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return { resourceFooAtom };
|
|
106
112
|
});
|
|
107
113
|
|
|
108
|
-
const resourceBarBunja = bunja(
|
|
114
|
+
const resourceBarBunja = bunja(() => {
|
|
115
|
+
const { send, on } = bunja.use(websocketBunja);
|
|
109
116
|
const resourceBarAtom = atom();
|
|
110
117
|
// ...
|
|
111
118
|
});
|
|
@@ -144,11 +151,14 @@ import { bunja, createScope } from "bunja";
|
|
|
144
151
|
|
|
145
152
|
const UrlScope = createScope();
|
|
146
153
|
|
|
147
|
-
const fetchBunja = bunja(
|
|
154
|
+
const fetchBunja = bunja(() => {
|
|
155
|
+
const url = bunja.use(UrlScope);
|
|
156
|
+
|
|
148
157
|
const queryAtom = atomWithQuery((get) => ({
|
|
149
158
|
queryKey: [url],
|
|
150
159
|
queryFn: async () => (await fetch(url)).json(),
|
|
151
160
|
}));
|
|
161
|
+
|
|
152
162
|
return { queryAtom };
|
|
153
163
|
});
|
|
154
164
|
```
|
|
@@ -168,23 +178,26 @@ const UrlContext = createContext("https://example.com/");
|
|
|
168
178
|
const UrlScope = createScope();
|
|
169
179
|
bindScope(UrlScope, UrlContext);
|
|
170
180
|
|
|
171
|
-
const fetchBunja = bunja(
|
|
181
|
+
const fetchBunja = bunja(() => {
|
|
182
|
+
const url = bunja.use(UrlScope);
|
|
183
|
+
|
|
172
184
|
const queryAtom = atomWithQuery((get) => ({
|
|
173
185
|
queryKey: [url],
|
|
174
186
|
queryFn: async () => (await fetch(url)).json(),
|
|
175
187
|
}));
|
|
188
|
+
|
|
176
189
|
return { queryAtom };
|
|
177
190
|
});
|
|
178
191
|
|
|
179
192
|
function ParentComponent() {
|
|
180
193
|
return (
|
|
181
194
|
<>
|
|
182
|
-
<UrlContext
|
|
195
|
+
<UrlContext value="https://example.com/foo">
|
|
183
196
|
<ChildComponent />
|
|
184
|
-
</UrlContext
|
|
185
|
-
<UrlContext
|
|
197
|
+
</UrlContext>
|
|
198
|
+
<UrlContext value="https://example.com/bar">
|
|
186
199
|
<ChildComponent />
|
|
187
|
-
</UrlContext
|
|
200
|
+
</UrlContext>
|
|
188
201
|
</>
|
|
189
202
|
);
|
|
190
203
|
}
|