Skip to content

Commit

Permalink
feat(wasm-api-schedule): add RAF schedule type, add now()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 16, 2024
1 parent dfd66ba commit 47bff01
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/wasm-api-schedule/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ export interface WasmScheduleImports extends WebAssembly.ModuleImports {
* @param listenerID
*/
_cancel(listenerID: number): void;
/**
* Returns fractional timestamp in milliseconds (using `performance.now()`)
*/
now(): number;
}
6 changes: 5 additions & 1 deletion packages/wasm-api-schedule/src/generated/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Generated by @thi.ng/wasm-api-bindgen at 2023-03-26T00:14:48.759Z
* Generated by @thi.ng/wasm-api-bindgen at 2024-08-16T09:18:10.788Z
* DO NOT EDIT!
*/

Expand All @@ -19,4 +19,8 @@ export enum ScheduleType {
* As soon as possible execution
*/
IMMEDIATE,
/**
* Same as `requestAnimationFrame()` (or equivalent if not available in JS env)
*/
RAF,
}
10 changes: 10 additions & 0 deletions packages/wasm-api-schedule/src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ const START: Record<ScheduleType, FnO<Fn0<void>, any>> = {
typeof setImmediate !== "undefined"
? setImmediate
: (x) => setTimeout(x, 0),
[ScheduleType.RAF]:
typeof requestAnimationFrame !== "undefined"
? requestAnimationFrame
: (x) => setTimeout(x, 16),
};

const CANCEL: Record<ScheduleType, Fn<any, void>> = {
[ScheduleType.ONCE]: clearTimeout,
[ScheduleType.INTERVAL]: clearInterval,
[ScheduleType.IMMEDIATE]:
typeof clearImmediate !== "undefined" ? clearImmediate : clearTimeout,
[ScheduleType.RAF]:
typeof cancelAnimationFrame !== "undefined"
? cancelAnimationFrame
: clearTimeout,
};

export class WasmSchedule implements IWasmAPI<WasmScheduleExports> {
Expand Down Expand Up @@ -72,6 +80,8 @@ export class WasmSchedule implements IWasmAPI<WasmScheduleExports> {
delete this.listeners[listenerID];
}
},

now: () => performance.now(),
};
}
}
6 changes: 5 additions & 1 deletion packages/wasm-api-schedule/src/typedefs.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"name": "interval",
"doc": "Recurring execution at fixed interval"
},
{ "name": "immediate", "doc": "As soon as possible execution" }
{ "name": "immediate", "doc": "As soon as possible execution" },
{
"name": "raf",
"doc": "Same as `requestAnimationFrame()` (or equivalent if not available in JS env)"
}
]
}
]
4 changes: 3 additions & 1 deletion packages/wasm-api-schedule/zig/api.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Generated by @thi.ng/wasm-api-bindgen at 2022-11-30T15:23:27.748Z
//! Generated by @thi.ng/wasm-api-bindgen at 2024-08-16T09:18:10.794Z
//! DO NOT EDIT!

const std = @import("std");
Expand All @@ -11,4 +11,6 @@ pub const ScheduleType = enum(i32) {
interval,
/// As soon as possible execution
immediate,
/// Same as `requestAnimationFrame()` (or equivalent if not available in JS env)
raf,
};
3 changes: 3 additions & 0 deletions packages/wasm-api-schedule/zig/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ pub fn cancel(callID: u16) void {
calls.remove(callID);
_cancel(callID);
}

/// Returns fractional timestamp in milliseconds (using `performance.now()`)
pub extern "schedule" fn now() f64;

0 comments on commit 47bff01

Please sign in to comment.