mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-06-11 17:46:32 -04:00
Worker.close()
This commit is contained in:
@@ -52,6 +52,11 @@ pub fn deinit(self: *Scheduler) void {
|
||||
finalizeTasks(&self.high_priority);
|
||||
}
|
||||
|
||||
pub fn reset(self: *Scheduler) void {
|
||||
self.low_priority.clearRetainingCapacity();
|
||||
self.high_priority.clearRetainingCapacity();
|
||||
}
|
||||
|
||||
const AddOpts = struct {
|
||||
name: []const u8 = "",
|
||||
low_priority: bool = false,
|
||||
|
||||
@@ -82,7 +82,7 @@ pub fn init(url: []const u8, exec: *Execution) !*Worker {
|
||||
log.warn(.js, "invalid blob", .{ .target = "worker" });
|
||||
return error.BlobNotFound;
|
||||
};
|
||||
try self.execute(blob._slice);
|
||||
try self.loadInitialScript(blob._slice);
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -164,10 +164,10 @@ fn httpDoneCallback(ctx: *anyopaque) !void {
|
||||
});
|
||||
}
|
||||
|
||||
try self.execute(script);
|
||||
try self.loadInitialScript(script);
|
||||
}
|
||||
|
||||
fn execute(self: *Worker, script: []const u8) !void {
|
||||
fn loadInitialScript(self: *Worker, script: []const u8) !void {
|
||||
var ls: js.Local.Scope = undefined;
|
||||
self._worker_scope.js.localScope(&ls);
|
||||
defer ls.deinit();
|
||||
|
||||
@@ -63,6 +63,7 @@ _worker: *Worker,
|
||||
_event_manager: EventManagerBase,
|
||||
|
||||
// These fields represent the "Window"-like component of the WGS
|
||||
_closed: bool = false,
|
||||
_proto: *EventTarget,
|
||||
_console: Console = .init,
|
||||
_crypto: Crypto = .init,
|
||||
@@ -199,6 +200,10 @@ pub fn postMessage(self: *WorkerGlobalScope, data: JS.Value) !void {
|
||||
|
||||
// Called internally by Worker when it wants to post a message to us
|
||||
pub fn receiveMessage(self: *WorkerGlobalScope, data: JS.Value) !void {
|
||||
if (self._closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
const cloned_data: ?JS.Value.Temp = blk: {
|
||||
// Enter our context to clone the message
|
||||
var ls: JS.Local.Scope = undefined;
|
||||
@@ -269,6 +274,12 @@ pub fn unhandledPromiseRejection(self: *WorkerGlobalScope, no_handler: bool, rej
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(self: *WorkerGlobalScope) void {
|
||||
// TOOD: we should also stop new tasks from being scheduled
|
||||
self.js.scheduler.reset();
|
||||
self._closed = true;
|
||||
}
|
||||
|
||||
pub fn reportError(self: *WorkerGlobalScope, err: JS.Value) !void {
|
||||
const error_event = try ErrorEvent.initTrusted(comptime .wrap("error"), .{
|
||||
.@"error" = try err.temp(),
|
||||
@@ -414,6 +425,7 @@ pub const JsApi = struct {
|
||||
pub const structuredClone = bridge.function(WorkerGlobalScope.structuredClone, .{});
|
||||
pub const postMessage = bridge.function(WorkerGlobalScope.postMessage, .{});
|
||||
pub const reportError = bridge.function(WorkerGlobalScope.reportError, .{});
|
||||
pub const close = bridge.function(WorkerGlobalScope.close, .{});
|
||||
|
||||
pub const onmessage = bridge.accessor(WorkerGlobalScope.getOnMessage, WorkerGlobalScope.setOnMessage, .{});
|
||||
pub const onmessageerror = bridge.accessor(WorkerGlobalScope.getOnMessageError, WorkerGlobalScope.setOnMessageError, .{});
|
||||
|
||||
Reference in New Issue
Block a user