From ae3284d46054b189e143b405e1bfc6c09643bf77 Mon Sep 17 00:00:00 2001 From: abishek-srinivasan Date: Thu, 11 Nov 2021 16:50:05 +0530 Subject: [PATCH] Use existing root.pubSub when present (#213) This means that loading `pubsub-js` more than once in the same environment will work as the author expects. A warning will be output on the console to make the developer aware of the duplication. --- src/pubsub.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pubsub.js b/src/pubsub.js index 16e1730..29f73bc 100644 --- a/src/pubsub.js +++ b/src/pubsub.js @@ -9,8 +9,14 @@ 'use strict'; var PubSub = {}; - root.PubSub = PubSub; - factory(PubSub); + + if (root.PubSub) { + PubSub = root.PubSub; + console.warn("PubSub already loaded, using existing version"); + } else { + root.PubSub = PubSub; + factory(PubSub); + } // CommonJS and Node.js module support if (typeof exports === 'object'){ if (module !== undefined && module.exports) {