bunja 0.0.11 → 0.1.0
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/.vscode/settings.json +2 -1
- package/README.md +167 -1
- package/bunja.ts +60 -48
- package/deno.json +9 -0
- package/deno.lock +487 -0
- package/dist/bunja-3B0nQ1vI.js +155 -0
- package/dist/bunja-QjknYXY-.cjs +186 -0
- package/dist/bunja.cjs +9 -0
- package/dist/bunja.d.cts +53 -0
- package/dist/bunja.d.ts +53 -0
- package/dist/bunja.js +3 -0
- package/dist/react.cjs +65 -0
- package/dist/react.d.cts +9 -0
- package/dist/react.d.ts +9 -0
- package/dist/react.js +36 -0
- package/package.json +44 -4
- package/react.ts +18 -13
- package/tsconfig.json +7 -1
- package/tsdown.config.ts +10 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
//#region bunja.ts
|
|
4
|
+
const bunjaEffectSymbol = Symbol("Bunja.effect");
|
|
5
|
+
var Bunja = class Bunja {
|
|
6
|
+
static bunjas = [];
|
|
7
|
+
id;
|
|
8
|
+
debugLabel = "";
|
|
9
|
+
constructor(deps, parents, relatedBunjas, relatedScopes, init) {
|
|
10
|
+
this.deps = deps;
|
|
11
|
+
this.parents = parents;
|
|
12
|
+
this.relatedBunjas = relatedBunjas;
|
|
13
|
+
this.relatedScopes = relatedScopes;
|
|
14
|
+
this.init = init;
|
|
15
|
+
this.id = Bunja.bunjas.length;
|
|
16
|
+
Bunja.bunjas.push(this);
|
|
17
|
+
}
|
|
18
|
+
static effect = bunjaEffectSymbol;
|
|
19
|
+
toString() {
|
|
20
|
+
const { id, debugLabel } = this;
|
|
21
|
+
return `[Bunja:${id}${debugLabel && ` - ${debugLabel}`}]`;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var Scope = class Scope {
|
|
25
|
+
static scopes = [];
|
|
26
|
+
id;
|
|
27
|
+
debugLabel = "";
|
|
28
|
+
constructor() {
|
|
29
|
+
this.id = Scope.scopes.length;
|
|
30
|
+
Scope.scopes.push(this);
|
|
31
|
+
}
|
|
32
|
+
toString() {
|
|
33
|
+
const { id, debugLabel } = this;
|
|
34
|
+
return `[Scope:${id}${debugLabel && ` - ${debugLabel}`}]`;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var BunjaStore = class {
|
|
38
|
+
#bunjas = {};
|
|
39
|
+
#scopes = new Map();
|
|
40
|
+
get(bunja$1, readScope) {
|
|
41
|
+
const scopeInstanceMap = new Map(bunja$1.relatedScopes.map((scope) => [scope, this.#getScopeInstance(scope, readScope(scope))]));
|
|
42
|
+
const bunjaInstance = this.#getBunjaInstance(bunja$1, scopeInstanceMap);
|
|
43
|
+
const { relatedBunjaInstanceMap } = bunjaInstance;
|
|
44
|
+
return {
|
|
45
|
+
value: bunjaInstance.value,
|
|
46
|
+
mount() {
|
|
47
|
+
relatedBunjaInstanceMap.forEach((related) => related.add());
|
|
48
|
+
bunjaInstance.add();
|
|
49
|
+
scopeInstanceMap.forEach((scope) => scope.add());
|
|
50
|
+
return function unmount() {
|
|
51
|
+
relatedBunjaInstanceMap.forEach((related) => related.sub());
|
|
52
|
+
bunjaInstance.sub();
|
|
53
|
+
scopeInstanceMap.forEach((scope) => scope.sub());
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
deps: Array.from(scopeInstanceMap.values()).map(({ value }) => value)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
#getBunjaInstance(bunja$1, scopeInstanceMap) {
|
|
60
|
+
const localScopeInstanceMap = new Map(bunja$1.relatedScopes.map((scope) => [scope, scopeInstanceMap.get(scope)]));
|
|
61
|
+
const scopeInstanceIds = Array.from(localScopeInstanceMap.values()).map(({ instanceId }) => instanceId).sort((a, b) => a - b);
|
|
62
|
+
const bunjaInstanceId = `${bunja$1.id}:${scopeInstanceIds.join(",")}`;
|
|
63
|
+
if (this.#bunjas[bunjaInstanceId]) return this.#bunjas[bunjaInstanceId];
|
|
64
|
+
const relatedBunjaInstanceMap = new Map(bunja$1.relatedBunjas.map((relatedBunja) => [relatedBunja, this.#getBunjaInstance(relatedBunja, scopeInstanceMap)]));
|
|
65
|
+
const args = bunja$1.deps.map((dep) => {
|
|
66
|
+
if (dep instanceof Bunja) return relatedBunjaInstanceMap.get(dep).value;
|
|
67
|
+
if (dep instanceof Scope) return localScopeInstanceMap.get(dep).value;
|
|
68
|
+
throw new Error("Invalid dependency");
|
|
69
|
+
});
|
|
70
|
+
const bunjaInstance = new BunjaInstance(() => delete this.#bunjas[bunjaInstanceId], bunjaInstanceId, relatedBunjaInstanceMap, bunja$1.init.apply(bunja$1, args));
|
|
71
|
+
this.#bunjas[bunjaInstanceId] = bunjaInstance;
|
|
72
|
+
return bunjaInstance;
|
|
73
|
+
}
|
|
74
|
+
#getScopeInstance(scope, value) {
|
|
75
|
+
const scopeInstanceMap = this.#scopes.get(scope) ?? this.#scopes.set(scope, new Map()).get(scope);
|
|
76
|
+
const init = () => new ScopeInstance(() => scopeInstanceMap.delete(value), ScopeInstance.counter++, scope, value);
|
|
77
|
+
return scopeInstanceMap.get(value) ?? scopeInstanceMap.set(value, init()).get(value);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const createBunjaStore = () => new BunjaStore();
|
|
81
|
+
function bunjaImpl(deps, init) {
|
|
82
|
+
const parents = deps.filter((dep) => dep instanceof Bunja);
|
|
83
|
+
const scopes = deps.filter((dep) => dep instanceof Scope);
|
|
84
|
+
const relatedBunjas = toposort(parents);
|
|
85
|
+
const relatedScopes = Array.from(new Set([...scopes, ...parents.flatMap((parent) => parent.relatedScopes)]));
|
|
86
|
+
return new Bunja(deps, parents, relatedBunjas, relatedScopes, init);
|
|
87
|
+
}
|
|
88
|
+
bunjaImpl.effect = Bunja.effect;
|
|
89
|
+
const bunja = bunjaImpl;
|
|
90
|
+
function createScope() {
|
|
91
|
+
return new Scope();
|
|
92
|
+
}
|
|
93
|
+
var RefCounter = class {
|
|
94
|
+
#disposed = false;
|
|
95
|
+
#count = 0;
|
|
96
|
+
add() {
|
|
97
|
+
this.#count++;
|
|
98
|
+
}
|
|
99
|
+
sub() {
|
|
100
|
+
this.#count--;
|
|
101
|
+
setTimeout(() => {
|
|
102
|
+
if (this.#disposed) return;
|
|
103
|
+
if (this.#count < 1) {
|
|
104
|
+
this.#disposed = true;
|
|
105
|
+
this.dispose();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const noop = () => {};
|
|
111
|
+
var BunjaInstance = class extends RefCounter {
|
|
112
|
+
#cleanup;
|
|
113
|
+
#dispose;
|
|
114
|
+
constructor(dispose, instanceId, relatedBunjaInstanceMap, value) {
|
|
115
|
+
super();
|
|
116
|
+
this.instanceId = instanceId;
|
|
117
|
+
this.relatedBunjaInstanceMap = relatedBunjaInstanceMap;
|
|
118
|
+
this.value = value;
|
|
119
|
+
this.#dispose = () => {
|
|
120
|
+
this.#cleanup?.();
|
|
121
|
+
dispose();
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
add() {
|
|
125
|
+
this.#cleanup ??= this.value[Bunja.effect]?.() ?? noop;
|
|
126
|
+
super.add();
|
|
127
|
+
}
|
|
128
|
+
dispose() {
|
|
129
|
+
this.#dispose();
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
var ScopeInstance = class extends RefCounter {
|
|
133
|
+
static counter = 0;
|
|
134
|
+
constructor(dispose, instanceId, scope, value) {
|
|
135
|
+
super();
|
|
136
|
+
this.dispose = dispose;
|
|
137
|
+
this.instanceId = instanceId;
|
|
138
|
+
this.scope = scope;
|
|
139
|
+
this.value = value;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
function toposort(nodes) {
|
|
143
|
+
const visited = new Set();
|
|
144
|
+
const result = [];
|
|
145
|
+
function visit(current) {
|
|
146
|
+
if (visited.has(current)) return;
|
|
147
|
+
visited.add(current);
|
|
148
|
+
for (const parent of current.parents) visit(parent);
|
|
149
|
+
result.push(current);
|
|
150
|
+
}
|
|
151
|
+
for (const node of nodes) visit(node);
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
Object.defineProperty(exports, 'Bunja', {
|
|
157
|
+
enumerable: true,
|
|
158
|
+
get: function () {
|
|
159
|
+
return Bunja;
|
|
160
|
+
}
|
|
161
|
+
});Object.defineProperty(exports, 'BunjaStore', {
|
|
162
|
+
enumerable: true,
|
|
163
|
+
get: function () {
|
|
164
|
+
return BunjaStore;
|
|
165
|
+
}
|
|
166
|
+
});Object.defineProperty(exports, 'Scope', {
|
|
167
|
+
enumerable: true,
|
|
168
|
+
get: function () {
|
|
169
|
+
return Scope;
|
|
170
|
+
}
|
|
171
|
+
});Object.defineProperty(exports, 'bunja', {
|
|
172
|
+
enumerable: true,
|
|
173
|
+
get: function () {
|
|
174
|
+
return bunja;
|
|
175
|
+
}
|
|
176
|
+
});Object.defineProperty(exports, 'createBunjaStore', {
|
|
177
|
+
enumerable: true,
|
|
178
|
+
get: function () {
|
|
179
|
+
return createBunjaStore;
|
|
180
|
+
}
|
|
181
|
+
});Object.defineProperty(exports, 'createScope', {
|
|
182
|
+
enumerable: true,
|
|
183
|
+
get: function () {
|
|
184
|
+
return createScope;
|
|
185
|
+
}
|
|
186
|
+
});
|
package/dist/bunja.cjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_bunja = require('./bunja-QjknYXY-.cjs');
|
|
3
|
+
|
|
4
|
+
exports.Bunja = require_bunja.Bunja
|
|
5
|
+
exports.BunjaStore = require_bunja.BunjaStore
|
|
6
|
+
exports.Scope = require_bunja.Scope
|
|
7
|
+
exports.bunja = require_bunja.bunja
|
|
8
|
+
exports.createBunjaStore = require_bunja.createBunjaStore
|
|
9
|
+
exports.createScope = require_bunja.createScope
|
package/dist/bunja.d.cts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type Dep<T> = Bunja<T> | Scope<T>;
|
|
2
|
+
declare const bunjaEffectSymbol: unique symbol;
|
|
3
|
+
type BunjaEffectSymbol = typeof bunjaEffectSymbol;
|
|
4
|
+
export declare class Bunja<T> {
|
|
5
|
+
deps: Dep<any>[];
|
|
6
|
+
parents: Bunja<any>[];
|
|
7
|
+
relatedBunjas: Bunja<any>[];
|
|
8
|
+
relatedScopes: Scope<any>[];
|
|
9
|
+
init: (...args: any[]) => T & BunjaValue;
|
|
10
|
+
static readonly bunjas: Bunja<any>[];
|
|
11
|
+
readonly id: number;
|
|
12
|
+
debugLabel: string;
|
|
13
|
+
constructor(deps: Dep<any>[], // one depth dependencies
|
|
14
|
+
parents: Bunja<any>[], // one depth parents
|
|
15
|
+
relatedBunjas: Bunja<any>[], // toposorted parents without self
|
|
16
|
+
relatedScopes: Scope<any>[], // deduped
|
|
17
|
+
init: (...args: any[]) => T & BunjaValue);
|
|
18
|
+
static readonly effect: BunjaEffectSymbol;
|
|
19
|
+
toString(): string;
|
|
20
|
+
}
|
|
21
|
+
export declare class Scope<T> {
|
|
22
|
+
static readonly scopes: Scope<any>[];
|
|
23
|
+
readonly id: number;
|
|
24
|
+
debugLabel: string;
|
|
25
|
+
constructor();
|
|
26
|
+
toString(): string;
|
|
27
|
+
}
|
|
28
|
+
export type ReadScope = <T>(scope: Scope<T>) => T;
|
|
29
|
+
export declare class BunjaStore {
|
|
30
|
+
#private;
|
|
31
|
+
get<T>(bunja: Bunja<T>, readScope: ReadScope): {
|
|
32
|
+
value: T;
|
|
33
|
+
mount: () => void;
|
|
34
|
+
deps: any[];
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export declare const createBunjaStore: () => BunjaStore;
|
|
38
|
+
export type BunjaEffectFn = () => () => void;
|
|
39
|
+
export interface BunjaValue {
|
|
40
|
+
[Bunja.effect]?: BunjaEffectFn;
|
|
41
|
+
}
|
|
42
|
+
export declare const bunja: {
|
|
43
|
+
<T>(deps: [], init: () => T & BunjaValue): Bunja<T>;
|
|
44
|
+
<T, U>(deps: [Dep<U>], init: (u: U) => T & BunjaValue): Bunja<T>;
|
|
45
|
+
<T, U, V>(deps: [Dep<U>, Dep<V>], init: (u: U, v: V) => T & BunjaValue): Bunja<T>;
|
|
46
|
+
<T, U, V, W>(deps: [Dep<U>, Dep<V>, Dep<W>], init: (u: U, v: V, w: W) => T & BunjaValue): Bunja<T>;
|
|
47
|
+
<T, U, V, W, X>(deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>], init: (u: U, v: V, w: W, x: X) => T & BunjaValue): Bunja<T>;
|
|
48
|
+
<T, U, V, W, X, Y>(deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>, Dep<Y>], init: (u: U, v: V, w: W, x: X, y: Y) => T & BunjaValue): Bunja<T>;
|
|
49
|
+
<T, U, V, W, X, Y, Z>(deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>, Dep<Y>, Dep<Z>], init: (u: U, v: V, w: W, x: X, y: Y, z: Z) => T & BunjaValue): Bunja<T>;
|
|
50
|
+
readonly effect: BunjaEffectSymbol;
|
|
51
|
+
};
|
|
52
|
+
export declare function createScope<T>(): Scope<T>;
|
|
53
|
+
export {};
|
package/dist/bunja.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type Dep<T> = Bunja<T> | Scope<T>;
|
|
2
|
+
declare const bunjaEffectSymbol: unique symbol;
|
|
3
|
+
type BunjaEffectSymbol = typeof bunjaEffectSymbol;
|
|
4
|
+
export declare class Bunja<T> {
|
|
5
|
+
deps: Dep<any>[];
|
|
6
|
+
parents: Bunja<any>[];
|
|
7
|
+
relatedBunjas: Bunja<any>[];
|
|
8
|
+
relatedScopes: Scope<any>[];
|
|
9
|
+
init: (...args: any[]) => T & BunjaValue;
|
|
10
|
+
static readonly bunjas: Bunja<any>[];
|
|
11
|
+
readonly id: number;
|
|
12
|
+
debugLabel: string;
|
|
13
|
+
constructor(deps: Dep<any>[], // one depth dependencies
|
|
14
|
+
parents: Bunja<any>[], // one depth parents
|
|
15
|
+
relatedBunjas: Bunja<any>[], // toposorted parents without self
|
|
16
|
+
relatedScopes: Scope<any>[], // deduped
|
|
17
|
+
init: (...args: any[]) => T & BunjaValue);
|
|
18
|
+
static readonly effect: BunjaEffectSymbol;
|
|
19
|
+
toString(): string;
|
|
20
|
+
}
|
|
21
|
+
export declare class Scope<T> {
|
|
22
|
+
static readonly scopes: Scope<any>[];
|
|
23
|
+
readonly id: number;
|
|
24
|
+
debugLabel: string;
|
|
25
|
+
constructor();
|
|
26
|
+
toString(): string;
|
|
27
|
+
}
|
|
28
|
+
export type ReadScope = <T>(scope: Scope<T>) => T;
|
|
29
|
+
export declare class BunjaStore {
|
|
30
|
+
#private;
|
|
31
|
+
get<T>(bunja: Bunja<T>, readScope: ReadScope): {
|
|
32
|
+
value: T;
|
|
33
|
+
mount: () => void;
|
|
34
|
+
deps: any[];
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export declare const createBunjaStore: () => BunjaStore;
|
|
38
|
+
export type BunjaEffectFn = () => () => void;
|
|
39
|
+
export interface BunjaValue {
|
|
40
|
+
[Bunja.effect]?: BunjaEffectFn;
|
|
41
|
+
}
|
|
42
|
+
export declare const bunja: {
|
|
43
|
+
<T>(deps: [], init: () => T & BunjaValue): Bunja<T>;
|
|
44
|
+
<T, U>(deps: [Dep<U>], init: (u: U) => T & BunjaValue): Bunja<T>;
|
|
45
|
+
<T, U, V>(deps: [Dep<U>, Dep<V>], init: (u: U, v: V) => T & BunjaValue): Bunja<T>;
|
|
46
|
+
<T, U, V, W>(deps: [Dep<U>, Dep<V>, Dep<W>], init: (u: U, v: V, w: W) => T & BunjaValue): Bunja<T>;
|
|
47
|
+
<T, U, V, W, X>(deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>], init: (u: U, v: V, w: W, x: X) => T & BunjaValue): Bunja<T>;
|
|
48
|
+
<T, U, V, W, X, Y>(deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>, Dep<Y>], init: (u: U, v: V, w: W, x: X, y: Y) => T & BunjaValue): Bunja<T>;
|
|
49
|
+
<T, U, V, W, X, Y, Z>(deps: [Dep<U>, Dep<V>, Dep<W>, Dep<X>, Dep<Y>, Dep<Z>], init: (u: U, v: V, w: W, x: X, y: Y, z: Z) => T & BunjaValue): Bunja<T>;
|
|
50
|
+
readonly effect: BunjaEffectSymbol;
|
|
51
|
+
};
|
|
52
|
+
export declare function createScope<T>(): Scope<T>;
|
|
53
|
+
export {};
|
package/dist/bunja.js
ADDED
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
const require_bunja = require('./bunja-QjknYXY-.cjs');
|
|
26
|
+
const { createContext, useContext, useEffect } = __toESM(require("react"));
|
|
27
|
+
|
|
28
|
+
//#region react.ts
|
|
29
|
+
const BunjaStoreContext = createContext(require_bunja.createBunjaStore());
|
|
30
|
+
const scopeContextMap = new Map();
|
|
31
|
+
function bindScope(scope, context) {
|
|
32
|
+
scopeContextMap.set(scope, context);
|
|
33
|
+
}
|
|
34
|
+
function createScopeFromContext(context) {
|
|
35
|
+
const scope = require_bunja.createScope();
|
|
36
|
+
bindScope(scope, context);
|
|
37
|
+
return scope;
|
|
38
|
+
}
|
|
39
|
+
const defaultReadScope = (scope) => {
|
|
40
|
+
const context = scopeContextMap.get(scope);
|
|
41
|
+
return useContext(context);
|
|
42
|
+
};
|
|
43
|
+
function useBunja(bunja, readScope = defaultReadScope) {
|
|
44
|
+
const store = useContext(BunjaStoreContext);
|
|
45
|
+
const { value, mount, deps } = store.get(bunja, readScope);
|
|
46
|
+
useEffect(mount, deps);
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
function inject(overrideTable) {
|
|
50
|
+
const map = new Map(overrideTable);
|
|
51
|
+
return (scope) => {
|
|
52
|
+
if (map.has(scope)) return map.get(scope);
|
|
53
|
+
const context = scopeContextMap.get(scope);
|
|
54
|
+
if (!context) throw new Error("Unable to read the scope. Please inject the value explicitly or bind scope to the React context.");
|
|
55
|
+
return useContext(context);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
exports.BunjaStoreContext = BunjaStoreContext
|
|
61
|
+
exports.bindScope = bindScope
|
|
62
|
+
exports.createScopeFromContext = createScopeFromContext
|
|
63
|
+
exports.inject = inject
|
|
64
|
+
exports.scopeContextMap = scopeContextMap
|
|
65
|
+
exports.useBunja = useBunja
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Context } from "react";
|
|
2
|
+
import { type Bunja, type BunjaStore, type ReadScope, type Scope } from "./bunja.ts";
|
|
3
|
+
export declare const BunjaStoreContext: Context<BunjaStore>;
|
|
4
|
+
export declare const scopeContextMap: Map<Scope<any>, Context<any>>;
|
|
5
|
+
export declare function bindScope(scope: Scope<any>, context: Context<any>): void;
|
|
6
|
+
export declare function createScopeFromContext<T>(context: Context<T>): Scope<T>;
|
|
7
|
+
export declare function useBunja<T>(bunja: Bunja<T>, readScope?: ReadScope): T;
|
|
8
|
+
export type ScopePair<T> = [Scope<T>, T];
|
|
9
|
+
export declare function inject<const T extends ScopePair<any>[]>(overrideTable: T): ReadScope;
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Context } from "react";
|
|
2
|
+
import { type Bunja, type BunjaStore, type ReadScope, type Scope } from "./bunja.ts";
|
|
3
|
+
export declare const BunjaStoreContext: Context<BunjaStore>;
|
|
4
|
+
export declare const scopeContextMap: Map<Scope<any>, Context<any>>;
|
|
5
|
+
export declare function bindScope(scope: Scope<any>, context: Context<any>): void;
|
|
6
|
+
export declare function createScopeFromContext<T>(context: Context<T>): Scope<T>;
|
|
7
|
+
export declare function useBunja<T>(bunja: Bunja<T>, readScope?: ReadScope): T;
|
|
8
|
+
export type ScopePair<T> = [Scope<T>, T];
|
|
9
|
+
export declare function inject<const T extends ScopePair<any>[]>(overrideTable: T): ReadScope;
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createBunjaStore, createScope } from "./bunja-3B0nQ1vI.js";
|
|
2
|
+
import { createContext, useContext, useEffect } from "react";
|
|
3
|
+
|
|
4
|
+
//#region react.ts
|
|
5
|
+
const BunjaStoreContext = createContext(createBunjaStore());
|
|
6
|
+
const scopeContextMap = new Map();
|
|
7
|
+
function bindScope(scope, context) {
|
|
8
|
+
scopeContextMap.set(scope, context);
|
|
9
|
+
}
|
|
10
|
+
function createScopeFromContext(context) {
|
|
11
|
+
const scope = createScope();
|
|
12
|
+
bindScope(scope, context);
|
|
13
|
+
return scope;
|
|
14
|
+
}
|
|
15
|
+
const defaultReadScope = (scope) => {
|
|
16
|
+
const context = scopeContextMap.get(scope);
|
|
17
|
+
return useContext(context);
|
|
18
|
+
};
|
|
19
|
+
function useBunja(bunja, readScope = defaultReadScope) {
|
|
20
|
+
const store = useContext(BunjaStoreContext);
|
|
21
|
+
const { value, mount, deps } = store.get(bunja, readScope);
|
|
22
|
+
useEffect(mount, deps);
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
function inject(overrideTable) {
|
|
26
|
+
const map = new Map(overrideTable);
|
|
27
|
+
return (scope) => {
|
|
28
|
+
if (map.has(scope)) return map.get(scope);
|
|
29
|
+
const context = scopeContextMap.get(scope);
|
|
30
|
+
if (!context) throw new Error("Unable to read the scope. Please inject the value explicitly or bind scope to the React context.");
|
|
31
|
+
return useContext(context);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { BunjaStoreContext, bindScope, createScopeFromContext, inject, scopeContextMap, useBunja };
|
package/package.json
CHANGED
|
@@ -1,9 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bunja",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
4
5
|
"description": "State Lifetime Manager",
|
|
5
|
-
"main": "bunja.
|
|
6
|
-
"
|
|
6
|
+
"main": "dist/bunja.cjs",
|
|
7
|
+
"module": "dist/bunja.js",
|
|
8
|
+
"types": "dist/bunja.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/bunja.d.ts",
|
|
13
|
+
"default": "./dist/bunja.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/bunja.d.cts",
|
|
17
|
+
"default": "./dist/bunja.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"./react": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/react.d.ts",
|
|
23
|
+
"default": "./dist/react.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/react.d.cts",
|
|
27
|
+
"default": "./dist/react.cjs"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"typesVersions": {
|
|
32
|
+
"*": {
|
|
33
|
+
"react": [
|
|
34
|
+
"./dist/react.d.ts"
|
|
35
|
+
],
|
|
36
|
+
"*": [
|
|
37
|
+
"./dist/bunja.d.ts"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsdown",
|
|
43
|
+
"check": "tsc --noEmit"
|
|
44
|
+
},
|
|
7
45
|
"keywords": [
|
|
8
46
|
"bunja",
|
|
9
47
|
"di"
|
|
@@ -12,7 +50,9 @@
|
|
|
12
50
|
"license": "Zlib",
|
|
13
51
|
"devDependencies": {
|
|
14
52
|
"@types/react": "^18",
|
|
15
|
-
"react": "^18"
|
|
53
|
+
"react": "^18",
|
|
54
|
+
"tsdown": "^0.2.17",
|
|
55
|
+
"typescript": "^5.6.3"
|
|
16
56
|
},
|
|
17
57
|
"peerDependencies": {
|
|
18
58
|
"@types/react": "*",
|
package/react.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { Context, createContext, useContext, useEffect } from "react";
|
|
1
|
+
import { type Context, createContext, useContext, useEffect } from "react";
|
|
2
2
|
import {
|
|
3
|
-
Bunja,
|
|
3
|
+
type Bunja,
|
|
4
|
+
type BunjaStore,
|
|
4
5
|
createBunjaStore,
|
|
5
6
|
createScope,
|
|
6
|
-
ReadScope,
|
|
7
|
-
Scope,
|
|
8
|
-
} from "./bunja";
|
|
7
|
+
type ReadScope,
|
|
8
|
+
type Scope,
|
|
9
|
+
} from "./bunja.ts";
|
|
9
10
|
|
|
10
|
-
export const BunjaStoreContext =
|
|
11
|
+
export const BunjaStoreContext: Context<BunjaStore> =
|
|
12
|
+
createContext(createBunjaStore());
|
|
11
13
|
|
|
12
|
-
export const scopeContextMap
|
|
13
|
-
export function bindScope(scope: Scope<any>, context: Context<any>) {
|
|
14
|
+
export const scopeContextMap: Map<Scope<any>, Context<any>> = new Map();
|
|
15
|
+
export function bindScope(scope: Scope<any>, context: Context<any>): void {
|
|
14
16
|
scopeContextMap.set(scope, context);
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -25,17 +27,20 @@ const defaultReadScope: ReadScope = (scope) => {
|
|
|
25
27
|
return useContext(context);
|
|
26
28
|
};
|
|
27
29
|
|
|
28
|
-
export function useBunja<T>(
|
|
30
|
+
export function useBunja<T>(
|
|
31
|
+
bunja: Bunja<T>,
|
|
32
|
+
readScope: ReadScope = defaultReadScope,
|
|
33
|
+
): T {
|
|
29
34
|
const store = useContext(BunjaStoreContext);
|
|
30
|
-
const { value, mount } = store.get(bunja, readScope);
|
|
31
|
-
useEffect(mount,
|
|
35
|
+
const { value, mount, deps } = store.get(bunja, readScope);
|
|
36
|
+
useEffect(mount, deps);
|
|
32
37
|
return value;
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
export type ScopePair<T> = [Scope<T>, T];
|
|
36
41
|
|
|
37
42
|
export function inject<const T extends ScopePair<any>[]>(
|
|
38
|
-
overrideTable: T
|
|
43
|
+
overrideTable: T,
|
|
39
44
|
): ReadScope {
|
|
40
45
|
const map = new Map(overrideTable);
|
|
41
46
|
return (scope) => {
|
|
@@ -43,7 +48,7 @@ export function inject<const T extends ScopePair<any>[]>(
|
|
|
43
48
|
const context = scopeContextMap.get(scope);
|
|
44
49
|
if (!context) {
|
|
45
50
|
throw new Error(
|
|
46
|
-
"Unable to read the scope. Please inject the value explicitly or bind scope to the React context."
|
|
51
|
+
"Unable to read the scope. Please inject the value explicitly or bind scope to the React context.",
|
|
47
52
|
);
|
|
48
53
|
}
|
|
49
54
|
return useContext(context);
|
package/tsconfig.json
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ESNext",
|
|
4
4
|
"strict": true,
|
|
5
|
-
"moduleResolution": "
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"allowImportingTsExtensions": true,
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"emitDeclarationOnly": true,
|
|
9
|
+
"isolatedDeclarations": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"outDir": "dist"
|
|
6
12
|
}
|
|
7
13
|
}
|