bunja 0.0.5 → 0.0.6
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/bunja.ts +15 -16
- package/package.json +1 -1
package/bunja.ts
CHANGED
|
@@ -58,14 +58,13 @@ export function bunja<T, const U extends any[]>(
|
|
|
58
58
|
const dedupedContexts = Array.from(
|
|
59
59
|
new Set([...contexts, ...bunjas.flatMap((def) => def.contexts)])
|
|
60
60
|
);
|
|
61
|
-
return new Bunja(bunja.counter++, deps, dedupedContexts, init);
|
|
61
|
+
return new Bunja(bunja.counter++, deps, dedupedContexts, init as any);
|
|
62
62
|
}
|
|
63
63
|
bunja.counter = 0;
|
|
64
64
|
|
|
65
65
|
export function useBunja<T>(bunja: Bunja<T>): T {
|
|
66
66
|
const { id, deps, contexts } = bunja;
|
|
67
67
|
const store = React.useContext(BunjaStoreContext);
|
|
68
|
-
const rid = useRid();
|
|
69
68
|
const tuples = contexts.map((c) => [c, React.useContext(c)] as const);
|
|
70
69
|
const scopes = tuples.map(([context, value]) => getScope(context, value));
|
|
71
70
|
const scopeMap = new Map(tuples);
|
|
@@ -79,13 +78,13 @@ export function useBunja<T>(bunja: Bunja<T>): T {
|
|
|
79
78
|
.join(",")}`;
|
|
80
79
|
const instance = store.get(bunja, biid, args);
|
|
81
80
|
React.useEffect(() => {
|
|
82
|
-
instance.
|
|
83
|
-
return () => instance.
|
|
81
|
+
instance.add();
|
|
82
|
+
return () => instance.sub();
|
|
84
83
|
}, [instance]);
|
|
85
84
|
React.useEffect(() => {
|
|
86
|
-
scopes.forEach((scope) => scope.
|
|
87
|
-
return () => scopes.forEach((scope) => scope.
|
|
88
|
-
},
|
|
85
|
+
scopes.forEach((scope) => scope.add());
|
|
86
|
+
return () => scopes.forEach((scope) => scope.sub());
|
|
87
|
+
}, scopes);
|
|
89
88
|
return instance.value as T;
|
|
90
89
|
}
|
|
91
90
|
|
|
@@ -101,17 +100,17 @@ function getScope(context: React.Context<any>, value: any) {
|
|
|
101
100
|
}
|
|
102
101
|
getScope.counter = 0;
|
|
103
102
|
|
|
104
|
-
abstract class RefCounter
|
|
103
|
+
abstract class RefCounter {
|
|
105
104
|
#disposed = false;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this
|
|
105
|
+
#count = 0;
|
|
106
|
+
add() {
|
|
107
|
+
this.#count++;
|
|
109
108
|
}
|
|
110
|
-
|
|
111
|
-
this
|
|
109
|
+
sub() {
|
|
110
|
+
this.#count--;
|
|
112
111
|
setTimeout(() => {
|
|
113
112
|
if (this.#disposed) return;
|
|
114
|
-
if (this
|
|
113
|
+
if (this.#count < 1) {
|
|
115
114
|
this.#disposed = true;
|
|
116
115
|
this.dispose();
|
|
117
116
|
}
|
|
@@ -130,9 +129,9 @@ class BunjaInstance extends RefCounter {
|
|
|
130
129
|
) {
|
|
131
130
|
super();
|
|
132
131
|
}
|
|
133
|
-
|
|
132
|
+
add() {
|
|
134
133
|
this.#cleanup ??= this.value[Bunja.effect]?.() ?? noop;
|
|
135
|
-
super.
|
|
134
|
+
super.add();
|
|
136
135
|
}
|
|
137
136
|
dispose = () => {
|
|
138
137
|
this.#cleanup?.();
|