@segment/analytics.js-core
Advanced tools
Comparing version 3.8.2 to 3.9.0
@@ -11,4 +11,6 @@ 'use strict'; | ||
var Emitter = require('component-emitter'); | ||
var Facade = require('segmentio-facade'); | ||
var Group = require('segmentio-facade').Group; | ||
var Identify = require('segmentio-facade').Identify; | ||
var MiddlewareChain = require('./middleware').Chain; | ||
var Page = require('segmentio-facade').Page; | ||
@@ -49,2 +51,3 @@ var Track = require('segmentio-facade').Track; | ||
this.Integrations = {}; | ||
this._integrationMiddlewares = new MiddlewareChain(); | ||
this._integrations = {}; | ||
@@ -98,2 +101,17 @@ this._readied = false; | ||
/** | ||
* Define a new `IntegrationMiddleware` | ||
* | ||
* @param {Function} Middleware | ||
* @return {Analytics} | ||
*/ | ||
Analytics.prototype.addIntegrationMiddleware = function(middleware) { | ||
if (this.initialized) | ||
throw new Error('attempted to add middleware after initialization'); | ||
this._integrationMiddlewares.add(middleware); | ||
return this; | ||
}; | ||
/** | ||
* Initialize with the given integration `settings` and `options`. | ||
@@ -195,4 +213,3 @@ * | ||
// backwards compat with angular plugin. | ||
// TODO: remove | ||
// backwards compat with angular plugin and used for init logic checks | ||
this.initialized = true; | ||
@@ -685,3 +702,5 @@ | ||
each(function(integration, name) { | ||
if (!facade.enabled(name)) return; | ||
var facadeCopy = extend(true, new Facade({}), facade); | ||
if (!facadeCopy.enabled(name)) return; | ||
// Check if an integration failed to initialize. | ||
@@ -691,3 +710,3 @@ // If so, do not process the message as the integration is in an unstable state. | ||
self.log( | ||
'Skipping invokation of .%s method of %s integration. Integation failed to initialize properly.', | ||
'Skipping invocation of .%s method of %s integration. Integration failed to initialize properly.', | ||
method, | ||
@@ -698,7 +717,29 @@ name | ||
try { | ||
metrics.increment('analytics_js.integration.invoke', { | ||
method: method, | ||
integration_name: integration.name | ||
}); | ||
integration.invoke.call(integration, method, facade); | ||
// Apply any integration middlewares that exist, then invoke the integration with the result. | ||
self._integrationMiddlewares.applyMiddlewares( | ||
facadeCopy, | ||
integration.name, | ||
function(result) { | ||
// A nullified payload should not be sent to an integration. | ||
if (result === null) { | ||
self.log( | ||
'Payload to integration "%s" was null and dropped.', | ||
name | ||
); | ||
return; | ||
} | ||
// Check if the payload is still a Facade. If not, convert it to one. | ||
if (!(result instanceof Facade)) { | ||
result = new Facade(result); | ||
} | ||
metrics.increment('analytics_js.integration.invoke', { | ||
method: method, | ||
integration_name: integration.name | ||
}); | ||
integration.invoke.call(integration, method, result); | ||
} | ||
); | ||
} catch (e) { | ||
@@ -705,0 +746,0 @@ metrics.increment('analytics_js.integration.invoke.error', { |
{ | ||
"name": "@segment/analytics.js-core", | ||
"author": "Segment <friends@segment.com>", | ||
"version": "3.8.2", | ||
"version": "3.9.0", | ||
"description": "The hassle-free way to integrate analytics into any web application.", | ||
@@ -58,3 +58,3 @@ "keywords": [ | ||
"debug": "^0.7.4", | ||
"extend": "3.0.1", | ||
"extend": "3.0.2", | ||
"inherits": "^2.0.1", | ||
@@ -61,0 +61,0 @@ "install": "^0.7.3", |
@@ -412,3 +412,2 @@ 'use strict'; | ||
var b = Test.prototype.invoke.args[0][1]; | ||
assert(b === a); | ||
assert(b.userId() === 'id'); | ||
@@ -456,3 +455,3 @@ assert(b.traits().trait === true); | ||
it('should record a metric when invoking an integration', function() { | ||
it('should record a metric when invoking an integration and getting an error', function() { | ||
Test.prototype.invoke = function() { | ||
@@ -2010,2 +2009,50 @@ throw new Error('Uh oh!'); | ||
}); | ||
describe('#addIntegrationMiddleware', function() { | ||
it('should have a defined _integrationMiddlewares property', function() { | ||
assert(analytics._integrationMiddlewares !== undefined); | ||
}); | ||
it('should allow users to add a valid Middleware', function() { | ||
try { | ||
analytics.addIntegrationMiddleware(function() {}); | ||
} catch (e) { | ||
// This assert should not run. | ||
assert(false, 'error was incorrectly thrown!'); | ||
} | ||
}); | ||
it('should throw an error if the selected Middleware is not a function', function() { | ||
try { | ||
analytics.addIntegrationMiddleware(7); | ||
// This assert should not run. | ||
assert(false, 'error was not thrown!'); | ||
} catch (e) { | ||
assert( | ||
e.message === 'attempted to add non-function middleware', | ||
'wrong error return' | ||
); | ||
} | ||
}); | ||
it('should throw an error if AJS has already initialized', function() { | ||
analytics.init(); | ||
try { | ||
analytics.addIntegrationMiddleware(function() {}); | ||
// This assert should not run. | ||
assert(false, 'error was not thrown!'); | ||
} catch (e) { | ||
assert( | ||
e.message === 'attempted to add middleware after initialization', | ||
'wrong error return' | ||
); | ||
} | ||
}); | ||
it('should return the analytics object', function() { | ||
assert(analytics === analytics.addIntegrationMiddleware(function() {})); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
466064
39
5319
+ Addedextend@3.0.2(transitive)
- Removedextend@3.0.1(transitive)
Updatedextend@3.0.2