Skip to content

Commit

Permalink
Expose SDK instance
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Apr 28, 2020
1 parent d66c8fc commit bfaefbc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
24 changes: 22 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import {
JsonValue,
SdkFacade as Sdk,
SdkFacadeConfiguration as Configuration,
Logger,
UserFacade,
SessionFacade,
TrackerFacade as Tracker,
EvaluatorFacade as Evaluator,
EvaluationFacadeOptions as EvaluationOptions,
EvaluationErrorType,
EvaluationError,
ExpressionError,
Event,
EventType,
ExternalEvent,
ExternalEventPayload,
ExternalEventType,
EventListener,
EventInfo,
JsonValue,
} from '@croct-tech/sdk';

export {
Sdk,
Configuration,
UserFacade,
SessionFacade,
Tracker,
Logger,
Evaluator,
EvaluationOptions,
Event,
EventType,
ExternalEventType,
ExternalEventPayload,
EventListener,
EventInfo,
ExternalEvent,
EvaluationOptions,
EvaluationErrorType,
EvaluationError,
ExpressionError,
Expand Down
45 changes: 23 additions & 22 deletions src/plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Plug {
readonly tracker: TrackerFacade;
readonly user: UserFacade;
readonly session: SessionFacade;
readonly sdk: SdkFacade;
readonly flushed: Promise<void>;

plug(configuration: Configuration): void;
Expand All @@ -39,87 +40,87 @@ export interface Plug {
}

class GlobalPlug implements Plug {
private facade?: SdkFacade;
private instance?: SdkFacade;

public plug(configuration: Configuration): void {
if (this.facade !== undefined) {
const logger = this.facade.getLogger();
if (this.instance !== undefined) {
const logger = this.instance.getLogger();

logger.info('Croct is already plugged in.');

return;
}

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

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

private get instance(): SdkFacade {
if (this.facade === undefined) {
public get sdk(): SdkFacade {
if (this.instance === undefined) {
throw new Error('Croct is not plugged in.');
}

return this.facade;
return this.instance;
}

public get tracker(): TrackerFacade {
return this.instance.tracker;
return this.sdk.tracker;
}

public get user(): UserFacade {
return this.instance.user;
return this.sdk.user;
}

public get session(): SessionFacade {
return this.instance.session;
return this.sdk.session;
}

public isAnonymous(): boolean {
return this.instance.context.isAnonymous();
return this.sdk.context.isAnonymous();
}

public getUserId(): string | null {
return this.instance.context.getUser();
return this.sdk.context.getUser();
}

public identify(userId: string): void {
this.instance.identify(userId);
this.sdk.identify(userId);
}

public anonymize(): void {
this.instance.anonymize();
this.sdk.anonymize();
}

public setToken(token: string): void {
this.instance.setToken(token);
this.sdk.setToken(token);
}

public unsetToken(): void {
this.instance.unsetToken();
this.sdk.unsetToken();
}

public track<T extends ExternalEventType>(type: T, payload: ExternalEventPayload<T>): Promise<ExternalEvent<T>> {
return this.instance.track(type, payload);
return this.sdk.track(type, payload);
}

public evaluate(expression: string, options: EvaluationOptions = {}): Promise<JsonValue> {
return this.instance.evaluate(expression, options);
return this.sdk.evaluate(expression, options);
}

public async unplug(): Promise<void> {
if (this.facade === undefined) {
if (this.instance === undefined) {
return;
}

const logger = this.instance.getLogger();
const logger = this.sdk.getLogger();

try {
await this.facade.close();
await this.instance.close();
} finally {
delete this.facade;
delete this.instance;

logger.info('🔌 Croct has been unplugged.');
}
Expand Down
7 changes: 5 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('The Croct plug', () => {
});

test('should initialize the SDK using the specified configuration', () => {
const initialize = jest.spyOn(SdkFacade, 'init');
const config: SdkFacadeConfiguration = {
appId: appId,
track: false,
Expand All @@ -23,15 +22,19 @@ describe('The Croct plug', () => {
},
};

const sdkFacade = SdkFacade.init(config);
const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);

croct.plug(config);

expect(initialize).toBeCalledWith(config);

expect(croct.sdk).toBe(sdkFacade);
});

test('should not fail if plugged more than once', () => {
const config: SdkFacadeConfiguration = {appId: appId};
const sdkFacade = SdkFacade.init(config);

const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);

croct.plug(config);
Expand Down

0 comments on commit bfaefbc

Please sign in to comment.