zigar.thread.WorkQueue(ns).promisify(self, func) ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

JavaScript | PHP


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(worker) = .{};

pub const startup = work_queue.promisify(.startup);
pub const shutdown = work_queue.promisify(.shutdown);
pub const hello = work_queue.promisify(worker.hello);

const worker = struct {
    pub fn hello() void {
        std.Thread.sleep(500 * 1000000); // pause for half a second
        std.debug.print("Hello, world!", .{});
    }
};
<?php

$m = zigar_use(__DIR__ . '/work-queue-example-7.zig');

$m->startup(1);
try {
    $m->hello();
} finally {
    $m->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:

Return value:

Promisified(func)

Note:

This function calls asyncify() and throws a compilation error if the resulting function does not return a promise.


WorkQueue(ns) | Promisified(func)