Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Add analytics.ready(name, fn) function #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function Analytics() {
this.Integrations = {};
this._integrations = {};
this._readied = false;
this._readiedIntegrations = {};
this._timeout = 300;
// XXX: BACKWARDS COMPATIBILITY
this._user = user;
Expand Down Expand Up @@ -106,6 +107,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o

this._options(options);
this._readied = false;
this._readiedIntegrations = {};

// clean unknown integrations from settings
var self = this;
Expand Down Expand Up @@ -148,8 +150,14 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
integration.page = after(2, integration.page);
}

var integrationReady = function() {
self._readiedIntegrations[integration.name] = true;
self.emit(integration.name + '-ready');
ready();
};

integration.analytics = self;
integration.once('ready', ready);
integration.once('ready', integrationReady);
try {
integration.initialize();
} catch (e) {
Expand Down Expand Up @@ -516,14 +524,58 @@ Analytics.prototype.alias = function(to, from, options, fn) {
return this;
};

/**
* Register a `fn` to be fired when integrations are ready.
*
* If the first parameter is a function `fn`, `fn` is fired when all
* integrations are ready.
*
* If the first parameter is a string `integration` and the second parameter is
* a function `fn`, `fn` is fired when the given integration is ready.
*
* It is recommended that you use the latter, as the global ready callback may
* not be fired if a single integration fails to load.
* See https://github.com/segmentio/analytics.js/issues/409.
*
* @param {(Function|String)} a Callback function or integration name.
* @param {Function} [b] Callback function.
* @return {Analytics}
*/

Analytics.prototype.ready = function(a, b) {
if (is.fn(a)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @segmentio/gateway what's the javascript convention here as to how to document this?

@tsholmes I think i've seen some code/PR by you that had this overloaded function behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this would be right:

@param {(Function|String)} a
@param {Function} [b]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rowno any suggestions for naming the parameters? I used a/b coz I couldn't think of anything better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯ At least add description to the parameters though. e.g:

@param {(Function|String)} a Callback function or integration name.
@param {Function} [b] Callback function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rowno updated

return this._ready(a);
}
return this._integrationReady(a, b);
};

/**
* Register a `fn` to be fired when the given integration is ready.
*
* @param {String} [name] integration name.
* @param {Function} [fn] Callback function.
* @return {Analytics}
*/

Analytics.prototype._integrationReady = function(name, fn) {
if (is.fn(fn)) {
if (this._readiedIntegrations[name]) {
nextTick(fn);
} else {
this.once(name + '-ready', fn);
}
}
return this;
};

/**
* Register a `fn` to be fired when all the analytics services are ready.
*
* @param {Function} fn
* @return {Analytics}
*/

Analytics.prototype.ready = function(fn) {
Analytics.prototype._ready = function(fn) {
if (is.fn(fn)) {
if (this._readied) {
nextTick(fn);
Expand Down
18 changes: 18 additions & 0 deletions test/analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ describe('Analytics', function() {
assert(analytics._readied === false);
});

it('should setup a _readiedIntegrations object', function() {
assert(type(analytics._readiedIntegrations) === 'object');
});

it('should set a default timeout', function() {
analytics = new Analytics();
assert(analytics._timeout === 300);
Expand Down Expand Up @@ -172,6 +176,13 @@ describe('Analytics', function() {
assert(!analytics._readied);
});

it('should reset analytics._readiedIntegrations to {}', function() {
analytics.addIntegration(Test);
analytics._readiedIntegrations = { Test: true };
analytics.initialize(settings);
assert(!analytics._readiedIntegrations.Test);
});

it('should add integration instance', function(done) {
Test.readyOnInitialize();
analytics.addIntegration(Test);
Expand Down Expand Up @@ -203,6 +214,13 @@ describe('Analytics', function() {
analytics.initialize({ Unknown: { key: 'key' } });
});

it('should listen on integration ready events for integration', function(done) {
Test.readyOnInitialize();
analytics.addIntegration(Test);
analytics.ready('Test', done);
analytics.initialize(settings);
});

it('should set analytics._readied to true', function(done) {
analytics.ready(function() {
assert(analytics._readied);
Expand Down