Skip to content

Commit

Permalink
feat: allow to disable preload devices and sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux committed Sep 18, 2019
1 parent de29b28 commit 4b6f3f5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ringcentral-call-control",
"version": "0.1.1",
"version": "0.1.2",
"main": "lib/index.js",
"license": "MIT",
"repository": {
Expand Down
37 changes: 30 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,32 @@ 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;
this._sessionsMap = new Map;
this._devices = [];
this._ready = false;
this._initializePromise = null;
this._preloadSessions = preloadSessions;
this._preloadDevices = preloadDevices;
this._currentExtension = extensionInfo;
this.initialize();
}

Expand All @@ -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');
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -188,7 +211,7 @@ export class RingCentralCallControl extends EventEmitter {
}
}

private async loadTelephoneSessions(activeCalls) {
public async loadSessions(activeCalls) {
if (activeCalls.length === 0) {
return;
}
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 4b6f3f5

Please sign in to comment.