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

Commit fd360ba

Browse files
committed
Add analytics.ready(name, fn) function
See segmentio/analytics.js#409 This adds the ability to set a ready callback per integration. todo: * document the function behaviour (need help here on what syntax to use) * test for already readied integration
1 parent 3b48c44 commit fd360ba

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lib/analytics.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
106106

107107
this._options(options);
108108
this._readied = false;
109+
this._readiedIntegrations = {};
109110

110111
// clean unknown integrations from settings
111112
var self = this;
@@ -146,8 +147,14 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(settings, o
146147
integration.page = after(2, integration.page);
147148
}
148149

150+
var integrationReady = function() {
151+
self._readiedIntegrations[integration.name] = true;
152+
self.emit(integration.name + '-ready');
153+
ready();
154+
};
155+
149156
integration.analytics = self;
150-
integration.once('ready', ready);
157+
integration.once('ready', integrationReady);
151158
integration.initialize();
152159
}, integrations);
153160

@@ -496,14 +503,48 @@ Analytics.prototype.alias = function(to, from, options, fn) {
496503
return this;
497504
};
498505

506+
/**
507+
* Register a `fn` to be fired when all the analytics services are ready.
508+
* TODO: document this function for the new overload behaviour.
509+
*
510+
* @param {Function} fn
511+
* @return {Analytics}
512+
*/
513+
514+
Analytics.prototype.ready = function(a, b) {
515+
if (is.fn(a)) {
516+
return this._ready(a);
517+
}
518+
return this._integrationReady(a, b);
519+
};
520+
521+
/**
522+
* Register a `fn` to be fired when the given analytics service is ready.
523+
*
524+
* @param {Function} fn
525+
* @return {Analytics}
526+
*/
527+
528+
Analytics.prototype._integrationReady = function(name, fn) {
529+
if (is.fn(fn)) {
530+
var integrations = this._readiedIntegrations || {};
531+
if (integrations[name]) {
532+
nextTick(fn);
533+
} else {
534+
this.once(name + '-ready', fn);
535+
}
536+
}
537+
return this;
538+
};
539+
499540
/**
500541
* Register a `fn` to be fired when all the analytics services are ready.
501542
*
502543
* @param {Function} fn
503544
* @return {Analytics}
504545
*/
505546

506-
Analytics.prototype.ready = function(fn) {
547+
Analytics.prototype._ready = function(fn) {
507548
if (is.fn(fn)) {
508549
if (this._readied) {
509550
nextTick(fn);

test/analytics.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ describe('Analytics', function() {
159159
analytics.initialize({ Unknown: { key: 'key' } });
160160
});
161161

162+
it('should listen on integration ready events for integration', function(done) {
163+
Test.readyOnInitialize();
164+
analytics.addIntegration(Test);
165+
analytics.ready('Test', done);
166+
analytics.initialize(settings);
167+
});
168+
162169
it('should set analytics._readied to true', function(done) {
163170
analytics.ready(function() {
164171
assert(analytics._readied);

0 commit comments

Comments
 (0)