From 9ad60b78d55382dd61991e7cc42c39fe9bbbd5f2 Mon Sep 17 00:00:00 2001 From: Marcos Passos Date: Fri, 28 May 2021 17:16:35 -0400 Subject: [PATCH] Add property to check whether the plug has been initialized --- src/plug.ts | 13 +++++++++---- test/plug.test.ts | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/plug.ts b/src/plug.ts index 5ab5bf9a..8062eb87 100644 --- a/src/plug.ts +++ b/src/plug.ts @@ -33,6 +33,7 @@ export interface Plug extends EapFeatures { readonly tracker: TrackerFacade; readonly user: UserFacade; readonly session: SessionFacade; + readonly initialized: boolean; readonly flushed: Promise; readonly plugged: Promise; @@ -78,10 +79,10 @@ export class GlobalPlug implements Plug { private initialize: {(): void}; - private initialized: Promise; + private ready: Promise; public constructor() { - this.initialized = new Promise(resolve => { + this.ready = new Promise(resolve => { this.initialize = resolve; }); } @@ -243,8 +244,12 @@ export class GlobalPlug implements Plug { }); } + public get initialized(): boolean { + return this.instance !== undefined; + } + public get plugged(): Promise { - return this.initialized.then(() => this); + return this.ready.then(() => this); } public get flushed(): Promise { @@ -361,7 +366,7 @@ export class GlobalPlug implements Plug { delete this.instance; this.plugins = {}; - this.initialized = new Promise(resolve => { + this.ready = new Promise(resolve => { this.initialize = resolve; }); diff --git a/test/plug.test.ts b/test/plug.test.ts index 39eb04fd..6e023598 100644 --- a/test/plug.test.ts +++ b/test/plug.test.ts @@ -1,10 +1,9 @@ import {SdkFacade, Configuration as SdkFacadeConfiguration} from '@croct/sdk/facade/sdkFacade'; import {Logger} from '../src/sdk'; import {Plugin, PluginFactory} from '../src/plugin'; -import {GlobalPlug} from '../src/plug'; +import {GlobalPlug, Plug} from '../src/plug'; import {CDN_URL} from '../src/constants'; import {Token} from '../src/sdk/token'; -import {Plug} from '../build'; jest.mock('../src/constants', () => { return { @@ -335,6 +334,20 @@ describe('The Croct plug', () => { expect(initialize).toBeCalledTimes(1); }); + test('should determine whether it is initialized', async () => { + const config: SdkFacadeConfiguration = {appId: APP_ID}; + + expect(croct.initialized).toBe(false); + + croct.plug(config); + + expect(croct.initialized).toBe(true); + + await croct.unplug(); + + expect(croct.initialized).toBe(false); + }); + test('should provide a callback that is called when the plug is plugged in', async () => { const config: SdkFacadeConfiguration = {appId: APP_ID};