zigar.function.Generator(T).pipe(self, iterator) - chung-leong/zigar GitHub Wiki

Obtain contents from an iterator and send them to the JavaScript side.

Usage:

const std = @import("std");
const zigar = @import("zigar");

var gpa = std.heap.DebugAllocator(.{}).init;
const allocator = gpa.allocator();

const Generator = zigar.function.Generator(?[]const u8);

pub fn start() !void {
    try zigar.thread.use();
}

pub fn stop() void {
    zigar.thread.end();
}

pub fn splitSequence(text: []const u8, delimiter: []const u8, generator: Generator) !void {
    const thread = try std.Thread.spawn(.{}, generateSequence, .{
        text,
        delimiter,
        generator,
    });
    thread.detach();
}

fn generateSequence(text: []const u8, delimiter: []const u8, generator: Generator) void {
    const iter = std.mem.splitSequence(u8, text, delimiter);
    generator.pipe(iter);
}
import { splitSequence, start, stop } from './generator-example-4.zig';

try {
    start();
    for await (const s of splitSequence('hello||world||123||chicken', '||')) {
        console.log(s.string);
    }
} finally {
    stop();
}
hello
world
123
chicken

Arguments:

  • self: @This()
  • iterator: anytype
    An iterator producing T or an error union of one.

Return value:

void


Generator(T)