SEARCH-154 - Changed the isInitialized method

This commit is contained in:
Keerthi Niranjan 2017-08-17 15:04:24 +05:30 committed by Keerthi Niranjan
parent 86dde3c71f
commit 9d97579e48
2 changed files with 21 additions and 22 deletions

View File

@ -132,8 +132,7 @@
buttonEl.addEventListener('click', function () { buttonEl.addEventListener('click', function () {
if (!search.isLibInit()) { if (!search.isLibInit()) {
alert('Library is not initiated. Init first...'); search.init();
return;
} }
let out; let out;
table.innerHTML = ''; table.innerHTML = '';

View File

@ -59,21 +59,11 @@ class Search {
} }
/** /**
* TO check whether SymphonySearchEngine library * returns isInitialized boolean
* initialized or initialise it * @returns {boolean}
* @returns {Promise}
*/ */
initLib() {
return new Promise((resolve) => {
if (!this.isInitialized) {
this.isInitialized = true;
}
resolve(libSymphonySearch.symSEInit());
});
}
isLibInit() { isLibInit() {
return this.initLib(); return this.isInitialized;
} }
/** /**
@ -103,12 +93,12 @@ class Search {
indexBatch(messages) { indexBatch(messages) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!Array.isArray(messages)) { if (!Array.isArray(messages)) {
reject('Messages should be an array'); reject(new Error('Messages should be an array'));
return; return;
} }
if (!this.isInitialized) { if (!this.isInitialized) {
reject("Not initialized"); reject(new Error("Library not initialized"));
return; return;
} }
@ -140,6 +130,11 @@ class Search {
if (!Array.isArray(message)) { if (!Array.isArray(message)) {
return new Error('Messages should be an array'); return new Error('Messages should be an array');
} }
if (!this.isInitialized) {
return new Error("Library not initialized");
}
let result = libSymphonySearch.symSEIndexRealTime(this.realTimeIndex, JSON.stringify(message)); let result = libSymphonySearch.symSEIndexRealTime(this.realTimeIndex, JSON.stringify(message));
return result === 0 ? "Successful" : result return result === 0 ? "Successful" : result
} }
@ -191,20 +186,20 @@ class Search {
let _sortOrder = sortOrder; let _sortOrder = sortOrder;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!fs.existsSync(this.indexFolderName) && !fs.existsSync(this.realTimeIndex)) { if (!this.isInitialized) {
reject('Index folder does not exist.'); reject(new Error("Library not initialized"));
return; return;
} }
if (!this.isInitialized) { if (!fs.existsSync(this.indexFolderName) || !fs.existsSync(this.realTimeIndex)) {
reject("Not initialized"); reject('Index folder does not exist.');
return; return;
} }
let q = Search.constructQuery(query, senderIds, threadIds); let q = Search.constructQuery(query, senderIds, threadIds);
if (q === undefined) { if (q === undefined) {
reject("Search query error"); reject(new Error("Search query error"));
return; return;
} }
@ -251,6 +246,11 @@ class Search {
*/ */
getLatestMessageTimestamp() { getLatestMessageTimestamp() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.isInitialized) {
reject("Not initialized");
return;
}
if (!fs.existsSync(this.indexFolderName)) { if (!fs.existsSync(this.indexFolderName)) {
reject('Index folder does not exist.'); reject('Index folder does not exist.');
return; return;