zigar.thread.WorkQueue(ns).promisify(self, func) - chung-leong/zigar GitHub Wiki
Define a function that call the given function in a worker thread. On the JavaScript side, the
resulting function will be async--i.e. it returns a
Promise.
Usage
const std = @import("std");
const zigar = @import("zigar");
var work_queue: zigar.thread.WorkQueue(ns) = .{};
pub const startup = work_queue.promisify(.startup);
pub const shutdown = work_queue.promisify(.shutdown);
pub const hello = work_queue.promisify(ns.hello);
const ns = struct {
pub fn hello() void {
std.Thread.sleep(500 * 1000000); // pause for half a second
std.debug.print("Hello, world!", .{});
}
};
import { hello, shutdown, startup } from './work-queue-example-7.zig';
await startup();
try {
await hello();
} finally {
await shutdown();
}
Hello, world!
Arguments:
self:@This()func:anytype
A public function in the namespace used to define the work queue type or an enum literal denoting one of the following control functions:.startup- start up the work queue with the specified number of threads, obtaining memory from either std.heap.c_allocator or std.heap.wasm_allocator..shutdown- shut down the work queue, returning when the
Return value:
Promisified(func)
Note:
This function calls asyncify() and throws a compilation error if the resulting function does not return a promise.