@weasel-js/registry 1.9.3 → 1.9.4

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.ts CHANGED
@@ -63,4 +63,32 @@ interface Reflectable<V, K = string> extends Reflection<V, K> {
63
63
  */
64
64
  declare function createReflectable<V, K = string>(): Reflectable<V, K>;
65
65
 
66
- export { type Reflectable, type ReflectedEntry, type Reflection, type RegisterOptions, type Registrant, createReflectable };
66
+ /** A set of listeners and the one way the kit notifies them. See {@link createEmitter}. */
67
+ interface Emitter<TArgs extends unknown[] = []> {
68
+ /** Add `listener`; a listener already held is held once. Returns the unsubscribe. */
69
+ subscribe(listener: (...args: TArgs) => void): () => void;
70
+ /** Call every listener held when the call began. */
71
+ emit(...args: TArgs): void;
72
+ /** How many listeners are held. */
73
+ readonly size: number;
74
+ }
75
+ /**
76
+ * A subscribe/notify pair with stated semantics, for a store to build on
77
+ * instead of keeping its own listener set:
78
+ *
79
+ * - `emit` walks the listeners held when it began, in subscription order. One
80
+ * that subscribes during a pass waits for the next; one unsubscribed before
81
+ * its turn is not called.
82
+ * - A listener that throws does not stop the others, and does not throw into
83
+ * whatever caused the notify: the error goes to `console.error`.
84
+ */
85
+ declare function createEmitter<TArgs extends unknown[] = []>(): Emitter<TArgs>;
86
+
87
+ /** Whether `proto` is the prototype a plain object has: `null`, or some
88
+ * realm's `Object.prototype`. Comparing against this realm's alone misses
89
+ * objects made in an iframe, a VM context, or by `structuredClone` under jsdom. */
90
+ declare function isPlainPrototype(proto: object | null): boolean;
91
+ /** Whether `value` is a plain object — a literal or `Object.create(null)` — from any realm. */
92
+ declare function isPlainObject(value: unknown): value is Record<string, unknown>;
93
+
94
+ export { type Emitter, type Reflectable, type ReflectedEntry, type Reflection, type RegisterOptions, type Registrant, createEmitter, createReflectable, isPlainObject, isPlainPrototype };
package/dist/index.js CHANGED
@@ -1,18 +1,38 @@
1
+ // src/emitter.ts
2
+ function createEmitter() {
3
+ const listeners = /* @__PURE__ */ new Set();
4
+ return {
5
+ subscribe(listener) {
6
+ listeners.add(listener);
7
+ return () => {
8
+ listeners.delete(listener);
9
+ };
10
+ },
11
+ emit(...args) {
12
+ for (const listener of [...listeners]) {
13
+ if (!listeners.has(listener)) continue;
14
+ try {
15
+ listener(...args);
16
+ } catch (err) {
17
+ console.error("weasel: a subscriber threw", err);
18
+ }
19
+ }
20
+ },
21
+ get size() {
22
+ return listeners.size;
23
+ }
24
+ };
25
+ }
26
+
1
27
  // src/reflectable.ts
2
28
  function createReflectable() {
3
29
  const stacks = /* @__PURE__ */ new Map();
4
- const listeners = /* @__PURE__ */ new Set();
30
+ const listeners = createEmitter();
5
31
  let version = 0;
6
32
  let snapshot = null;
7
33
  function changed() {
8
34
  version++;
9
- for (const listener of listeners) {
10
- try {
11
- listener();
12
- } catch (err) {
13
- console.error("weasel registry: subscriber threw", err);
14
- }
15
- }
35
+ listeners.emit();
16
36
  }
17
37
  const reflection = {
18
38
  get: (key) => stacks.get(key)?.at(-1)?.value,
@@ -32,12 +52,7 @@ function createReflectable() {
32
52
  snapshot = { version, entries: Object.freeze(entries) };
33
53
  return snapshot.entries;
34
54
  },
35
- subscribe(listener) {
36
- listeners.add(listener);
37
- return () => {
38
- listeners.delete(listener);
39
- };
40
- },
55
+ subscribe: listeners.subscribe,
41
56
  getVersion: () => version
42
57
  };
43
58
  return {
@@ -82,6 +97,14 @@ function createReflectable() {
82
97
  };
83
98
  }
84
99
 
85
- export { createReflectable };
100
+ // src/isPlainObject.ts
101
+ function isPlainPrototype(proto) {
102
+ return proto === Object.prototype || proto === null || Object.getPrototypeOf(proto) === null;
103
+ }
104
+ function isPlainObject(value) {
105
+ return typeof value === "object" && value !== null && isPlainPrototype(Object.getPrototypeOf(value));
106
+ }
107
+
108
+ export { createEmitter, createReflectable, isPlainObject, isPlainPrototype };
86
109
  //# sourceMappingURL=index.js.map
87
110
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 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,MAAA,KAAW,CAAA,EAAG,MAAA,CAAO,OAAO,GAAG,CAAA;AACxC,QAAA,OAAA,EAAQ;AAAA,MACV,CAAA;AAAA,IACF,CAAA;AAAA,IACA,GAAA,CAAI,GAAA,EAAK,KAAA,EAAO,OAAA,EAAS;AACvB,MAAA,MAAM,UAAA,GAA4B,OAAO,MAAA,CAAO,EAAE,OAAO,MAAA,EAAQ,OAAA,EAAS,QAAQ,CAAA;AAClF,MAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,GAAA,CAAI,GAAG,CAAA;AAC5B,MAAA,IAAI,KAAA,EAAO,KAAA,CAAM,KAAA,CAAM,MAAA,GAAS,CAAC,CAAA,GAAI,UAAA;AAAA,WAChC,MAAA,CAAO,GAAA,CAAI,GAAA,EAAK,CAAC,UAAU,CAAC,CAAA;AACjC,MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAAA,IACA,OAAO,GAAA,EAAK;AACV,MAAA,IAAI,CAAC,MAAA,CAAO,MAAA,CAAO,GAAG,GAAG,OAAO,KAAA;AAChC,MAAA,OAAA,EAAQ;AACR,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,KAAA,GAAQ;AACN,MAAA,IAAI,MAAA,CAAO,SAAS,CAAA,EAAG;AACvB,MAAA,MAAA,CAAO,KAAA,EAAM;AACb,MAAA,OAAA,EAAQ;AAAA,IACV,CAAA;AAAA,IACA,IAAA,EAAM;AAAA,GACR;AACF","file":"index.js","sourcesContent":["/** One registrant of a key: its value and, when the registrant named one, where it came from. */\nexport interface Registrant<V> {\n readonly value: V;\n readonly source: string | undefined;\n}\n\n/** One key as a reflection reports it. */\nexport interface ReflectedEntry<V, K = string> extends Registrant<V> {\n readonly key: K;\n /** The registrants this one displaced, oldest first. Non-empty means two\n * registrants claimed one key — a conflict, resolved newest-wins. */\n readonly shadowed: readonly Registrant<V>[];\n}\n\n/**\n * The read-only face of a registry: what is registered, what displaced what,\n * and a way to hear about changes. Every kit registry that embeds a\n * {@link Reflectable} hands this out, so a debug overlay or a conflict check\n * can walk any of them the same way.\n *\n * `subscribe` and `getVersion` are shaped for `useSyncExternalStore`; so is\n * `entries`, which returns the same frozen array until the next change.\n */\nexport interface Reflection<V, K = string> {\n get(key: K): V | undefined;\n has(key: K): boolean;\n /** Every key's live registrant, in the order each key was first registered. */\n entries(): readonly ReflectedEntry<V, K>[];\n /** Called after every change. Returns the unsubscribe. */\n subscribe(listener: () => void): () => void;\n /** Advances on every change. */\n getVersion(): number;\n}\n\n/** Options for {@link Reflectable.push} and {@link Reflectable.set}. */\nexport interface RegisterOptions {\n /** Who registered this — `'kit'`, a package, a plugin id. Reported, never interpreted. */\n source?: string;\n}\n\n/**\n * A keyed store that keeps its own {@link Reflection}. The registry owning it\n * writes through `push` / `set` / `delete` and hands out `reflection`.\n */\nexport interface Reflectable<V, K = string> extends Reflection<V, K> {\n /** Register `value` over whatever `key` holds and return its release.\n * Registrants stack, newest live: releasing the live one uncovers the one\n * it displaced, and releasing a displaced one removes only it. Double-release safe. */\n push(key: K, value: V, options?: RegisterOptions): () => void;\n /** Replace `key`'s live registrant in place, the way `Map.set` does — for\n * registries where a second registration means \"newer version\", not\n * \"override\". Records no conflict. */\n set(key: K, value: V, options?: RegisterOptions): void;\n /** Drop every registrant of `key`. Returns whether there was one. */\n delete(key: K): boolean;\n clear(): void;\n /** Announce a change the store cannot see — a registered value mutated in place. */\n bump(): void;\n /** This store as a {@link Reflection}, with no write methods on it. */\n readonly reflection: Reflection<V, K>;\n}\n\n/**\n * Create a keyed store with a uniform reflection surface.\n *\n * Deliberately only the cross-cutting part of a registry: validation, lookup\n * fallbacks and lifecycle stay with the registry that embeds it.\n */\nexport function createReflectable<V, K = string>(): Reflectable<V, K> {\n const stacks = new Map<K, Registrant<V>[]>();\n const listeners = new Set<() => void>();\n let version = 0;\n let snapshot: { version: number; entries: readonly ReflectedEntry<V, K>[] } | null = null;\n\n function changed(): void {\n version++;\n for (const listener of listeners) {\n try {\n listener();\n } catch (err) {\n console.error('weasel registry: subscriber threw', err);\n }\n }\n }\n\n const reflection: Reflection<V, K> = {\n get: (key) => stacks.get(key)?.at(-1)?.value,\n has: (key) => stacks.has(key),\n entries() {\n if (snapshot?.version === version) return snapshot.entries;\n const entries: ReflectedEntry<V, K>[] = [];\n for (const [key, stack] of stacks) {\n const live = stack.at(-1)!;\n entries.push(Object.freeze({\n key, value: live.value, source: live.source, shadowed: Object.freeze(stack.slice(0, -1)),\n }));\n }\n snapshot = { version, entries: Object.freeze(entries) };\n return snapshot.entries;\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n getVersion: () => version,\n };\n\n return {\n ...reflection,\n reflection,\n push(key, value, options) {\n const registrant: Registrant<V> = Object.freeze({ value, source: options?.source });\n const stack = stacks.get(key);\n if (stack) stack.push(registrant);\n else stacks.set(key, [registrant]);\n changed();\n let released = false;\n return () => {\n if (released) return;\n released = true;\n const cur = stacks.get(key);\n const i = cur ? cur.lastIndexOf(registrant) : -1;\n if (i === -1) return;\n cur!.splice(i, 1);\n if (cur!.length === 0) stacks.delete(key);\n changed();\n };\n },\n set(key, value, options) {\n const registrant: Registrant<V> = Object.freeze({ value, source: options?.source });\n const stack = stacks.get(key);\n if (stack) stack[stack.length - 1] = registrant;\n else stacks.set(key, [registrant]);\n changed();\n },\n delete(key) {\n if (!stacks.delete(key)) return false;\n changed();\n return true;\n },\n clear() {\n if (stacks.size === 0) return;\n stacks.clear();\n changed();\n },\n bump: changed,\n };\n}\n"]}
1
+ {"version":3,"sources":["../src/emitter.ts","../src/reflectable.ts","../src/isPlainObject.ts"],"names":[],"mappings":";AAoBO,SAAS,aAAA,GAA8D;AAC5E,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAA8B;AACpD,EAAA,OAAO;AAAA,IACL,UAAU,QAAA,EAAU;AAClB,MAAA,SAAA,CAAU,IAAI,QAAQ,CAAA;AACtB,MAAA,OAAO,MAAM;AAAE,QAAA,SAAA,CAAU,OAAO,QAA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EAAA,OAAO,OAAO,UAAU,QAAA,IAAY,KAAA,KAAU,QAAQ,gBAAA,CAAiB,MAAA,CAAO,cAAA,CAAe,KAAK,CAAC,CAAA;AACrG","file":"index.js","sourcesContent":["/** A set of listeners and the one way the kit notifies them. See {@link createEmitter}. */\nexport interface Emitter<TArgs extends unknown[] = []> {\n /** Add `listener`; a listener already held is held once. Returns the unsubscribe. */\n subscribe(listener: (...args: TArgs) => void): () => void;\n /** Call every listener held when the call began. */\n emit(...args: TArgs): void;\n /** How many listeners are held. */\n readonly size: number;\n}\n\n/**\n * A subscribe/notify pair with stated semantics, for a store to build on\n * instead of keeping its own listener set:\n *\n * - `emit` walks the listeners held when it began, in subscription order. One\n * that subscribes during a pass waits for the next; one unsubscribed before\n * its turn is not called.\n * - A listener that throws does not stop the others, and does not throw into\n * whatever caused the notify: the error goes to `console.error`.\n */\nexport function createEmitter<TArgs extends unknown[] = []>(): Emitter<TArgs> {\n const listeners = new Set<(...args: TArgs) => void>();\n return {\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n emit(...args) {\n for (const listener of [...listeners]) {\n if (!listeners.has(listener)) continue;\n try {\n listener(...args);\n } catch (err) {\n console.error('weasel: a subscriber threw', err);\n }\n }\n },\n get size() {\n return listeners.size;\n },\n };\n}\n","import { createEmitter } from './emitter';\n\n/** One registrant of a key: its value and, when the registrant named one, where it came from. */\nexport interface Registrant<V> {\n readonly value: V;\n readonly source: string | undefined;\n}\n\n/** One key as a reflection reports it. */\nexport interface ReflectedEntry<V, K = string> extends Registrant<V> {\n readonly key: K;\n /** The registrants this one displaced, oldest first. Non-empty means two\n * registrants claimed one key — a conflict, resolved newest-wins. */\n readonly shadowed: readonly Registrant<V>[];\n}\n\n/**\n * The read-only face of a registry: what is registered, what displaced what,\n * and a way to hear about changes. Every kit registry that embeds a\n * {@link Reflectable} hands this out, so a debug overlay or a conflict check\n * can walk any of them the same way.\n *\n * `subscribe` and `getVersion` are shaped for `useSyncExternalStore`; so is\n * `entries`, which returns the same frozen array until the next change.\n */\nexport interface Reflection<V, K = string> {\n get(key: K): V | undefined;\n has(key: K): boolean;\n /** Every key's live registrant, in the order each key was first registered. */\n entries(): readonly ReflectedEntry<V, K>[];\n /** Called after every change. Returns the unsubscribe. */\n subscribe(listener: () => void): () => void;\n /** Advances on every change. */\n getVersion(): number;\n}\n\n/** Options for {@link Reflectable.push} and {@link Reflectable.set}. */\nexport interface RegisterOptions {\n /** Who registered this — `'kit'`, a package, a plugin id. Reported, never interpreted. */\n source?: string;\n}\n\n/**\n * A keyed store that keeps its own {@link Reflection}. The registry owning it\n * writes through `push` / `set` / `delete` and hands out `reflection`.\n */\nexport interface Reflectable<V, K = string> extends Reflection<V, K> {\n /** Register `value` over whatever `key` holds and return its release.\n * Registrants stack, newest live: releasing the live one uncovers the one\n * it displaced, and releasing a displaced one removes only it. Double-release safe. */\n push(key: K, value: V, options?: RegisterOptions): () => void;\n /** Replace `key`'s live registrant in place, the way `Map.set` does — for\n * registries where a second registration means \"newer version\", not\n * \"override\". Records no conflict. */\n set(key: K, value: V, options?: RegisterOptions): void;\n /** Drop every registrant of `key`. Returns whether there was one. */\n delete(key: K): boolean;\n clear(): void;\n /** Announce a change the store cannot see — a registered value mutated in place. */\n bump(): void;\n /** This store as a {@link Reflection}, with no write methods on it. */\n readonly reflection: Reflection<V, K>;\n}\n\n/**\n * Create a keyed store with a uniform reflection surface.\n *\n * Deliberately only the cross-cutting part of a registry: validation, lookup\n * fallbacks and lifecycle stay with the registry that embeds it.\n */\nexport function createReflectable<V, K = string>(): Reflectable<V, K> {\n const stacks = new Map<K, Registrant<V>[]>();\n const listeners = createEmitter();\n let version = 0;\n let snapshot: { version: number; entries: readonly ReflectedEntry<V, K>[] } | null = null;\n\n function changed(): void {\n version++;\n listeners.emit();\n }\n\n const reflection: Reflection<V, K> = {\n get: (key) => stacks.get(key)?.at(-1)?.value,\n has: (key) => stacks.has(key),\n entries() {\n if (snapshot?.version === version) return snapshot.entries;\n const entries: ReflectedEntry<V, K>[] = [];\n for (const [key, stack] of stacks) {\n const live = stack.at(-1)!;\n entries.push(Object.freeze({\n key, value: live.value, source: live.source, shadowed: Object.freeze(stack.slice(0, -1)),\n }));\n }\n snapshot = { version, entries: Object.freeze(entries) };\n return snapshot.entries;\n },\n subscribe: listeners.subscribe,\n getVersion: () => version,\n };\n\n return {\n ...reflection,\n reflection,\n push(key, value, options) {\n const registrant: Registrant<V> = Object.freeze({ value, source: options?.source });\n const stack = stacks.get(key);\n if (stack) stack.push(registrant);\n else stacks.set(key, [registrant]);\n changed();\n let released = false;\n return () => {\n if (released) return;\n released = true;\n const cur = stacks.get(key);\n const i = cur ? cur.lastIndexOf(registrant) : -1;\n if (i === -1) return;\n cur!.splice(i, 1);\n if (cur!.length === 0) stacks.delete(key);\n changed();\n };\n },\n set(key, value, options) {\n const registrant: Registrant<V> = Object.freeze({ value, source: options?.source });\n const stack = stacks.get(key);\n if (stack) stack[stack.length - 1] = registrant;\n else stacks.set(key, [registrant]);\n changed();\n },\n delete(key) {\n if (!stacks.delete(key)) return false;\n changed();\n return true;\n },\n clear() {\n if (stacks.size === 0) return;\n stacks.clear();\n changed();\n },\n bump: changed,\n };\n}\n","/** Whether `proto` is the prototype a plain object has: `null`, or some\n * realm's `Object.prototype`. Comparing against this realm's alone misses\n * objects made in an iframe, a VM context, or by `structuredClone` under jsdom. */\nexport function isPlainPrototype(proto: object | null): boolean {\n return proto === Object.prototype || proto === null || Object.getPrototypeOf(proto) === null;\n}\n\n/** Whether `value` is a plain object — a literal or `Object.create(null)` — from any realm. */\nexport function isPlainObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && isPlainPrototype(Object.getPrototypeOf(value));\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weasel-js/registry",
3
- "version": "1.9.3",
3
+ "version": "1.9.4",
4
4
  "description": "Keyed registries with a uniform reflection surface: enumerate the entries, see which ones displaced others, subscribe to changes. No React, no DOM.",
5
5
  "license": "MIT",
6
6
  "type": "module",