From 54a32367d74f0d17f0efa2a7fbc7bf025dd555de Mon Sep 17 00:00:00 2001 From: Reinier Battenberg Date: Wed, 22 Aug 2018 12:11:18 +0200 Subject: [PATCH] feat: The getCognos function now returns the same object if you omit the url parameter if you call getCognos("http://www.example.com") and then call it again like getCognos(), it will return the same object. --- dist/jcognos.cjs.js | 13 ++++++++++--- dist/jcognos.esm.js | 13 ++++++++++--- dist/jcognos.js | 16 +++++++++++++--- src/Cognos.js | 9 +++++---- test/4_Reset.js | 20 ++++++++++++++++++++ 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/dist/jcognos.cjs.js b/dist/jcognos.cjs.js index 0650fb0..01ec188 100644 --- a/dist/jcognos.cjs.js +++ b/dist/jcognos.cjs.js @@ -524,6 +524,7 @@ var Cognos = (function() { this.username = ''; this.password = ''; + this.defaultNamespace = ''; this.namespace = ''; this.namespaces = ''; @@ -998,16 +999,21 @@ var Cognos = (function() { return Cognos; })(); -function getCognos(url) { +function getCognos() { + var url = + arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var debug = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; + console.log('Start'); var reset = false; - if (url !== cognosUrl) { + if (url && url !== cognosUrl) { + console.log('New URL, Resetting', url); jCognos = undefined; reset = true; } - if (typeof jCognos == 'undefined') { + if (typeof jCognos == 'undefined' && url) { + console.log('No jCognos obejct and a url. So we create a new jCognos', url); var myRequest = getCognosRequest(url, debug, reset) .then(function(cRequest) { jCognos = new Cognos(debug); @@ -1022,6 +1028,7 @@ function getCognos(url) { }); return myRequest; } else { + console.log('Returning Resolved jCognos promise'); return Promise.resolve(jCognos); } } diff --git a/dist/jcognos.esm.js b/dist/jcognos.esm.js index d554b20..ac97844 100644 --- a/dist/jcognos.esm.js +++ b/dist/jcognos.esm.js @@ -516,6 +516,7 @@ var Cognos = (function() { this.username = ''; this.password = ''; + this.defaultNamespace = ''; this.namespace = ''; this.namespaces = ''; @@ -990,16 +991,21 @@ var Cognos = (function() { return Cognos; })(); -function getCognos(url) { +function getCognos() { + var url = + arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var debug = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; + console.log('Start'); var reset = false; - if (url !== cognosUrl) { + if (url && url !== cognosUrl) { + console.log('New URL, Resetting', url); jCognos = undefined; reset = true; } - if (typeof jCognos == 'undefined') { + if (typeof jCognos == 'undefined' && url) { + console.log('No jCognos obejct and a url. So we create a new jCognos', url); var myRequest = getCognosRequest(url, debug, reset) .then(function(cRequest) { jCognos = new Cognos(debug); @@ -1014,6 +1020,7 @@ function getCognos(url) { }); return myRequest; } else { + console.log('Returning Resolved jCognos promise'); return Promise.resolve(jCognos); } } diff --git a/dist/jcognos.js b/dist/jcognos.js index a7a2e52..3e1d188 100644 --- a/dist/jcognos.js +++ b/dist/jcognos.js @@ -18380,6 +18380,7 @@ this.username = ''; this.password = ''; + this.defaultNamespace = ''; this.namespace = ''; this.namespaces = ''; @@ -18854,16 +18855,24 @@ return Cognos; })(); - function getCognos(url) { + function getCognos() { + var url = + arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var debug = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; + console.log('Start'); var reset = false; - if (url !== cognosUrl) { + if (url && url !== cognosUrl) { + console.log('New URL, Resetting', url); jCognos = undefined; reset = true; } - if (typeof jCognos == 'undefined') { + if (typeof jCognos == 'undefined' && url) { + console.log( + 'No jCognos obejct and a url. So we create a new jCognos', + url + ); var myRequest = getCognosRequest(url, debug, reset) .then(function(cRequest) { jCognos = new Cognos(debug); @@ -18878,6 +18887,7 @@ }); return myRequest; } else { + console.log('Returning Resolved jCognos promise'); return Promise.resolve(jCognos); } } diff --git a/src/Cognos.js b/src/Cognos.js index 499da40..de7d2cc 100644 --- a/src/Cognos.js +++ b/src/Cognos.js @@ -527,16 +527,17 @@ class Cognos { * getCognos - Static function to get the Cognos Object. You can have only 1 Cognos object in your application * at any time. * - * @param {String} url The URL of your Cognos installation + * @param {String} url The URL of your Cognos installation. If empty, this function becomes static and the current jCognos object is returned. + * @param {Boolean} debug If true, starts debugging into the console * @return {Cognos} The Cognos object */ -function getCognos(url, debug = false) { +function getCognos(url = false, debug = false) { var reset = false; - if (url !== cognosUrl) { + if (url && url !== cognosUrl) { jCognos = undefined; reset = true; } - if (typeof jCognos == 'undefined') { + if (typeof jCognos == 'undefined' && url) { var myRequest = getCognosRequest(url, debug, reset) .then(function(cRequest) { jCognos = new Cognos(debug); diff --git a/test/4_Reset.js b/test/4_Reset.js index f505a40..55ebde0 100644 --- a/test/4_Reset.js +++ b/test/4_Reset.js @@ -89,5 +89,25 @@ describe('jcognos Reset Tests', function() { .catch(function(err) { console.log('There was an error listing the root folder', err); }); + }), + it('getCognos should return same object', done => { + var newCognos = jcognos.getCognos().then(function(freshCognos) { + assert.equal(cognos, freshCognos, 'Two Cognosses are the same.'); + cognos + .reset() + .then(function() { + jcognos.getCognos().then(function(newerCognos) { + assert.notEqual( + cognos, + newCognos, + 'After reset, Two Cognosses are not the same.' + ); + }); + }) + .catch(function(err) { + console.log('There was an error fetching the root folder', err); + }) + .then(done, done); + }); }); });