获取 Cordova 插件文件以在 Web Worker 中完全工作

Get Cordova plugin file to fully work in a web worker

我正在 运行使用 Cordova 安装一个应用程序,该应用程序的可选文件下载量非常大。当 运行 在主线程上时 iOS 将关闭应用程序,因为它 运行ning 太长了。为了解决这个问题,我决定使用网络工作者,这样它就可以 运行 在一个单独的线程上,(我说的错误是 运行 在一个单独的线程上的进程)。从那时起,我就很难让插件在一个单独的 worker 中工作。到目前为止,我已经能够加载 Cordova 及其插件脚本并能够调用模块,但它们无法调用本机代码,因为本机代码依赖于 window.webkit.messageHandlers.cordova。据我所知,当 swift 公开 js 方法时,它只将它公开给主线程,并且无法从 worker 访问本机代码。有没有其他方法可以从我缺少的工作人员调用本机代码?

(self as any).window = self;
(self as any).window.document = self;
(self as any).document = self;
(self as any).window.webkit = (window as any).webkit || {
  messageHandlers: {}
};
// import the scripts from their location in the www folder
const pathToCordovaPluginFile = "../plugins/cordova-plugin-file/www/";
importScripts(
  "../cordova.js",
  pathToCordovaPluginFile + "requestFileSystem.js",
  pathToCordovaPluginFile + "FileError.js",
  pathToCordovaPluginFile + "FileSystem.js",
  pathToCordovaPluginFile + "fileSystems.js",
  pathToCordovaPluginFile + "DirectoryEntry.js",
  pathToCordovaPluginFile + "Entry.js",
  pathToCordovaPluginFile + "DirectoryReader.js",
  pathToCordovaPluginFile + "Metadata.js"

  // pathToCordovaPluginFile + "File.js",
  // pathToCordovaPluginFile + "FileEntry.js",
  // pathToCordovaPluginFile + "FileReader.js",
  // pathToCordovaPluginFile + "FileWriter.js",
);

const getFileSystem = (PERSISTENT: number) => {
  try {
    const requestFileSystem: Window["requestFileSystem"] = cordova.require(
      "cordova-plugin-file.requestFileSystem"
    );
    return new Promise<FileSystem>((resolve, reject) =>
      requestFileSystem(PERSISTENT, 0, resolve, reject)
    );
  } catch {
    console.error("unable to request file system");
  }
};

(我的代码在技术上是打字稿而不是 javascript)

AFAIK 网络工作者在 iOS WKWebView 上不可用 iOS < 14

更多信息here and here

您应该考虑将插件修改为 运行 在子原生线程上下载。