0

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

Techs
  • 169
  • 1
  • 1
  • 10
  • What do you mean by "finishes"? What output do you get that is in unexpected order? – Bergi Nov 16 '16 at 09:24
  • Maybe you want to place the `deferred.resolve()` after the `console.log('got all time series');` line? – Bergi Nov 16 '16 at 09:25
  • What is this weird `if (cursor) { while(cursor.continue()) } else { deferred.resolve() }` thing? Are you expecting `onsuccess` to be called multiple times per request? – Bergi Nov 16 '16 at 09:26
  • Your last two functions [lack the `var` keyword](http://stackoverflow.com/q/1470488/1048572) for the variables `timeSeries`, `request`, `req`, and `cursor`, and have `stringTimeSeriesTableData` and `sendTimeSeries` nowhere declared. That's probably the issue, especially when your function is called multiple times concurrently. – Bergi Nov 16 '16 at 15:00

0 Answers0