bunja 0.0.10 → 0.0.12
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 +2 -1
- package/bunja.ts +6 -2
- package/package.json +1 -1
- package/react.ts +20 -2
package/README.md
CHANGED
|
@@ -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
|
|
|
@@ -59,6 +62,7 @@ export class BunjaStore {
|
|
|
59
62
|
scopeInstanceMap.forEach((scope) => scope.sub());
|
|
60
63
|
};
|
|
61
64
|
},
|
|
65
|
+
deps: Array.from(scopeInstanceMap.values()).map(({ value }) => value),
|
|
62
66
|
};
|
|
63
67
|
}
|
|
64
68
|
#getBunjaInstance(
|
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, mount } = store.get(bunja, readScope);
|
|
31
|
-
useEffect(mount,
|
|
30
|
+
const { value, mount, deps } = store.get(bunja, readScope);
|
|
31
|
+
useEffect(mount, deps);
|
|
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
|
+
}
|