mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-19 11:01:21 +00:00
fix: Enable async loading of all dependency resources (#408)
* Async loading * Fix lint
This commit is contained in:
@ -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 {
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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;
|
||||
};
|
||||
|
Reference in New Issue
Block a user