mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 13:51:47 +00:00
✂️ dead code
This commit is contained in:
@@ -1835,104 +1835,6 @@ pub const Dirent = struct {
|
||||
}
|
||||
};
|
||||
|
||||
pub const Emitter = struct {
|
||||
pub const Listener = struct {
|
||||
once: bool = false,
|
||||
callback: JSC.JSValue,
|
||||
|
||||
pub const List = struct {
|
||||
pub const ArrayList = std.MultiArrayList(Listener);
|
||||
list: ArrayList = ArrayList{},
|
||||
once_count: u32 = 0,
|
||||
|
||||
pub fn append(this: *List, allocator: std.mem.Allocator, ctx: JSC.C.JSContextRef, listener: Listener) !void {
|
||||
JSC.C.JSValueProtect(ctx, listener.callback.asObjectRef());
|
||||
try this.list.append(allocator, listener);
|
||||
this.once_count +|= @as(u32, @intFromBool(listener.once));
|
||||
}
|
||||
|
||||
pub fn prepend(this: *List, allocator: std.mem.Allocator, ctx: JSC.C.JSContextRef, listener: Listener) !void {
|
||||
JSC.C.JSValueProtect(ctx, listener.callback.asObjectRef());
|
||||
try this.list.ensureUnusedCapacity(allocator, 1);
|
||||
this.list.insertAssumeCapacity(0, listener);
|
||||
this.once_count +|= @as(u32, @intFromBool(listener.once));
|
||||
}
|
||||
|
||||
// removeListener() will remove, at most, one instance of a listener from the
|
||||
// listener array. If any single listener has been added multiple times to the
|
||||
// listener array for the specified eventName, then removeListener() must be
|
||||
// called multiple times to remove each instance.
|
||||
pub fn remove(this: *List, ctx: JSC.C.JSContextRef, callback: JSC.JSValue) bool {
|
||||
const callbacks = this.list.items(.callback);
|
||||
|
||||
for (callbacks, 0..) |item, i| {
|
||||
if (callback.eqlValue(item)) {
|
||||
JSC.C.JSValueUnprotect(ctx, callback.asObjectRef());
|
||||
this.once_count -|= @as(u32, @intFromBool(this.list.items(.once)[i]));
|
||||
this.list.orderedRemove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn emit(this: *List, globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) void {
|
||||
var i: usize = 0;
|
||||
outer: while (true) {
|
||||
var slice = this.list.slice();
|
||||
var callbacks = slice.items(.callback);
|
||||
var once = slice.items(.once);
|
||||
while (i < callbacks.len) : (i += 1) {
|
||||
const callback = callbacks[i];
|
||||
|
||||
globalObject.enqueueMicrotask1(
|
||||
callback,
|
||||
value,
|
||||
);
|
||||
|
||||
if (once[i]) {
|
||||
this.once_count -= 1;
|
||||
JSC.C.JSValueUnprotect(globalObject, callback.asObjectRef());
|
||||
this.list.orderedRemove(i);
|
||||
slice = this.list.slice();
|
||||
callbacks = slice.items(.callback);
|
||||
once = slice.items(.once);
|
||||
continue :outer;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
pub fn New(comptime EventType: type) type {
|
||||
return struct {
|
||||
const EventEmitter = @This();
|
||||
pub const Map = std.enums.EnumArray(EventType, Listener.List);
|
||||
listeners: Map = Map.initFill(Listener.List{}),
|
||||
|
||||
pub fn addListener(this: *EventEmitter, ctx: JSC.C.JSContextRef, event: EventType, listener: Emitter.Listener) !void {
|
||||
try this.listeners.getPtr(event).append(bun.default_allocator, ctx, listener);
|
||||
}
|
||||
|
||||
pub fn prependListener(this: *EventEmitter, ctx: JSC.C.JSContextRef, event: EventType, listener: Emitter.Listener) !void {
|
||||
try this.listeners.getPtr(event).prepend(bun.default_allocator, ctx, listener);
|
||||
}
|
||||
|
||||
pub fn emit(this: *EventEmitter, event: EventType, globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) void {
|
||||
this.listeners.getPtr(event).emit(globalObject, value);
|
||||
}
|
||||
|
||||
pub fn removeListener(this: *EventEmitter, ctx: JSC.C.JSContextRef, event: EventType, callback: JSC.JSValue) bool {
|
||||
return this.listeners.getPtr(event).remove(ctx, callback);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const Process = struct {
|
||||
pub fn getArgv0(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue {
|
||||
return JSC.ZigString.fromUTF8(bun.argv[0]).toJS(globalObject);
|
||||
|
||||
Reference in New Issue
Block a user