Javascript 使用 dexie db 时以意外顺序调用的函数
Javascript functions called in unexpected order when using dexie db
我有一个函数在加载页面时执行,该函数调用其他四个函数:
// 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 - dexie db 的初始化
2 - 从服务器同步数据,将数据保存在本地tables
3 - 使用本地 tables 上保存的数据。
4 - 以 5 秒为间隔执行 #2 的循环
由于某些原因,它们按照#1 #3 #2和#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 ();
....
}
语言table 以上是 table 与语言
结果是这样的:
> 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
所以语言不会出现,因为列表是在加载数据之前填充的。
我知道问题可能是 dexie db 是异步的,但我几乎在所有地方都添加了 "await",它一直是我迄今为止遇到的所有问题的解决方案。
有人能发现我做错了什么吗?
谢谢!
查看您的代码,我最初的想法是您的 synchroniseClientAndServer()
在 addLanguagesToSelection()
之前完成。 但是,在synchroniseClientAndServer()
中您有xhr.onreadystatechange
,它是异步的并且在程序的其余部分继续运行时等待服务器响应。
本质上顺序是运行这样的:#1,#2,#3,#2.1(xhr.onreadystatechange
),#4.
一个解决方案可能是从 xhr.onreadystatechange
.
内部调用 addLanguagesToSelection()
我有一个函数在加载页面时执行,该函数调用其他四个函数:
// 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 - dexie db 的初始化
2 - 从服务器同步数据,将数据保存在本地tables
3 - 使用本地 tables 上保存的数据。
4 - 以 5 秒为间隔执行 #2 的循环
由于某些原因,它们按照#1 #3 #2和#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 ();
....
}
语言table 以上是 table 与语言
结果是这样的:
> 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
所以语言不会出现,因为列表是在加载数据之前填充的。 我知道问题可能是 dexie db 是异步的,但我几乎在所有地方都添加了 "await",它一直是我迄今为止遇到的所有问题的解决方案。
有人能发现我做错了什么吗?
谢谢!
查看您的代码,我最初的想法是您的 synchroniseClientAndServer()
在 addLanguagesToSelection()
之前完成。 但是,在synchroniseClientAndServer()
中您有xhr.onreadystatechange
,它是异步的并且在程序的其余部分继续运行时等待服务器响应。
本质上顺序是运行这样的:#1,#2,#3,#2.1(xhr.onreadystatechange
),#4.
一个解决方案可能是从 xhr.onreadystatechange
.
addLanguagesToSelection()