@wirestate/lit 0.6.0 → 0.6.1
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/CHANGELOG.md +11 -0
- package/README.md +245 -9
- package/cjs/development/index.js +774 -51
- package/cjs/development/index.js.map +1 -1
- package/cjs/production/index.js +1 -1
- package/cjs/production/index.js.map +1 -1
- package/esm/development/commands/on-command-controller.js +58 -0
- package/esm/development/commands/on-command-controller.js.map +1 -0
- package/esm/development/commands/on-command.js +45 -0
- package/esm/development/commands/on-command.js.map +1 -0
- package/esm/development/commands/use-on-command.js +31 -0
- package/esm/development/commands/use-on-command.js.map +1 -0
- package/esm/development/consumption/injection.js +40 -6
- package/esm/development/consumption/injection.js.map +1 -1
- package/esm/development/consumption/use-injection.js +38 -4
- package/esm/development/consumption/use-injection.js.map +1 -1
- package/esm/development/context/ioc-context.js +10 -0
- package/esm/development/context/ioc-context.js.map +1 -1
- package/esm/development/error/error-code.js +4 -0
- package/esm/development/error/error-code.js.map +1 -0
- package/esm/development/events/on-event-controller.js +66 -0
- package/esm/development/events/on-event-controller.js.map +1 -0
- package/esm/development/events/on-event.js +65 -0
- package/esm/development/events/on-event.js.map +1 -0
- package/esm/development/events/use-on-events.js +42 -0
- package/esm/development/events/use-on-events.js.map +1 -0
- package/esm/development/index.js +15 -3
- package/esm/development/index.js.map +1 -1
- package/esm/development/provision/injectables-provide.js +47 -0
- package/esm/development/provision/injectables-provide.js.map +1 -0
- package/esm/development/provision/injectables-provider-controller.js +123 -0
- package/esm/development/provision/injectables-provider-controller.js.map +1 -0
- package/esm/development/provision/ioc-provide.js +55 -0
- package/esm/development/provision/ioc-provide.js.map +1 -0
- package/esm/development/provision/ioc-provider-controller.js +72 -0
- package/esm/development/provision/ioc-provider-controller.js.map +1 -0
- package/esm/development/provision/use-injectables-provider.js +33 -0
- package/esm/development/provision/use-injectables-provider.js.map +1 -0
- package/esm/development/provision/use-ioc-provision.js +29 -0
- package/esm/development/provision/use-ioc-provision.js.map +1 -0
- package/esm/development/queries/on-query-controller.js +58 -0
- package/esm/development/queries/on-query-controller.js.map +1 -0
- package/esm/development/queries/on-query.js +45 -0
- package/esm/development/queries/on-query.js.map +1 -0
- package/esm/development/queries/use-on-query.js +31 -0
- package/esm/development/queries/use-on-query.js.map +1 -0
- package/esm/production/commands/on-command-controller.js +1 -0
- package/esm/production/commands/on-command-controller.js.map +1 -0
- package/esm/production/commands/on-command.js +1 -0
- package/esm/production/commands/on-command.js.map +1 -0
- package/esm/production/commands/use-on-command.js +1 -0
- package/esm/production/commands/use-on-command.js.map +1 -0
- package/esm/production/consumption/injection.js +1 -1
- package/esm/production/consumption/injection.js.map +1 -1
- package/esm/production/consumption/use-injection.js +1 -1
- package/esm/production/consumption/use-injection.js.map +1 -1
- package/esm/production/context/ioc-context.js.map +1 -1
- package/esm/production/error/error-code.js +1 -0
- package/esm/production/error/error-code.js.map +1 -0
- package/esm/production/events/on-event-controller.js +1 -0
- package/esm/production/events/on-event-controller.js.map +1 -0
- package/esm/production/events/on-event.js +1 -0
- package/esm/production/events/on-event.js.map +1 -0
- package/esm/production/events/use-on-events.js +1 -0
- package/esm/production/events/use-on-events.js.map +1 -0
- package/esm/production/index.js +1 -1
- package/esm/production/provision/injectables-provide.js +1 -0
- package/esm/production/provision/injectables-provide.js.map +1 -0
- package/esm/production/provision/injectables-provider-controller.js +1 -0
- package/esm/production/provision/injectables-provider-controller.js.map +1 -0
- package/esm/production/provision/ioc-provide.js +1 -0
- package/esm/production/provision/ioc-provide.js.map +1 -0
- package/esm/production/provision/ioc-provider-controller.js +1 -0
- package/esm/production/provision/ioc-provider-controller.js.map +1 -0
- package/esm/production/provision/use-injectables-provider.js +1 -0
- package/esm/production/provision/use-injectables-provider.js.map +1 -0
- package/esm/production/provision/use-ioc-provision.js +1 -0
- package/esm/production/provision/use-ioc-provision.js.map +1 -0
- package/esm/production/queries/on-query-controller.js +1 -0
- package/esm/production/queries/on-query-controller.js.map +1 -0
- package/esm/production/queries/on-query.js +1 -0
- package/esm/production/queries/on-query.js.map +1 -0
- package/esm/production/queries/use-on-query.js +1 -0
- package/esm/production/queries/use-on-query.js.map +1 -0
- package/index.d.ts +702 -28
- package/package.json +1 -1
- package/esm/development/provision/container-provider-controller.js +0 -41
- package/esm/development/provision/container-provider-controller.js.map +0 -1
- package/esm/development/provision/services-provider-controller.js +0 -63
- package/esm/development/provision/services-provider-controller.js.map +0 -1
- package/esm/development/provision/use-container-provision.js +0 -9
- package/esm/development/provision/use-container-provision.js.map +0 -1
- package/esm/production/provision/container-provider-controller.js +0 -1
- package/esm/production/provision/container-provider-controller.js.map +0 -1
- package/esm/production/provision/services-provider-controller.js +0 -1
- package/esm/production/provision/services-provider-controller.js.map +0 -1
- package/esm/production/provision/use-container-provision.js +0 -1
- package/esm/production/provision/use-container-provision.js.map +0 -1
package/index.d.ts
CHANGED
|
@@ -1,14 +1,44 @@
|
|
|
1
1
|
import { ReactiveElement, ReactiveControllerHost, ReactiveController } from '@lit/reactive-element';
|
|
2
|
-
import { ServiceIdentifier, Container, Newable, InjectableDescriptor, SeedEntries } from '@wirestate/core';
|
|
2
|
+
import { ServiceIdentifier, CommandType, CommandHandler, Event, EventType, EventHandler, QueryType, QueryHandler, Container, Newable, InjectableDescriptor, SeedEntries } from '@wirestate/core';
|
|
3
3
|
import { ContextProvider, ContextConsumer } from '@lit/context';
|
|
4
|
-
import { Callable } from '@wirestate/core/types/general';
|
|
5
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Represents any object with string or symbol keys.
|
|
7
|
+
*
|
|
8
|
+
* @group types
|
|
9
|
+
*/
|
|
10
|
+
type AnyObject = Record<string | symbol, any>;
|
|
11
|
+
/**
|
|
12
|
+
* Represents a type that can be null.
|
|
13
|
+
*
|
|
14
|
+
* @group types
|
|
15
|
+
*/
|
|
6
16
|
type Optional<T> = T | null;
|
|
7
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Represents a type that can be a value or a promise of that value.
|
|
19
|
+
*
|
|
20
|
+
* @group types
|
|
21
|
+
*/
|
|
22
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Helper to extract the interface of a type.
|
|
25
|
+
*
|
|
26
|
+
* @group types
|
|
27
|
+
*/
|
|
8
28
|
type Interface<T> = {
|
|
9
29
|
[K in keyof T]: T[K];
|
|
10
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* Return type for decorators.
|
|
33
|
+
*
|
|
34
|
+
* @group types
|
|
35
|
+
*/
|
|
11
36
|
type DecoratorReturn = void | any;
|
|
37
|
+
/**
|
|
38
|
+
* Type helper to ensure that a decorated field matches the type being provided.
|
|
39
|
+
*
|
|
40
|
+
* @group types
|
|
41
|
+
*/
|
|
12
42
|
type FieldMustMatchProvidedType<Obj, Key extends PropertyKey, ProvidedType> = Obj extends Record<Key, infer ConsumingType> ? [ProvidedType] extends [ConsumingType] ? DecoratorReturn : {
|
|
13
43
|
message: "provided type not assignable to consuming field";
|
|
14
44
|
provided: ProvidedType;
|
|
@@ -19,30 +49,455 @@ type FieldMustMatchProvidedType<Obj, Key extends PropertyKey, ProvidedType> = Ob
|
|
|
19
49
|
consuming: ConsumingType | undefined;
|
|
20
50
|
} : DecoratorReturn;
|
|
21
51
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Type definition for the injection decorator.
|
|
54
|
+
*
|
|
55
|
+
* @group consumption
|
|
56
|
+
*/
|
|
57
|
+
interface InjectionDecorator<T> {
|
|
58
|
+
<C extends Interface<Omit<ReactiveElement, "renderRoot">>, V extends T>(value: ClassAccessorDecoratorTarget<C, V>, context: ClassAccessorDecoratorContext<C, V>): void;
|
|
59
|
+
<K extends PropertyKey, Proto extends Interface<Omit<ReactiveElement, "renderRoot">>>(protoOrDescriptor: Proto, name?: K): FieldMustMatchProvidedType<Proto, K, T>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Options for the {@link injection} decorator.
|
|
63
|
+
*
|
|
64
|
+
* @group consumption
|
|
65
|
+
*/
|
|
26
66
|
interface InjectionOptions<T> {
|
|
67
|
+
/**
|
|
68
|
+
* The service identifier to inject from the IoC container.
|
|
69
|
+
*/
|
|
27
70
|
injectionId: ServiceIdentifier<T>;
|
|
28
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Whether to subscribe to changes in the context.
|
|
73
|
+
*
|
|
74
|
+
* If true, the property will be updated if the container in the context changes.
|
|
75
|
+
* False by default.
|
|
76
|
+
*/
|
|
77
|
+
once?: boolean;
|
|
29
78
|
}
|
|
30
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Decorator to inject a service from the IoC container into a Lit element property.
|
|
81
|
+
*
|
|
82
|
+
* @group consumption
|
|
83
|
+
*
|
|
84
|
+
* @param optionsOrInjectionId - injection options including the service identifier or the service identifier itself
|
|
85
|
+
* @returns injection decorator instance
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* class MyElement extends LitElement {
|
|
90
|
+
* @injection(MyService)
|
|
91
|
+
* private myService!: MyService;
|
|
92
|
+
*
|
|
93
|
+
* public render() {
|
|
94
|
+
* return html`<div>${this.myService.getName()}</div>`;
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* class MyElement extends LitElement {
|
|
102
|
+
* @injection({ injectionId: MyService, once: true })
|
|
103
|
+
* private myService!: MyService;
|
|
104
|
+
*
|
|
105
|
+
* public render() {
|
|
106
|
+
* return html`<div>${this.myService.getName()}</div>`;
|
|
107
|
+
* }
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare function injection<T>(optionsOrInjectionId: InjectionOptions<T> | ServiceIdentifier<T>): InjectionDecorator<T>;
|
|
31
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Options for the {@link useInjection} hook.
|
|
115
|
+
*
|
|
116
|
+
* @group consumption
|
|
117
|
+
*/
|
|
32
118
|
interface UseInjectionOptions<T> {
|
|
119
|
+
/**
|
|
120
|
+
* If true, the service will be fetched only once when the controller is created.
|
|
121
|
+
* If false (default), it will update if the container in the context changes.
|
|
122
|
+
*/
|
|
33
123
|
once?: boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Initial value for the injection before it's fetched from the container.
|
|
126
|
+
*/
|
|
34
127
|
value?: Optional<T>;
|
|
128
|
+
/**
|
|
129
|
+
* The service identifier to inject.
|
|
130
|
+
*/
|
|
35
131
|
injectionId: ServiceIdentifier<T>;
|
|
36
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* The return value of the {@link useInjection} hook, containing the injected service.
|
|
135
|
+
*
|
|
136
|
+
* @group consumption
|
|
137
|
+
*/
|
|
37
138
|
interface UseInjectionValue<T> {
|
|
139
|
+
/**
|
|
140
|
+
* The service identifier used for injection.
|
|
141
|
+
*/
|
|
38
142
|
injectionId: ServiceIdentifier<T>;
|
|
143
|
+
/**
|
|
144
|
+
* The injected service instance.
|
|
145
|
+
*/
|
|
39
146
|
value: T;
|
|
40
147
|
}
|
|
41
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Hook (controller) to inject a service from the IoC container in a Lit component.
|
|
150
|
+
*
|
|
151
|
+
* @group consumption
|
|
152
|
+
*
|
|
153
|
+
* @param host - the host element
|
|
154
|
+
* @param optionsOrInjectionId - injection options including the service identifier or the service identifier itself
|
|
155
|
+
* @returns injection descriptor object
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* class MyElement extends LitElement {
|
|
160
|
+
* private myService = useInjection(this, MyService);
|
|
161
|
+
*
|
|
162
|
+
* render() {
|
|
163
|
+
* return html`<div>${this.myService.value.getName()}</div>`;
|
|
164
|
+
* }
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* class MyElement extends LitElement {
|
|
171
|
+
* private myService = useInjection(this, { injectionId: MyService, once: true });
|
|
172
|
+
*
|
|
173
|
+
* render() {
|
|
174
|
+
* return html`<div>${this.myService.value.getName()}</div>`;
|
|
175
|
+
* }
|
|
176
|
+
* }
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
declare function useInjection<T extends object, E extends ReactiveControllerHost & HTMLElement>(host: E, optionsOrInjectionId: UseInjectionOptions<T> | ServiceIdentifier<T>): UseInjectionValue<T>;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Interface for the {@link onCommand} decorator.
|
|
183
|
+
*
|
|
184
|
+
* Supports both standard (TC39) and legacy experimental decorators.
|
|
185
|
+
*
|
|
186
|
+
* @group commands
|
|
187
|
+
*/
|
|
188
|
+
interface OnCommandDecorator<D = unknown, R = unknown> {
|
|
189
|
+
<This extends Interface<Omit<ReactiveElement, "renderRoot">>>(value: (this: This, data: D) => MaybePromise<R>, context: ClassMethodDecoratorContext<This>): void;
|
|
190
|
+
(target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor): void;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Decorator that registers a Lit element method as a command handler for the given type.
|
|
194
|
+
*
|
|
195
|
+
* The handler is registered when the host element connects to the DOM and unregistered when it disconnects.
|
|
196
|
+
*
|
|
197
|
+
* @group commands
|
|
198
|
+
*
|
|
199
|
+
* @param type - the command type to handle
|
|
200
|
+
* @returns the decorator function.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* class MyElement extends LitElement {
|
|
205
|
+
* @onCommand("SAVE")
|
|
206
|
+
* private handleSave(data: SomeData): void {
|
|
207
|
+
* // handle command
|
|
208
|
+
* }
|
|
209
|
+
* }
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
declare function onCommand<D = unknown, R = unknown>(type: CommandType): OnCommandDecorator<D, R>;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Reactive controller that registers a command handler on the {@link CommandBus} for the host element's lifetime.
|
|
216
|
+
*
|
|
217
|
+
* The handler is registered when the host connects and unregistered when it disconnects.
|
|
218
|
+
* When the IoC context is updated (container revision change), the handler is re-registered automatically.
|
|
219
|
+
*
|
|
220
|
+
* @group commands
|
|
221
|
+
*/
|
|
222
|
+
declare class OnCommandController<D = unknown, R = unknown> implements ReactiveController {
|
|
223
|
+
private bus;
|
|
224
|
+
private unregister;
|
|
225
|
+
private readonly type;
|
|
226
|
+
private readonly handler;
|
|
227
|
+
/**
|
|
228
|
+
* @param host - The host element.
|
|
229
|
+
* @param type - The command type to handle.
|
|
230
|
+
* @param handler - The command handler function.
|
|
231
|
+
*/
|
|
232
|
+
constructor(host: ReactiveElement, type: CommandType, handler: CommandHandler<D, R>);
|
|
233
|
+
hostConnected(): void;
|
|
234
|
+
hostDisconnected(): void;
|
|
235
|
+
private reregister;
|
|
236
|
+
private cleanup;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Options for the {@link useOnCommand} hook.
|
|
241
|
+
*
|
|
242
|
+
* @group commands
|
|
243
|
+
*/
|
|
244
|
+
interface UseOnCommandOptions<D = unknown, R = unknown> {
|
|
245
|
+
/**
|
|
246
|
+
* The command type to listen for.
|
|
247
|
+
*/
|
|
248
|
+
type: CommandType;
|
|
249
|
+
/**
|
|
250
|
+
* The command handler function.
|
|
251
|
+
*/
|
|
252
|
+
handler: CommandHandler<D, R>;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Registers a command handler on the CommandBus for the host element's lifetime.
|
|
256
|
+
*
|
|
257
|
+
* @group commands
|
|
258
|
+
*
|
|
259
|
+
* @param host - the host element
|
|
260
|
+
* @param options - command handling options
|
|
261
|
+
* @param options.type - the command type to listen for
|
|
262
|
+
* @param options.handler - the command handler function
|
|
263
|
+
* @returns the command controller instance
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* class MyElement extends LitElement {
|
|
268
|
+
* private onSave = useOnCommand(this, {
|
|
269
|
+
* type: "SAVE",
|
|
270
|
+
* handler: (data) => console.log("Saving:", data),
|
|
271
|
+
* });
|
|
272
|
+
* }
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
declare function useOnCommand<D = unknown, R = unknown>(host: ReactiveElement, { type, handler }: UseOnCommandOptions<D, R>): OnCommandController<D, R>;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Type definition for the on-event decorator.
|
|
279
|
+
*
|
|
280
|
+
* @group events
|
|
281
|
+
*/
|
|
282
|
+
interface OnEventDecorator<E extends Event = Event> {
|
|
283
|
+
<This extends Interface<Omit<ReactiveElement, "renderRoot">>>(value: (this: This, event: E) => void, context: ClassMethodDecoratorContext<This>): void;
|
|
284
|
+
(target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor): void;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Decorator to handle events from the event bus.
|
|
288
|
+
*
|
|
289
|
+
* @group events
|
|
290
|
+
*
|
|
291
|
+
* @param types - Event types to listen for. If omitted, all events will be handled.
|
|
292
|
+
* @returns The decorator function.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* class MyElement extends LitElement {
|
|
297
|
+
* @onEvent()
|
|
298
|
+
* private onMyEvent(event: Event) {
|
|
299
|
+
* console.log('Event received:', event);
|
|
300
|
+
* }
|
|
301
|
+
* }
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* class MyElement extends LitElement {
|
|
307
|
+
* @onEvent('MY_EVENT_TYPE')
|
|
308
|
+
* private onMyEvent(event: MyEvent) {
|
|
309
|
+
* console.log('Event received:', event);
|
|
310
|
+
* }
|
|
311
|
+
* }
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* class MyElement extends LitElement {
|
|
317
|
+
* @onEvent(['MY_EVENT_TYPE_1', 'MY_EVENT_TYPE_2'])
|
|
318
|
+
* private onMyEvent(event: Event) {
|
|
319
|
+
* console.log('Event received:', event);
|
|
320
|
+
* }
|
|
321
|
+
* }
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
declare function onEvent<E extends Event = Event>(types?: EventType | ReadonlyArray<EventType>): OnEventDecorator<E>;
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Controller that subscribes to events from the event bus.
|
|
328
|
+
*
|
|
329
|
+
* It automatically handles subscription and unsubscription based on the host element's lifecycle.
|
|
330
|
+
*
|
|
331
|
+
* @group events
|
|
332
|
+
*/
|
|
333
|
+
declare class OnEventController<E extends Event = Event> implements ReactiveController {
|
|
334
|
+
private bus;
|
|
335
|
+
private unsubscriber;
|
|
336
|
+
private readonly types;
|
|
337
|
+
private readonly handler;
|
|
338
|
+
/**
|
|
339
|
+
* @param host - the host element
|
|
340
|
+
* @param types - event types to listen for, if null, all events will be handled
|
|
341
|
+
* @param handler - the event handler function
|
|
342
|
+
*/
|
|
343
|
+
constructor(host: ReactiveElement, types: Optional<ReadonlyArray<EventType>>, handler: EventHandler<E>);
|
|
344
|
+
hostConnected(): void;
|
|
345
|
+
hostDisconnected(): void;
|
|
346
|
+
private resubscribe;
|
|
347
|
+
private cleanup;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Options for the {@link useOnEvents} hook.
|
|
352
|
+
*
|
|
353
|
+
* @group events
|
|
354
|
+
*/
|
|
355
|
+
interface UseOnEventsOptions<E extends Event = Event> {
|
|
356
|
+
/**
|
|
357
|
+
* Event handler function.
|
|
358
|
+
*/
|
|
359
|
+
handler: EventHandler<E>;
|
|
360
|
+
/**
|
|
361
|
+
* Event types to listen for. If null or undefined, all events will be handled.
|
|
362
|
+
*/
|
|
363
|
+
types?: Optional<EventType | ReadonlyArray<EventType>>;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Hook (controller) to handle events from the event bus.
|
|
367
|
+
*
|
|
368
|
+
* @group events
|
|
369
|
+
*
|
|
370
|
+
* @param host - the host element
|
|
371
|
+
* @param options - event handling options
|
|
372
|
+
* @param options.handler - event handler function
|
|
373
|
+
* @param options.types - event types to listen for, if null or undefined, all events will be handled
|
|
374
|
+
* @returns events subscription controller
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```typescript
|
|
378
|
+
* class MyElement extends LitElement {
|
|
379
|
+
* private eventHandler = useOnEvents(this, {
|
|
380
|
+
* handler: (event) => console.log(event),
|
|
381
|
+
* });
|
|
382
|
+
* }
|
|
383
|
+
* ```
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* ```typescript
|
|
387
|
+
* class MyElement extends LitElement {
|
|
388
|
+
* private eventHandler = useOnEvents(this, {
|
|
389
|
+
* types: [MyEvent],
|
|
390
|
+
* handler: (event) => console.log(event),
|
|
391
|
+
* });
|
|
392
|
+
* }
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
395
|
+
declare function useOnEvents<E extends Event = Event>(host: ReactiveElement, { types, handler }: UseOnEventsOptions): OnEventController<E>;
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Interface for the {@link onQuery} decorator.
|
|
399
|
+
*
|
|
400
|
+
* Supports both standard (TC39) and legacy experimental decorators.
|
|
401
|
+
*
|
|
402
|
+
* @group queries
|
|
403
|
+
*/
|
|
404
|
+
interface OnQueryDecorator<D = unknown, R = unknown> {
|
|
405
|
+
<This extends Interface<Omit<ReactiveElement, "renderRoot">>>(value: (this: This, data: D) => MaybePromise<R>, context: ClassMethodDecoratorContext<This>): void;
|
|
406
|
+
(target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor): void;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Decorator that registers a Lit element method as a query handler for the given type.
|
|
410
|
+
*
|
|
411
|
+
* The handler is registered when the host element connects to the DOM and unregistered when it disconnects.
|
|
412
|
+
*
|
|
413
|
+
* @group queries
|
|
414
|
+
*
|
|
415
|
+
* @param type - the query type to handle
|
|
416
|
+
* @returns the decorator function
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```typescript
|
|
420
|
+
* class MyElement extends LitElement {
|
|
421
|
+
* @onQuery("GET_USER_NAME")
|
|
422
|
+
* public onGetUserName(data: QueryData) {
|
|
423
|
+
* return "Alice";
|
|
424
|
+
* }
|
|
425
|
+
* }
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
declare function onQuery<D = unknown, R = unknown>(type: QueryType): OnQueryDecorator<D, R>;
|
|
42
429
|
|
|
430
|
+
/**
|
|
431
|
+
* Reactive controller that registers a query handler on the {@link QueryBus} for the host element's lifetime.
|
|
432
|
+
*
|
|
433
|
+
* The handler is registered when the host connects and unregistered when it disconnects.
|
|
434
|
+
* When the IoC context is updated (container revision change), the handler is re-registered automatically.
|
|
435
|
+
*
|
|
436
|
+
* @group queries
|
|
437
|
+
*/
|
|
438
|
+
declare class OnQueryController<D = unknown, R = unknown> implements ReactiveController {
|
|
439
|
+
private bus;
|
|
440
|
+
private unregister;
|
|
441
|
+
private readonly type;
|
|
442
|
+
private readonly handler;
|
|
443
|
+
/**
|
|
444
|
+
* @param host - the host element
|
|
445
|
+
* @param type - the query type to handle
|
|
446
|
+
* @param handler - the query handler function
|
|
447
|
+
*/
|
|
448
|
+
constructor(host: ReactiveElement, type: QueryType, handler: QueryHandler<D, R>);
|
|
449
|
+
hostConnected(): void;
|
|
450
|
+
hostDisconnected(): void;
|
|
451
|
+
private reregister;
|
|
452
|
+
private cleanup;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Options for the {@link useOnQuery} hook.
|
|
457
|
+
*
|
|
458
|
+
* @group queries
|
|
459
|
+
*/
|
|
460
|
+
interface UseOnQueryOptions<D = unknown, R = unknown> {
|
|
461
|
+
/**
|
|
462
|
+
* The query type to handle.
|
|
463
|
+
*/
|
|
464
|
+
type: QueryType;
|
|
465
|
+
/**
|
|
466
|
+
* The query handler function.
|
|
467
|
+
*/
|
|
468
|
+
handler: QueryHandler<D, R>;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Registers a query handler on the {@link QueryBus} for the host element's lifetime.
|
|
472
|
+
*
|
|
473
|
+
* @group queries
|
|
474
|
+
*
|
|
475
|
+
* @param host - the host element
|
|
476
|
+
* @param options - query handling options
|
|
477
|
+
* @param options.type - the query type to handle
|
|
478
|
+
* @param options.handler - the query handler function
|
|
479
|
+
* @returns the query controller instance
|
|
480
|
+
*
|
|
481
|
+
* @example
|
|
482
|
+
* ```typescript
|
|
483
|
+
* class MyElement extends LitElement {
|
|
484
|
+
* private getUserController = useOnQuery(this, {
|
|
485
|
+
* type: "GET_USER",
|
|
486
|
+
* handler: (data) => ({ name: "Alice" }),
|
|
487
|
+
* });
|
|
488
|
+
* }
|
|
489
|
+
* ```
|
|
490
|
+
*/
|
|
491
|
+
declare function useOnQuery<D = unknown, R = unknown>(host: ReactiveElement, { type, handler }: UseOnQueryOptions<D, R>): OnQueryController<D, R>;
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Interface for the IoC context value.
|
|
495
|
+
*
|
|
496
|
+
* @group context
|
|
497
|
+
*/
|
|
43
498
|
interface IocContext {
|
|
44
499
|
/**
|
|
45
|
-
* Inversify container.
|
|
500
|
+
* The Inversify IoC container.
|
|
46
501
|
*/
|
|
47
502
|
readonly container: Container;
|
|
48
503
|
/**
|
|
@@ -50,47 +505,266 @@ interface IocContext {
|
|
|
50
505
|
*/
|
|
51
506
|
readonly revision: number;
|
|
52
507
|
/**
|
|
53
|
-
*
|
|
508
|
+
* Function to force a revision update.
|
|
54
509
|
*/
|
|
55
510
|
readonly nextRevision: () => number;
|
|
56
511
|
}
|
|
512
|
+
/**
|
|
513
|
+
* Lit context for providing and consuming the IoC container.
|
|
514
|
+
*
|
|
515
|
+
* @group context
|
|
516
|
+
*/
|
|
57
517
|
declare const ContainerContext: {
|
|
58
518
|
__context__: IocContext;
|
|
59
519
|
};
|
|
60
520
|
|
|
61
|
-
|
|
521
|
+
/**
|
|
522
|
+
* Options for the {@link IocProviderController}.
|
|
523
|
+
*
|
|
524
|
+
* @group provision
|
|
525
|
+
*/
|
|
526
|
+
interface IocProviderControllerOptions {
|
|
527
|
+
/**
|
|
528
|
+
* Optional existing container to use. If not provided, a new one will be created.
|
|
529
|
+
*/
|
|
530
|
+
container?: Container;
|
|
531
|
+
/**
|
|
532
|
+
* Optional seed data to apply to the container.
|
|
533
|
+
*/
|
|
534
|
+
seed?: AnyObject;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Controller that provides an IoC container context to the host element and its children.
|
|
538
|
+
*
|
|
539
|
+
* It manages the lifecycle of the container and handles revision updates to notify consumers.
|
|
540
|
+
*
|
|
541
|
+
* @group provision
|
|
542
|
+
*/
|
|
543
|
+
declare class IocProviderController<E extends ReactiveControllerHost & HTMLElement = ReactiveControllerHost & HTMLElement> implements ReactiveController {
|
|
62
544
|
private readonly host;
|
|
63
|
-
protected provider: ContextProvider<typeof ContainerContext>;
|
|
545
|
+
protected readonly provider: ContextProvider<typeof ContainerContext>;
|
|
546
|
+
protected readonly seed?: AnyObject;
|
|
64
547
|
protected revision: number;
|
|
548
|
+
/**
|
|
549
|
+
* The managed Inversify IoC container.
|
|
550
|
+
*/
|
|
65
551
|
container: Container;
|
|
66
|
-
|
|
552
|
+
/**
|
|
553
|
+
* @returns current {@link IocContext} value served to child consumers
|
|
554
|
+
*/
|
|
555
|
+
get value(): IocContext;
|
|
556
|
+
/**
|
|
557
|
+
* @param host - the host element
|
|
558
|
+
* @param options - provisioning options
|
|
559
|
+
* @param options.container - optional existing container to use. If not provided, a new one will be created
|
|
560
|
+
* @param options.seed - optional seed data to apply to the container
|
|
561
|
+
*/
|
|
562
|
+
constructor(host: E, { container, seed }?: IocProviderControllerOptions);
|
|
67
563
|
hostConnected(): void;
|
|
68
564
|
hostDisconnected(): void;
|
|
69
565
|
nextRevision(): number;
|
|
70
566
|
}
|
|
71
567
|
|
|
72
|
-
|
|
568
|
+
/**
|
|
569
|
+
* Type definition for the ioc-provide decorator.
|
|
570
|
+
*
|
|
571
|
+
* @group provision
|
|
572
|
+
*/
|
|
573
|
+
interface IocProviderDecorator<E extends ReactiveElement = ReactiveElement> {
|
|
574
|
+
<C extends Interface<Omit<ReactiveElement, "renderRoot">>, V extends IocProviderController<E>>(value: ClassAccessorDecoratorTarget<C, V>, context: ClassAccessorDecoratorContext<C, V>): void;
|
|
575
|
+
<K extends PropertyKey, Proto extends Interface<Omit<ReactiveElement, "renderRoot">>>(protoOrDescriptor: Proto, name?: K): FieldMustMatchProvidedType<Proto, K, IocProviderController<E>>;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Decorator to provide an IoC container to child components.
|
|
579
|
+
*
|
|
580
|
+
* @group provision
|
|
581
|
+
*
|
|
582
|
+
* @param options - provisioning options including container and seed data
|
|
583
|
+
* @param options.container - optional existing container to use, if not provided, a new one will be created
|
|
584
|
+
* @param options.seed - optional seed data to apply to the container
|
|
585
|
+
* @returns IOC provision controller instance
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* ```typescript
|
|
589
|
+
* class MyRootElement extends LitElement {
|
|
590
|
+
* @iocProvide({ seed: { someData: 'value' } })
|
|
591
|
+
* private ioc!: IocProviderController;
|
|
592
|
+
* }
|
|
593
|
+
* ```
|
|
594
|
+
*/
|
|
595
|
+
declare function iocProvide<E extends ReactiveElement>({ container, seed, }?: IocProviderControllerOptions): IocProviderDecorator<E>;
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* @group general-types
|
|
599
|
+
*/
|
|
600
|
+
type Callable<T> = () => T;
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Options for the {@link InjectablesProviderController}.
|
|
604
|
+
*
|
|
605
|
+
* @group provision
|
|
606
|
+
*/
|
|
607
|
+
interface InjectablesProviderControllerOptions {
|
|
608
|
+
/**
|
|
609
|
+
* List of service entries to bind to the container.
|
|
610
|
+
*/
|
|
73
611
|
entries: ReadonlyArray<Newable<object> | InjectableDescriptor>;
|
|
74
|
-
|
|
612
|
+
/**
|
|
613
|
+
* Target IoC context to bind injectables into.
|
|
614
|
+
* If not provided, uses the context from the nearest provider.
|
|
615
|
+
* Accepts a static {@link IocContext} reference or a resolver function.
|
|
616
|
+
*/
|
|
617
|
+
into?: IocContext | Callable<IocContext>;
|
|
618
|
+
/**
|
|
619
|
+
* List of service identifiers to activate (get from container) immediately after binding.
|
|
620
|
+
*/
|
|
75
621
|
activate?: ReadonlyArray<ServiceIdentifier>;
|
|
622
|
+
/**
|
|
623
|
+
* Seed data to apply to the container before binding.
|
|
624
|
+
* Applied before entries are bound so that `@Inject(SEEDS_TOKEN)` works during activation.
|
|
625
|
+
*/
|
|
76
626
|
seeds?: SeedEntries;
|
|
77
627
|
}
|
|
78
|
-
|
|
628
|
+
/**
|
|
629
|
+
* Controller that binds a set of injectables to an IoC container when the host connects
|
|
630
|
+
* and unbinds them when the host disconnects.
|
|
631
|
+
*
|
|
632
|
+
* When no `into` context is provided, the controller uses the nearest {@link IocProviderController}
|
|
633
|
+
* ancestor via Lit context. Seeds are applied before entries so that `@Inject(SEEDS_TOKEN)`
|
|
634
|
+
* works during service activation.
|
|
635
|
+
*
|
|
636
|
+
* @group provision
|
|
637
|
+
*
|
|
638
|
+
* @example
|
|
639
|
+
* ```typescript
|
|
640
|
+
* class MyComponent extends LitElement {
|
|
641
|
+
* private services = new InjectablesProviderController(this, {
|
|
642
|
+
* entries: [AuthService, UserService],
|
|
643
|
+
* activate: [AuthService],
|
|
644
|
+
* seeds: [[AuthService, { role: "admin" }]],
|
|
645
|
+
* });
|
|
646
|
+
* }
|
|
647
|
+
* ```
|
|
648
|
+
*/
|
|
649
|
+
declare class InjectablesProviderController<E extends ReactiveControllerHost & HTMLElement = ReactiveControllerHost & HTMLElement> implements ReactiveController {
|
|
79
650
|
private readonly host;
|
|
80
|
-
readonly consumer
|
|
81
|
-
readonly entries
|
|
82
|
-
readonly activate
|
|
83
|
-
readonly seeds
|
|
84
|
-
readonly into
|
|
85
|
-
|
|
651
|
+
protected readonly consumer?: ContextConsumer<typeof ContainerContext, E>;
|
|
652
|
+
private readonly entries;
|
|
653
|
+
private readonly activate;
|
|
654
|
+
private readonly seeds;
|
|
655
|
+
private readonly into;
|
|
656
|
+
/**
|
|
657
|
+
* Tracks the context to which entries are currently bound, for correct cleanup on disconnect.
|
|
658
|
+
*/
|
|
659
|
+
private boundContext;
|
|
660
|
+
/**
|
|
661
|
+
* @param host - the host element
|
|
662
|
+
* @param options - provisioning options
|
|
663
|
+
* @param options.entries - list of service entries to bind to the container
|
|
664
|
+
* @param options.into - target IoC context; if omitted, uses the nearest provider context
|
|
665
|
+
* @param options.activate - list of service identifiers to activate immediately after binding
|
|
666
|
+
* @param options.seeds - seed data applied before binding
|
|
667
|
+
*/
|
|
668
|
+
constructor(host: E, options: InjectablesProviderControllerOptions);
|
|
86
669
|
hostConnected(): void;
|
|
87
670
|
hostDisconnected(): void;
|
|
671
|
+
private bind;
|
|
672
|
+
private unbind;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Type for the {@link injectablesProvide} decorator.
|
|
677
|
+
*
|
|
678
|
+
* Supports both standard (TC39) and legacy experimental decorators.
|
|
679
|
+
*
|
|
680
|
+
* @group provision
|
|
681
|
+
*/
|
|
682
|
+
interface InjectablesProviderDecorator<T extends ReactiveElement = ReactiveElement> {
|
|
683
|
+
<C extends Interface<Omit<ReactiveElement, "renderRoot">>, V extends InjectablesProviderController<T>>(value: ClassAccessorDecoratorTarget<C, V>, context: ClassAccessorDecoratorContext<C, V>): void;
|
|
684
|
+
<K extends PropertyKey, Proto extends Interface<Omit<ReactiveElement, "renderRoot">>>(protoOrDescriptor: Proto, name?: K): FieldMustMatchProvidedType<Proto, K, InjectablesProviderController<T>>;
|
|
88
685
|
}
|
|
686
|
+
/**
|
|
687
|
+
* Decorator that binds a set of injectables to the nearest IoC container for the host element's lifetime.
|
|
688
|
+
*
|
|
689
|
+
* Entries are bound when the host connects and unbound when it disconnects.
|
|
690
|
+
* The decorated accessor or property holds the resulting {@link InjectablesProviderController} instance.
|
|
691
|
+
*
|
|
692
|
+
* @group provision
|
|
693
|
+
*
|
|
694
|
+
* @param options - provisioning options
|
|
695
|
+
* @returns injectables provider decorator instance
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
698
|
+
* ```typescript
|
|
699
|
+
* class MyComponent extends LitElement {
|
|
700
|
+
* @injectablesProvide({ entries: [AuthService, UserService], activate: [AuthService] })
|
|
701
|
+
* public services!: InjectablesProviderController<MyComponent>;
|
|
702
|
+
* }
|
|
703
|
+
* ```
|
|
704
|
+
*/
|
|
705
|
+
declare function injectablesProvide<E extends ReactiveElement = ReactiveElement>(options: InjectablesProviderControllerOptions): InjectablesProviderDecorator<E>;
|
|
89
706
|
|
|
90
|
-
|
|
707
|
+
/**
|
|
708
|
+
* Binds a set of injectables to the nearest IoC container for the host element's lifetime.
|
|
709
|
+
*
|
|
710
|
+
* Entries are bound when the host connects and unbound when it disconnects.
|
|
711
|
+
*
|
|
712
|
+
* @group provision
|
|
713
|
+
*
|
|
714
|
+
* @param host - the host element
|
|
715
|
+
* @param options - provisioning options
|
|
716
|
+
* @param options.entries - list of service entries to bind to the container
|
|
717
|
+
* @param options.into - target IoC context; if omitted, uses the nearest provider context
|
|
718
|
+
* @param options.activate - list of service identifiers to activate immediately after binding
|
|
719
|
+
* @param options.seeds - seed data applied before binding
|
|
720
|
+
* @returns the controller instance
|
|
721
|
+
*
|
|
722
|
+
* @example
|
|
723
|
+
* ```typescript
|
|
724
|
+
* class MyComponent extends LitElement {
|
|
725
|
+
* private services = useInjectablesProvider(this, {
|
|
726
|
+
* entries: [AuthService, UserService],
|
|
727
|
+
* activate: [AuthService],
|
|
728
|
+
* });
|
|
729
|
+
* }
|
|
730
|
+
* ```
|
|
731
|
+
*/
|
|
732
|
+
declare function useInjectablesProvider<E extends ReactiveControllerHost & HTMLElement>(host: E, options: InjectablesProviderControllerOptions): InjectablesProviderController<E>;
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Options for the {@link useIocProvision} hook.
|
|
736
|
+
*
|
|
737
|
+
* @group provision
|
|
738
|
+
*/
|
|
739
|
+
interface UseIocProvisionOptions {
|
|
740
|
+
/**
|
|
741
|
+
* Optional existing container to use.
|
|
742
|
+
*/
|
|
91
743
|
container?: Container;
|
|
744
|
+
/**
|
|
745
|
+
* Optional seed data to apply to the container.
|
|
746
|
+
*/
|
|
747
|
+
seed?: AnyObject;
|
|
92
748
|
}
|
|
93
|
-
|
|
749
|
+
/**
|
|
750
|
+
* Hook (controller) to provide an IoC container to the host element and its children.
|
|
751
|
+
*
|
|
752
|
+
* @group provision
|
|
753
|
+
*
|
|
754
|
+
* @param host - the host element
|
|
755
|
+
* @param options - provisioning options
|
|
756
|
+
* @param options.container - optional existing container to use
|
|
757
|
+
* @param options.seed - optional seed data to apply to the container
|
|
758
|
+
* @returns ioc provision controller instance
|
|
759
|
+
*
|
|
760
|
+
* @example
|
|
761
|
+
* ```typescript
|
|
762
|
+
* class MyRootElement extends LitElement {
|
|
763
|
+
* private ioc = useIocProvision(this, { seed: { initialData: '...' } });
|
|
764
|
+
* }
|
|
765
|
+
* ```
|
|
766
|
+
*/
|
|
767
|
+
declare function useIocProvision<E extends ReactiveControllerHost & HTMLElement>(host: E, options?: UseIocProvisionOptions): IocProviderController<E>;
|
|
94
768
|
|
|
95
|
-
export { ContainerContext,
|
|
96
|
-
export type { InjectionDecorator, UseInjectionOptions, UseInjectionValue };
|
|
769
|
+
export { ContainerContext, InjectablesProviderController, IocProviderController, OnCommandController, OnEventController, OnQueryController, injectablesProvide, injection, iocProvide, onCommand, onEvent, onQuery, useInjectablesProvider, useInjection, useIocProvision, useOnCommand, useOnEvents, useOnQuery };
|
|
770
|
+
export type { InjectablesProviderControllerOptions, InjectablesProviderDecorator, InjectionDecorator, InjectionOptions, IocProviderControllerOptions, IocProviderDecorator, OnCommandDecorator, OnEventDecorator, OnQueryDecorator, UseInjectionOptions, UseInjectionValue, UseIocProvisionOptions, UseOnCommandOptions, UseOnEventsOptions, UseOnQueryOptions };
|