.then promise with sendMessage not working - javascript

I have the following code inside a webworker,
onmessage = function(e)
{
var data;
var message = e.data;
sendTimeSeries = false;
var semicolon = message.split(";");
for (var i=0; i<semicolon.length;i++)
{
var colon= semicolon[i].split(":");
for(var j=0; j<colon.length; j++)
{
if(colon[j] == "Insert TimeSeries")
{
stringTimeSeriesTableData = ""
data = colon[j+1].split(" ");
prevLetter = data[0];
getAllRecords(prevLetter).then(addTimeSeries).then(getAllTimeSeries).then(sendMessage); //use of promises
}
}
}
}
The methods getAllRecords, addTimeSeries and getAllTimeSeries are being called in the correct order (won't go into the next method until the previous one has finished executing. However, the sendMessage webworker method is being called before finishing the getAllTimeSeries method.
getAllRecords():
function getAllRecords(letter)
{
var deferred = $.Deferred();
var trans = db.transaction(["ObservableStates"],"readonly").objectStore("ObservableStates").index('letterIndex');
var request = trans.openCursor(IDBKeyRange.only(letter));
request.onsuccess = function(event)
{
cursor = event.target.result;
if(cursor)
{
count = count + 1;
console.log(cursor.value);
addRecordToHistory(cursor.value);
deleteObservableStates(cursor.primaryKey);
while(cursor.continue())
{
}
}
if(cursor==null)
{
console.log('ready from count');
deferred.resolve();
}
}
request.onerror = function(event)
{
console.log('ERROR LOADING DATA FROM TABLE');
}
return deferred.promise();
}
addTimeSeries
function addTimeSeries()
{
var deferred = $.Deferred();
timeSeries = {
l:prevLetter, t:count
}
request = db.transaction(['TimeSeries'], "readwrite").objectStore('TimeSeries').add(timeSeries);
request.onerror = function(event)
{
console.log("Unable to add timeSeries to table TimeSeries");
}
request.onsuccess = function(event)
{
console.log("TimeSeries State Added");
deferred.resolve();
}
return deferred.promise();
}
getAllTimeSeries()
function getAllTimeSeries()
{
var deferred = $.Deferred();
req = db.transaction(['TimeSeries'], "readonly").objectStore('TimeSeries').openCursor();
req.onerror = function(event)
{
console.log("Unable to stringify all records");
}
req.onsuccess = function(event)
{
cursor=event.target.result;
if(cursor)
{
stringTimeSeriesTableData += cursor.value.l + "," + cursor.value.t + ";";
while(cursor.continue())
{
}
}
if(cursor==null)
{
console.log(stringTimeSeriesTableData);
sendTimeSeries = true;
deferred.resolve();
console.log('got all time series');
}
}
return deferred.promise();
}
Is there something I'm missing here? Thanks

Related

why isn't this promise resolving?

I have a promise that is meant to collect metadata on a file and then resolve it with the metadata that was collected. Here is how I am trying to get its result:
getMetadata: function(text, url){
return this.getpageno(url).then(function(pagecount){
return new Promise(function(accept, reject){
var result = []
var dataMatch = rx.exec(text)
var produktDaten = dataMatch[1].split("&&&").filter(Boolean);
var dokuArr = dataMatch[2].split("&&&").filter(Boolean);
console.log('the produktdaten are ' + produktDaten)
for (var i=0; i<produktDaten.length; i+=4){
var entry = {}
for(var j=0; j<dokuArr.length; j+=3){
var seite = dokuArr[j+2];
// make sure seite is a digit
if (!(/^\d+$/.test(seite))){
console.log(seite + ' was not a valid page number')
throw err
}
if (/(A|a)lle?/i.test(nummer)){
entry.kks.pages[beschreibung] = seite;
// })
}
else if (nummer === kksNummer) {
entry.kks.pages[beschreibung] = seite;
}
}
entry.hersteller = produktDaten[i+1]
entry.typ = produktDaten[i+2]
entry.artikelNummer = produktDaten[i+3]
result.push(entry)
}
if (result.length>0){
return accept(result)
}
return reject()
})
})
},
getpageno: function(url){
var self=this
var pdf = pdfjs.getDocument(url);
return pdf.then(function(pdf){
var maxPages = pdf.pdfInfo.numPages;
var countPromises = []; // collecting all page promises
for (var j = 1; j <= maxPages; j++) {
try {
var page = pdf.getPage(j);
var txt = "";
countPromises.push(page.then(function(page) { // add page promise
var textContent = page.getTextContent();
return textContent.then(function(text){ // return content promise
return text.items.map(function (s) { return s.str; }).join('&&&'); // value page text
});
}));
}
catch(e){
console.log(e)
}
}
// Wait for all pages and join text
return Promise.all(countPromises).then(function (texts) {
// since doumentation pages do not add the final '&&&', must add one manually (only after rx has been found)
var fulltext = texts.reduce(function(full, text){
if (rx.test(full)){
var next = '&&&'+text
return full+=next
}
return full+=text
}, '')
return [fulltext, texts]
});
}).then(function(textarr){
var fulltext = textarr[0]
self.fulltext = fulltext;
var texts = textarr[1]
try {
var partialmatch = rx.exec(fulltext)[0]
var count = texts.reduce(function(pageno, text){
var tomatch = text.replace(/.*Typ&&&/, '')
if (tomatch.length>0 && partialmatch.indexOf(tomatch) > -1){
pageno++
}
return pageno;
}, 0)
}
catch(e){
console.log(e)
}
return count;
})
}
Edited the entry to show that I'm now returning the value of the gepageno function.
The data that I am expecting is logged but not available as a result of accept(). Can anyone tell what could be going wrong?
I have tried to simulate and give a solution of your promise. I have posted pseudo code.
Promise flow
var testFunction = ((result) => {
return new Promise(function (resolve, reject) {
var err = null;
if (!!err) {
reject(err);
} else {
console.log("2. testFunction resolved");
resolve("Final response");
}
});
});
var getpageno = ((url) => {
return new Promise((resolve, reject) => {
var err = null;
if (!!err) {
reject(err);
} else {
console.log("1. getpageno resolved");
resolve(45);
}
});
});
var getMetadata = ((text, url) => {
console.log("0. getmetadata resolved");
var self = this;
getpageno(url)
.then((pageCount) => {
console.log("> pageCount :", pageCount);
return testFunction(pageCount);
})
.then((data) => {
console.log(">", data);
});
});
getMetadata("hell", "https://www.google.com");
output
0. getmetadata resolved
1. getpageno resolved
> pageCount : 452. testFunction resolved
> Final response
you need to return the promise on the third line
return self.getpageno(url).then
to access its eventual fulfillment value.

What in this IndexedDB code is causing the device storage to fill up to 1.6GB?

Our application that utilizes IndexedDB to track tallies of data over periods of time. In using the app for a number of months, it seems that there is some sort of storage leak causing a relatively small amount of data to require 1.6 GB of storage on the device?
The app has a feature to download its full data as a ZIP file, and that file is under 50KB. So there must be a long-term leak somewhere. What we're trying now is to download the data, clear storage, and re-import to track down where this leak might be.
However, looking at the following code that interacts with IndexedDB, it's not obvious to me where that leak may be:
// Initializing the Database
if(!dbengine){
dbengine = this;
var request = window.indexedDB.open("DB", 1);
request.onerror = function(event) {
throw new Error("Error opening indexedDb")
};
// This creates the schema the first time around
request.onupgradeneeded = (event) => {
event.target.result.createObjectStore("db", { keyPath: 'id' });
};
request.onsuccess = (event) => {
dbengine.db = event.target.result;
var dbReadyEvent = new CustomEvent('db-ready', {});
document.dispatchEvent(dbReadyEvent);
dbengine.db.onerror = function(event) {
throw new Error("Database error: " + event.target.errorCode);
};
dbengine.db.onabort = function(event) {
throw new Error("Database aborted: " + event.target.error);
// This is the error that is being called for us - QuotaExceededError
};
};
}// if !dbengine
var initialTransaction = dbengine.db.transaction(['db'], 'readwrite');
initialTransaction.onabort = (event) => {
var error = event.target.error; // DOMError
alert(error.name);
} // onabort
initialTransaction.objectStore('db').openCursor().onsuccess = (event) => {
var cursor = event.target.result;
if(cursor) {
this.encryption.loadIdentity((success) => {
if(success) {
this.loadDb(() => {
document.dispatchEvent(new CustomEvent('api-ready', {}));
setInterval(this.syncDb.bind(this), this.syncInterval);
this.syncDb();
});
} else {
document.dispatchEvent(new CustomEvent('api-fail', {}));
}
});
} else {
initialTransaction.objectStore('db').add({
/* [...] */
});
document.dispatchEvent(new CustomEvent('db-created', {}));
} // if/else cursor
}; // objectStore
loadDb(callback) {
this._tallies = [];
this._collections = [];
var dbStore = dbengine.db.transaction(['db'], 'readwrite').objectStore('db');
dbStore.get(1).onsuccess = (event) => {
var result = event.target.result;
var tallies = result.tallies;
if(result.tallies.length !== 0) {
tallies = this._encryption.decrypt(result.tallies);
}
tallies.forEach(((t) => {
/* [...] */
}));
var collections = result.collections;
if(collections.length !== 0) {
collections = this._encryption.decrypt(result.collections);
}
collections.forEach(((c) => {
/* [...] */
}));
callback()
}
} // loadDb
logout() {
var dbStore = dbengine.db.transaction(['db'], 'readwrite').objectStore('db');
var request = dbStore.get(1);
request.onsuccess = function(e) {
delete e.currentTarget.result.identity;
dbStore.put(e.currentTarget.result);
redirectTo('welcome');
};
} // logout
syncDb() {
var tallyDataString = JSON.stringify(this.tallies);
var collectionDataString = JSON.stringify(this.collections);
var encryptedTallyDataString = this._encryption.encrypt(tallyDataString);
var encryptedCollectionDataString = this._encryption.encrypt(collectionDataString);
var dbStore = dbengine.db.transaction(['db'], 'readwrite').objectStore('db');
dbStore.get(1).onsuccess = (event) => {
var data = event.target.result;
if(data.tallies !== encryptedTallyDataString) {
data.tallies = encryptedTallyDataString;
dbStore.put(data);
}
if(data.collections !== encryptedCollectionDataString) {
data.collections = encryptedCollectionDataString;
dbStore.put(data);
}
var syncEvent = new CustomEvent('sync', {});
document.dispatchEvent(syncEvent);
} // onsuccess
dbStore.get(1).onabort = (event) => {
var error = event.target.error; // DOMError
// if (error.name == 'QuotaExceededError') {
// alert('Sync failed. Quota exceeded.')
// }
alert(error.name);
} // onabort
} // syncDb
loadIdentity(callback) {
var dbStore = dbengine.db.transaction(['db'], 'readwrite').objectStore('db');
dbStore.get(1).onsuccess = (event) => {
var result = event.target.result;
if(!result.identity || !result.identity.publicKey || !result.identity.privateKey) {
if(window.location.pathname !== "/app/welcome/") {
redirectTo('welcome');
}
}
this.identity = {
/* [...] */
};
var solid = false;
if(result.identity.publicKey && result.identity.privateKey) {
solid = true;
}
callback(solid);
};
} // loadIdentity
setIdentity(identity, username) {
var dbStore = dbengine.db.transaction(['db'], 'readwrite').objectStore('db');
var getRequest = dbStore.get(1);
getRequest.onsuccess = (event) => {
var result = event.target.result;
result.identity = {
/* [...] */
};
this.identity = identity;
var request = dbStore.put(result);
} // getRequest
} // setIdentity

Why Node.js script doesn't exit

I wrote code to get data from HTTP service and save it in MongoDB.
Now I test it directly from the console node script.js
The script does its job, but it doesn't exit. I have to do CTRL+C to stop it
Why does it not exit automatically? What place in the code is best for process.exit(0) (if it is needed)?
/*global Promise, getData, httpReq, searchData, getHotData, storeData*/
var http = require('http');
var CoubVideo = require('./models/coubdb.js');
function CoubApi (url, numPerPage, numOfPages) {
this.url = url;
this.numOfPages = numOfPages;
this.numPerPage = numPerPage;
this.getData = getData;
this.httpReq = httpReq;
this.searchData = searchData;
this.getHotData = getHotData;
this.storeData = storeData;
}
// Get data from server
function httpReq (url) {
var promise = new Promise (function (resolve, reject) {
http.get(url, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
if(data.length > 0) {
resolve(JSON.parse(data));
} else {
reject("Error: HTTP request rejected!");
}
});
}).on('error', function (err) {
console.log(err);
});
});
return promise;
}
// Store data in MongoDB
function storeData (data, per_page) {
function insertVideoDoc (i) {
CoubVideo.count({id: data.coubs[i].id}, function (err, count) {
if (err) {
console.log(err);
}
if (count === 0 || count === null) {
var video = new CoubVideo(data.coubs[i]);
video.save(function (err) {
if (err) {
console.log(err);
}
console.log("Saved successfully!");
});
} else {
console.log("Duplicate");
}
});
}
var i;
for(i = 0; i < per_page; i++) {
insertVideoDoc(i);
}
}
// Search for coubs
function searchData (searchtext, order, page, per_page) {
var url = this.url +
"search?q=" + searchtext +
"&order_by=" + order +
"&per_page=" + per_page +
"&page=" + page;
this.httpReq(url).then(function (data) {
this.storeData(data, per_page);
}.bind(this));
}
// Get hot coubs
function getHotData (order, page, per_page) {
var url = this.url +
"timeline/hot?page=" + page +
"&per_page=" + per_page +
"&order_by=" + order;
this.httpReq(url).then(function (data) {
this.storeData(data, per_page);
}.bind(this));
}
// Get data
function getData () {
var i;
for(i = 0; i < this.numOfPages; i++) {
this.getHotData("newest_popular", i, this.numPerPage);
}
}
var coub = new CoubApi("http://coub.com/api/v2/", 2, 50);
coub.getData();

