bunja 0.0.5 → 0.0.7

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 +27 -16
  2. package/package.json +1 -1
package/bunja.ts CHANGED
@@ -47,6 +47,18 @@ export function bunja<T, U, V, W>(
47
47
  deps: [Dep<U>, Dep<V>, Dep<W>],
48
48
  init: (u: U, v: V, w: W) => T & BunjaValue
49
49
  ): Bunja<T>;
50
+ export function bunja<T, U, V, W, X>(
51
+ deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>],
52
+ init: (u: U, v: V, w: W, x: X) => T & BunjaValue
53
+ ): Bunja<T>;
54
+ export function bunja<T, U, V, W, X, Y>(
55
+ deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>, Dep<Y>],
56
+ init: (u: U, v: V, w: W, x: X, y: Y) => T & BunjaValue
57
+ ): Bunja<T>;
58
+ export function bunja<T, U, V, W, X, Y, Z>(
59
+ deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>, Dep<Y>, Dep<Z>],
60
+ init: (u: U, v: V, w: W, x: X, y: Y, z: Z) => T & BunjaValue
61
+ ): Bunja<T>;
50
62
  export function bunja<T, const U extends any[]>(
51
63
  deps: { [K in keyof U]: Dep<U[K]> },
52
64
  init: (...args: U) => T & BunjaValue
@@ -58,14 +70,13 @@ export function bunja<T, const U extends any[]>(
58
70
  const dedupedContexts = Array.from(
59
71
  new Set([...contexts, ...bunjas.flatMap((def) => def.contexts)])
60
72
  );
61
- return new Bunja(bunja.counter++, deps, dedupedContexts, init);
73
+ return new Bunja(bunja.counter++, deps, dedupedContexts, init as any);
62
74
  }
63
75
  bunja.counter = 0;
64
76
 
65
77
  export function useBunja<T>(bunja: Bunja<T>): T {
66
78
  const { id, deps, contexts } = bunja;
67
79
  const store = React.useContext(BunjaStoreContext);
68
- const rid = useRid();
69
80
  const tuples = contexts.map((c) => [c, React.useContext(c)] as const);
70
81
  const scopes = tuples.map(([context, value]) => getScope(context, value));
71
82
  const scopeMap = new Map(tuples);
@@ -79,13 +90,13 @@ export function useBunja<T>(bunja: Bunja<T>): T {
79
90
  .join(",")}`;
80
91
  const instance = store.get(bunja, biid, args);
81
92
  React.useEffect(() => {
82
- instance.reg(rid);
83
- return () => instance.dereg(rid);
93
+ instance.add();
94
+ return () => instance.sub();
84
95
  }, [instance]);
85
96
  React.useEffect(() => {
86
- scopes.forEach((scope) => scope.reg(rid));
87
- return () => scopes.forEach((scope) => scope.dereg(rid));
88
- }, [rid, ...scopes]);
97
+ scopes.forEach((scope) => scope.add());
98
+ return () => scopes.forEach((scope) => scope.sub());
99
+ }, scopes);
89
100
  return instance.value as T;
90
101
  }
91
102
 
@@ -101,17 +112,17 @@ function getScope(context: React.Context<any>, value: any) {
101
112
  }
102
113
  getScope.counter = 0;
103
114
 
104
- abstract class RefCounter<T = number> {
115
+ abstract class RefCounter {
105
116
  #disposed = false;
106
- refs = new Set<T>();
107
- reg(reference: T) {
108
- this.refs.add(reference);
117
+ #count = 0;
118
+ add() {
119
+ this.#count++;
109
120
  }
110
- dereg(reference: T) {
111
- this.refs.delete(reference);
121
+ sub() {
122
+ this.#count--;
112
123
  setTimeout(() => {
113
124
  if (this.#disposed) return;
114
- if (this.refs.size < 1) {
125
+ if (this.#count < 1) {
115
126
  this.#disposed = true;
116
127
  this.dispose();
117
128
  }
@@ -130,9 +141,9 @@ class BunjaInstance extends RefCounter {
130
141
  ) {
131
142
  super();
132
143
  }
133
- reg(reference: number) {
144
+ add() {
134
145
  this.#cleanup ??= this.value[Bunja.effect]?.() ?? noop;
135
- super.reg(reference);
146
+ super.add();
136
147
  }
137
148
  dispose = () => {
138
149
  this.#cleanup?.();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunja",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "State Lifetime Manager for React",
5
5
  "main": "bunja.ts",
6
6
  "scripts": {},