diff --git a/package.json b/package.json index 6e47a34..38acc6b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ringcentral-call-control", - "version": "0.1.1", + "version": "0.1.2", "main": "lib/index.js", "license": "MIT", "repository": { diff --git a/src/index.ts b/src/index.ts index e310f62..37e90ec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,8 +74,22 @@ export class RingCentralCallControl extends EventEmitter { private _accountLevel: boolean; private _ready: boolean; private _initializePromise: any; + private _preloadSessions: boolean; + private _preloadDevices: boolean; - constructor({ sdk, accountLevel } : { sdk: RingCentral, accountLevel?: boolean }) { + constructor({ + sdk, + accountLevel, + preloadSessions = true, + preloadDevices = true, + extensionInfo, + } : { + sdk: RingCentral, + accountLevel?: boolean, + preloadSessions?: boolean, + preloadDevices?: boolean, + extensionInfo?: Extension, + }) { super(); this._accountLevel = !!accountLevel; this._sdk = sdk; @@ -83,6 +97,9 @@ export class RingCentralCallControl extends EventEmitter { this._devices = []; this._ready = false; this._initializePromise = null; + this._preloadSessions = preloadSessions; + this._preloadDevices = preloadDevices; + this._currentExtension = extensionInfo; this.initialize(); } @@ -98,9 +115,15 @@ export class RingCentralCallControl extends EventEmitter { } private async _initialize() { - await this.loadCurrentExtension(); - await this.loadSessions(); - await this.loadDevices(); + if (!this._currentExtension) { + await this.loadCurrentExtension(); + } + if (this._preloadSessions) { + await this.preloadSessions(); + } + if (this._preloadDevices) { + await this.loadDevices(); + } this._ready = true; this.emit('initialized'); } @@ -157,9 +180,9 @@ export class RingCentralCallControl extends EventEmitter { } } - private async loadSessions() { + private async preloadSessions() { const activeCalls = await this.loadActiveCalls(); - await this.loadTelephoneSessions(activeCalls); + await this.loadSessions(activeCalls); } private async loadActiveCalls() { @@ -188,7 +211,7 @@ export class RingCentralCallControl extends EventEmitter { } } - private async loadTelephoneSessions(activeCalls) { + public async loadSessions(activeCalls) { if (activeCalls.length === 0) { return; } diff --git a/test/index.test.ts b/test/index.test.ts index b77e3cc..37ebfa6 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -63,6 +63,26 @@ describe('RingCentral Call Control :: Index', () => { }); }); + describe('Initialize without preload', () => { + beforeAll(async () => { + rcCallControl = new RingCentralCallControl({ + sdk, + preloadDevices: false, + preloadSessions: false, + extensionInfo, + }); + await rcCallControl.initialize(); + }); + + it('should be ready after initialized', () => { + expect(rcCallControl.ready).toEqual(true); + expect(rcCallControl.devices.length).toEqual(0); + expect(rcCallControl.sessions.length).toEqual(0); + expect(rcCallControl.extensionId).toEqual(String(extensionInfo.id)); + expect(rcCallControl.accountId).toEqual(String(extensionInfo.account.id)); + }); + }); + describe('Initialize with API failed', () => { beforeAll(async () => { rcCallControl = new RingCentralCallControl({ sdk });