Skip to content

Commit

Permalink
Adding support for exposing ready on analytics object
Browse files Browse the repository at this point in the history
update dependencies
  • Loading branch information
sayan-rudder committed Feb 19, 2020
1 parent 296c9a7 commit 66f2744
Show file tree
Hide file tree
Showing 15 changed files with 478 additions and 266 deletions.
36 changes: 34 additions & 2 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Storage from "./utils/storage";
import { EventRepository } from "./utils/EventRepository";
import logger from "./utils/logUtil";
import { addDomEventHandlers } from "./utils/autotrack.js";
import Emitter from "component-emitter";
import after from "after";

//https://unpkg.com/test-rudder-sdk@1.0.5/dist/browser.js

Expand Down Expand Up @@ -44,7 +46,6 @@ class Analytics {
this.autoTrackHandlersRegistered = false;
this.autoTrackFeatureEnabled = false;
this.initialized = false;
this.ready = false;
this.trackValues = [];
this.eventsBuffer = [];
this.clientIntegrations = [];
Expand All @@ -66,6 +67,8 @@ class Analytics {
this.anonymousId = this.getAnonymousId();
this.storage.setUserId(this.userId);
this.eventRepository = EventRepository;
this.readyCallback = undefined;
this.executeReadyCallback = undefined;
}

/**
Expand Down Expand Up @@ -96,7 +99,7 @@ class Analytics {
" Use Native SDK? " +
destination.config.useNativeSDK
);
if (destination.enabled && destination.config.useNativeSDK) {
if (destination.enabled) {
this.clientIntegrations.push(destination.destinationDefinition.name);
this.configArray.push(destination.config);
}
Expand Down Expand Up @@ -130,6 +133,9 @@ class Analytics {
this.clientIntegrationObjects = [];

if (!intgArray || intgArray.length == 0) {
if (this.readyCallback) {
this.readyCallback();
}
this.toBeProcessedByIntegrationArray = [];
return;
}
Expand All @@ -152,6 +158,20 @@ class Analytics {
object.clientIntegrations.length
) {
object.clientIntegrationObjects = object.successfullyLoadedIntegration;

object.executeReadyCallback = after(
object.clientIntegrationObjects.length,
object.readyCallback
);

object.on("ready", object.executeReadyCallback);

object.clientIntegrationObjects.forEach(intg => {
if (!intg["isReady"] || intg["isReady"]()) {
object.emit("ready");
}
});

//send the queued events to the fetched integration
object.toBeProcessedByIntegrationArray.forEach(event => {
let methodName = event[0];
Expand Down Expand Up @@ -604,6 +624,14 @@ class Analytics {
}
}
}

ready(callback) {
if (typeof callback == "function") {
this.readyCallback = callback;
return;
}
logger.error("ready callback is not a function");
}
}

if (process.browser) {
Expand All @@ -618,6 +646,8 @@ if (process.browser) {

let instance = new Analytics();

Emitter(instance);

if (process.browser) {
let eventsPushedAlready =
!!window.rudderanalytics &&
Expand Down Expand Up @@ -645,6 +675,7 @@ if (process.browser) {
}
}

let ready = instance.ready.bind(instance);
let identify = instance.identify.bind(instance);
let page = instance.page.bind(instance);
let track = instance.track.bind(instance);
Expand All @@ -657,6 +688,7 @@ let setAnonymousId = instance.setAnonymousId.bind(instance);

export {
initialized,
ready,
page,
track,
load,
Expand Down
Loading

0 comments on commit 66f2744

Please sign in to comment.