fix: Enable async loading of all dependency resources (#408)

* Async loading

* Fix lint
This commit is contained in:
Akim
2023-12-29 03:33:28 +07:00
committed by GitHub
parent 1e42e58a83
commit f5425b4746
6 changed files with 27 additions and 54 deletions

View File

@ -14,7 +14,8 @@
* limitations under the License.
*/
import { Worker } from "@fluencelabs/threads/master";
import type { MarineBackgroundInterface } from "@fluencelabs/marine-worker";
import { ModuleThread } from "@fluencelabs/threads/master";
import versions from "./versions.js";
@ -23,7 +24,7 @@ type VersionedPackage = { name: string; version: string };
export type GetWorkerFn = (
pkg: FetchedPackages,
CDNUrl: string,
) => Promise<Worker>;
) => Promise<ModuleThread<MarineBackgroundInterface>>;
export const getVersionedPackage = (pkg: FetchedPackages): VersionedPackage => {
return {

View File

@ -14,7 +14,8 @@
* limitations under the License.
*/
import { BlobWorker } from "@fluencelabs/threads/master";
import type { MarineBackgroundInterface } from "@fluencelabs/marine-worker";
import { BlobWorker, ModuleThread, spawn } from "@fluencelabs/threads/master";
import { fetchResource } from "../fetchers/browser.js";
import type { FetchedPackages, GetWorkerFn } from "../types.js";
@ -34,5 +35,9 @@ export const getWorker: GetWorkerFn = async (
};
const workerCode = await fetchWorkerCode();
return BlobWorker.fromText(workerCode);
const workerThread: ModuleThread<MarineBackgroundInterface> =
await spawn<MarineBackgroundInterface>(BlobWorker.fromText(workerCode));
return workerThread;
};

View File

@ -18,12 +18,13 @@ import { createRequire } from "module";
import { dirname, relative } from "path";
import { fileURLToPath } from "url";
import { Worker } from "@fluencelabs/threads/master";
import type { MarineBackgroundInterface } from "@fluencelabs/marine-worker";
import { ModuleThread, spawn, Worker } from "@fluencelabs/threads/master";
import type { FetchedPackages, GetWorkerFn } from "../types.js";
import { getVersionedPackage } from "../types.js";
export const getWorker: GetWorkerFn = (pkg: FetchedPackages) => {
export const getWorker: GetWorkerFn = async (pkg: FetchedPackages) => {
const require = createRequire(import.meta.url);
const pathToThisFile = dirname(fileURLToPath(import.meta.url));
@ -33,5 +34,8 @@ export const getWorker: GetWorkerFn = (pkg: FetchedPackages) => {
const relativePathToWorker = relative(pathToThisFile, pathToWorker);
return Promise.resolve(new Worker(relativePathToWorker));
const workerThread: ModuleThread<MarineBackgroundInterface> =
await spawn<MarineBackgroundInterface>(new Worker(relativePathToWorker));
return workerThread;
};