Fix [Symbol.dispose] on Bun.listen() & Bun.connect() + add types (#12739)

Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2024-07-25 18:49:35 -07:00
committed by GitHub
parent 5a18b7d2fc
commit 3bfeb83e7e
3 changed files with 17 additions and 9 deletions

View File

@@ -3847,7 +3847,7 @@ declare module "bun" {
*/
const isMainThread: boolean;
interface Socket<Data = undefined> {
interface Socket<Data = undefined> extends Disposable {
/**
* Write `data` to the socket
*
@@ -4129,7 +4129,7 @@ declare module "bun" {
setMaxSendFragment(size: number): boolean;
}
interface SocketListener<Data = undefined> {
interface SocketListener<Data = undefined> extends Disposable {
stop(closeActiveConnections?: boolean): void;
ref(): void;
unref(): void;

View File

@@ -874,11 +874,22 @@ pub const Listener = struct {
return JSValue.jsUndefined();
}
pub fn dispose(this: *Listener, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue {
this.doStop(true);
return .undefined;
}
pub fn stop(this: *Listener, _: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) JSValue {
const arguments = callframe.arguments(1);
log("close", .{});
var listener = this.listener orelse return JSValue.jsUndefined();
this.doStop(if (arguments.len > 0 and arguments.ptr[0].isBoolean()) arguments.ptr[0].toBoolean() else false);
return .undefined;
}
fn doStop(this: *Listener, force_close: bool) void {
var listener = this.listener orelse return;
this.listener = null;
this.poll_ref.unref(this.handlers.vm);
@@ -891,8 +902,7 @@ pub const Listener = struct {
this.strong_self.clear();
this.strong_data.clear();
} else {
const forceClose = arguments.len > 0 and arguments.ptr[0].isBoolean() and arguments.ptr[0].toBoolean() and this.socket_context != null;
if (forceClose) {
if (force_close) {
// close all connections in this context and wait for them to close
this.socket_context.?.close(this.ssl);
} else {
@@ -900,8 +910,6 @@ pub const Listener = struct {
listener.close(this.ssl);
}
}
return JSValue.jsUndefined();
}
pub fn finalize(this: *Listener) callconv(.C) void {

View File

@@ -116,7 +116,7 @@ function generate(ssl) {
},
"@@dispose": {
fn: "shutdown",
fn: "end",
length: 0,
},
@@ -191,7 +191,7 @@ export default [
length: 1,
},
"@@dispose": {
fn: "stop",
fn: "dispose",
length: 0,
},