|
| 1 | +--- |
| 2 | +title: contextRegistry |
| 3 | +--- |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`ContextRegistry` is a utility class for explicit context sharing across |
| 8 | +component trees. |
| 9 | + |
| 10 | +It stores context instances by constructor and lets you resolve them by type. |
| 11 | +Lookup uses `instanceof`, so querying by a base class can return a subclass |
| 12 | +instance. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```ts |
| 17 | +import { ContextRegistry } from 'regor' |
| 18 | + |
| 19 | +class AppServices { |
| 20 | + readonly apiBase = '/v1' |
| 21 | +} |
| 22 | + |
| 23 | +const registry = new ContextRegistry() |
| 24 | +registry.register(new AppServices()) |
| 25 | + |
| 26 | +const services = registry.require(AppServices) |
| 27 | +``` |
| 28 | + |
| 29 | +## API |
| 30 | + |
| 31 | +### `register(context)` |
| 32 | + |
| 33 | +Registers an instance under its runtime constructor. |
| 34 | + |
| 35 | +- If another instance of the same constructor is already registered, it is |
| 36 | + replaced. |
| 37 | + |
| 38 | +### `unregisterByClass(ContextClass)` |
| 39 | + |
| 40 | +Removes the entry for a constructor. |
| 41 | + |
| 42 | +- No-op when that constructor is not registered. |
| 43 | + |
| 44 | +### `unregister(context)` |
| 45 | + |
| 46 | +Removes an instance only if it is currently the registered value for its |
| 47 | +constructor. |
| 48 | + |
| 49 | +- This prevents deleting a newer replacement instance registered later. |
| 50 | + |
| 51 | +### `find(ContextClass)` |
| 52 | + |
| 53 | +Returns the first registered instance matching `instanceof ContextClass`, or |
| 54 | +`undefined`. |
| 55 | + |
| 56 | +### `require(ContextClass)` |
| 57 | + |
| 58 | +Returns the same result as `find`, but throws when not found. |
| 59 | + |
| 60 | +- Error message format: `ClassName is not registered in ContextRegistry.` |
| 61 | + |
| 62 | +## Example: parent provides registry, child resolves from `head.requireContext` |
| 63 | + |
| 64 | +```ts |
| 65 | +import { ComponentHead, ContextRegistry, defineComponent } from 'regor' |
| 66 | + |
| 67 | +class AppServices { |
| 68 | + readonly apiBase = '/v1' |
| 69 | +} |
| 70 | + |
| 71 | +class Parent { |
| 72 | + readonly registry = new ContextRegistry() |
| 73 | + |
| 74 | + constructor() { |
| 75 | + this.registry.register(new AppServices()) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class Child { |
| 80 | + readonly apiBase: string |
| 81 | + |
| 82 | + constructor(head: ComponentHead<object>) { |
| 83 | + // Parent can provide registry from any component context shape. |
| 84 | + const parent = head.requireContext(Parent) |
| 85 | + const services = parent.registry.require(AppServices) |
| 86 | + this.apiBase = services.apiBase |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +const parent = defineComponent<Parent>('<div><slot></slot></div>', { |
| 91 | + context: () => new Parent(), |
| 92 | +}) |
| 93 | + |
| 94 | +const child = defineComponent<Child>('<p r-text="apiBase"></p>', { |
| 95 | + context: (head) => { |
| 96 | + return new Child(head) |
| 97 | + }, |
| 98 | +}) |
| 99 | +``` |
| 100 | + |
| 101 | +## Example: component ctx <=> component ctx communication through parent registry |
| 102 | + |
| 103 | +```ts |
| 104 | +import { |
| 105 | + ComponentHead, |
| 106 | + ContextRegistry, |
| 107 | + createApp, |
| 108 | + defineComponent, |
| 109 | + html, |
| 110 | + ref, |
| 111 | +} from 'regor' |
| 112 | + |
| 113 | +class Parent { |
| 114 | + components = { producer, consumer } |
| 115 | + registry = new ContextRegistry() |
| 116 | +} |
| 117 | + |
| 118 | +class Consumer { |
| 119 | + message = ref('idle') |
| 120 | + private readonly parent: Parent |
| 121 | + |
| 122 | + constructor(head: ComponentHead<object>) { |
| 123 | + this.parent = head.requireContext(Parent) |
| 124 | + this.parent.registry.register(this) |
| 125 | + } |
| 126 | + |
| 127 | + receive = (next: string): void => { |
| 128 | + this.message(next) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +class Producer { |
| 133 | + private readonly parent: Parent |
| 134 | + |
| 135 | + constructor(head: ComponentHead<object>) { |
| 136 | + this.parent = head.requireContext(Parent) |
| 137 | + this.parent.registry.register(this) |
| 138 | + } |
| 139 | + |
| 140 | + send = (): void => { |
| 141 | + // direct component-ctx -> component-ctx call through registry |
| 142 | + const consumer = this.parent.registry.require(Consumer) |
| 143 | + consumer.receive('message-from-producer') |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +const producer = defineComponent<Producer>( |
| 148 | + html`<button class="send" @click="send">send</button>`, |
| 149 | + { |
| 150 | + context: (head) => new Producer(head), |
| 151 | + }, |
| 152 | +) |
| 153 | + |
| 154 | +const consumer = defineComponent<Consumer>( |
| 155 | + html`<p class="value">{{ message }}</p>`, |
| 156 | + { |
| 157 | + context: (head) => new Consumer(head), |
| 158 | + }, |
| 159 | +) |
| 160 | + |
| 161 | +const parent = defineComponent<Parent>( |
| 162 | + html`<section> |
| 163 | + <Producer></Producer> |
| 164 | + <Consumer></Consumer> |
| 165 | + </section>`, |
| 166 | + { |
| 167 | + context: () => new Parent(), |
| 168 | + }, |
| 169 | +) |
| 170 | + |
| 171 | +createApp( |
| 172 | + { components: { parent } }, |
| 173 | + { |
| 174 | + element: document.querySelector('#app')!, |
| 175 | + template: '<Parent></Parent>', |
| 176 | + }, |
| 177 | +) |
| 178 | +``` |
| 179 | + |
| 180 | +## See Also |
| 181 | + |
| 182 | +- [`defineComponent`](/api/defineComponent) |
| 183 | +- [`useScope`](/api/useScope) |
| 184 | +- [Components Guide](/guide/components) |
| 185 | +- [TypeScript Guide](/guide/typescript) |
| 186 | + |
| 187 | +[Back to the API list](/api/) |
0 commit comments