【问题标题】:Get Cordova plugin file to fully work in a web worker获取 Cordova 插件文件以在 Web Worker 中完全工作
【发布时间】:2021-10-16 04:50:56
【问题描述】:

我正在使用 Cordova 运行一个应用程序,该应用程序具有极其繁重的可选文件下载。在主线程上运行时,iOS 将关闭应用程序,因为它运行时间过长。为了解决这个问题,我决定使用网络工作者,这样它就可以在单独的线程上运行,(我被说的错误是在单独的线程上运行进程)。从那以后,我很难让插件在单独的工作人员中工作。到目前为止,我已经能够加载 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)

【问题讨论】:

    标签: javascript swift typescript cordova web-worker


    【解决方案1】:

    AFAIK 网络工作者不适用于 iOS WKWebView for iOS

    更多信息herehere

    您应该考虑修改插件以在子本地线程上运行您的下载。

    【讨论】:

    • 这就是我所担心的。 iOS
    • 我相信它是“计划好的”,但我不确定它是否真的看到了曙光,因为它仍在草稿中。用正确的链接更新了我的答案...
    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    相关资源
    最近更新 更多