@interopio/ng
Version:
IO Connect library for Angular - Browser and Desktop
187 lines (180 loc) • 8.73 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Inject, APP_INITIALIZER, NgModule } from '@angular/core';
import { Subject, ReplaySubject } from 'rxjs';
const CONFIG_TOKEN = new InjectionToken("CONFIG_TOKEN");
class IOConnectConfigService {
constructor(userSettings) {
this._userSettings = Object.assign({ holdInit: true }, userSettings);
this.isDesktop = (typeof window.glue42gd !== "undefined") || (typeof window.iodesktop !== "undefined");
}
getSettings() {
return this._userSettings;
}
verifyConfig() {
if (this._userSettings.browser && this._userSettings.browserPlatform) {
throw new Error("Cannot initialize, because the config is over-specified: it contains settings for both web and webPlatform. Please set one or the other");
}
}
getFactory() {
if (this.isDesktop) {
return this._userSettings.desktop?.factory ||
this._userSettings.browser?.factory ||
this._userSettings.browserPlatform?.factory ||
window.Glue;
}
return this._userSettings.browser?.factory || this._userSettings.browserPlatform?.factory || window.IOBrowser || window.IOBrowserPlatform;
}
getConfig() {
if (this.isDesktop) {
return this._userSettings.desktop?.config ||
this._userSettings.browser?.config ||
this._userSettings.browserPlatform?.config;
}
return this._userSettings.browser?.config || this._userSettings.browserPlatform?.config;
}
getHoldInit() {
return !!this._userSettings.holdInit;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectConfigService, deps: [{ token: CONFIG_TOKEN }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectConfigService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectConfigService, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
type: Inject,
args: [CONFIG_TOKEN]
}] }]; } });
class IOConnectInitializer {
constructor(configService) {
this.configService = configService;
this.defaultInitTimeoutMilliseconds = 60000;
this.initializationSource = new Subject();
}
start() {
try {
this.configService.verifyConfig();
}
catch (error) {
this.initializationSource.next({ error: { message: error.message } });
this.initializationSource.complete();
return Promise.resolve();
}
const config = this.configService.getConfig();
const factory = this.configService.getFactory();
if (!factory) {
const errorMessage = "Initialization failed, because no Glue Factory function was found. Please provide a factory function when importing the Glue42Ng module. Alternatively make sure there is a GlueWeb or Glue or GlueWebPlatform function attached to the global window object";
this.initializationSource.next({ error: { message: errorMessage } });
this.initializationSource.complete();
return Promise.resolve();
}
const gluePromise = this.safeCallFactory(config, factory, this.defaultInitTimeoutMilliseconds, `Glue factory timeout hit. Set at: ${this.defaultInitTimeoutMilliseconds} milliseconds`)
.then((glueResult) => {
this.initializationSource.next({ glueInstance: glueResult.glue });
this.initializationSource.complete();
})
.catch((error) => {
this.initializationSource.next({ error });
this.initializationSource.complete();
});
return this.configService.getSettings().holdInit ? gluePromise : Promise.resolve();
}
onState() {
return this.initializationSource.asObservable();
}
safeCallFactory(config, factory, timeoutMilliseconds, timeoutMessage) {
return new Promise((resolve, reject) => {
let timeoutHit = false;
const timeout = setTimeout(() => {
timeoutHit = true;
const message = timeoutMessage;
reject(message);
}, timeoutMilliseconds);
factory(config)
.then((result) => {
if (!timeoutHit) {
clearTimeout(timeout);
const glue = result.glue || result.io || result;
resolve({ glue });
}
})
.catch((error) => {
if (!timeoutHit) {
clearTimeout(timeout);
reject(error);
}
});
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectInitializer, deps: [{ token: IOConnectConfigService }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectInitializer }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectInitializer, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: IOConnectConfigService }]; } });
class IOConnectStore {
constructor(initializer) {
this.initializer = initializer;
this.readySource = new ReplaySubject(1);
this.initializer.onState().subscribe(this.handleState.bind(this));
}
ready() {
return this.readySource.asObservable();
}
getInitError() {
return this._initError;
}
getIOConnect() {
if (!this.glueInstance) {
throw new Error("Accessing uninitialized glue. This might happen, because Glue is not initialized yet or because there was an error during Glue initialization. Please check the initError object or subscribe to the ready observable.");
}
return this.glueInstance;
}
handleState(result) {
if (result.glueInstance) {
this.glueInstance = result.glueInstance;
this.readySource.next({});
return;
}
if (result.error) {
this._initError = result.error;
this.readySource.next({ error: this.getInitError() });
return;
}
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectStore, deps: [{ token: IOConnectInitializer }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectStore }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectStore, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: IOConnectInitializer }]; } });
const initFactory = (initializer) => () => initializer.start();
class IOConnectNg {
static forRoot(settings) {
return {
ngModule: IOConnectNg,
providers: [
{
provide: APP_INITIALIZER,
useFactory: initFactory,
multi: true,
deps: [IOConnectInitializer, IOConnectConfigService, IOConnectStore]
},
{
provide: CONFIG_TOKEN,
useValue: settings
},
IOConnectConfigService,
IOConnectStore,
IOConnectInitializer
]
};
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectNg, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: IOConnectNg }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectNg }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: IOConnectNg, decorators: [{
type: NgModule
}] });
export { IOConnectNg, IOConnectStore, initFactory };
//# sourceMappingURL=interopio-ng.mjs.map