0

無法使用importScript解決依賴關係serviceWorker推送承諾已解決;有人可以建議更好的方法來得到這個排序嗎?`importScript`無法在serviceWorker推送事件之前解決依賴關係

ServiceWorker fetch事件:

var iDB; // Global Variable 
self.addEventListener("fetch", function(e) { 
    self.importScripts("dexie.min.js"); 
    var o = new Dexie('database_name'); 
    o.version(1).stores({ 
     iDBStore: "++id,adspotkey" 
     iDB = o 
    }); 
}); 

服務工作者push事件

self.addEventListener("push", function(e) { 
    e.waitUntil(getDetails().then(function(ex) { 
     try { 
      e = wpSDK.iDB; 
      return e.iDBStore.where("id").above(0).toArray().then(function(ox) { 
       console.log(ox); 
      } 
     })); 
}); 

注:

回答

1

你應該在這樣的背景下運行importScripts

self.importScripts("dexie.min.js"); 
var iDB; // Global Variable 
self.addEventListener("fetch", function(e) { 
    var o = new Dexie('database_name'); 
    o.version(1).stores({ 
     iDBStore: "++id,adspotkey" 
     iDB = o 
    }); 
}); 

importScripts是一種同步方法。因此,當您在後臺運行此方法時,它將阻止ServiceWorker處於活動狀態,直到腳本完全加載。

+0

謝謝特雷斯丁 - 我一定會嘗試你的建議! –