Skip to content

Latest commit

 

History

History

wasm-api-schedule

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@thi.ng/wasm-api-schedule

npm version npm downloads Mastodon Follow

Note

This is one of 199 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

Delayed & scheduled function execution (via setTimeout() etc.) for hybrid WASM apps. This is a support package for @thi.ng/wasm-api.

The package provides a WASM bridge API and abstraction for scheduling function calls via:

  • once: setTimeout() / clearTimeout()
  • interval: setInterval() / clearInterval()
  • immediate: setImmediate() / clearImmediate()(1)
  • raf: requestAnimationFrame() / cancelAnimationFrame()(1)

(1) Fallback provided in case the JS host env has no native support

These different types of delayed execution are unified into the single schedule() function and the ScheduleType enum. Scheduled callbacks can be cancelled via cancel()...

Zig example:

const wasm = @import("wasm-api");
const schedule = @import("wasm-api-schedule");

// ...

// the WASM API modules auto-initialize themselves if the root source
// file exposes a `WASM_ALLOCATOR`, otherwise you'll have to initialize manually:
try schedule.init(customAllocator);

// user callback function
fn exampleCallback(raw: ?*anyopaque) callconv(.C) void {
    if (wasm.ptrCast(*u32, raw)) |state| {
        // do something ...
    }
}

// arbitrary user state
var state: u32 = 0xdecafbad;

// schedule a single/one-off callback 500ms in the future
const listenerID = try schedule.schedule(
    .once,
    500,
    exampleCallback,
    &state,
);

// ...or maybe cancel it again
schedule.cancel(listenerID);

IMPORTANT: In Zig v0.12+ all event handlers must explicitly specify callconv(.C) See docs for more reference.

Also see the zig-counter and zig-todo-list example projects for more advanced usage...

Status

STABLE - used in production

Search or submit any issues for this package

Installation

yarn add @thi.ng/wasm-api-schedule

ESM import:

import * as was from "@thi.ng/wasm-api-schedule";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/wasm-api-schedule"></script>

JSDelivr documentation

Package sizes (brotli'd, pre-treeshake): ESM: 561 bytes

Dependencies

Note: @thi.ng/api is in most cases a type-only import (not used at runtime)

Usage examples

Three projects in this repo's /examples directory are using this package:

Screenshot Description Live demo Source
Simple Zig/WASM click counter DOM component Demo Source
Zig-based To-Do list, DOM creation, local storage task persistence Demo Source
Basic Zig/WebAssembly WebGL demo Demo Source

API

Generated API docs

TODO

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-wasm-api-schedule,
  title = "@thi.ng/wasm-api-schedule",
  author = "Karsten Schmidt",
  note = "https://thi.ng/wasm-api-schedule",
  year = 2022
}

License

© 2022 - 2024 Karsten Schmidt // Apache License 2.0