@valentin30/signal 0.0.9 → 0.2.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/dist/index.d.mts +233 -147
- package/dist/index.d.ts +233 -147
- package/dist/index.js +397 -255
- package/dist/index.mjs +389 -249
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -20,27 +20,89 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
Equals: () => Equals,
|
|
24
|
+
Internal: () => internal_exports,
|
|
23
25
|
batch: () => batch,
|
|
24
|
-
|
|
26
|
+
comparator: () => comparator,
|
|
25
27
|
composed: () => composed,
|
|
26
28
|
computed: () => computed,
|
|
27
|
-
|
|
28
|
-
effect: () => effect,
|
|
29
|
-
factory: () => factory,
|
|
29
|
+
configuration: () => configuration,
|
|
30
30
|
ignore: () => ignore,
|
|
31
|
-
|
|
32
|
-
signal: () => signal
|
|
31
|
+
reader: () => reader,
|
|
32
|
+
signal: () => signal,
|
|
33
|
+
utils: () => utils_exports,
|
|
34
|
+
writer: () => writer
|
|
33
35
|
});
|
|
34
36
|
module.exports = __toCommonJS(index_exports);
|
|
35
37
|
|
|
36
|
-
// src/core/
|
|
38
|
+
// src/core/contracts/comparable.ts
|
|
39
|
+
var Equals;
|
|
40
|
+
((Equals2) => {
|
|
41
|
+
function strict(value, other) {
|
|
42
|
+
if (value === other) return true;
|
|
43
|
+
if (value !== value && other !== other) return true;
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
Equals2.strict = strict;
|
|
47
|
+
})(Equals || (Equals = {}));
|
|
48
|
+
function comparator(equals = Equals.strict) {
|
|
49
|
+
return { equals };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/core/contracts/readable.ts
|
|
53
|
+
function reader(read) {
|
|
54
|
+
return { read };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/core/contracts/writable.ts
|
|
58
|
+
function writer(write) {
|
|
59
|
+
return {
|
|
60
|
+
write(value) {
|
|
61
|
+
const result = write(value);
|
|
62
|
+
if (typeof result === "boolean") return result;
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/internal/internal.ts
|
|
69
|
+
var internal_exports = {};
|
|
70
|
+
__export(internal_exports, {
|
|
71
|
+
BasicValue: () => BasicValue,
|
|
72
|
+
BatchWriter: () => BatchWriter,
|
|
73
|
+
CallbackScheduler: () => CallbackScheduler,
|
|
74
|
+
CollectorBasedBatcher: () => CollectorBasedBatcher,
|
|
75
|
+
CollectorController: () => CollectorController,
|
|
76
|
+
Composed: () => Composed,
|
|
77
|
+
ComputedValue: () => ComputedValue,
|
|
78
|
+
DependencyManager: () => DependencyManager,
|
|
79
|
+
EmittingValue: () => EmittingValue,
|
|
80
|
+
Reactive: () => Reactive,
|
|
81
|
+
ScheduledEmitter: () => ScheduledEmitter,
|
|
82
|
+
Snapshot: () => Snapshot
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// src/utils/utils.ts
|
|
86
|
+
var utils_exports = {};
|
|
87
|
+
__export(utils_exports, {
|
|
88
|
+
call: () => call,
|
|
89
|
+
factory: () => factory,
|
|
90
|
+
singleton: () => singleton
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// src/utils/call.ts
|
|
94
|
+
function call(callback) {
|
|
95
|
+
callback();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/utils/factory.ts
|
|
37
99
|
function factory(name) {
|
|
38
100
|
let __default__ = null;
|
|
39
101
|
let __factory__ = null;
|
|
40
102
|
function object(...args) {
|
|
41
103
|
if (__factory__) return __factory__(...args);
|
|
42
104
|
if (__default__) return __default__(...args);
|
|
43
|
-
throw new Error(`${name}
|
|
105
|
+
throw new Error(`${name}::factory() not configured!`);
|
|
44
106
|
}
|
|
45
107
|
object.configured = () => __default__ !== null || __factory__ !== null;
|
|
46
108
|
object.default = (factory2) => {
|
|
@@ -52,304 +114,384 @@ function factory(name) {
|
|
|
52
114
|
return object;
|
|
53
115
|
}
|
|
54
116
|
|
|
55
|
-
// src/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// src/core/computed.ts
|
|
65
|
-
var computed = factory("computed");
|
|
66
|
-
|
|
67
|
-
// src/core/effect.ts
|
|
68
|
-
var effect = factory("effect");
|
|
69
|
-
|
|
70
|
-
// src/core/ignore.ts
|
|
71
|
-
var ignore = factory("ignore");
|
|
72
|
-
|
|
73
|
-
// src/core/signal.ts
|
|
74
|
-
var signal = factory("signal");
|
|
75
|
-
|
|
76
|
-
// src/internal/index.ts
|
|
77
|
-
var internal_exports = {};
|
|
78
|
-
__export(internal_exports, {
|
|
79
|
-
Collector: () => Collector,
|
|
80
|
-
Composed: () => Composed,
|
|
81
|
-
Computed: () => Computed,
|
|
82
|
-
Signal: () => Signal,
|
|
83
|
-
batch: () => batch2,
|
|
84
|
-
collector: () => collector2,
|
|
85
|
-
composed: () => composed2,
|
|
86
|
-
computed: () => computed2,
|
|
87
|
-
effect: () => effect2,
|
|
88
|
-
ignore: () => ignore2,
|
|
89
|
-
shared: () => shared,
|
|
90
|
-
signal: () => signal2
|
|
91
|
-
});
|
|
117
|
+
// src/utils/singleton.ts
|
|
118
|
+
function singleton(factory2) {
|
|
119
|
+
function object() {
|
|
120
|
+
return object.instance ??= factory2();
|
|
121
|
+
}
|
|
122
|
+
object.instance = void 0;
|
|
123
|
+
return object;
|
|
124
|
+
}
|
|
92
125
|
|
|
93
126
|
// src/internal/batch.ts
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
127
|
+
var CollectorBasedBatcher = class {
|
|
128
|
+
collector;
|
|
129
|
+
constructor(collector) {
|
|
130
|
+
this.collector = collector;
|
|
131
|
+
}
|
|
132
|
+
batch(callback, ...args) {
|
|
133
|
+
if (this.collector.collecting()) return callback(...args);
|
|
134
|
+
const [value, collected] = this.collector.collect(callback, ...args);
|
|
135
|
+
collected.forEach(utils_exports.call);
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
99
139
|
|
|
100
140
|
// src/internal/collector.ts
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
#values;
|
|
106
|
-
constructor(values) {
|
|
107
|
-
this.#values = values;
|
|
141
|
+
var CollectorController = class {
|
|
142
|
+
values;
|
|
143
|
+
constructor(values = null) {
|
|
144
|
+
this.values = values;
|
|
108
145
|
}
|
|
109
146
|
collecting() {
|
|
110
|
-
return this
|
|
147
|
+
return !!this.values;
|
|
111
148
|
}
|
|
112
149
|
add(value) {
|
|
113
|
-
if (!this.
|
|
114
|
-
this
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
150
|
+
if (!this.values) return;
|
|
151
|
+
this.values.add(value);
|
|
152
|
+
}
|
|
153
|
+
track(value) {
|
|
154
|
+
if (!this.values) return;
|
|
155
|
+
this.values.add(value);
|
|
156
|
+
}
|
|
157
|
+
collect(callback, ...args) {
|
|
158
|
+
const current = this.values;
|
|
159
|
+
this.values = /* @__PURE__ */ new Set();
|
|
160
|
+
const response = [];
|
|
161
|
+
try {
|
|
162
|
+
response[0] = callback(...args);
|
|
163
|
+
} finally {
|
|
164
|
+
response[1] = this.values;
|
|
165
|
+
this.values = current;
|
|
166
|
+
return response;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
ignore(callback, ...args) {
|
|
170
|
+
const current = this.values;
|
|
171
|
+
this.values = null;
|
|
172
|
+
try {
|
|
173
|
+
return callback(...args);
|
|
174
|
+
} finally {
|
|
175
|
+
this.values = current;
|
|
176
|
+
}
|
|
129
177
|
}
|
|
130
178
|
};
|
|
131
179
|
|
|
132
|
-
// src/internal/
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
/* @__PURE__ */ new Set(),
|
|
140
|
-
computed2.collector(),
|
|
141
|
-
compute,
|
|
142
|
-
equals
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
computed2.collector = factory("computed.collector");
|
|
146
|
-
var Computed = class {
|
|
147
|
-
#empty;
|
|
148
|
-
#value;
|
|
149
|
-
#values;
|
|
150
|
-
#dependencies;
|
|
151
|
-
#compute;
|
|
152
|
-
#equals;
|
|
153
|
-
#listeners;
|
|
154
|
-
#collector;
|
|
155
|
-
constructor(empty, value, values, dependencies, listeners, collector3, compute, equals) {
|
|
156
|
-
this.#empty = empty;
|
|
157
|
-
this.#value = value;
|
|
158
|
-
this.#values = values;
|
|
159
|
-
this.#dependencies = dependencies;
|
|
160
|
-
this.#listeners = listeners;
|
|
161
|
-
this.#collector = collector3;
|
|
162
|
-
this.#compute = compute;
|
|
163
|
-
this.#equals = equals;
|
|
180
|
+
// src/internal/composed.ts
|
|
181
|
+
var Composed = class {
|
|
182
|
+
value;
|
|
183
|
+
writer;
|
|
184
|
+
constructor(value, writer2) {
|
|
185
|
+
this.value = value;
|
|
186
|
+
this.writer = writer2;
|
|
164
187
|
}
|
|
165
188
|
read() {
|
|
166
|
-
this
|
|
167
|
-
return this.peek();
|
|
189
|
+
return this.value.read();
|
|
168
190
|
}
|
|
169
191
|
peek() {
|
|
170
|
-
|
|
171
|
-
const current = this.#dependencies;
|
|
172
|
-
const next = this.#collector.collect(() => this.#value = this.#compute());
|
|
173
|
-
this.#dependencies = next;
|
|
174
|
-
this.#values = Array.from(next).map((dep) => [dep, dep.read()]);
|
|
175
|
-
this.#empty = false;
|
|
176
|
-
if (!current.size && !next.size) return this.#value;
|
|
177
|
-
if (!this.#listeners.size) return this.#value;
|
|
178
|
-
current.forEach((dep) => !next.has(dep) && this.#listeners.forEach((listener) => dep.unsubscribe(listener)));
|
|
179
|
-
next.forEach((dep) => !current.has(dep) && this.#listeners.forEach((listener) => dep.subscribe(listener)));
|
|
180
|
-
return this.#value;
|
|
192
|
+
return this.value.peek();
|
|
181
193
|
}
|
|
182
|
-
|
|
183
|
-
if (this
|
|
184
|
-
return this.
|
|
194
|
+
write(value) {
|
|
195
|
+
if (this.value.equals(this.peek(), value)) return false;
|
|
196
|
+
return this.writer.write(value);
|
|
197
|
+
}
|
|
198
|
+
equals(...args) {
|
|
199
|
+
const [value, other] = args.length === 1 ? [this.read(), args[0]] : args;
|
|
200
|
+
return this.value.equals(value, other);
|
|
185
201
|
}
|
|
186
202
|
subscribe(callback) {
|
|
187
|
-
this
|
|
188
|
-
this.#dependencies.forEach((dep) => dep.subscribe(callback));
|
|
189
|
-
return this.unsubscribe.bind(this, callback);
|
|
203
|
+
return this.value.subscribe(callback);
|
|
190
204
|
}
|
|
191
205
|
unsubscribe(callback) {
|
|
192
|
-
this
|
|
193
|
-
this.#dependencies.forEach((dep) => dep.unsubscribe(callback));
|
|
206
|
+
return this.value.unsubscribe(callback);
|
|
194
207
|
}
|
|
195
208
|
};
|
|
196
209
|
|
|
197
|
-
// src/internal/
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
[],
|
|
203
|
-
/* @__PURE__ */ new Set(),
|
|
204
|
-
/* @__PURE__ */ new Set(),
|
|
205
|
-
composed2.batcher(),
|
|
206
|
-
composed2.collector(),
|
|
207
|
-
compute,
|
|
208
|
-
write,
|
|
209
|
-
equals
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
composed2.batcher = factory("composed.batcher");
|
|
213
|
-
composed2.collector = factory("composed.collector");
|
|
214
|
-
var Composed = class extends Computed {
|
|
215
|
-
#batcher;
|
|
216
|
-
#write;
|
|
217
|
-
constructor(empty, value, values, dependencies, listeners, batcher, collector3, compute, write, equals) {
|
|
218
|
-
super(empty, value, values, dependencies, listeners, collector3, compute, equals);
|
|
219
|
-
this.#batcher = batcher;
|
|
220
|
-
this.#write = write;
|
|
210
|
+
// src/internal/dependency.ts
|
|
211
|
+
var Snapshot;
|
|
212
|
+
((Snapshot2) => {
|
|
213
|
+
function changed([value, snapshot]) {
|
|
214
|
+
return !value.equals(value.peek(), snapshot);
|
|
221
215
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
216
|
+
Snapshot2.changed = changed;
|
|
217
|
+
function from(values) {
|
|
218
|
+
if (!values.size) return [];
|
|
219
|
+
return Array.from(values).map(create);
|
|
220
|
+
}
|
|
221
|
+
Snapshot2.from = from;
|
|
222
|
+
function create(value) {
|
|
223
|
+
return [value, value.peek()];
|
|
224
|
+
}
|
|
225
|
+
})(Snapshot || (Snapshot = {}));
|
|
226
|
+
var DependencyManager = class {
|
|
227
|
+
snapshot;
|
|
228
|
+
dependencies;
|
|
229
|
+
collector;
|
|
230
|
+
listeners;
|
|
231
|
+
constructor(collector, listeners = /* @__PURE__ */ new Set(), dependencies = /* @__PURE__ */ new Set(), snapshot = null) {
|
|
232
|
+
this.collector = collector;
|
|
233
|
+
this.listeners = listeners;
|
|
234
|
+
this.dependencies = dependencies;
|
|
235
|
+
this.snapshot = snapshot;
|
|
236
|
+
}
|
|
237
|
+
changed() {
|
|
238
|
+
if (!this.snapshot) return true;
|
|
239
|
+
return this.snapshot.some(Snapshot.changed);
|
|
240
|
+
}
|
|
241
|
+
track(callback) {
|
|
242
|
+
const [_, dependencies] = this.collector.collect(callback);
|
|
243
|
+
if (!this.dependencies.size && !dependencies.size) return;
|
|
244
|
+
this.snapshot = Snapshot.from(dependencies);
|
|
245
|
+
if (this.equals(dependencies)) return;
|
|
246
|
+
if (this.listeners.size) {
|
|
247
|
+
this.dependencies.forEach((dep) => !dependencies.has(dep) && this.listeners.forEach((cb) => dep.unsubscribe(cb)));
|
|
248
|
+
dependencies.forEach((dep) => !this.dependencies.has(dep) && this.listeners.forEach((cb) => dep.subscribe(cb)));
|
|
249
|
+
}
|
|
250
|
+
this.dependencies = dependencies;
|
|
251
|
+
}
|
|
252
|
+
subscribe(callback) {
|
|
253
|
+
this.listeners.add(callback);
|
|
254
|
+
this.dependencies.forEach((dep) => dep.subscribe(callback));
|
|
255
|
+
return () => this.unsubscribe(callback);
|
|
256
|
+
}
|
|
257
|
+
unsubscribe(callback) {
|
|
258
|
+
this.listeners.delete(callback);
|
|
259
|
+
this.dependencies.forEach((dep) => dep.unsubscribe(callback));
|
|
260
|
+
}
|
|
261
|
+
equals(other) {
|
|
262
|
+
if (this.dependencies === other) return true;
|
|
263
|
+
if (this.dependencies.size !== other.size) return false;
|
|
264
|
+
for (const item of this.dependencies) if (!other.has(item)) return false;
|
|
265
|
+
return true;
|
|
225
266
|
}
|
|
226
267
|
};
|
|
227
268
|
|
|
228
|
-
// src/internal/
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
effect2.collector = factory("effect.collector");
|
|
248
|
-
|
|
249
|
-
// src/internal/ignore.ts
|
|
250
|
-
function ignore2(callback, ...args) {
|
|
251
|
-
let value;
|
|
252
|
-
ignore2.collector().ignore(() => value = typeof callback === "function" ? callback(...args) : callback.read());
|
|
253
|
-
return value;
|
|
254
|
-
}
|
|
255
|
-
ignore2.collector = factory("signal.collector");
|
|
256
|
-
|
|
257
|
-
// src/internal/shared.ts
|
|
258
|
-
var __batcher__ = null;
|
|
259
|
-
var __collector__ = null;
|
|
260
|
-
var shared = {
|
|
261
|
-
batcher() {
|
|
262
|
-
if (!__batcher__) __batcher__ = collector();
|
|
263
|
-
return __batcher__;
|
|
264
|
-
},
|
|
265
|
-
collector() {
|
|
266
|
-
if (!__collector__) __collector__ = collector();
|
|
267
|
-
return __collector__;
|
|
269
|
+
// src/internal/emitter.ts
|
|
270
|
+
var ScheduledEmitter = class {
|
|
271
|
+
listeners;
|
|
272
|
+
scheduler;
|
|
273
|
+
constructor(scheduler, listeners = /* @__PURE__ */ new Set()) {
|
|
274
|
+
this.scheduler = scheduler;
|
|
275
|
+
this.listeners = listeners;
|
|
276
|
+
}
|
|
277
|
+
emit() {
|
|
278
|
+
this.scheduler.schedule(...this.listeners);
|
|
279
|
+
}
|
|
280
|
+
subscribe(callback) {
|
|
281
|
+
this.listeners.add(callback);
|
|
282
|
+
return () => this.unsubscribe(callback);
|
|
283
|
+
}
|
|
284
|
+
unsubscribe(callback) {
|
|
285
|
+
this.listeners.delete(callback);
|
|
268
286
|
}
|
|
269
287
|
};
|
|
270
288
|
|
|
271
|
-
// src/internal/
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
#listeners;
|
|
281
|
-
#batcher;
|
|
282
|
-
#collector;
|
|
283
|
-
constructor(value, equals, listeners, batcher, collector3) {
|
|
284
|
-
this.#value = value;
|
|
285
|
-
this.#equals = equals;
|
|
286
|
-
this.#listeners = listeners;
|
|
287
|
-
this.#batcher = batcher;
|
|
288
|
-
this.#collector = collector3;
|
|
289
|
+
// src/internal/reactive.ts
|
|
290
|
+
var Reactive = class {
|
|
291
|
+
value;
|
|
292
|
+
tracker;
|
|
293
|
+
subscription;
|
|
294
|
+
constructor(value, tracker, subscription) {
|
|
295
|
+
this.value = value;
|
|
296
|
+
this.tracker = tracker;
|
|
297
|
+
this.subscription = subscription;
|
|
289
298
|
}
|
|
290
299
|
read() {
|
|
291
|
-
this
|
|
300
|
+
this.tracker.track(this);
|
|
292
301
|
return this.peek();
|
|
293
302
|
}
|
|
294
303
|
peek() {
|
|
295
|
-
return this
|
|
304
|
+
return this.value.read();
|
|
296
305
|
}
|
|
297
306
|
write(value) {
|
|
298
|
-
|
|
299
|
-
this.#value = value;
|
|
300
|
-
if (this.#batcher.collecting()) this.#listeners.forEach((listener) => this.#batcher.add(listener));
|
|
301
|
-
else this.#listeners.forEach((listener) => listener());
|
|
307
|
+
return this.value.write(value);
|
|
302
308
|
}
|
|
303
|
-
equals(
|
|
304
|
-
|
|
305
|
-
return this
|
|
309
|
+
equals(...args) {
|
|
310
|
+
const [value, other] = args.length === 1 ? [this.read(), args[0]] : args;
|
|
311
|
+
return this.value.equals(value, other);
|
|
306
312
|
}
|
|
307
313
|
subscribe(callback) {
|
|
308
|
-
this
|
|
309
|
-
return this.unsubscribe.bind(this, callback);
|
|
314
|
+
return this.subscription.subscribe(callback);
|
|
310
315
|
}
|
|
311
316
|
unsubscribe(callback) {
|
|
312
|
-
this
|
|
317
|
+
return this.subscription.unsubscribe(callback);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// src/internal/scheduler.ts
|
|
322
|
+
var CallbackScheduler = class {
|
|
323
|
+
collector;
|
|
324
|
+
add;
|
|
325
|
+
constructor(collector) {
|
|
326
|
+
this.collector = collector;
|
|
327
|
+
this.add = this.collector.add.bind(this.collector);
|
|
328
|
+
}
|
|
329
|
+
schedule(...args) {
|
|
330
|
+
if (args.length === 0) return;
|
|
331
|
+
if (!this.collector.collecting()) return args.forEach(utils_exports.call);
|
|
332
|
+
args.forEach(this.add);
|
|
313
333
|
}
|
|
314
334
|
};
|
|
315
335
|
|
|
316
|
-
// src/
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
336
|
+
// src/internal/value.ts
|
|
337
|
+
var BasicValue = class {
|
|
338
|
+
value;
|
|
339
|
+
comparator;
|
|
340
|
+
constructor(value, comparator2) {
|
|
341
|
+
this.value = value;
|
|
342
|
+
this.comparator = comparator2;
|
|
343
|
+
}
|
|
344
|
+
read() {
|
|
345
|
+
return this.value;
|
|
346
|
+
}
|
|
347
|
+
write(value) {
|
|
348
|
+
if (this.comparator.equals(this.value, value)) return false;
|
|
349
|
+
this.value = value;
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
equals(...args) {
|
|
353
|
+
const [value, other] = args.length === 1 ? [this.read(), args[0]] : args;
|
|
354
|
+
return this.comparator.equals(value, other);
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
var EmittingValue = class {
|
|
358
|
+
value;
|
|
359
|
+
emitter;
|
|
360
|
+
constructor(value, emitter) {
|
|
361
|
+
this.value = value;
|
|
362
|
+
this.emitter = emitter;
|
|
363
|
+
}
|
|
364
|
+
read() {
|
|
365
|
+
return this.value.read();
|
|
366
|
+
}
|
|
367
|
+
write(value) {
|
|
368
|
+
const changed = this.value.write(value);
|
|
369
|
+
if (changed) this.emitter.emit();
|
|
370
|
+
return changed;
|
|
371
|
+
}
|
|
372
|
+
equals(...args) {
|
|
373
|
+
const [value, other] = args.length === 1 ? [this.read(), args[0]] : args;
|
|
374
|
+
return this.value.equals(value, other);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
var ComputedValue = class {
|
|
378
|
+
cache;
|
|
379
|
+
computation;
|
|
380
|
+
dependencies;
|
|
381
|
+
constructor(cache, computation, dependencies) {
|
|
382
|
+
this.cache = cache;
|
|
383
|
+
this.computation = computation;
|
|
384
|
+
this.dependencies = dependencies;
|
|
385
|
+
this.recompute = this.recompute.bind(this);
|
|
386
|
+
}
|
|
387
|
+
read() {
|
|
388
|
+
if (this.dependencies.changed()) this.dependencies.track(this.recompute);
|
|
389
|
+
return this.cache.read();
|
|
390
|
+
}
|
|
391
|
+
recompute() {
|
|
392
|
+
this.cache.write(this.computation.read());
|
|
393
|
+
}
|
|
394
|
+
write(_) {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
equals(...args) {
|
|
398
|
+
const [value, other] = args.length === 1 ? [this.read(), args[0]] : args;
|
|
399
|
+
return this.cache.equals(value, other);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
// src/internal/writable.ts
|
|
404
|
+
var BatchWriter = class {
|
|
405
|
+
writer;
|
|
406
|
+
batcher;
|
|
407
|
+
constructor(writer2, batcher) {
|
|
408
|
+
this.writer = writer2;
|
|
409
|
+
this.batcher = batcher;
|
|
410
|
+
}
|
|
411
|
+
write(value) {
|
|
412
|
+
const result = this.batcher.batch(() => this.writer.write(value));
|
|
413
|
+
if (typeof result === "boolean") return result;
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
// src/app/index.ts
|
|
419
|
+
function configuration() {
|
|
420
|
+
return __configuration__;
|
|
325
421
|
}
|
|
422
|
+
var __configuration__ = {
|
|
423
|
+
signal: utils_exports.factory("@configuration.signal"),
|
|
424
|
+
computed: utils_exports.factory("@configuration.computed"),
|
|
425
|
+
composed: utils_exports.factory("@configuration.composed"),
|
|
426
|
+
batcher: utils_exports.factory("@configuration.batch"),
|
|
427
|
+
ignorer: utils_exports.factory("@configuration.ignore"),
|
|
428
|
+
// Internal
|
|
429
|
+
scheduler: utils_exports.factory("@configuration.scheduler"),
|
|
430
|
+
tracker: utils_exports.factory("@configuration.tracker"),
|
|
431
|
+
collector: utils_exports.factory("@configuration.collector"),
|
|
432
|
+
dependency_tracker: utils_exports.factory("@configuration.dependency.tracker"),
|
|
433
|
+
dependency_collector: utils_exports.factory("@configuration.dependency.collector"),
|
|
434
|
+
callback_collector: utils_exports.factory("@configuration.callback.collector")
|
|
435
|
+
};
|
|
436
|
+
__configuration__.signal.default((value, equals) => {
|
|
437
|
+
const emitter = new internal_exports.ScheduledEmitter(configuration().scheduler());
|
|
438
|
+
const state = new internal_exports.EmittingValue(new internal_exports.BasicValue(value, comparator(equals)), emitter);
|
|
439
|
+
return new internal_exports.Reactive(state, configuration().tracker(), emitter);
|
|
440
|
+
});
|
|
441
|
+
__configuration__.computed.default((compute, equals) => {
|
|
442
|
+
const dependency = configuration().dependency_tracker();
|
|
443
|
+
const cache = new internal_exports.BasicValue(void 0, comparator(equals));
|
|
444
|
+
const value = new internal_exports.ComputedValue(cache, reader(compute), dependency);
|
|
445
|
+
return new internal_exports.Reactive(value, configuration().tracker(), dependency);
|
|
446
|
+
});
|
|
447
|
+
__configuration__.composed.default(
|
|
448
|
+
(value, write) => new internal_exports.Composed(value, new internal_exports.BatchWriter(writer(write), configuration().batcher()))
|
|
449
|
+
);
|
|
450
|
+
__configuration__.batcher.default(utils_exports.singleton(() => new internal_exports.CollectorBasedBatcher(configuration().callback_collector())));
|
|
451
|
+
__configuration__.ignorer.default(configuration().dependency_collector);
|
|
452
|
+
__configuration__.scheduler.default(utils_exports.singleton(() => new internal_exports.CallbackScheduler(configuration().callback_collector())));
|
|
453
|
+
__configuration__.collector.default(configuration().dependency_collector);
|
|
454
|
+
__configuration__.tracker.default(configuration().dependency_collector);
|
|
455
|
+
__configuration__.dependency_tracker.default(() => new internal_exports.DependencyManager(configuration().collector()));
|
|
456
|
+
__configuration__.dependency_collector.default(utils_exports.singleton(() => new internal_exports.CollectorController()));
|
|
457
|
+
__configuration__.callback_collector.default(utils_exports.singleton(() => new internal_exports.CollectorController()));
|
|
326
458
|
|
|
327
|
-
// src/
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
computed
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
ignore.
|
|
459
|
+
// src/core/batch.ts
|
|
460
|
+
function batch(callback, ...args) {
|
|
461
|
+
return configuration().batcher().batch(callback, ...args);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// src/core/composed.ts
|
|
465
|
+
function composed(value, write) {
|
|
466
|
+
return configuration().composed(value, write);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// src/core/computed.ts
|
|
470
|
+
function computed(compute, equals) {
|
|
471
|
+
return configuration().computed(compute, equals);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// src/core/ignore.ts
|
|
475
|
+
function ignore(callback, ...args) {
|
|
476
|
+
return configuration().ignorer().ignore(callback, ...args);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// src/core/signal.ts
|
|
480
|
+
function signal(value, equals) {
|
|
481
|
+
return configuration().signal(value, equals);
|
|
482
|
+
}
|
|
343
483
|
// Annotate the CommonJS export names for ESM import in node:
|
|
344
484
|
0 && (module.exports = {
|
|
485
|
+
Equals,
|
|
486
|
+
Internal,
|
|
345
487
|
batch,
|
|
346
|
-
|
|
488
|
+
comparator,
|
|
347
489
|
composed,
|
|
348
490
|
computed,
|
|
349
|
-
|
|
350
|
-
effect,
|
|
351
|
-
factory,
|
|
491
|
+
configuration,
|
|
352
492
|
ignore,
|
|
353
|
-
|
|
354
|
-
signal
|
|
493
|
+
reader,
|
|
494
|
+
signal,
|
|
495
|
+
utils,
|
|
496
|
+
writer
|
|
355
497
|
});
|