regor 1.3.7 → 1.3.9

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/regor.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
+ export type ContextClass<TValue extends object> = abstract new (...args: never[]) => TValue;
3
4
  /**
4
5
  * Runtime metadata passed to a component's `context(head)` factory.
5
6
  *
@@ -125,6 +126,50 @@ export declare class ComponentHead<TContext extends IRegorContext | object = IRe
125
126
  * ```
126
127
  */
127
128
  emit: (event: string, args: Record<string, unknown>) => void;
129
+ /**
130
+ * Finds a parent context instance by constructor type from the captured
131
+ * context stack.
132
+ *
133
+ * Matching uses `instanceof` and respects stack order.
134
+ *
135
+ * `occurrence` selects which matching instance to return:
136
+ * - `0` (default): first match
137
+ * - `1`: second match
138
+ * - `2`: third match
139
+ * - negative values: always `undefined`
140
+ *
141
+ * Example:
142
+ * ```ts
143
+ * // stack: [RootCtx, ParentCtx, ParentCtx]
144
+ * head.findContext(ParentCtx) // first ParentCtx
145
+ * head.findContext(ParentCtx, 1) // second ParentCtx
146
+ * ```
147
+ *
148
+ * @param constructor - Class constructor used for `instanceof` matching.
149
+ * @param occurrence - Zero-based index of the matching instance to return.
150
+ * @returns The matching parent context instance, or `undefined` when not found.
151
+ */
152
+ findContext<TValue extends object>(constructor: ContextClass<TValue>, occurrence?: number): TValue | undefined;
153
+ /**
154
+ * Returns a parent context instance by constructor type from the captured
155
+ * context stack.
156
+ *
157
+ * The stack is scanned in order and each entry is checked with `instanceof`.
158
+ * `occurrence` is zero-based (`0` = first match, `1` = second match, ...).
159
+ * If no instance exists at the requested occurrence, this method throws.
160
+ *
161
+ * Example:
162
+ * ```ts
163
+ * const auth = head.requireContext(AuthContext) // first AuthContext
164
+ * const outer = head.requireContext(LayoutCtx, 1) // second LayoutCtx
165
+ * ```
166
+ *
167
+ * @param constructor - Class constructor used for `instanceof` matching.
168
+ * @param occurrence - Zero-based index of the instance to return.
169
+ * @returns The parent context instance at the requested occurrence.
170
+ * @throws Error when no matching instance exists at the requested occurrence.
171
+ */
172
+ requireContext<TValue extends object>(constructor: ContextClass<TValue>, occurrence?: number): TValue;
128
173
  /**
129
174
  * Unmounts this component instance by removing nodes between `start` and `end`
130
175
  * and calling unmount lifecycle handlers for captured contexts.
@@ -483,6 +528,62 @@ export declare const getBindData: (node: object) => BindData;
483
528
  export declare const removeNode: (node: ChildNode) => void;
484
529
  export declare const drainUnbind: () => Promise<void>;
485
530
  export declare const unbind: (node: Node) => void;
531
+ /**
532
+ * Registry for sharing typed context instances across component boundaries.
533
+ *
534
+ * Entries are keyed by the runtime constructor of each registered instance:
535
+ * - one active entry per concrete constructor
536
+ * - registering another instance of the same constructor replaces the previous
537
+ *
538
+ * Lookup is type-based and uses `instanceof`, so querying a base class can
539
+ * resolve a registered subclass instance.
540
+ */
541
+ export declare class ContextRegistry {
542
+ private readonly byConstructor;
543
+ /**
544
+ * Registers a context instance under its concrete runtime constructor.
545
+ *
546
+ * If an instance with the same constructor already exists, it is replaced.
547
+ *
548
+ * @param context - Context instance to register.
549
+ */
550
+ register<TContext extends object>(context: TContext): void;
551
+ /**
552
+ * Removes the entry for a constructor.
553
+ *
554
+ * No-op when the constructor is not registered.
555
+ *
556
+ * @param contextClass - Constructor key to remove.
557
+ */
558
+ unregisterByClass<TContext extends object>(contextClass: ContextClass<TContext>): void;
559
+ /**
560
+ * Removes a specific context instance if it is currently registered for its
561
+ * constructor.
562
+ *
563
+ * This prevents deleting a newer replacement instance of the same class.
564
+ *
565
+ * @param context - Context instance to remove.
566
+ */
567
+ unregister<TContext extends object>(context: TContext): void;
568
+ /**
569
+ * Finds a context instance by constructor type.
570
+ *
571
+ * The registry is scanned in insertion order and each entry is checked with
572
+ * `instanceof contextClass`.
573
+ *
574
+ * @param contextClass - Class to match via `instanceof`.
575
+ * @returns Matching instance, or `undefined` when not found.
576
+ */
577
+ find<TContext extends object>(contextClass: ContextClass<TContext>): TContext | undefined;
578
+ /**
579
+ * Resolves a context instance by constructor type and guarantees a value.
580
+ *
581
+ * @param contextClass - Class to match via `instanceof`.
582
+ * @returns Matching context instance.
583
+ * @throws Error when no matching context is registered.
584
+ */
585
+ require<TContext extends object>(contextClass: ContextClass<TContext>): TContext;
586
+ }
486
587
  export declare const onMounted: (onMounted: OnMounted) => void;
