[Bun.js] Bun.sleep() lets you synchronously sleep. Thanks @evanwashere

This commit is contained in:
Jarred Sumner
2021-10-31 00:39:37 -07:00
parent ef94826f95
commit fb2aa186b7
2 changed files with 29 additions and 0 deletions

View File

@@ -455,6 +455,22 @@ pub const Bun = struct {
}
}
pub fn sleep(
this: void,
ctx: js.JSContextRef,
function: js.JSObjectRef,
thisObject: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSValueRef {
if (js.JSValueIsNumber(ctx, arguments[0])) {
const ms = JSValue.fromRef(arguments[0]).asNumber();
if (ms > 0 and std.math.isFinite(ms)) std.time.sleep(@floatToInt(u64, @floor(@floatCast(f128, ms) * std.time.ns_per_ms)));
}
return js.JSValueMakeUndefined(ctx);
}
var public_path_temp_str: [std.fs.MAX_PATH_BYTES]u8 = undefined;
pub fn getPublicPathJS(
@@ -493,6 +509,9 @@ pub const Bun = struct {
.rfn = Router.match,
.ts = Router.match_type_definition,
},
.sleep = .{
.rfn = sleep,
},
.fetch = .{
.rfn = Fetch.call,
.ts = d.ts{},