Skip to content

Commit

Permalink
First stab at disconnecting 'integrations' from core SDK and dynamica…
Browse files Browse the repository at this point in the history
…lly loading client integrations
  • Loading branch information
saikumarrs committed Aug 16, 2021
1 parent 1e488cf commit 154a797
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
MAX_WAIT_FOR_INTEGRATION_LOAD,
INTEGRATION_LOAD_CHECK_INTERVAL
} from "./utils/constants";
import { integrations } from "./integrations";
import RudderElementBuilder from "./utils/RudderElementBuilder";
import Storage from "./utils/storage";
import { EventRepository } from "./utils/EventRepository";
Expand Down Expand Up @@ -96,6 +95,7 @@ class Analytics {
};
this.loaded = false;
this.loadIntegration = true;
this.dynamicallyLoadedIntegrations = {};
}

/**
Expand Down Expand Up @@ -180,9 +180,14 @@ class Analytics {
this.clientIntegrations
);

// Load all the client integrations dynamically
this.clientIntegrations.forEach((intg) => {
this.loadIntegrationModule(intg);
});

// remove from the list which don't have support yet in SDK
this.clientIntegrations = this.clientIntegrations.filter(intg => {
return integrations[intg.name] != undefined;
this.clientIntegrations = this.clientIntegrations.filter((intg) => {
return this.dynamicallyLoadedIntegrations[intg.name] != undefined;
});

this.init(this.clientIntegrations);
Expand All @@ -200,6 +205,24 @@ class Analytics {
}
}

/*
* Dyanamically load the required client integration from CDN
*/
loadIntegrationModule(modName) {
const cdnURL = `https://cdn.rudderstack.com/v2/js-integrations/${modName}.js`;
/*import(cdnURL)
.then((modObj) => {
this.dynamicallyLoadedIntegrations[modName] = modObj;
})
.catch((err) => {
logger.debug(
"[Analytics] loadIntegrationModule :: failed trying to dynamically load integration module:: ",
modName,
err
);
});*/
}

/**
* Initialize integrations by addinfg respective scripts
* keep the instances reference in core
Expand All @@ -210,8 +233,10 @@ class Analytics {
*/
init(intgArray) {
const self = this;
logger.debug("supported intgs ", integrations);
// this.clientIntegrationObjects = [];
logger.debug(
"Dyanmically loaded intgs ",
this.dynamicallyLoadedIntegrations
);

if (!intgArray || intgArray.length == 0) {
if (this.readyCallback) {
Expand All @@ -227,7 +252,7 @@ class Analytics {
"[Analytics] init :: trying to initialize integration name:: ",
intg.name
);
const intgClass = integrations[intg.name];
const intgClass = this.dynamicallyLoadedIntegrations[intg.name];
const destConfig = intg.config;
intgInstance = new intgClass(destConfig, self);
intgInstance.init();
Expand All @@ -241,7 +266,7 @@ class Analytics {
intg.name
);
this.failedToBeLoadedIntegration.push(intgInstance);
}
}
});
}

Expand Down

0 comments on commit 154a797

Please sign in to comment.