summaryrefslogtreecommitdiff
path: root/src/sketch/request.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-28 21:07:13 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit590ba7b9b579b97e34b51ab7e3460ebc85713343 (patch)
tree6a73a7570ff4f79f41deff078455877b9bc6571b /src/sketch/request.zig
parentbc8e6eb1bfef0c5e9b3c40fc1e4fd5a1659e2b03 (diff)
downloadsketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.tar.gz
sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.zip
sketchbook: Zig 0.17, a Store on disk, requests, a scope (waveform, spectrum, spectrogram), audio codecs and fonts, web host
Diffstat (limited to 'src/sketch/request.zig')
-rw-r--r--src/sketch/request.zig59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/sketch/request.zig b/src/sketch/request.zig
new file mode 100644
index 0000000..0b8b400
--- /dev/null
+++ b/src/sketch/request.zig
@@ -0,0 +1,59 @@
+//! Requests from `update` that `draw` carries out once. `update` sees only
+//! State and input, so when a key should do something to Session or Store
+//! (start the microphone, open a dialog, seek a stream), State holds the
+//! request and Session remembers which ones it has handled:
+//!
+//! // State
+//! seek: sk.Request(f32) = .{},
+//! // Session
+//! seek: sk.Handled = .{},
+//!
+//! // update
+//! if (in.pressed(.home)) s.seek.send(0);
+//! // draw
+//! if (ss.seek.take(s.seek)) |to| cursor = to * len;
+const std = @import("std");
+
+/// A request carrying `T`, starting at `T`'s zero value.
+pub fn Request(comptime T: type) type {
+ return struct {
+ value: T = std.mem.zeroes(T),
+ /// Bumped by every `send`, so the same value can be asked for twice.
+ serial: u32 = 0,
+
+ pub const Value = T;
+
+ pub fn send(r: *@This(), value: T) void {
+ r.value = value;
+ r.serial +%= 1;
+ }
+ };
+}
+
+/// Which request a Session has carried out.
+pub const Handled = struct {
+ serial: ?u32 = null,
+
+ /// The request's value if it is new to this Session. A fresh Session (a
+ /// restart, or Session's layout changed) counts earlier requests as
+ /// handled: nothing re-fires from saved state, as `Input.started` asks
+ /// of `update`. So take every request on every frame, even when its
+ /// value will be ignored: the first take only catches up.
+ pub fn take(h: *Handled, request: anytype) ?@TypeOf(request).Value {
+ defer h.serial = request.serial;
+ const last = h.serial orelse return null;
+ return if (last == request.serial) null else request.value;
+ }
+};
+
+test "a request is taken once, and none from before the session" {
+ var r: Request(f32) = .{};
+ r.send(0.5);
+ var h: Handled = .{};
+ try std.testing.expectEqual(@as(?f32, null), h.take(r));
+ r.send(0.25);
+ try std.testing.expectEqual(@as(?f32, 0.25), h.take(r));
+ try std.testing.expectEqual(@as(?f32, null), h.take(r));
+ r.send(0.25);
+ try std.testing.expectEqual(@as(?f32, 0.25), h.take(r));
+}