123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- import { Deferred } from '@firebase/util';
- class Component {
-
- constructor(name, instanceFactory, type) {
- this.name = name;
- this.instanceFactory = instanceFactory;
- this.type = type;
- this.multipleInstances = false;
-
- this.serviceProps = {};
- this.instantiationMode = "LAZY" ;
- this.onInstanceCreated = null;
- }
- setInstantiationMode(mode) {
- this.instantiationMode = mode;
- return this;
- }
- setMultipleInstances(multipleInstances) {
- this.multipleInstances = multipleInstances;
- return this;
- }
- setServiceProps(props) {
- this.serviceProps = props;
- return this;
- }
- setInstanceCreatedCallback(callback) {
- this.onInstanceCreated = callback;
- return this;
- }
- }
- const DEFAULT_ENTRY_NAME = '[DEFAULT]';
- class Provider {
- constructor(name, container) {
- this.name = name;
- this.container = container;
- this.component = null;
- this.instances = new Map();
- this.instancesDeferred = new Map();
- this.instancesOptions = new Map();
- this.onInitCallbacks = new Map();
- }
-
- get(identifier) {
-
- const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
- if (!this.instancesDeferred.has(normalizedIdentifier)) {
- const deferred = new Deferred();
- this.instancesDeferred.set(normalizedIdentifier, deferred);
- if (this.isInitialized(normalizedIdentifier) ||
- this.shouldAutoInitialize()) {
-
- try {
- const instance = this.getOrInitializeService({
- instanceIdentifier: normalizedIdentifier
- });
- if (instance) {
- deferred.resolve(instance);
- }
- }
- catch (e) {
-
-
- }
- }
- }
- return this.instancesDeferred.get(normalizedIdentifier).promise;
- }
- getImmediate(options) {
- var _a;
-
- const normalizedIdentifier = this.normalizeInstanceIdentifier(options === null || options === void 0 ? void 0 : options.identifier);
- const optional = (_a = options === null || options === void 0 ? void 0 : options.optional) !== null && _a !== void 0 ? _a : false;
- if (this.isInitialized(normalizedIdentifier) ||
- this.shouldAutoInitialize()) {
- try {
- return this.getOrInitializeService({
- instanceIdentifier: normalizedIdentifier
- });
- }
- catch (e) {
- if (optional) {
- return null;
- }
- else {
- throw e;
- }
- }
- }
- else {
-
- if (optional) {
- return null;
- }
- else {
- throw Error(`Service ${this.name} is not available`);
- }
- }
- }
- getComponent() {
- return this.component;
- }
- setComponent(component) {
- if (component.name !== this.name) {
- throw Error(`Mismatching Component ${component.name} for Provider ${this.name}.`);
- }
- if (this.component) {
- throw Error(`Component for ${this.name} has already been provided`);
- }
- this.component = component;
-
- if (!this.shouldAutoInitialize()) {
- return;
- }
-
- if (isComponentEager(component)) {
- try {
- this.getOrInitializeService({ instanceIdentifier: DEFAULT_ENTRY_NAME });
- }
- catch (e) {
-
-
-
-
- }
- }
-
-
-
- for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
- const normalizedIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
- try {
-
- const instance = this.getOrInitializeService({
- instanceIdentifier: normalizedIdentifier
- });
- instanceDeferred.resolve(instance);
- }
- catch (e) {
-
-
- }
- }
- }
- clearInstance(identifier = DEFAULT_ENTRY_NAME) {
- this.instancesDeferred.delete(identifier);
- this.instancesOptions.delete(identifier);
- this.instances.delete(identifier);
- }
-
-
- async delete() {
- const services = Array.from(this.instances.values());
- await Promise.all([
- ...services
- .filter(service => 'INTERNAL' in service)
-
- .map(service => service.INTERNAL.delete()),
- ...services
- .filter(service => '_delete' in service)
-
- .map(service => service._delete())
- ]);
- }
- isComponentSet() {
- return this.component != null;
- }
- isInitialized(identifier = DEFAULT_ENTRY_NAME) {
- return this.instances.has(identifier);
- }
- getOptions(identifier = DEFAULT_ENTRY_NAME) {
- return this.instancesOptions.get(identifier) || {};
- }
- initialize(opts = {}) {
- const { options = {} } = opts;
- const normalizedIdentifier = this.normalizeInstanceIdentifier(opts.instanceIdentifier);
- if (this.isInitialized(normalizedIdentifier)) {
- throw Error(`${this.name}(${normalizedIdentifier}) has already been initialized`);
- }
- if (!this.isComponentSet()) {
- throw Error(`Component ${this.name} has not been registered yet`);
- }
- const instance = this.getOrInitializeService({
- instanceIdentifier: normalizedIdentifier,
- options
- });
-
- for (const [instanceIdentifier, instanceDeferred] of this.instancesDeferred.entries()) {
- const normalizedDeferredIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
- if (normalizedIdentifier === normalizedDeferredIdentifier) {
- instanceDeferred.resolve(instance);
- }
- }
- return instance;
- }
-
- onInit(callback, identifier) {
- var _a;
- const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
- const existingCallbacks = (_a = this.onInitCallbacks.get(normalizedIdentifier)) !== null && _a !== void 0 ? _a : new Set();
- existingCallbacks.add(callback);
- this.onInitCallbacks.set(normalizedIdentifier, existingCallbacks);
- const existingInstance = this.instances.get(normalizedIdentifier);
- if (existingInstance) {
- callback(existingInstance, normalizedIdentifier);
- }
- return () => {
- existingCallbacks.delete(callback);
- };
- }
-
- invokeOnInitCallbacks(instance, identifier) {
- const callbacks = this.onInitCallbacks.get(identifier);
- if (!callbacks) {
- return;
- }
- for (const callback of callbacks) {
- try {
- callback(instance, identifier);
- }
- catch (_a) {
-
- }
- }
- }
- getOrInitializeService({ instanceIdentifier, options = {} }) {
- let instance = this.instances.get(instanceIdentifier);
- if (!instance && this.component) {
- instance = this.component.instanceFactory(this.container, {
- instanceIdentifier: normalizeIdentifierForFactory(instanceIdentifier),
- options
- });
- this.instances.set(instanceIdentifier, instance);
- this.instancesOptions.set(instanceIdentifier, options);
-
- this.invokeOnInitCallbacks(instance, instanceIdentifier);
-
- if (this.component.onInstanceCreated) {
- try {
- this.component.onInstanceCreated(this.container, instanceIdentifier, instance);
- }
- catch (_a) {
-
- }
- }
- }
- return instance || null;
- }
- normalizeInstanceIdentifier(identifier = DEFAULT_ENTRY_NAME) {
- if (this.component) {
- return this.component.multipleInstances ? identifier : DEFAULT_ENTRY_NAME;
- }
- else {
- return identifier;
- }
- }
- shouldAutoInitialize() {
- return (!!this.component &&
- this.component.instantiationMode !== "EXPLICIT" );
- }
- }
- function normalizeIdentifierForFactory(identifier) {
- return identifier === DEFAULT_ENTRY_NAME ? undefined : identifier;
- }
- function isComponentEager(component) {
- return component.instantiationMode === "EAGER" ;
- }
- class ComponentContainer {
- constructor(name) {
- this.name = name;
- this.providers = new Map();
- }
-
- addComponent(component) {
- const provider = this.getProvider(component.name);
- if (provider.isComponentSet()) {
- throw new Error(`Component ${component.name} has already been registered with ${this.name}`);
- }
- provider.setComponent(component);
- }
- addOrOverwriteComponent(component) {
- const provider = this.getProvider(component.name);
- if (provider.isComponentSet()) {
-
- this.providers.delete(component.name);
- }
- this.addComponent(component);
- }
-
- getProvider(name) {
- if (this.providers.has(name)) {
- return this.providers.get(name);
- }
-
- const provider = new Provider(name, this);
- this.providers.set(name, provider);
- return provider;
- }
- getProviders() {
- return Array.from(this.providers.values());
- }
- }
- export { Component, ComponentContainer, Provider };
|