Skip to content

Instantly share code, notes, and snippets.

@jeffskelton3
Created April 6, 2023 02:33
Show Gist options
  • Save jeffskelton3/58f75b2bd0f5056d955f3fa57c2f1918 to your computer and use it in GitHub Desktop.
Save jeffskelton3/58f75b2bd0f5056d955f3fa57c2f1918 to your computer and use it in GitHub Desktop.
// import segment from 'segment'
const segment = {
init: () => console.log("initialized!!"),
trackEvent: (eventName: string, data: any) => console.log(data),
};
interface SegmentEvent {
name: string;
data: {};
}
class SegmentClient implements AnalyticsClient<SegmentEvent> {
private _segment = segment;
intialize = () => {
this._segment.init();
return Promise.resolve();
};
publishEvent = (e: SegmentEvent) => {
this._segment.trackEvent(e.name, {
...e.data,
});
return Promise.resolve();
};
addUser = (user: User) => {
this._segment.trackEvent("userLoggedIn", {
...user,
});
return Promise.resolve();
};
}
// singleton pattern in TS
class AnalyticsClientFactory {
private static instance?: SegmentClient;
static getInstance() {
if (!this.instance) {
return new SegmentClient();
} else {
return this.instance;
}
}
}
class GetAShirtService {
constructor(private analyticsClient: AnalyticsClient<SegmentEvent>) {}
getThatShirt = () => {
this.analyticsClient.publishEvent({
name: "shirt got got",
data: {
price: 5.99,
},
});
console.log("Heres your shirt");
};
}
export function getGetAShirtService(): GetAShirtService {
return new GetAShirtService(AnalyticsClientFactory.getInstance());
}
export function Bootstrap(props: any) {
getAnalyticsClient().intialize();
return <>{props.children}</>;
}
// Bootstrap
// Route
// Resolver
// Page
// Route
// Resolver
// Page
// Route
// Resolver
// Page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment