Search code examples
javascriptdexie

Javascript functions called in unexpected order when using dexie db


I have a function executed when a page is loaded that calls four other functions:

// Initialise DB
await initialiseDb                  ();

// Synchronise server's and local db
await synchroniseClientAndServer    ();

// Use the languages from the local db
addLanguagesToSelection             ();

// Start synchronisation loop
synchroniseClientAndServerLoop      ();

1 - initialisation of dexie db

2 - synchronisation of data from the server, saving the data on local tables

3 - using the data saved on the local tables.

4 - loop which executes #2 at 5 seconds intervals

For some reasons, they are executed in the order #1 #3 #2 and #4

async function initialiseDb ()
{
    await database.version ( 1 ).stores ( { values_lookup: '++name, value' } );
    await database.version ( 1 ).stores ( { languages:     '++id, identifier, name'    } );

    languagesTable      = await database.languages;
    valuesLookupTable   = await database.values_lookup;

    // Set synchronisation millisecond = 0 if the database has just been created
    if ( await valuesLookupTable.where ( 'name' ).equals ( "synchronisationMs" ).count () == 0 )
    {
        await valuesLookupTable.put ( { name : 'synchronisationMs', value : 0 } );
    }

    console.log ( "> 111 > valuesLookupTable initialised : " + await valuesLookupTable.where ( 'name' ).equals ( "synchronisationMs" ).count () );
    console.log ( "> 112 > valuesLookupTable initialised : " + await languagesTable.count () );
}


async function synchroniseClientAndServer ()
{
    var previousSyncTime = ( await valuesLookupTable.where ( 'name' ).equals ( "synchronisationMs" ).first () ).value;

    var syncData = JSON.stringify ( { previousSynchronisationTime : previousSyncTime } );

    var xhr = new XMLHttpRequest();
    xhr.open ( "POST", service + "  ClientServerSynchronisation", true );
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8" );
    xhr.send ( syncData );

    xhr.onreadystatechange = async function () 
    {
        // Redirect to home page if sessioin is not active
        if ( this.status === 418 )
        {
            goHome ();
        }
        else if ( this.readyState === 4 && this.status === 200 ) 
        {    
            var syncData = JSON.parse ( xhr.responseText );

            // Update last synchronisation time for the next request
            await database.values_lookup.put ( { name : 'synchronisationMs', value : syncData.synchronisationTime } );

            // Update the list of languages with newly added and deleted
            var updatedLanguages = syncData.updatedLanguages;
            for ( var index = 0; index < updatedLanguages.length; index = index + 1 )
            {
                var language = updatedLanguages [ index ];

                if ( language.addedOrDeleted === ADDED )
                {
                    await languagesTable.put ( { id : language.id, identifier : language.identifier, name : language.name } );
                    console.log ( "> 200 > valuesLookupTable initialised : " + await languagesTable.count () );         
                }
                else
                {
                    await languagesTable.delete ( language.id );
                    console.log ( "> 200 > valuesLookupTable initialised : " + await languagesTable.count () );         
                }
            }
        }
    };
}



async function addLanguagesToSelection ()
{
    console.log ( "> 300 > " );
    var selector = ge ( "languageId" );
    var languages = await languagesTable.toArray ();    
    ....
}   

Languages table above is the table with the languages

The outcome is this:

> 111 > valuesLookupTable initialised : 1 desktop_js.jsp:720:13
> 112 > valuesLookupTable initialised : 0 desktop_js.jsp:721:13

> 300 >                                   desktop_js.jsp:501:10


> 200 > valuesLookupTable initialised : 1 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 2 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 3 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 4 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 5 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 6 desktop_js.jsp:409:17
> 200 > valuesLookupTable initialised : 7 desktop_js.jsp:409:17

So the languages do not appear because the list is populated before the data are loaded. I understand that the problem is probably by dexie db being asynchronous, but I have added "await" pretty much everywhere, and it has always been the solution for all the issues I have had so far.

Can anybody spot what I am doing wrong?

Thanks!


Solution

  • Looking at your code, my initial thought is that your synchroniseClientAndServer() is completing before your addLanguagesToSelection(). However, inside synchroniseClientAndServer() you have xhr.onreadystatechange which is asynchronous and waiting for a server response while the rest of the program continues.

    In essence, the order is running like this: #1, #2, #3, #2.1 (xhr.onreadystatechange), #4.

    One solution might be to call addLanguagesToSelection() from inside xhr.onreadystatechange.