487
588
  export declare const onUnmounted: (onUnmounted: OnUnmounted, noThrow?: boolean) => void;
488
589
  export declare const useScope: <TRegorContext extends IRegorContext | object>(context: () => TRegorContext) => Scope<TRegorContext>;
@@ -60,6 +60,7 @@ var __async = (__this, __arguments, generator) => {
60
60
  var index_exports = {};
61
61
  __export(index_exports, {
62
62
  ComponentHead: () => ComponentHead,
63
+ ContextRegistry: () => ContextRegistry,
63
64
  RegorConfig: () => RegorConfig,
64
65
  addUnbinder: () => addUnbinder,
65
66
  batch: () => batch,
@@ -357,6 +358,66 @@ var ComponentHead = class {
357
358
  this.start = start;
358
359
  this.end = end;
359
360
  }
361
+ /**
362
+ * Finds a parent context instance by constructor type from the captured
363
+ * context stack.
364
+ *
365
+ * Matching uses `instanceof` and respects stack order.
366
+ *
367
+ * `occurrence` selects which matching instance to return:
368
+ * - `0` (default): first match
369
+ * - `1`: second match
370
+ * - `2`: third match
371
+ * - negative values: always `undefined`
372
+ *
373
+ * Example:
374
+ * ```ts
375
+ * // stack: [RootCtx, ParentCtx, ParentCtx]
376
+ * head.findContext(ParentCtx) // first ParentCtx
377
+ * head.findContext(ParentCtx, 1) // second ParentCtx
378
+ * ```
379
+ *
380
+ * @param constructor - Class constructor used for `instanceof` matching.
381
+ * @param occurrence - Zero-based index of the matching instance to return.
382
+ * @returns The matching parent context instance, or `undefined` when not found.
383
+ */
384
+ findContext(constructor, occurrence = 0) {
385
+ var _a;
386
+ if (occurrence < 0) return void 0;
387
+ let current = 0;
388
+ for (const value of (_a = this.ctx) != null ? _a : []) {
389
+ if (!(value instanceof constructor)) continue;
390
+ if (current === occurrence) return value;
391
+ ++current;
392
+ }
393
+ return void 0;
394
+ }
395
+ /**
396
+ * Returns a parent context instance by constructor type from the captured
397
+ * context stack.
398
+ *
399
+ * The stack is scanned in order and each entry is checked with `instanceof`.
400
+ * `occurrence` is zero-based (`0` = first match, `1` = second match, ...).
401
+ * If no instance exists at the requested occurrence, this method throws.
402
+ *
403
+ * Example:
404
+ * ```ts
405
+ * const auth = head.requireContext(AuthContext) // first AuthContext
406
+ * const outer = head.requireContext(LayoutCtx, 1) // second LayoutCtx
407
+ * ```
408
+ *
409
+ * @param constructor - Class constructor used for `instanceof` matching.
410
+ * @param occurrence - Zero-based index of the instance to return.
411
+ * @returns The parent context instance at the requested occurrence.
412
+ * @throws Error when no matching instance exists at the requested occurrence.
413
+ */
414
+ requireContext(constructor, occurrence = 0) {
415
+ const value = this.findContext(constructor, occurrence);
416
+ if (value !== void 0) return value;
417
+ throw new Error(
418
+ `${constructor} was not found in the context stack at occurrence ${occurrence}.`
419
+ );
420
+ }
360
421
  /**
361
422
  * Unmounts this component instance by removing nodes between `start` and `end`
362
423
  * and calling unmount lifecycle handlers for captured contexts.
@@ -5967,6 +6028,76 @@ var defineComponent = (template, options = {}) => {
5967
6028
  };
5968
6029
  };
5969
6030
 
6031
+ // src/composition/ContextRegistry.ts
6032
+ var ContextRegistry = class {
6033
+ constructor() {
6034
+ __publicField(this, "byConstructor", /* @__PURE__ */ new Map());
6035
+ }
6036
+ /**
6037
+ * Registers a context instance under its concrete runtime constructor.
6038
+ *
6039
+ * If an instance with the same constructor already exists, it is replaced.
6040
+ *
6041
+ * @param context - Context instance to register.
6042
+ */
6043
+ register(context) {
6044
+ this.byConstructor.set(context.constructor, context);
6045
+ }
6046
+ /**
6047
+ * Removes the entry for a constructor.
6048
+ *
6049
+ * No-op when the constructor is not registered.
6050
+ *
6051
+ * @param contextClass - Constructor key to remove.
6052
+ */
6053
+ unregisterByClass(contextClass) {
6054
+ this.byConstructor.delete(contextClass);
6055
+ }
6056
+ /**
6057
+ * Removes a specific context instance if it is currently registered for its
6058
+ * constructor.
6059
+ *
6060
+ * This prevents deleting a newer replacement instance of the same class.
6061
+ *
6062
+ * @param context - Context instance to remove.
6063
+ */
6064
+ unregister(context) {
6065
+ const key = context.constructor;
6066
+ if (this.byConstructor.get(key) === context) {
6067
+ this.byConstructor.delete(key);
6068
+ }
6069
+ }
6070
+ /**
6071
+ * Finds a context instance by constructor type.
6072
+ *
6073
+ * The registry is scanned in insertion order and each entry is checked with
6074
+ * `instanceof contextClass`.
6075
+ *
6076
+ * @param contextClass - Class to match via `instanceof`.
6077
+ * @returns Matching instance, or `undefined` when not found.
6078
+ */
6079
+ find(contextClass) {
6080
+ for (const value of this.byConstructor.values()) {
6081
+ if (value instanceof contextClass) return value;
6082
+ }
6083
+ return void 0;
6084
+ }
6085
+ /**
6086
+ * Resolves a context instance by constructor type and guarantees a value.
6087
+ *
6088
+ * @param contextClass - Class to match via `instanceof`.
6089
+ * @returns Matching context instance.
6090
+ * @throws Error when no matching context is registered.
6091
+ */
6092
+ require(contextClass) {
6093
+ const value = this.find(contextClass);
6094
+ if (value) return value;
6095
+ throw new Error(
6096
+ `${contextClass.name} is not registered in ContextRegistry.`
6097
+ );
6098
+ }
6099
+ };
6100
+
5970
6101
  // src/composition/onMounted.ts
5971
6102
  var onMounted = (onMounted2) => {
5972
6103
  var _a;