bunja 0.0.4 → 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.
Files changed (2) hide show
  1. package/bunja.ts +15 -16
  2. package/package.json +7 -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.reg(rid);
83
- return () => instance.dereg(rid);
81
+ instance.add();
82
+ return () => instance.sub();
84
83
  }, [instance]);
85
84
  React.useEffect(() => {
86
- scopes.forEach((scope) => scope.reg(rid));
87
- return () => scopes.forEach((scope) => scope.dereg(rid));
88
- }, [rid, ...scopes]);
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<T = number> {
103
+ abstract class RefCounter {
105
104
  #disposed = false;
106
- refs = new Set<T>();
107
- reg(reference: T) {
108
- this.refs.add(reference);
105
+ #count = 0;
106
+ add() {
107
+ this.#count++;
109
108
  }
110
- dereg(reference: T) {
111
- this.refs.delete(reference);
109
+ sub() {
110
+ this.#count--;
112
111
  setTimeout(() => {
113
112
  if (this.#disposed) return;
114
- if (this.refs.size < 1) {
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
- reg(reference: number) {
132
+ add() {
134
133
  this.#cleanup ??= this.value[Bunja.effect]?.() ?? noop;
135
- super.reg(reference);
134
+ super.add();
136
135
  }
137
136
  dispose = () => {
138
137
  this.#cleanup?.();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunja",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "State Lifetime Manager for React",
5
5
  "main": "bunja.ts",
6
6
  "scripts": {},
@@ -16,6 +16,12 @@
16
16
  "react": "^18"
17
17
  },
18
18
  "peerDependencies": {
19
+ "@types/react": "*",
19
20
  "react": ">=17"
21
+ },
22
+ "peerDependenciesMeta": {
23
+ "@types/react": {
24
+ "optional": true
25
+ }
20
26
  }
21
27
  }