123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', { value: true });
- var tslib = require('tslib');
- var util = require('@firebase/util');
- var Component = (function () {
-
- function Component(name, instanceFactory, type) {
- this.name = name;
- this.instanceFactory = instanceFactory;
- this.type = type;
- this.multipleInstances = false;
-
- this.serviceProps = {};
- this.instantiationMode = "LAZY" ;
- this.onInstanceCreated = null;
- }
- Component.prototype.setInstantiationMode = function (mode) {
- this.instantiationMode = mode;
- return this;
- };
- Component.prototype.setMultipleInstances = function (multipleInstances) {
- this.multipleInstances = multipleInstances;
- return this;
- };
- Component.prototype.setServiceProps = function (props) {
- this.serviceProps = props;
- return this;
- };
- Component.prototype.setInstanceCreatedCallback = function (callback) {
- this.onInstanceCreated = callback;
- return this;
- };
- return Component;
- }());
- var DEFAULT_ENTRY_NAME = '[DEFAULT]';
- var Provider = (function () {
- function Provider(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();
- }
-
- Provider.prototype.get = function (identifier) {
-
- var normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
- if (!this.instancesDeferred.has(normalizedIdentifier)) {
- var deferred = new util.Deferred();
- this.instancesDeferred.set(normalizedIdentifier, deferred);
- if (this.isInitialized(normalizedIdentifier) ||
- this.shouldAutoInitialize()) {
-
- try {
- var instance = this.getOrInitializeService({
- instanceIdentifier: normalizedIdentifier
- });
- if (instance) {
- deferred.resolve(instance);
- }
- }
- catch (e) {
-
-
- }
- }
- }
- return this.instancesDeferred.get(normalizedIdentifier).promise;
- };
- Provider.prototype.getImmediate = function (options) {
- var _a;
-
- var normalizedIdentifier = this.normalizeInstanceIdentifier(options === null || options === void 0 ? void 0 : options.identifier);
- var 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 ".concat(this.name, " is not available"));
- }
- }
- };
- Provider.prototype.getComponent = function () {
- return this.component;
- };
- Provider.prototype.setComponent = function (component) {
- var e_1, _a;
- if (component.name !== this.name) {
- throw Error("Mismatching Component ".concat(component.name, " for Provider ").concat(this.name, "."));
- }
- if (this.component) {
- throw Error("Component for ".concat(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) {
-
-
-
-
- }
- }
- try {
-
-
-
- for (var _b = tslib.__values(this.instancesDeferred.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
- var _d = tslib.__read(_c.value, 2), instanceIdentifier = _d[0], instanceDeferred = _d[1];
- var normalizedIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
- try {
-
- var instance = this.getOrInitializeService({
- instanceIdentifier: normalizedIdentifier
- });
- instanceDeferred.resolve(instance);
- }
- catch (e) {
-
-
- }
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_1) throw e_1.error; }
- }
- };
- Provider.prototype.clearInstance = function (identifier) {
- if (identifier === void 0) { identifier = DEFAULT_ENTRY_NAME; }
- this.instancesDeferred.delete(identifier);
- this.instancesOptions.delete(identifier);
- this.instances.delete(identifier);
- };
-
-
- Provider.prototype.delete = function () {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var services;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- services = Array.from(this.instances.values());
- return [4 , Promise.all(tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(services
- .filter(function (service) { return 'INTERNAL' in service; })
-
- .map(function (service) { return service.INTERNAL.delete(); })), false), tslib.__read(services
- .filter(function (service) { return '_delete' in service; })
-
- .map(function (service) { return service._delete(); })), false))];
- case 1:
- _a.sent();
- return [2 ];
- }
- });
- });
- };
- Provider.prototype.isComponentSet = function () {
- return this.component != null;
- };
- Provider.prototype.isInitialized = function (identifier) {
- if (identifier === void 0) { identifier = DEFAULT_ENTRY_NAME; }
- return this.instances.has(identifier);
- };
- Provider.prototype.getOptions = function (identifier) {
- if (identifier === void 0) { identifier = DEFAULT_ENTRY_NAME; }
- return this.instancesOptions.get(identifier) || {};
- };
- Provider.prototype.initialize = function (opts) {
- var e_2, _a;
- if (opts === void 0) { opts = {}; }
- var _b = opts.options, options = _b === void 0 ? {} : _b;
- var normalizedIdentifier = this.normalizeInstanceIdentifier(opts.instanceIdentifier);
- if (this.isInitialized(normalizedIdentifier)) {
- throw Error("".concat(this.name, "(").concat(normalizedIdentifier, ") has already been initialized"));
- }
- if (!this.isComponentSet()) {
- throw Error("Component ".concat(this.name, " has not been registered yet"));
- }
- var instance = this.getOrInitializeService({
- instanceIdentifier: normalizedIdentifier,
- options: options
- });
- try {
-
- for (var _c = tslib.__values(this.instancesDeferred.entries()), _d = _c.next(); !_d.done; _d = _c.next()) {
- var _e = tslib.__read(_d.value, 2), instanceIdentifier = _e[0], instanceDeferred = _e[1];
- var normalizedDeferredIdentifier = this.normalizeInstanceIdentifier(instanceIdentifier);
- if (normalizedIdentifier === normalizedDeferredIdentifier) {
- instanceDeferred.resolve(instance);
- }
- }
- }
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
- finally {
- try {
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
- }
- finally { if (e_2) throw e_2.error; }
- }
- return instance;
- };
-
- Provider.prototype.onInit = function (callback, identifier) {
- var _a;
- var normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
- var existingCallbacks = (_a = this.onInitCallbacks.get(normalizedIdentifier)) !== null && _a !== void 0 ? _a : new Set();
- existingCallbacks.add(callback);
- this.onInitCallbacks.set(normalizedIdentifier, existingCallbacks);
- var existingInstance = this.instances.get(normalizedIdentifier);
- if (existingInstance) {
- callback(existingInstance, normalizedIdentifier);
- }
- return function () {
- existingCallbacks.delete(callback);
- };
- };
-
- Provider.prototype.invokeOnInitCallbacks = function (instance, identifier) {
- var e_3, _a;
- var callbacks = this.onInitCallbacks.get(identifier);
- if (!callbacks) {
- return;
- }
- try {
- for (var callbacks_1 = tslib.__values(callbacks), callbacks_1_1 = callbacks_1.next(); !callbacks_1_1.done; callbacks_1_1 = callbacks_1.next()) {
- var callback = callbacks_1_1.value;
- try {
- callback(instance, identifier);
- }
- catch (_b) {
-
- }
- }
- }
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
- finally {
- try {
- if (callbacks_1_1 && !callbacks_1_1.done && (_a = callbacks_1.return)) _a.call(callbacks_1);
- }
- finally { if (e_3) throw e_3.error; }
- }
- };
- Provider.prototype.getOrInitializeService = function (_a) {
- var instanceIdentifier = _a.instanceIdentifier, _b = _a.options, options = _b === void 0 ? {} : _b;
- var instance = this.instances.get(instanceIdentifier);
- if (!instance && this.component) {
- instance = this.component.instanceFactory(this.container, {
- instanceIdentifier: normalizeIdentifierForFactory(instanceIdentifier),
- options: 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 (_c) {
-
- }
- }
- }
- return instance || null;
- };
- Provider.prototype.normalizeInstanceIdentifier = function (identifier) {
- if (identifier === void 0) { identifier = DEFAULT_ENTRY_NAME; }
- if (this.component) {
- return this.component.multipleInstances ? identifier : DEFAULT_ENTRY_NAME;
- }
- else {
- return identifier;
- }
- };
- Provider.prototype.shouldAutoInitialize = function () {
- return (!!this.component &&
- this.component.instantiationMode !== "EXPLICIT" );
- };
- return Provider;
- }());
- function normalizeIdentifierForFactory(identifier) {
- return identifier === DEFAULT_ENTRY_NAME ? undefined : identifier;
- }
- function isComponentEager(component) {
- return component.instantiationMode === "EAGER" ;
- }
- var ComponentContainer = (function () {
- function ComponentContainer(name) {
- this.name = name;
- this.providers = new Map();
- }
-
- ComponentContainer.prototype.addComponent = function (component) {
- var provider = this.getProvider(component.name);
- if (provider.isComponentSet()) {
- throw new Error("Component ".concat(component.name, " has already been registered with ").concat(this.name));
- }
- provider.setComponent(component);
- };
- ComponentContainer.prototype.addOrOverwriteComponent = function (component) {
- var provider = this.getProvider(component.name);
- if (provider.isComponentSet()) {
-
- this.providers.delete(component.name);
- }
- this.addComponent(component);
- };
-
- ComponentContainer.prototype.getProvider = function (name) {
- if (this.providers.has(name)) {
- return this.providers.get(name);
- }
-
- var provider = new Provider(name, this);
- this.providers.set(name, provider);
- return provider;
- };
- ComponentContainer.prototype.getProviders = function () {
- return Array.from(this.providers.values());
- };
- return ComponentContainer;
- }());
- exports.Component = Component;
- exports.ComponentContainer = ComponentContainer;
- exports.Provider = Provider;
|