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

This project is part of the @thi.ng/umbrella monorepo.

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()

These different types of delayed execution are unified into the single schedule() function and the TimerType 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) 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);

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

Status

ALPHA - bleeding edge / work-in-progress

Search or submit any issues for this package

Installation

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

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/wasm-api-schedule"></script>

Skypack documentation

For Node.js REPL:

const wasmApiSchedule = await import("@thi.ng/wasm-api-schedule");

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

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

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

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 - 2023 Karsten Schmidt // Apache License 2.0