summaryrefslogtreecommitdiff
path: root/dev/file_watch.zig
diff options
context:
space:
mode:
Diffstat (limited to 'dev/file_watch.zig')
-rw-r--r--dev/file_watch.zig122
1 files changed, 122 insertions, 0 deletions
diff --git a/dev/file_watch.zig b/dev/file_watch.zig
index 84f37c81..58229d90 100644
--- a/dev/file_watch.zig
+++ b/dev/file_watch.zig
@@ -664,6 +664,88 @@ pub fn reloadTheme(
return false;
}
+/// A session's file watches: the kernel's descriptor, the table (each
+/// pane's mark and the theme's slot) and the reader that posts a change
+/// to the session's queue (Inbox) as `files_changed`. One a session, the
+/// session loop's (runtime/session.zig): the Host's three file calls are
+/// answered here whatever the frontend (`Calls`), and a frontend taking
+/// `files_changed` off the queue hands it back (`reload`).
+pub const Watches = struct {
+ fd: c_int = -1,
+ table: Table = @splat(null),
+ io: std.Io,
+ gpa: std.mem.Allocator,
+ queue: *Inbox,
+ reader: ?std.Io.Future(void) = null,
+
+ const Inbox = @import("Inbox.zig");
+
+ pub fn open(io: std.Io, gpa: std.mem.Allocator, queue: *Inbox) Watches {
+ return .{ .fd = init(false), .io = io, .gpa = gpa, .queue = queue };
+ }
+
+ /// The reader, from the loop's start on (none: nothing is watched for
+ /// a change, the marks still made).
+ pub fn start(w: *Watches) void {
+ if (w.fd >= 0) w.reader = w.io.concurrent(read, .{ w.io, w.fd, w.queue }) catch null;
+ }
+
+ pub fn deinit(w: *Watches) void {
+ stop(w.fd);
+ if (w.reader) |*r| r.cancel(w.io);
+ w.reader = null;
+ if (w.fd >= 0) _ = libc.close(w.fd);
+ w.fd = -1;
+ }
+
+ /// The marks reconciled after a wake; a second pass asked of the
+ /// queue when one wants it (`reloadPane`).
+ pub fn reload(w: *Watches, core: *pardes.Pardes) void {
+ if (reloadChanged(core, w.io, w.gpa, &w.table)) w.queue.push(.files_changed);
+ }
+
+ /// Blocked in a read of the descriptor (a kqueue's `wait`, where there
+ /// is nothing to read): a change posted, whatever it was (`reload`
+ /// reads every mark); canceled (`deinit`), it ends.
+ fn read(io: std.Io, fd: c_int, queue: *Inbox) void {
+ if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) {
+ while (wait(fd)) queue.push(.files_changed);
+ return;
+ }
+ const file: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } };
+ var buf: [4096]u8 = undefined;
+ while (true) {
+ const n = file.readStreaming(io, &.{&buf}) catch return;
+ if (n == 0) return;
+ queue.push(.files_changed);
+ }
+ }
+
+ /// The Host's file calls, for a host whose `ctx` is a `*Ctx` with the
+ /// fields `watches: *Watches` and `core: *Pardes`.
+ pub fn Calls(comptime Ctx: type) type {
+ return struct {
+ fn of(ctx: ?*anyopaque) *Ctx {
+ return @ptrCast(@alignCast(ctx.?));
+ }
+ pub fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void {
+ const s = of(ctx);
+ @import("host_io.zig").writeFile(s.core, &s.watches.table, id.index(), path, bytes);
+ }
+ pub fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void {
+ const s = of(ctx);
+ const w = s.watches;
+ if (applyEffect(s.core, w.io, w.fd, &w.table, id.index(), on, mode)) w.queue.push(.files_changed);
+ }
+ pub fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void {
+ const s = of(ctx);
+ const w = s.watches;
+ if (applyThemeEffect(s.core, w.gpa, w.fd, &w.table, generation, on)) w.queue.push(.files_changed);
+ }
+ };
+ }
+};
+
pub fn identify(io: std.Io, path: []const u8) !Identity {
const native = filesystem.localPath(path) orelse return error.NonLocalPath;
pardes.turn.yield();
@@ -961,3 +1043,43 @@ test "a watched file deleted wakes the watch at once, and its pane says so" {
_ = reloadChanged(core, io, gpa, &watches);
try std.testing.expect(pane.file.?.disk_gone);
}
+
+test "a session's watches: a marked file's change posted to the queue, the reader ended at the end" {
+ if (comptime !supported or !filesystem.platform_has_fs) return;
+ const io = std.testing.io;
+ const gpa = std.testing.allocator;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "before\n" });
+ var path_buf: [256]u8 = undefined;
+ const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/watched", .{tmp.sub_path});
+ const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
+ defer core.deinit();
+ while (core.nextEffect()) |_| {}
+ const id: u8 = @intCast(core.freeSlot().?);
+ const pane = try pardes.panes.File.open(core, id, path, 0);
+ var queue: Watches.Inbox = .{ .io = io, .gpa = gpa, .lsp_allocator = gpa };
+ defer queue.deinit();
+ var w: Watches = .open(io, gpa, &queue);
+ defer w.deinit();
+ if (w.fd < 0) return error.NoWatcher;
+ defer unwatchAll(w.fd, &w.table);
+ while (core.nextEffect()) |effect| switch (effect) {
+ .watch => |watch| if (watch.pane.index() == id and watch.on) {
+ _ = applyEffect(core, io, w.fd, &w.table, id, watch.on, watch.mode);
+ },
+ else => {},
+ };
+ w.start();
+ try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "after\n" });
+ var batch: Watches.Inbox.Batch = .{};
+ for (0..500) |_| {
+ queue.takeInto(&batch);
+ if (batch.len > 0) break;
+ try io.sleep(.fromMilliseconds(10), .awake);
+ }
+ try std.testing.expectEqual(@as(usize, 1), batch.len);
+ try std.testing.expect(batch.items[0] == .files_changed);
+ w.reload(core);
+ try std.testing.expectEqualStrings("after\n", pane.file.?.content);
+}