jQuery.Deferred() is not working properly

I'm trying to implement indexedDB. For that I need to use $.Deferred to get the status of database creation. But the problem is, Differed is not working as expected.
Here is the fiddle, you can find the process in the console.
And here is the code:
$(function($) {
var table = 'price';
$.when(dbConnection('cw', table)).done(function(db) {
console.log(db);
var data = [];
dbInsert(db, data, table);
});
function dbConnection(dbname, table) {
var dfd = $.Deferred();
var request = indexedDB.open(dbname);
request.onupgradeneeded = function() {
// The database did not previously exist, so create object stores and indexes.
var db = request.result;
var store = db.createObjectStore(table, {
keyPath: "id"
});
var styleCode = store.createIndex("style_code", "style_code");
var colorCode = store.createIndex("color_code", "color_code");
var size = store.createIndex("size", "size");
var price1 = store.createIndex("price1", "price1");
};
request.onsuccess = function() {
db = request.result;
console.log(request.result);
dfd.resolve(db);
return dfd.promise();
};
request.onerror = function() {
report(request.error);
console.log(request.error);
dfd.resolve(null);
return dfd.promise();
};
request.onabort = function() {
report(request.error);
console.log(request.error);
dfd.resolve(null);
return dfd.promise();
};
}
function dbInsert(db, data, table) {
var tx = db.transaction(table, "readwrite");
var store = tx.objectStore(table);
$.each(data, function(i, rows) {
var style = rows['style-code'],
color = rows['color-code'],
size = rows['size'],
price = rows['price1'];
store.put({
id: i,
style_code: style,
color_code: color,
size: size,
price1: price
});
});
tx.oncomplete = function() {
console.log('Data inserted successfully.');
};
}
})(jQuery);
Whether I'm doing anything wrong? or am I missing anything in this code?. Can anyone tell me what is wrong in this code.
You are expecting dbConnection to return a promise, but do not return anything from that function. Return the promise immediately (last line below) and not inside all the callbacks:
function dbConnection(dbname, table) {
var dfd = $.Deferred();
var request = indexedDB.open(dbname);
request.onupgradeneeded = function() {
// The database did not previously exist, so create object stores and indexes.
var db = request.result;
var store = db.createObjectStore(table, {
keyPath: "id"
});
var styleCode = store.createIndex("style_code", "style_code");
var colorCode = store.createIndex("color_code", "color_code");
var size = store.createIndex("size", "size");
var price1 = store.createIndex("price1", "price1");
};
request.onsuccess = function() {
db = request.result;
console.log(request.result);
dfd.resolve(db);
};
request.onerror = function() {
report(request.error);
console.log(request.error);
dfd.resolve(null);
};
request.onabort = function() {
report(request.error);
console.log(request.error);
dfd.resolve(null);
};
return dfd.promise();
}
Updated JSFiddle: http://jsfiddle.net/TrueBlueAussie/9kjcm49b/2/
Your various callbacks simply resolve or reject the deferred. The readonly promise returned earlier then triggers the next operation.
Note: You should probably use reject for the two error cases (unless you actually want to proceed with a null db value). e.g.
request.onerror = function() {
report(request.error);
console.log(request.error);
dfd.reject("Error occurred");
};
request.onabort = function() {
report(request.error);
console.log(request.error);
dfd.reject("Operation aborted");
};
and use like this:
$.when(dbConnection('cw', table)).done(function(db) {
console.log(db);
var data = [];
dbInsert(db, data, table);
}).fail(function(message){
alert(message);
});

