@async/framework 0.2.2 → 0.3.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/src/signals.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { asyncSignal as createAsyncSignal, isAsyncSignal } from "./async-signal.js";
2
+ import { attachRegistryInspection, createRegistryStore } from "./registry-store.js";
2
3
 
3
4
  const signalKind = Symbol.for("@async/framework.signal");
4
5
  const computedKind = Symbol.for("@async/framework.computed");
@@ -116,12 +117,15 @@ export function effect(fn) {
116
117
  };
117
118
  }
118
119
 
119
- export function createSignalRegistry(initialMap = {}) {
120
- const entries = new Map();
120
+ export function createSignalRegistry(initialMap = {}, options = {}) {
121
+ const registryStore = options.registry ?? createRegistryStore();
122
+ const type = options.type ?? "signal";
123
+ const entries = registryStore._map(type);
121
124
  const registryCleanups = new Set();
122
125
  const runtimeContext = {};
126
+ const boundEntries = new Set();
123
127
 
124
- const registry = {
128
+ const registry = attachRegistryInspection({
125
129
  register(id, signalLike) {
126
130
  assertId(id);
127
131
  if (entries.has(id)) {
@@ -129,12 +133,7 @@ export function createSignalRegistry(initialMap = {}) {
129
133
  }
130
134
  const entry = normalizeSignal(signalLike);
131
135
  entries.set(id, entry);
132
- if (typeof entry._bindRegistry === "function") {
133
- const cleanup = entry._bindRegistry(registry, id);
134
- if (typeof cleanup === "function") {
135
- registryCleanups.add(cleanup);
136
- }
137
- }
136
+ bindEntry(id, entry);
138
137
  return registry.ref(id);
139
138
  },
140
139
 
@@ -289,11 +288,34 @@ export function createSignalRegistry(initialMap = {}) {
289
288
 
290
289
  _context() {
291
290
  return runtimeContext;
291
+ },
292
+
293
+ _adoptMany(map = {}) {
294
+ for (const id of Object.keys(map ?? {})) {
295
+ if (entries.has(id)) {
296
+ bindEntry(id, entries.get(id));
297
+ }
298
+ }
299
+ return registry;
292
300
  }
293
- };
301
+ }, registryStore, type);
294
302
 
303
+ for (const [id, entry] of entries) {
304
+ bindEntry(id, entry);
305
+ }
295
306
  registry.registerMany(initialMap);
296
307
  return registry;
308
+
309
+ function bindEntry(id, entry) {
310
+ if (boundEntries.has(id) || typeof entry?._bindRegistry !== "function") {
311
+ return;
312
+ }
313
+ boundEntries.add(id);
314
+ const cleanup = entry._bindRegistry(registry, id);
315
+ if (typeof cleanup === "function") {
316
+ registryCleanups.add(cleanup);
317
+ }
318
+ }
297
319
  }
298
320
 
299
321
  function normalizeSignal(signalLike) {