bunja 1.0.0 → 2.0.0-alpha.2

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.
@@ -1,189 +0,0 @@
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: id$1, debugLabel } = this;
21
- return `[Bunja:${id$1}${debugLabel && ` - ${debugLabel}`}]`;
22
- }
23
- };
24
- var Scope = class Scope {
25
- static scopes = [];
26
- id;
27
- debugLabel = "";
28
- constructor(hash = id) {
29
- this.hash = hash;
30
- this.id = Scope.scopes.length;
31
- Scope.scopes.push(this);
32
- }
33
- toString() {
34
- const { id: id$1, debugLabel } = this;
35
- return `[Scope:${id$1}${debugLabel && ` - ${debugLabel}`}]`;
36
- }
37
- };
38
- var BunjaStore = class {
39
- #bunjas = {};
40
- #scopes = new Map();
41
- get(bunja$1, readScope) {
42
- const scopeInstanceMap = new Map(bunja$1.relatedScopes.map((scope) => [scope, this.#getScopeInstance(scope, readScope(scope))]));
43
- const bunjaInstance = this.#getBunjaInstance(bunja$1, scopeInstanceMap);
44
- const { relatedBunjaInstanceMap } = bunjaInstance;
45
- return {
46
- value: bunjaInstance.value,
47
- mount() {
48
- relatedBunjaInstanceMap.forEach((related) => related.add());
49
- bunjaInstance.add();
50
- scopeInstanceMap.forEach((scope) => scope.add());
51
- return function unmount() {
52
- relatedBunjaInstanceMap.forEach((related) => related.sub());
53
- bunjaInstance.sub();
54
- scopeInstanceMap.forEach((scope) => scope.sub());
55
- };
56
- },
57
- deps: Array.from(scopeInstanceMap.values()).map(({ value }) => value)
58
- };
59
- }
60
- #getBunjaInstance(bunja$1, scopeInstanceMap) {
61
- const localScopeInstanceMap = new Map(bunja$1.relatedScopes.map((scope) => [scope, scopeInstanceMap.get(scope)]));
62
- const scopeInstanceIds = Array.from(localScopeInstanceMap.values()).map(({ instanceId }) => instanceId).sort((a, b) => a - b);
63
- const bunjaInstanceId = `${bunja$1.id}:${scopeInstanceIds.join(",")}`;
64
- if (this.#bunjas[bunjaInstanceId]) return this.#bunjas[bunjaInstanceId];
65
- const relatedBunjaInstanceMap = new Map(bunja$1.relatedBunjas.map((relatedBunja) => [relatedBunja, this.#getBunjaInstance(relatedBunja, scopeInstanceMap)]));
66
- const args = bunja$1.deps.map((dep) => {
67
- if (dep instanceof Bunja) return relatedBunjaInstanceMap.get(dep).value;
68
- if (dep instanceof Scope) return localScopeInstanceMap.get(dep).value;
69
- throw new Error("Invalid dependency");
70
- });
71
- const bunjaInstance = new BunjaInstance(() => delete this.#bunjas[bunjaInstanceId], bunjaInstanceId, relatedBunjaInstanceMap, bunja$1.init.apply(bunja$1, args));
72
- this.#bunjas[bunjaInstanceId] = bunjaInstance;
73
- return bunjaInstance;
74
- }
75
- #getScopeInstance(scope, value) {
76
- const key = scope.hash(value);
77
- const scopeInstanceMap = this.#scopes.get(scope) ?? this.#scopes.set(scope, new Map()).get(scope);
78
- const init = () => new ScopeInstance(() => scopeInstanceMap.delete(key), ScopeInstance.counter++, scope, value);
79
- return scopeInstanceMap.get(key) ?? scopeInstanceMap.set(key, init()).get(key);
80
- }
81
- };
82
- const createBunjaStore = () => new BunjaStore();
83
- function bunjaImpl(deps, init) {
84
- const parents = deps.filter((dep) => dep instanceof Bunja);
85
- const scopes = deps.filter((dep) => dep instanceof Scope);
86
- const relatedBunjas = toposort(parents);
87
- const relatedScopes = Array.from(new Set([...scopes, ...parents.flatMap((parent) => parent.relatedScopes)]));
88
- return new Bunja(deps, parents, relatedBunjas, relatedScopes, init);
89
- }
90
- bunjaImpl.effect = Bunja.effect;
91
- const bunja = bunjaImpl;
92
- function createScope(hash) {
93
- return new Scope(hash);
94
- }
95
- var RefCounter = class {
96
- #disposed = false;
97
- #count = 0;
98
- add() {
99
- this.#count++;
100
- }
101
- sub() {
102
- this.#count--;
103
- setTimeout(() => {
104
- if (this.#disposed) return;
105
- if (this.#count < 1) {
106
- this.#disposed = true;
107
- this.dispose();
108
- }
109
- });
110
- }
111
- };
112
- const id = (x) => x;
113
- const noop = () => {};
114
- var BunjaInstance = class extends RefCounter {
115
- #cleanup;
116
- #dispose;
117
- constructor(dispose, instanceId, relatedBunjaInstanceMap, value) {
118
- super();
119
- this.instanceId = instanceId;
120
- this.relatedBunjaInstanceMap = relatedBunjaInstanceMap;
121
- this.value = value;
122
- this.#dispose = () => {
123
- this.#cleanup?.();
124
- dispose();
125
- };
126
- }
127
- add() {
128
- this.#cleanup ??= this.value[Bunja.effect]?.() ?? noop;
129
- super.add();
130
- }
131
- dispose() {
132
- this.#dispose();
133
- }
134
- };
135
- var ScopeInstance = class extends RefCounter {
136
- static counter = 0;
137
- constructor(dispose, instanceId, scope, value) {
138
- super();
139
- this.dispose = dispose;
140
- this.instanceId = instanceId;
141
- this.scope = scope;
142
- this.value = value;
143
- }
144
- };
145
- function toposort(nodes) {
146
- const visited = new Set();
147
- const result = [];
148
- function visit(current) {
149
- if (visited.has(current)) return;
150
- visited.add(current);
151
- for (const parent of current.parents) visit(parent);
152
- result.push(current);
153
- }
154
- for (const node of nodes) visit(node);
155
- return result;
156
- }
157
-
158
- //#endregion
159
- Object.defineProperty(exports, 'Bunja', {
160
- enumerable: true,
161
- get: function () {
162
- return Bunja;
163
- }
164
- });Object.defineProperty(exports, 'BunjaStore', {
165
- enumerable: true,
166
- get: function () {
167
- return BunjaStore;
168
- }
169
- });Object.defineProperty(exports, 'Scope', {
170
- enumerable: true,
171
- get: function () {
172
- return Scope;
173
- }
174
- });Object.defineProperty(exports, 'bunja', {
175
- enumerable: true,
176
- get: function () {
177
- return bunja;
178
- }
179
- });Object.defineProperty(exports, 'createBunjaStore', {
180
- enumerable: true,
181
- get: function () {
182
- return createBunjaStore;
183
- }
184
- });Object.defineProperty(exports, 'createScope', {
185
- enumerable: true,
186
- get: function () {
187
- return createScope;
188
- }
189
- });
@@ -1,158 +0,0 @@
1
-
2
- //#region bunja.ts
3
- const bunjaEffectSymbol = Symbol("Bunja.effect");
4
- var Bunja = class Bunja {
5
- static bunjas = [];
6
- id;
7
- debugLabel = "";
8
- constructor(deps, parents, relatedBunjas, relatedScopes, init) {
9
- this.deps = deps;
10
- this.parents = parents;
11
- this.relatedBunjas = relatedBunjas;
12
- this.relatedScopes = relatedScopes;
13
- this.init = init;
14
- this.id = Bunja.bunjas.length;
15
- Bunja.bunjas.push(this);
16
- }
17
- static effect = bunjaEffectSymbol;
18
- toString() {
19
- const { id: id$1, debugLabel } = this;
20
- return `[Bunja:${id$1}${debugLabel && ` - ${debugLabel}`}]`;
21
- }
22
- };
23
- var Scope = class Scope {
24
- static scopes = [];
25
- id;
26
- debugLabel = "";
27
- constructor(hash = id) {
28
- this.hash = hash;
29
- this.id = Scope.scopes.length;
30
- Scope.scopes.push(this);
31
- }
32
- toString() {
33
- const { id: id$1, debugLabel } = this;
34
- return `[Scope:${id$1}${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 key = scope.hash(value);
76
- const scopeInstanceMap = this.#scopes.get(scope) ?? this.#scopes.set(scope, new Map()).get(scope);
77
- const init = () => new ScopeInstance(() => scopeInstanceMap.delete(key), ScopeInstance.counter++, scope, value);
78
- return scopeInstanceMap.get(key) ?? scopeInstanceMap.set(key, init()).get(key);
79
- }
80
- };
81
- const createBunjaStore = () => new BunjaStore();
82
- function bunjaImpl(deps, init) {
83
- const parents = deps.filter((dep) => dep instanceof Bunja);
84
- const scopes = deps.filter((dep) => dep instanceof Scope);
85
- const relatedBunjas = toposort(parents);
86
- const relatedScopes = Array.from(new Set([...scopes, ...parents.flatMap((parent) => parent.relatedScopes)]));
87
- return new Bunja(deps, parents, relatedBunjas, relatedScopes, init);
88
- }
89
- bunjaImpl.effect = Bunja.effect;
90
- const bunja = bunjaImpl;
91
- function createScope(hash) {
92
- return new Scope(hash);
93
- }
94
- var RefCounter = class {
95
- #disposed = false;
96
- #count = 0;
97
- add() {
98
- this.#count++;
99
- }
100
- sub() {
101
- this.#count--;
102
- setTimeout(() => {
103
- if (this.#disposed) return;
104
- if (this.#count < 1) {
105
- this.#disposed = true;
106
- this.dispose();
107
- }
108
- });
109
- }
110
- };
111
- const id = (x) => x;
112
- const noop = () => {};
113
- var BunjaInstance = class extends RefCounter {
114
- #cleanup;
115
- #dispose;
116
- constructor(dispose, instanceId, relatedBunjaInstanceMap, value) {
117
- super();
118
- this.instanceId = instanceId;
119
- this.relatedBunjaInstanceMap = relatedBunjaInstanceMap;
120
- this.value = value;
121
- this.#dispose = () => {
122
- this.#cleanup?.();
123
- dispose();
124
- };
125
- }
126
- add() {
127
- this.#cleanup ??= this.value[Bunja.effect]?.() ?? noop;
128
- super.add();
129
- }
130
- dispose() {
131
- this.#dispose();
132
- }
133
- };
134
- var ScopeInstance = class extends RefCounter {
135
- static counter = 0;
136
- constructor(dispose, instanceId, scope, value) {
137
- super();
138
- this.dispose = dispose;
139
- this.instanceId = instanceId;
140
- this.scope = scope;
141
- this.value = value;
142
- }
143
- };
144
- function toposort(nodes) {
145
- const visited = new Set();
146
- const result = [];
147
- function visit(current) {
148
- if (visited.has(current)) return;
149
- visited.add(current);
150
- for (const parent of current.parents) visit(parent);
151
- result.push(current);
152
- }
153
- for (const node of nodes) visit(node);
154
- return result;
155
- }
156
-
157
- //#endregion
158
- export { Bunja, BunjaStore, Scope, bunja, createBunjaStore, createScope };