Skip to content

Commit

Permalink
feat:Better api for EventTimer (#155)
Browse files Browse the repository at this point in the history
Adds a EventTimer.get() method
Authored-by: Ashish Puliyel <[email protected]>
  • Loading branch information
ashishpuliyel authored Feb 3, 2021
1 parent eee7a61 commit 6c2531b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/EventTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,27 @@ export class EventTimer {

/**
* Initalise the EventTimer class on page.
* Returns a singleton instance of the EventTimer class and binds
* Returns the singleton instance of the EventTimer class and binds
* to window.guardian.commercialTimer. If it's been previously
* initalised and bound it returns the original instance
* Note: We save to window.guardian.commercialTimer because
* different bundles (DCR / DCP) can use commercial core, and we want
* all timer events saved to a single instance per-page
*
* @returns {EventTimer} Instance of EventTimer
*/
static init(): EventTimer {
return (window.guardian.commercialTimer ||= new EventTimer());
}

/**
* Just a helper method to access the singleton instance of EventTimer.
* Typical use case is EventTimer.get().trigger
*/
static get(): EventTimer {
return EventTimer.init();
}

constructor() {
this.events = [];
this.startTS = performance.now();
Expand Down

0 comments on commit 6c2531b

Please sign in to comment.