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 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` field.
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
- return {
45
- countAtom,
46
- [bunja.effect]() {
47
- console.log("mounted");
48
- return () => console.log("unmounted");
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
- return {
81
- send,
82
- on,
83
- [bunja.effect]() {
84
- socket = new WebSocket("...");
85
- socket.onmessage = (e) => emitter.emit("message", JSON.parse(e.data));
86
- return () => socket.close();
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([websocketBunja], ({ send, on }) => {
96
+ const resourceFooBunja = bunja(() => {
97
+ const { send, on } = bunja.use(websocketBunja);
92
98
  const resourceFooAtom = atom();
93
- return {
94
- resourceFooAtom,
95
- [bunja.effect]() {
96
- const off = on((message) => {
97
- if (message.type === "foo") store.set(resourceAtom, message.value);
98
- });
99
- send("subscribe-foo");
100
- return () => {
101
- send("unsubscribe-foo");
102
- off();
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([websocketBunja], ({ send, on }) => {
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([UrlScope], (url) => {
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([UrlScope], (url) => {
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.Provider value="https://example.com/foo">
195
+ <UrlContext value="https://example.com/foo">
183
196
  <ChildComponent />
184
- </UrlContext.Provider>
185
- <UrlContext.Provider value="https://example.com/bar">
197
+ </UrlContext>
198
+ <UrlContext value="https://example.com/bar">
186
199
  <ChildComponent />
187
- </UrlContext.Provider>
200
+ </UrlContext>
188
201
  </>
189
202
  );
190
203
  }