@tmorin/ceb-messaging-builder-inversion
Version:
The package is part of the `<ceb/>` library. It provides a builder to resolve and inject a bus from a container and then subscribe to event.
62 lines • 1.8 kB
JavaScript
import { GatewaySymbol } from "@tmorin/ceb-messaging-core";
import { AbstractGatewayBuilder } from "@tmorin/ceb-messaging-builder-core";
/**
* The builder injects a {@link Gateway} in Custom Elements.
*
* @template E the type of the Custom Element
*/
export class GatewayInversionBuilder extends AbstractGatewayBuilder {
/**
* @internal
*/
constructor(_propName = "gateway", _key = GatewaySymbol, _provider) {
super(() => {
let _container;
if (_provider) {
_container = _provider();
}
else if (GatewayInversionBuilder.DEFAULT_CONTAINER) {
_container = GatewayInversionBuilder.DEFAULT_CONTAINER;
}
else {
throw new TypeError("GatewayInversionBuilder - unable to resolve a Container");
}
return _container.registry.resolve(_key);
}, _propName);
this._key = _key;
this._provider = _provider;
}
/**
* Set the default {@link Container}.
* @param container the container
* @internal
*/
static setDefaultContainer(container) {
GatewayInversionBuilder.DEFAULT_CONTAINER = container;
}
/**
* Provides a fresh builder.
* @param propName the property name
* @template E the type of the Custom Element
*/
static get(propName) {
return new GatewayInversionBuilder(propName);
}
/**
* Set the registry key.
* @param key the registry key
*/
key(key) {
this._key = key;
return this;
}
/**
* Set the Container provider.
* @param provider the provider
*/
provider(provider) {
this._provider = provider;
return this;
}
}
//# sourceMappingURL=builder.js.map