bunja 0.0.9 → 0.0.11
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 +3 -2
- package/bunja.ts +7 -4
- package/package.json +1 -1
- package/react.ts +20 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Bunja
|
|
2
2
|
|
|
3
|
-
Bunja is State Lifetime Manager
|
|
3
|
+
Bunja is lightweight State Lifetime Manager.\
|
|
4
4
|
Heavily inspired by [Bunshi](https://github.com/saasquatch/bunshi).
|
|
5
5
|
|
|
6
6
|
> Definition: Bunja (分子 / 분자) - Korean for molecule, member or element.
|
|
@@ -36,7 +36,8 @@ If all components in the render tree that refer to the bunja disappear, the bunj
|
|
|
36
36
|
If you want to trigger effects when the lifetime of a bunja starts and ends, you can use the `bunja.effect` field.
|
|
37
37
|
|
|
38
38
|
```ts
|
|
39
|
-
import { bunja
|
|
39
|
+
import { bunja } from "bunja";
|
|
40
|
+
import { useBunja } from "bunja/react";
|
|
40
41
|
|
|
41
42
|
const countBunja = bunja([], () => {
|
|
42
43
|
const countAtom = atom(0);
|
package/bunja.ts
CHANGED
|
@@ -16,19 +16,22 @@ export class Bunja<T> {
|
|
|
16
16
|
}
|
|
17
17
|
static readonly effect = Symbol("Bunja.effect");
|
|
18
18
|
toString() {
|
|
19
|
-
|
|
19
|
+
const { id, debugLabel } = this;
|
|
20
|
+
return `[Bunja:${id}${debugLabel && ` - ${debugLabel}`}]`;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export class Scope<T> {
|
|
24
25
|
public static readonly scopes: Scope<any>[] = [];
|
|
25
26
|
public readonly id: number;
|
|
27
|
+
public debugLabel: string = "";
|
|
26
28
|
constructor() {
|
|
27
29
|
this.id = Scope.scopes.length;
|
|
28
30
|
Scope.scopes.push(this);
|
|
29
31
|
}
|
|
30
32
|
toString() {
|
|
31
|
-
|
|
33
|
+
const { id, debugLabel } = this;
|
|
34
|
+
return `[Scope:${id}${debugLabel && ` - ${debugLabel}`}]`;
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
|
|
@@ -48,11 +51,11 @@ export class BunjaStore {
|
|
|
48
51
|
const { relatedBunjaInstanceMap } = bunjaInstance; // toposorted
|
|
49
52
|
return {
|
|
50
53
|
value: bunjaInstance.value as T,
|
|
51
|
-
|
|
54
|
+
mount() {
|
|
52
55
|
relatedBunjaInstanceMap.forEach((related) => related.add());
|
|
53
56
|
bunjaInstance.add();
|
|
54
57
|
scopeInstanceMap.forEach((scope) => scope.add());
|
|
55
|
-
return ()
|
|
58
|
+
return function unmount() {
|
|
56
59
|
// concern: reverse order?
|
|
57
60
|
relatedBunjaInstanceMap.forEach((related) => related.sub());
|
|
58
61
|
bunjaInstance.sub();
|
package/package.json
CHANGED
package/react.ts
CHANGED
|
@@ -27,7 +27,25 @@ const defaultReadScope: ReadScope = (scope) => {
|
|
|
27
27
|
|
|
28
28
|
export function useBunja<T>(bunja: Bunja<T>, readScope = defaultReadScope): T {
|
|
29
29
|
const store = useContext(BunjaStoreContext);
|
|
30
|
-
const { value,
|
|
31
|
-
useEffect(
|
|
30
|
+
const { value, mount } = store.get(bunja, readScope);
|
|
31
|
+
useEffect(mount, []);
|
|
32
32
|
return value;
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
export type ScopePair<T> = [Scope<T>, T];
|
|
36
|
+
|
|
37
|
+
export function inject<const T extends ScopePair<any>[]>(
|
|
38
|
+
overrideTable: T
|
|
39
|
+
): ReadScope {
|
|
40
|
+
const map = new Map(overrideTable);
|
|
41
|
+
return (scope) => {
|
|
42
|
+
if (map.has(scope)) return map.get(scope);
|
|
43
|
+
const context = scopeContextMap.get(scope);
|
|
44
|
+
if (!context) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"Unable to read the scope. Please inject the value explicitly or bind scope to the React context."
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
return useContext(context);
|
|
50
|
+
};
|
|
51
|
+
}
|