Created
April 6, 2023 02:33
-
-
Save jeffskelton3/58f75b2bd0f5056d955f3fa57c2f1918 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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