From 5168de015085c1be235145ebff5f8bc0ef52e08b Mon Sep 17 00:00:00 2001 From: Amit Bansil Date: Thu, 10 Nov 2016 14:33:43 -0500 Subject: [PATCH] feat: implement onNonBranchLink --- www.es6/branch.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/www.es6/branch.js b/www.es6/branch.js index 458d91ee..ca9e576d 100644 --- a/www.es6/branch.js +++ b/www.es6/branch.js @@ -60,6 +60,14 @@ var Branch = function() { }; +var disableGlobalListenersWarnings = false; + +/** + * Don't emit warnings regarding use of global listeners. + */ +Branch.prototype.disableGlobalListenersWarnings = function() { + disableGlobalListenersWarnings = true; +}; /** * Initialize the Branch instance. * @@ -71,6 +79,33 @@ Branch.prototype.initSession = function() { }; + +var nonBranchLinkListener = null; +function onNonBranchLinkStub(data) { + nonBranchLinkListener(data); +} + +/** + * Register listener for non branch links. + */ +Branch.prototype.onNonBranchLink = function(newHook) { + if(!hook) { + throw new Error('non branch link hook is falsy, expected a function, not: "' + hook + '"'); + } + + var currentHook = window.NonBranchLinkHandler; + if(currentHook !== undefined && currentHook !== onNonBranchLinkStub) { + if(!disableGlobalListenersWarnings) { + console.log('WARNING: you are calling onNonBranchLink when an ' + + 'existing global NonBranchLinkHandler is defined. The global ' + + 'NonBranchLinkHandler will be overwritten. See https://goo.gl/GijGKP ' + + 'for details.'); + } + } + nonBranchLinkListener = newHook; + window.NonBranchLinkHandler = onNonBranchLinkStub; +} + /** * Get Mixpanel tolen/assisstance. * NOTE: This must be called before initSession