From 215766123a46d9e236fdc0e5756d8a1ea967db0c Mon Sep 17 00:00:00 2001 From: Marcos Passos Date: Wed, 6 May 2020 12:14:41 -0300 Subject: [PATCH] Introduce initialization callback and pass plug instance to callbacks --- src/plug.ts | 26 +++++++++++++++++++++++--- test/index.test.ts | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/plug.ts b/src/plug.ts index ed3ce8c45..b256e2580 100644 --- a/src/plug.ts +++ b/src/plug.ts @@ -16,7 +16,8 @@ export interface Plug { readonly user: UserFacade; readonly session: SessionFacade; readonly sdk: SdkFacade; - readonly flushed: Promise; + readonly flushed: Promise; + readonly plugged: Promise; plug(configuration: Configuration): void; @@ -42,6 +43,16 @@ export interface Plug { export class GlobalPlug implements Plug { private instance?: SdkFacade; + private initialize: {(): void}; + + private initialized: Promise; + + public constructor() { + this.initialized = new Promise(resolve => { + this.initialize = resolve + }); + } + public plug(configuration: Configuration): void { if (this.instance !== undefined) { const logger = this.instance.getLogger(); @@ -52,10 +63,15 @@ export class GlobalPlug implements Plug { } this.instance = SdkFacade.init(configuration); + this.initialize(); } - public get flushed(): Promise { - return this.tracker.flushed; + public get plugged(): Promise { + return this.initialized.then(() => this); + } + + public get flushed(): Promise { + return this.tracker.flushed.then(() => this); } public get sdk(): SdkFacade { @@ -122,6 +138,10 @@ export class GlobalPlug implements Plug { } finally { delete this.instance; + this.initialized = new Promise(resolve => { + this.initialize = resolve + }); + logger.info('🔌 Croct has been unplugged.'); } } diff --git a/test/index.test.ts b/test/index.test.ts index 72d5d20a2..37a5858c5 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -44,6 +44,42 @@ describe('The Croct plug', () => { expect(initialize).toBeCalledTimes(1); }); + test('should provide a callback that is called when the plug is plugged in', async () => { + const config: SdkFacadeConfiguration = {appId: appId}; + + // First time + const firstCallback = jest.fn(); + + const firstPromise = croct.plugged.then(firstCallback); + + await new Promise(resolve => window.setTimeout(resolve, 15)); + + expect(firstCallback).not.toHaveBeenCalled(); + + croct.plug(config); + + await firstPromise; + + expect(firstCallback).toHaveBeenCalledWith(croct); + + // Second time + await croct.unplug(); + + const secondCallback = jest.fn(); + + const secondPromise = croct.plugged.then(secondCallback); + + await new Promise(resolve => window.setTimeout(resolve, 15)); + + expect(secondCallback).not.toHaveBeenCalled(); + + croct.plug(config); + + await secondPromise; + + expect(secondCallback).toHaveBeenCalledWith(croct); + }); + test('should provide a callback that is called when the current pending events are flushed', async () => { const config: SdkFacadeConfiguration = {appId: appId}; @@ -59,7 +95,7 @@ describe('The Croct plug', () => { croct.plug(config); - await expect(croct.flushed).resolves.toBeUndefined(); + await expect(croct.flushed).resolves.toBe(croct); expect(flushed).toHaveBeenCalledTimes(1); });