Skip to content

Commit

Permalink
feat: The getCognos function now returns the same object if you omit …
Browse files Browse the repository at this point in the history
…the url parameter

if you call getCognos("http://www.example.com") and then call it again like getCognos(), it will
return the same object.
  • Loading branch information
batje committed Aug 22, 2018
1 parent f280c0d commit 54a3236
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
13 changes: 10 additions & 3 deletions dist/jcognos.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ var Cognos = (function() {
this.username = '';
this.password = '';

this.defaultNamespace = '';
this.namespace = '';

this.namespaces = '';
Expand Down Expand Up @@ -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);
Expand All @@ -1022,6 +1028,7 @@ function getCognos(url) {
});
return myRequest;
} else {
console.log('Returning Resolved jCognos promise');
return Promise.resolve(jCognos);
}
}
Expand Down
13 changes: 10 additions & 3 deletions dist/jcognos.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ var Cognos = (function() {
this.username = '';
this.password = '';

this.defaultNamespace = '';
this.namespace = '';

this.namespaces = '';
Expand Down Expand Up @@ -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);
Expand All @@ -1014,6 +1020,7 @@ function getCognos(url) {
});
return myRequest;
} else {
console.log('Returning Resolved jCognos promise');
return Promise.resolve(jCognos);
}
}
Expand Down
16 changes: 13 additions & 3 deletions dist/jcognos.js
Original file line number Diff line number Diff line change
Expand Up @@ -18380,6 +18380,7 @@
this.username = '';
this.password = '';

this.defaultNamespace = '';
this.namespace = '';

this.namespaces = '';
Expand Down Expand Up @@ -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);
Expand All @@ -18878,6 +18887,7 @@
});
return myRequest;
} else {
console.log('Returning Resolved jCognos promise');
return Promise.resolve(jCognos);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Cognos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions test/4_Reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 54a3236

Please sign in to comment.