zigar.thread.WorkQueue(ns).promsify(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.
func must be a public function in the namespace used to define the work queue type.
Usage
const std = @import("std");
const zigar = @import("zigar");
var work_queue: zigar.thread.WorkQueue(ns) = .{};
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
pub fn startup() !void {
try work_queue.init(.{
.allocator = allocator,
.n_jobs = 1,
});
}
pub fn shutdown(promise: zigar.function.Promise(void)) void {
return work_queue.deinitAsync(promise);
}
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';
startup();
try {
await hello();
} finally {
await shutdown();
}
Hello, world!
Arguments:
self:@This()func:anytypeMust be a function that does not return a iterator.
Return value:
Asyncified(@TypeOf(func))
Note:
This function calls asyncify() and throws a compilation error if the resulting function does not return a promise.