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

Commit 2ff25cb

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 2ff25cb

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/analytics.js

Lines changed: 42 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

@@ -503,7 +510,40 @@ Analytics.prototype.alias = function(to, from, options, fn) {
503510
* @return {Analytics}
504511
*/
505512

506-
Analytics.prototype.ready = function(fn) {
513+
Analytics.prototype.ready = function(a, b) {
514+
if (is.fn(a)) {
515+
return this._ready(a);
516+
}
517+
return this._integrationReady(a, b);
518+
};
519+
520+
/**
521+
* Register a `fn` to be fired when the given analytics service is ready.
522+
*
523+
* @param {Function} fn
524+
* @return {Analytics}
525+
*/
526+
527+
Analytics.prototype._integrationReady = function(name, fn) {
528+
if (is.fn(fn)) {
529+
var integrations = this._readiedIntegrations || {};
530+
if (integrations[name]) {
531+
nextTick(fn);
532+
} else {
533+
this.once(name + '-ready', fn);
534+
}
535+
}
536+
return this;
537+
};
538+
539+
/**
540+
* Register a `fn` to be fired when all the given analytics service is ready.
541+
*
542+
* @param {Function} fn
543+
* @return {Analytics}
544+
*/
545+
546+
Analytics.prototype._ready = function(fn) {
507547
if (is.fn(fn)) {
508548
if (this._readied) {
509549
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)