From 6c2531b87aff320821c9cbdadea76a60df35c7d2 Mon Sep 17 00:00:00 2001 From: Ashish Puliyel Date: Wed, 3 Feb 2021 14:13:05 +0000 Subject: [PATCH] feat:Better api for EventTimer (#155) Adds a EventTimer.get() method Authored-by: Ashish Puliyel --- src/EventTimer.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/EventTimer.ts b/src/EventTimer.ts index 63e08c073..cf6c937d3 100644 --- a/src/EventTimer.ts +++ b/src/EventTimer.ts @@ -25,9 +25,12 @@ 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 */ @@ -35,6 +38,14 @@ export class 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();