Skip to content

Commit

Permalink
Cleanup SDK API
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Jun 8, 2020
1 parent dcb5c91 commit 42f4d4e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 136 deletions.
3 changes: 2 additions & 1 deletion src/eventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export interface EventSubscriber<TEvents extends EventMap> {
removeListener<T extends keyof TEvents>(eventName: T, listener: EventListener<TEvents[T]>): void;
}

export interface EventManager<TEvents extends EventMap> extends EventDispatcher<TEvents>, EventSubscriber<TEvents> {
export interface EventManager<DEvents extends EventMap, SEvents extends EventMap = DEvents>
extends EventDispatcher<DEvents>, EventSubscriber<SEvents> {
}

export class SynchronousEventManager<TEvents extends EventMap> implements EventManager<TEvents> {
Expand Down
71 changes: 29 additions & 42 deletions src/facade/sdkFacade.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import EvaluatorFacade, {EvaluationOptions, TabContextFactory} from './evaluatorFacade';
import EvaluatorFacade, {TabContextFactory} from './evaluatorFacade';
import TrackerFacade from './trackerFacade';
import Context, {TokenScope} from '../context';
import UserFacade from './userFacade';
import Token from '../token';
import {JsonValue} from '../json';
import {formatCause} from '../error';
import {configurationSchema} from '../schema/sdkFacadeSchemas';
import Sdk from '../sdk';
import SessionFacade from './sessionFacade';
import {Logger} from '../logging';
import {
ExternalTrackingEvent as ExternalEvent,
ExternalTrackingEventPayload as ExternalEventPayload,
ExternalTrackingEventType as ExternalEventType,
PartialTrackingEvent as PartialEvent,
} from '../trackingEvents';
import {SdkEvent, SdkEventMap, SdkEventType} from '../sdkEvents';
import {EventListener, EventManager} from '../eventManager';
import {SdkEventMap} from '../sdkEvents';
import {EventManager} from '../eventManager';
import CidAssigner from '../cid/index';
import {PartialTrackingEvent} from '../trackingEvents';

export type Configuration = {
appId: string,
Expand Down Expand Up @@ -44,7 +39,7 @@ function validateConfiguration(configuration: unknown): asserts configuration is
}
}

export default class SdkFacade implements EventManager<SdkEventMap> {
export default class SdkFacade {
private readonly sdk: Sdk;

private trackerFacade?: TrackerFacade;
Expand Down Expand Up @@ -93,8 +88,8 @@ export default class SdkFacade implements EventManager<SdkEventMap> {
return this.sdk.context;
}

public getCid(): Promise<string> {
return this.sdk.getCid();
public get cidAssigner(): CidAssigner {
return this.sdk.cidAssigner;
}

public get tracker(): TrackerFacade {
Expand Down Expand Up @@ -132,6 +127,26 @@ export default class SdkFacade implements EventManager<SdkEventMap> {
return this.evaluatorFacade;
}

public get eventManager(): EventManager<Record<string, object>, SdkEventMap> {
const {eventManager} = this.sdk;

return {
addListener: eventManager.addListener.bind(eventManager),
removeListener: eventManager.removeListener.bind(eventManager),
dispatch: (eventName: string, event: object): void => {
if (!/[a-z][a-z_]+\.[a-z][a-z_]+/i.test(eventName)) {
throw new Error(
'The event name must be in the form of "namespaced.eventName", where '
+ 'both the namespace and event name must start with a letter, followed by '
+ 'any series of letters and underscores.',
);
}

eventManager.dispatch(eventName, event);
},
}
}

public identify(userId: string): void {
this.setToken(Token.issue(this.sdk.appId, userId));
}
Expand Down Expand Up @@ -212,20 +227,12 @@ export default class SdkFacade implements EventManager<SdkEventMap> {
logger.debug('Token removed');
}

private trackInternalEvent(event: PartialEvent): void {
private trackInternalEvent(event: PartialTrackingEvent): void {
this.sdk.tracker.track(event).catch(() => {
// suppress error as it is already logged by the tracker
});
}

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

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

public getLogger(...namespace: string[]): Logger {
return this.sdk.getLogger(...namespace);
}
Expand All @@ -238,26 +245,6 @@ export default class SdkFacade implements EventManager<SdkEventMap> {
return this.sdk.getBrowserStorage(namespace, ...subnamespace);
}

public addListener<T extends SdkEventType>(type: T, listener: EventListener<SdkEvent<T>>): void {
this.sdk.getEventManager().addListener(type, listener);
}

public removeListener<T extends SdkEventType>(type: T, listener: EventListener<SdkEvent<T>>): void {
this.sdk.getEventManager().removeListener(type, listener);
}

public dispatch<T extends keyof SdkEventMap>(eventName: T, event: SdkEventMap[T]): void {
if (!/[a-z][a-z_]+\.[a-z][a-z_]+/i.test(eventName)) {
throw new Error(
'The event name must be in the form of "namespaced.eventName", where '
+ 'both the namespace and event name must start with a letter, followed by '
+ 'any series of letters and underscores.',
);
}

this.sdk.getEventManager().dispatch(eventName, event);
}

public close(): Promise<void> {
return this.sdk.close();
}
Expand Down
15 changes: 7 additions & 8 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Tracker from './tracker';
import Evaluator from './evaluator';
import {SdkEventMap} from './sdkEvents';
import {EventManager} from './eventManager';
import CidAssigner from './cid/index';

export type Configuration = {
appId: string,
Expand Down Expand Up @@ -99,10 +100,8 @@ export default class Sdk {
return appId;
}

public getCid(): Promise<string> {
const assigner = this.container.getCidAssigner();

return assigner.assignCid();
public get cidAssigner(): CidAssigner {
return this.container.getCidAssigner();
}

public get context(): Context {
Expand All @@ -117,6 +116,10 @@ export default class Sdk {
return this.container.getEvaluator();
}

public get eventManager(): EventManager<SdkEventMap> {
return this.container.getEventManager();
}

public getLogger(...namespace: string[]): Logger {
return this.container.getLogger(...namespace);
}
Expand All @@ -129,10 +132,6 @@ export default class Sdk {
return this.container.getBrowserStorage(namespace, ...subnamespace);
}

public getEventManager(): EventManager<SdkEventMap> {
return this.container.getEventManager();
}

public async close(): Promise<void> {
if (this.closed) {
return;
Expand Down
Loading

0 comments on commit 42f4d4e

Please sign in to comment.