-
Notifications
You must be signed in to change notification settings - Fork 6
zigar.thread.WorkQueue(ns).asyncify(self, func)
Chung Leong edited this page Dec 3, 2025
·
2 revisions
Define a function that call the given function in a worker thread. On the JavaScript side, the resulting function will return either a Promise or an AsyncGenerator depending whether the original function returns an iterator or not.
Usage
const std = @import("std");
const zigar = @import("zigar");
var work_queue: zigar.thread.WorkQueue(ns) = .{};
pub const shutdown = work_queue.promisify(.shutdown);
pub const sequence = work_queue.asyncify(ns.sequence);
const ns = struct {
const Iterator = struct {
current: usize = 0,
total: usize,
pub fn next(self: *@This()) ?usize {
if (self.current >= self.total) return null;
std.Thread.sleep(100 * 1000000); // pause for 100us
self.current += 1;
return self.current;
}
};
pub fn sequence(len: usize) Iterator {
return .{ .total = len };
}
};import { sequence, shutdown, startup } from './work-queue-example-8.zig';
try {
for await (const number of sequence(7)) {
// stderr is unbuffered so text appear immediately
process.stderr.write(`${number}\n`);
}
} finally {
await shutdown();
}Arguments:
-
self:@This() -
func:anytype
A public function in the namespace used to define the work queue type.
Return value:
Asyncified(@TypeOf(func))