Skip to content

Commit

Permalink
Introduce initialization callback and pass plug instance to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed May 6, 2020
1 parent 8a623e6 commit 2157661
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export interface Plug {
readonly user: UserFacade;
readonly session: SessionFacade;
readonly sdk: SdkFacade;
readonly flushed: Promise<void>;
readonly flushed: Promise<this>;
readonly plugged: Promise<this>;

plug(configuration: Configuration): void;

Expand All @@ -42,6 +43,16 @@ export interface Plug {
export class GlobalPlug implements Plug {
private instance?: SdkFacade;

private initialize: {(): void};

private initialized: Promise<void>;

public constructor() {
this.initialized = new Promise(resolve => {
this.initialize = resolve
});
}

public plug(configuration: Configuration): void {
if (this.instance !== undefined) {
const logger = this.instance.getLogger();
Expand All @@ -52,10 +63,15 @@ export class GlobalPlug implements Plug {
}

this.instance = SdkFacade.init(configuration);
this.initialize();
}

public get flushed(): Promise<void> {
return this.tracker.flushed;
public get plugged(): Promise<this> {
return this.initialized.then(() => this);
}

public get flushed(): Promise<this> {
return this.tracker.flushed.then(() => this);
}

public get sdk(): SdkFacade {
Expand Down Expand Up @@ -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.');
}
}
Expand Down
38 changes: 37 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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);
});
Expand Down

0 comments on commit 2157661

Please sign in to comment.