@valentin30/signal 0.0.9 → 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/dist/index.d.mts +108 -115
- package/dist/index.d.ts +108 -115
- package/dist/index.js +202 -185
- package/dist/index.mjs +202 -185
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -76,163 +76,170 @@ var signal = factory("signal");
|
|
|
76
76
|
// src/internal/index.ts
|
|
77
77
|
var internal_exports = {};
|
|
78
78
|
__export(internal_exports, {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
effect: () => effect2,
|
|
88
|
-
ignore: () => ignore2,
|
|
89
|
-
shared: () => shared,
|
|
90
|
-
signal: () => signal2
|
|
79
|
+
batch: () => internal_batch,
|
|
80
|
+
collector: () => internal_collector,
|
|
81
|
+
composed: () => internal_composed,
|
|
82
|
+
computed: () => internal_computed,
|
|
83
|
+
effect: () => internal_effect,
|
|
84
|
+
ignore: () => internal_ignore,
|
|
85
|
+
shared: () => internal_shared,
|
|
86
|
+
signal: () => internal_signal
|
|
91
87
|
});
|
|
92
88
|
|
|
93
89
|
// src/internal/batch.ts
|
|
94
|
-
function
|
|
95
|
-
if (
|
|
96
|
-
|
|
90
|
+
function internal_batch(callback, collector2 = internal_batch.collector()) {
|
|
91
|
+
if (collector2.collecting()) return callback();
|
|
92
|
+
collector2.collect(callback).forEach((listener) => listener());
|
|
97
93
|
}
|
|
98
|
-
|
|
94
|
+
((internal_batch2) => {
|
|
95
|
+
internal_batch2.collector = factory("batch.collector");
|
|
96
|
+
})(internal_batch || (internal_batch = {}));
|
|
99
97
|
|
|
100
98
|
// src/internal/collector.ts
|
|
101
|
-
function
|
|
102
|
-
return new
|
|
99
|
+
function internal_collector() {
|
|
100
|
+
return new internal_collector.Constructor(null);
|
|
103
101
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
((internal_collector2) => {
|
|
103
|
+
class Constructor {
|
|
104
|
+
#values;
|
|
105
|
+
constructor(values) {
|
|
106
|
+
this.#values = values;
|
|
107
|
+
}
|
|
108
|
+
collecting() {
|
|
109
|
+
return this.#values !== null;
|
|
110
|
+
}
|
|
111
|
+
add(value) {
|
|
112
|
+
if (!this.collecting() || !this.#values) return;
|
|
113
|
+
this.#values.add(value);
|
|
114
|
+
}
|
|
115
|
+
collect(callback) {
|
|
116
|
+
const current = this.#values;
|
|
117
|
+
this.#values = /* @__PURE__ */ new Set();
|
|
118
|
+
callback();
|
|
119
|
+
const collected = this.#values;
|
|
120
|
+
this.#values = current;
|
|
121
|
+
return collected;
|
|
122
|
+
}
|
|
123
|
+
ignore(callback) {
|
|
124
|
+
const current = this.#values;
|
|
125
|
+
this.#values = null;
|
|
126
|
+
callback();
|
|
127
|
+
this.#values = current;
|
|
128
|
+
}
|
|
108
129
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
add(value) {
|
|
113
|
-
if (!this.collecting() || !this.#values) return;
|
|
114
|
-
this.#values.add(value);
|
|
115
|
-
}
|
|
116
|
-
collect(callback) {
|
|
117
|
-
const current = this.#values;
|
|
118
|
-
this.#values = /* @__PURE__ */ new Set();
|
|
119
|
-
callback();
|
|
120
|
-
const collected = this.#values;
|
|
121
|
-
this.#values = current;
|
|
122
|
-
return collected;
|
|
123
|
-
}
|
|
124
|
-
ignore(callback) {
|
|
125
|
-
const current = this.#values;
|
|
126
|
-
this.#values = null;
|
|
127
|
-
callback();
|
|
128
|
-
this.#values = current;
|
|
129
|
-
}
|
|
130
|
-
};
|
|
130
|
+
internal_collector2.Constructor = Constructor;
|
|
131
|
+
})(internal_collector || (internal_collector = {}));
|
|
131
132
|
|
|
132
133
|
// src/internal/computed.ts
|
|
133
|
-
function
|
|
134
|
-
return new
|
|
134
|
+
function internal_computed(compute, equals) {
|
|
135
|
+
return new internal_computed.Constructor(
|
|
135
136
|
true,
|
|
136
137
|
void 0,
|
|
137
138
|
[],
|
|
138
139
|
/* @__PURE__ */ new Set(),
|
|
139
140
|
/* @__PURE__ */ new Set(),
|
|
140
|
-
|
|
141
|
+
internal_computed.collector(),
|
|
141
142
|
compute,
|
|
142
143
|
equals
|
|
143
144
|
);
|
|
144
145
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
146
|
+
((internal_computed2) => {
|
|
147
|
+
internal_computed2.collector = factory("computed.collector");
|
|
148
|
+
class Constructor {
|
|
149
|
+
#empty;
|
|
150
|
+
#value;
|
|
151
|
+
#values;
|
|
152
|
+
#dependencies;
|
|
153
|
+
#compute;
|
|
154
|
+
#equals;
|
|
155
|
+
#listeners;
|
|
156
|
+
#collector;
|
|
157
|
+
constructor(empty, value, values, dependencies, listeners, collector3, compute, equals) {
|
|
158
|
+
this.#empty = empty;
|
|
159
|
+
this.#value = value;
|
|
160
|
+
this.#values = values;
|
|
161
|
+
this.#dependencies = dependencies;
|
|
162
|
+
this.#listeners = listeners;
|
|
163
|
+
this.#collector = collector3;
|
|
164
|
+
this.#compute = compute;
|
|
165
|
+
this.#equals = equals;
|
|
166
|
+
}
|
|
167
|
+
read() {
|
|
168
|
+
this.#collector.add(this);
|
|
169
|
+
return this.peek();
|
|
170
|
+
}
|
|
171
|
+
peek() {
|
|
172
|
+
if (!this.#empty && !this.#values.some(([dep, value]) => !dep.equals(value))) return this.#value;
|
|
173
|
+
const current = this.#dependencies;
|
|
174
|
+
const next = this.#collector.collect(() => this.#value = this.#compute());
|
|
175
|
+
this.#dependencies = next;
|
|
176
|
+
this.#values = Array.from(next).map((dep) => [dep, dep.read()]);
|
|
177
|
+
this.#empty = false;
|
|
178
|
+
if (!current.size && !next.size) return this.#value;
|
|
179
|
+
if (!this.#listeners.size) return this.#value;
|
|
180
|
+
current.forEach((dep) => !next.has(dep) && this.#listeners.forEach((listener) => dep.unsubscribe(listener)));
|
|
181
|
+
next.forEach((dep) => !current.has(dep) && this.#listeners.forEach((listener) => dep.subscribe(listener)));
|
|
182
|
+
return this.#value;
|
|
183
|
+
}
|
|
184
|
+
equals(other) {
|
|
185
|
+
if (this.#equals) return this.#equals(this.read(), other);
|
|
186
|
+
return this.read() === other;
|
|
187
|
+
}
|
|
188
|
+
subscribe(callback) {
|
|
189
|
+
this.#listeners.add(callback);
|
|
190
|
+
this.#dependencies.forEach((dep) => dep.subscribe(callback));
|
|
191
|
+
return this.unsubscribe.bind(this, callback);
|
|
192
|
+
}
|
|
193
|
+
unsubscribe(callback) {
|
|
194
|
+
this.#listeners.delete(callback);
|
|
195
|
+
this.#dependencies.forEach((dep) => dep.unsubscribe(callback));
|
|
196
|
+
}
|
|
168
197
|
}
|
|
169
|
-
|
|
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;
|
|
181
|
-
}
|
|
182
|
-
equals(other) {
|
|
183
|
-
if (this.#equals) return this.#equals(this.read(), other);
|
|
184
|
-
return this.read() === other;
|
|
185
|
-
}
|
|
186
|
-
subscribe(callback) {
|
|
187
|
-
this.#listeners.add(callback);
|
|
188
|
-
this.#dependencies.forEach((dep) => dep.subscribe(callback));
|
|
189
|
-
return this.unsubscribe.bind(this, callback);
|
|
190
|
-
}
|
|
191
|
-
unsubscribe(callback) {
|
|
192
|
-
this.#listeners.delete(callback);
|
|
193
|
-
this.#dependencies.forEach((dep) => dep.unsubscribe(callback));
|
|
194
|
-
}
|
|
195
|
-
};
|
|
198
|
+
internal_computed2.Constructor = Constructor;
|
|
199
|
+
})(internal_computed || (internal_computed = {}));
|
|
196
200
|
|
|
197
201
|
// src/internal/composed.ts
|
|
198
|
-
function
|
|
199
|
-
return new
|
|
202
|
+
function internal_composed(compute, write, equals) {
|
|
203
|
+
return new internal_composed.Constructor(
|
|
200
204
|
true,
|
|
201
205
|
void 0,
|
|
202
206
|
[],
|
|
203
207
|
/* @__PURE__ */ new Set(),
|
|
204
208
|
/* @__PURE__ */ new Set(),
|
|
205
|
-
|
|
206
|
-
|
|
209
|
+
internal_composed.batcher(),
|
|
210
|
+
internal_composed.collector(),
|
|
207
211
|
compute,
|
|
208
212
|
write,
|
|
209
213
|
equals
|
|
210
214
|
);
|
|
211
215
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
216
|
+
((internal_composed2) => {
|
|
217
|
+
internal_composed2.batcher = factory("composed.batcher");
|
|
218
|
+
internal_composed2.collector = factory("composed.collector");
|
|
219
|
+
class Constructor extends internal_computed.Constructor {
|
|
220
|
+
#batcher;
|
|
221
|
+
#write;
|
|
222
|
+
constructor(empty, value, values, dependencies, listeners, batcher2, collector3, compute, write, equals) {
|
|
223
|
+
super(empty, value, values, dependencies, listeners, collector3, compute, equals);
|
|
224
|
+
this.#batcher = batcher2;
|
|
225
|
+
this.#write = write;
|
|
226
|
+
}
|
|
227
|
+
write(value) {
|
|
228
|
+
if (this.equals(value)) return;
|
|
229
|
+
batch(() => this.#write(value), this.#batcher);
|
|
230
|
+
}
|
|
221
231
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
batch(() => this.#write(value), this.#batcher);
|
|
225
|
-
}
|
|
226
|
-
};
|
|
232
|
+
internal_composed2.Constructor = Constructor;
|
|
233
|
+
})(internal_composed || (internal_composed = {}));
|
|
227
234
|
|
|
228
235
|
// src/internal/effect.ts
|
|
229
|
-
function
|
|
236
|
+
function internal_effect(cb) {
|
|
230
237
|
let cleanupFn = null;
|
|
231
|
-
let dependencies =
|
|
238
|
+
let dependencies = internal_effect.collector().collect(() => cleanupFn = cb(true) ?? null);
|
|
232
239
|
dependencies.forEach((dependency) => dependency.subscribe(callback));
|
|
233
240
|
function callback() {
|
|
234
241
|
if (cleanupFn) cleanupFn(false);
|
|
235
|
-
const next =
|
|
242
|
+
const next = internal_effect.collector().collect(() => cleanupFn = cb(false) ?? null);
|
|
236
243
|
next.forEach((dependency) => !dependencies.has(dependency) && dependency.subscribe(callback));
|
|
237
244
|
dependencies.forEach((dependency) => !next.has(dependency) && dependency.unsubscribe(callback));
|
|
238
245
|
dependencies = next;
|
|
@@ -244,74 +251,84 @@ function effect2(cb) {
|
|
|
244
251
|
cleanupFn = null;
|
|
245
252
|
};
|
|
246
253
|
}
|
|
247
|
-
|
|
254
|
+
((internal_effect2) => {
|
|
255
|
+
internal_effect2.collector = factory("effect.collector");
|
|
256
|
+
})(internal_effect || (internal_effect = {}));
|
|
248
257
|
|
|
249
258
|
// src/internal/ignore.ts
|
|
250
|
-
function
|
|
259
|
+
function internal_ignore(callback, ...args) {
|
|
251
260
|
let value;
|
|
252
|
-
|
|
261
|
+
internal_ignore.collector().ignore(() => value = typeof callback === "function" ? callback(...args) : callback.read());
|
|
253
262
|
return value;
|
|
254
263
|
}
|
|
255
|
-
|
|
264
|
+
((internal_ignore2) => {
|
|
265
|
+
internal_ignore2.collector = factory("signal.collector");
|
|
266
|
+
})(internal_ignore || (internal_ignore = {}));
|
|
256
267
|
|
|
257
268
|
// src/internal/shared.ts
|
|
258
|
-
var
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
269
|
+
var internal_shared;
|
|
270
|
+
((internal_shared2) => {
|
|
271
|
+
let __batcher__ = null;
|
|
272
|
+
let __collector__ = null;
|
|
273
|
+
function batcher() {
|
|
262
274
|
if (!__batcher__) __batcher__ = collector();
|
|
263
275
|
return __batcher__;
|
|
264
|
-
}
|
|
265
|
-
|
|
276
|
+
}
|
|
277
|
+
internal_shared2.batcher = batcher;
|
|
278
|
+
function collector2() {
|
|
266
279
|
if (!__collector__) __collector__ = collector();
|
|
267
280
|
return __collector__;
|
|
268
281
|
}
|
|
269
|
-
|
|
282
|
+
internal_shared2.collector = collector2;
|
|
283
|
+
})(internal_shared || (internal_shared = {}));
|
|
270
284
|
|
|
271
285
|
// src/internal/signal.ts
|
|
272
|
-
function
|
|
273
|
-
return new
|
|
286
|
+
function internal_signal(value, equals) {
|
|
287
|
+
return new internal_signal.Constructor(value, equals, /* @__PURE__ */ new Set(), internal_signal.batcher(), internal_signal.collector());
|
|
274
288
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
289
|
+
((internal_signal2) => {
|
|
290
|
+
internal_signal2.batcher = factory("signal.batcher");
|
|
291
|
+
internal_signal2.collector = factory("signal.collector");
|
|
292
|
+
class Constructor {
|
|
293
|
+
#value;
|
|
294
|
+
#equals;
|
|
295
|
+
#listeners;
|
|
296
|
+
#batcher;
|
|
297
|
+
#collector;
|
|
298
|
+
constructor(value, equals, listeners, batcher2, collector3) {
|
|
299
|
+
this.#value = value;
|
|
300
|
+
this.#equals = equals;
|
|
301
|
+
this.#listeners = listeners;
|
|
302
|
+
this.#batcher = batcher2;
|
|
303
|
+
this.#collector = collector3;
|
|
304
|
+
}
|
|
305
|
+
read() {
|
|
306
|
+
this.#collector.add(this);
|
|
307
|
+
return this.peek();
|
|
308
|
+
}
|
|
309
|
+
peek() {
|
|
310
|
+
return this.#value;
|
|
311
|
+
}
|
|
312
|
+
write(value) {
|
|
313
|
+
if (this.equals(value)) return;
|
|
314
|
+
this.#value = value;
|
|
315
|
+
if (this.#batcher.collecting()) this.#listeners.forEach((listener) => this.#batcher.add(listener));
|
|
316
|
+
else this.#listeners.forEach((listener) => listener());
|
|
317
|
+
}
|
|
318
|
+
equals(other) {
|
|
319
|
+
if (this.#equals) return this.#equals(this.#value, other);
|
|
320
|
+
return this.#value === other;
|
|
321
|
+
}
|
|
322
|
+
subscribe(callback) {
|
|
323
|
+
this.#listeners.add(callback);
|
|
324
|
+
return this.unsubscribe.bind(this, callback);
|
|
325
|
+
}
|
|
326
|
+
unsubscribe(callback) {
|
|
327
|
+
this.#listeners.delete(callback);
|
|
328
|
+
}
|
|
296
329
|
}
|
|
297
|
-
|
|
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());
|
|
302
|
-
}
|
|
303
|
-
equals(other) {
|
|
304
|
-
if (this.#equals) return this.#equals(this.#value, other);
|
|
305
|
-
return this.#value === other;
|
|
306
|
-
}
|
|
307
|
-
subscribe(callback) {
|
|
308
|
-
this.#listeners.add(callback);
|
|
309
|
-
return this.unsubscribe.bind(this, callback);
|
|
310
|
-
}
|
|
311
|
-
unsubscribe(callback) {
|
|
312
|
-
this.#listeners.delete(callback);
|
|
313
|
-
}
|
|
314
|
-
};
|
|
330
|
+
internal_signal2.Constructor = Constructor;
|
|
331
|
+
})(internal_signal || (internal_signal = {}));
|
|
315
332
|
|
|
316
333
|
// src/core/config.ts
|
|
317
334
|
function config(config2 = {}) {
|
|
@@ -325,21 +342,21 @@ function config(config2 = {}) {
|
|
|
325
342
|
}
|
|
326
343
|
|
|
327
344
|
// src/index.ts
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
collector.default(
|
|
337
|
-
signal.default(
|
|
338
|
-
computed.default(
|
|
339
|
-
composed.default(
|
|
340
|
-
effect.default(
|
|
341
|
-
batch.default(
|
|
342
|
-
ignore.default(
|
|
345
|
+
internal_signal.collector.default(internal_shared.collector);
|
|
346
|
+
internal_computed.collector.default(internal_shared.collector);
|
|
347
|
+
internal_composed.collector.default(internal_shared.collector);
|
|
348
|
+
internal_effect.collector.default(internal_shared.collector);
|
|
349
|
+
internal_ignore.collector.default(internal_shared.collector);
|
|
350
|
+
internal_signal.batcher.default(internal_shared.batcher);
|
|
351
|
+
internal_composed.batcher.default(internal_shared.batcher);
|
|
352
|
+
internal_batch.collector.default(internal_shared.batcher);
|
|
353
|
+
collector.default(internal_collector);
|
|
354
|
+
signal.default(internal_signal);
|
|
355
|
+
computed.default(internal_computed);
|
|
356
|
+
composed.default(internal_composed);
|
|
357
|
+
effect.default(internal_effect);
|
|
358
|
+
batch.default(internal_batch);
|
|
359
|
+
ignore.default(internal_ignore);
|
|
343
360
|
// Annotate the CommonJS export names for ESM import in node:
|
|
344
361
|
0 && (module.exports = {
|
|
345
362
|
batch,
|