diff options
-rw-r--r-- | src/bun.js/javascript.zig | 10 | ||||
-rw-r--r-- | src/bun.js/web_worker.zig | 6 | ||||
-rw-r--r-- | test/js/third_party/comlink/comlink.test.ts | 15 | ||||
-rw-r--r-- | test/js/third_party/comlink/package.json | 6 | ||||
-rw-r--r-- | test/js/third_party/comlink/worker.fixture.ts | 9 | ||||
-rw-r--r-- | test/package.json | 3 |
6 files changed, 46 insertions, 3 deletions
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index a198259ed..94d82c496 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -1869,6 +1869,16 @@ pub const VirtualMachine = struct { return promise; } + // worker dont has bun_watcher and also we dont wanna call autoTick before dispatchOnline + pub fn loadEntryPointForWebWorker(this: *VirtualMachine, entry_path: string) anyerror!*JSInternalPromise { + var promise = try this.reloadEntryPoint(entry_path); + this.eventLoop().performGC(); + this.waitForPromise(JSC.AnyPromise{ + .Internal = promise, + }); + return this.pending_internal_promise; + } + pub fn loadEntryPoint(this: *VirtualMachine, entry_path: string) anyerror!*JSInternalPromise { var promise = try this.reloadEntryPoint(entry_path); diff --git a/src/bun.js/web_worker.zig b/src/bun.js/web_worker.zig index 7bbd73e27..504b0adeb 100644 --- a/src/bun.js/web_worker.zig +++ b/src/bun.js/web_worker.zig @@ -266,7 +266,7 @@ pub const WebWorker = struct { std.debug.assert(this.status == .start); this.setStatus(.starting); - var promise = vm.loadEntryPoint(this.specifier) catch { + var promise = vm.loadEntryPointForWebWorker(this.specifier) catch { this.flushLogs(); this.onTerminate(); return; @@ -298,9 +298,11 @@ pub const WebWorker = struct { vm.global.vm().releaseWeakRefs(); _ = vm.arena.gc(false); _ = vm.global.vm().runGC(false); - vm.tick(); } + // always doing a first tick so we call CppTask without delay after dispatchOnline + vm.tick(); + { while (true) { while (vm.eventLoop().tasks.count > 0 or vm.active_tasks > 0 or vm.uws_event_loop.?.active > 0) { diff --git a/test/js/third_party/comlink/comlink.test.ts b/test/js/third_party/comlink/comlink.test.ts new file mode 100644 index 000000000..77d810950 --- /dev/null +++ b/test/js/third_party/comlink/comlink.test.ts @@ -0,0 +1,15 @@ +import { test, expect, describe } from "bun:test"; +import { join } from "path"; +import * as Comlink from "comlink"; + +describe("comlink", () => { + test("should start without big delay", async () => { + const worker = new Worker(join(import.meta.dir, "worker.fixture.ts")); + const obj = Comlink.wrap(worker); + const start = performance.now(); + //@ts-ignore + await obj.counter; + const end = performance.now(); + expect(end - start).toBeLessThan(100); + }); +}); diff --git a/test/js/third_party/comlink/package.json b/test/js/third_party/comlink/package.json new file mode 100644 index 000000000..707196be3 --- /dev/null +++ b/test/js/third_party/comlink/package.json @@ -0,0 +1,6 @@ +{ + "name": "comlink", + "dependencies": { + "comlink": "4.4.1" + } +} diff --git a/test/js/third_party/comlink/worker.fixture.ts b/test/js/third_party/comlink/worker.fixture.ts new file mode 100644 index 000000000..1cfcfbe9f --- /dev/null +++ b/test/js/third_party/comlink/worker.fixture.ts @@ -0,0 +1,9 @@ +import * as Comlink from "comlink"; +const obj = { + counter: 0, + inc() { + this.counter++; + }, +}; + +Comlink.expose(obj); diff --git a/test/package.json b/test/package.json index b7e84b19a..bc4ed1d8c 100644 --- a/test/package.json +++ b/test/package.json @@ -30,7 +30,8 @@ "undici": "5.20.0", "vitest": "0.32.2", "webpack": "5.88.0", - "webpack-cli": "4.7.2" + "webpack-cli": "4.7.2", + "comlink": "4.4.1" }, "private": true, "scripts": { |