Prepending to a file in javascript using OS.File

Any idea why onSuccess is never called?
filesystem ..
rob#work:~$ cat test.txt
hello
hello2
rob#work:~$ pwd
/home/rob
addon code ..
var filePath = '/home/rob/test.txt',
combinedString = 'new file content';
const {TextDecoder, TextEncoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
OS.File.read(filePath).then(function(data) { // check if specified file exists
console.log(data)
function onSuccess(array) { // prepend to file
let text = new TextDecoder().decode(array);
let encodedArray = new TextEncoder().encode(combinedString + text);
console.log('exists: ' + combinedString + text);
var promise = OS.File.writeAtomic(filePath, encodedArray);
promise.then(
function() {
console.log('success');
},
function(ex) {
console.log('fail');
}
);
}
}, function(ex) { // file doesn't exist, create a new one
if (ex.becauseNoSuchFile) {
let encodedArray = new TextEncoder().encode(combinedString);
var promise = OS.File.writeAtomic(filePath, encodedArray);
console.log('new: ' + combinedString + text);
promise.then(
function() {
console.log('success');
},
function(ex) {
console.log('fail');
}
);
}
});
this is the only output in console ..
console.log: savetexttofile: Uint8Array {"0":104,"1":101,"2":108,"3":108,"4":111,"5":10,"6":104,"7":101,"8":108,"9":108,"10":111,"11":50,"12":10}
Total time: 5.596181 seconds
Program terminated successfully.
let promise = OS.File.read(filePath);
promise = promise.then(function onSuccess(contents) {
let text = new TextDecoder().decode(contents);
combinedString = combinedString + '\n\n' + text;
if (System.getPlatform().indexOf('win') >= 0)
combinedString = combinedString.replace(/[\n]/g, '\r\n');
let encodedArray = new TextEncoder().encode(combinedString);
var promise = OS.File.writeAtomic(filePath, encodedArray);
promise.then(
function() {
console.log('success');
},
function(ex) {
console.log('error');
}
);
return true;
},
function onError(reason) {
if (System.getPlatform().indexOf('win') >= 0)
combinedString = combinedString.replace(/[\n]/g, '\r\n');
let encodedArray = new TextEncoder().encode(combinedString);
if (reason.becauseNoSuchFile()) {
var promise = OS.File.writeAtomic(filePath, encodedArray);
promise.then(
function() {
console.log('success');
},
function(ex) {
console.log('error');
}
);
return false;
}
});

Categories

Resources