激情欧美日韩一区二区|国产18在线播放|黄的日本免费大片|国产色在线 | 亚洲|青青操夜夜操

IndexedDB之并發問題(Concurrency Issues)

歡歡歡歡 發表于 2018-3-2 13:29

While IndexedDB is an asynchronous API inside of a web page, there are still concurrency issues. If the same web page is open in two different browser tabs at the same time, it’s possible that one may attempt to upgrade the database before the other is ready. The problematic operation is in setting the database to a new version, and so calls to setVersion() can be completed only when there is just one tab in the browser using the database.

翻譯:

雖然IndexDB在網頁里是一個異步接口,但是仍然會有并發問題。假如相同的頁面同時在兩個不同的瀏覽器頁簽打開,很有可能在一個準備好之前另一個正在嘗試更新。成問題的操作是在把數據庫設置成要給新版本的時候,因此只有當瀏覽器上只有一個正在使用數據庫的頁簽打開時,setVersion()的調用才能被完成。

When you first open a database, it’s important to assign an onversionchange event handler. This callback is executed when another tab from the same origin calls setVersion(). The best response to this event is to immediately close the database so that the version upgrade can be completed. For example:

翻譯:

當你第一次打開數據庫的時候,綁定一個onversionchange事件是很重要的。當其相同域名的其他頁簽調用setVersion()方法時,該事件會被執行。對該事件最好的響應是關閉數據庫,一遍版本更新能被完成。例如:

var request, database; request = indexedDB.open(“admin”);

request.onsuccess = function(event){

database = event.target.result;

database.onversionchange = function(){ database.close(); };

};

You should assign onversionchange after every successful opening of a database.

翻譯:

每次成功的打開數據庫都應該給onversionchange事件賦值。

When you are calling setVersion(), it’s also important to assign an onblocked event handler to the request. This event handler executes when another tab has the database open while you’re trying to update the version. In that case, you may want to inform the user to close all other tabs before attempting to retry setVersion(). For example:

翻譯:

當你調用方法setVersion()時,給請求綁定一個onblocked事件也很重要。當其他頁面讓數據庫是開著,同時正試圖更新版本的時候,該事件發生。在那種情況下,你可能想要告知用戶在嘗試setVersion()方法之前,關閉所有其他的頁面。例如:

var request = database.setVersion(“2.0”);

request.onblocked = function(){ alert(“Please close all other tabs and try again.”); };

request.onsuccess = function(){ //handle success, continue on };

Remember, onversionchange will have been called in the other tab(s) as well.

翻譯:

切記,onversionchange事件是在其他tab被調用的。

By always assigning these event handlers, you will ensure your web application will be able to better handle concurrency issues related to IndexedDB.

翻譯:

通過上述這些事件的處理,你能確保你的網頁應用能夠更好的處理跟IndexDB相關的并發問題。