ReviewOS

also looking at this

craft-native/craft

fix(ios): hand the page the answers native was already sending

#222
Merged glennmichael123 wants to merge fix/ios-unanswered-posts into main
18 files +597 -164
packages/zig/src/bridge_mobile_haptics.zigmodified+26-30
Changes to packages/zig/src/bridge_mobile_haptics.zig
@@ -1,8 +1,8 @@
11//! The haptics half of the `mobile` namespace: `haptic` and `vibrate`.
22//!
3//! Split out of `bridge_mobile.zig` because the two actions have opposite
4//! contracts: `haptic` is fire-and-forget and gated on `config.enableHaptics`,
5//! `vibrate` replies and is gated on nothing.
3//! Split out of `bridge_mobile.zig` because the two actions are gated
4//! differently: `haptic` on `config.enableHaptics`, `vibrate` on nothing. Both
5//! answer `true` once UIKit has taken the call.
66//!
77//! ## The refusal that used to live here
88//!
@@ -83,23 +83,21 @@ fn hapticTypeIn(allocator: std.mem.Allocator, data: []const u8) !HapticType {
8383/// `craft.vibrate([100])` fires impacts on an app with haptics "disabled".
8484/// Adding a gate here would be a divergence dressed as a tidy-up.
8585pub const capability_actions = [_]capabilities.ActionDecl{
86 // `.none` is the contract: the page's `craft.haptic()` returns undefined
87 // and the spec's `case "haptic"` never touches `resolveCallback`. An error
88 // still reaches the page — with no pending entry to settle,
89 // `craft-bridge.js` reports it to the console — so a refusal is logged
90 // rather than swallowed.
91 .{ .name = A.haptic, .reply = .none },
86 // `.result`, the same `true` as `vibrate`. The page's `craft.haptic()`
87 // awaits it, so a refusal from the gate reaches the caller, and `.none`
88 // would leave that promise open on every call that succeeds (#207).
89 .{ .name = A.haptic, .reply = .result },
9290 // `.result` carrying the JSON literal `true`. Not a shape worth improving:
9391 // the hand-off path already delivers exactly this today, and `.none` would
9492 // park any `_req`-based caller for the full 30s timeout.
9593 .{ .name = A.vibrate, .reply = .result },
9694};
9795
98/// The reply `vibrate` sends, byte-identical to what the hand-off path delivers
99/// today. A bare `true`, not an object — the spec serialises with
96/// The reply `haptic` and `vibrate` send, byte-identical to what the hand-off
97/// path delivers. A bare `true`, not an object — the spec serialises with
10098/// `.fragmentsAllowed`. It stays truthy on purpose: `craft-bridge.js` resolves
10199/// with `payload || {}`, so `false` or `0` would arrive as `{}`.
102const vibrate_reply = "true";
100const success_reply = "true";
103101
104102pub const HapticsBridge = struct {
105103 allocator: std.mem.Allocator,
@@ -136,12 +134,13 @@ pub const HapticsBridge = struct {
136134 /// unrecognised style to `.impact_medium` as well, matching the spec's
137135 /// `default:` arm, and this path exercises the same branch.
138136 ///
139 /// No reply on success, matching `.none` and the spec. The error from
140 /// `triggerHapticChecked` is the one thing that can come back, and it means
141 /// UIKit was not there to accept the call — never that the device stayed
142 /// still, which no iOS API reports.
137 /// `true` once UIKit has accepted the call, as the spec's `case "haptic"`
138 /// answers. The error from `triggerHapticChecked` means UIKit was not there
139 /// to accept it — never that the device stayed still, which no iOS API
140 /// reports.
143141 fn haptic(self: *Self, data: []const u8) !void {
144142 try triggerHapticChecked(try hapticTypeIn(self.allocator, data));
143 bridge_error.sendResultToJS(self.allocator, A.haptic, success_reply);
145144 }
146145
147146 /// `case "vibrate"` (`CraftApp.swift:685-691`).
@@ -176,7 +175,7 @@ pub const HapticsBridge = struct {
176175 try triggerHapticChecked(.impact_medium);
177176 }
178177
179 bridge_error.sendResultToJS(self.allocator, A.vibrate, vibrate_reply);
178 bridge_error.sendResultToJS(self.allocator, A.vibrate, success_reply);
180179 }
181180};
182181
@@ -282,8 +281,7 @@ fn schedulePattern(items: []const std.json.Value) !usize {
282281/// Engine is absent on the simulator and on iPad, and System Haptics can be off
283282/// in Settings — all three are silent no-ops with no API that reports them. So
284283/// "returned without error" means "UIKit accepted the call", never "the device
285/// buzzed", which is the other half of why `haptic` has no success reply to
286/// fabricate.
284/// buzzed", and that is all the `true` `haptic` answers with claims.
287285///
288286/// `pub` for one caller outside this file. `bridge_mobile_speech.zig` fires a
289287/// light impact on start and on stop because `CraftApp.swift:2544` and `:2558`
@@ -384,8 +382,8 @@ pub fn triggerHapticChecked(haptic_type: HapticType) !void {
384382/// tag, so honouring `'soft'` as `UIImpactFeedbackStyleSoft` would mean
385383/// widening the enum *and* changing what the shipped API does.
386384/// - **An unknown style is absorbed, not rejected.** A typo is indistinguishable
387/// from `"medium"`. With no reply channel there is nowhere to report it, and
388/// the spec's `default:` swallows it too.
385/// from `"medium"`, because the spec's `default:` swallows it and rejecting
386/// it here would be stricter than the surface pages are written against.
389387///
390388/// The field is `style`. Not `type` — `examples/web_to_native/app.html` posts
391389/// `{type: ...}` but through a local `invoke` shim that never reaches this
@@ -482,9 +480,8 @@ test "the table declares exactly the two actions the dispatcher serves" {
482480 try testing.expectEqualStrings(A.vibrate, capability_actions[1].name);
483481
484482 // The replies are the halves that go wrong invisibly: a `.result` that
485 // never replies parks the caller for 30s, and a `.none` that is awaited
486 // resolves immediately and means nothing.
487 try testing.expectEqual(capabilities.Reply.none, capability_actions[0].reply);
483 // never replies leaves the page's promise open. Both are awaited (#207).
484 try testing.expectEqual(capabilities.Reply.result, capability_actions[0].reply);
488485 try testing.expectEqual(capabilities.Reply.result, capability_actions[1].reply);
489486}
490487
@@ -548,7 +545,7 @@ test "haptic reads the style the way the spec's cast reads it" {
548545
549546 // `as? String` fails for each of these, and `?? "medium"` catches them all.
550547 // A page posting `{style: 2}` gets a medium impact, not an error — matching
551 // the spec, which cannot report one either: `case "haptic"` has no reply.
548 // the spec, whose cast takes the same default.
552549 try testing.expectEqual(HapticType.impact_medium, try hapticTypeIn(alloc, "{}"));
553550 try testing.expectEqual(HapticType.impact_medium, try hapticTypeIn(alloc, "{\"style\":null}"));
554551 try testing.expectEqual(HapticType.impact_medium, try hapticTypeIn(alloc, "{\"style\":2}"));
@@ -584,8 +581,7 @@ test "the spec's default arm absorbs everything else, including 'soft'" {
584581 // because fixing it is a behaviour change, not a port.
585582 try testing.expectEqual(HapticType.impact_medium, hapticTypeForStyle("soft"));
586583 try testing.expectEqual(HapticType.impact_medium, hapticTypeForStyle("rigid"));
587 // A typo is indistinguishable from "medium". There is no reply channel to
588 // say otherwise on, which is exactly what the spec does too.
584 // A typo is indistinguishable from "medium", exactly as in the spec.
589585 try testing.expectEqual(HapticType.impact_medium, hapticTypeForStyle("mediuim"));
590586 try testing.expectEqual(HapticType.impact_medium, hapticTypeForStyle(""));
591587}
@@ -688,14 +684,14 @@ test "an absurd duration saturates instead of panicking" {
688684 );
689685}
690686
691test "the vibrate reply is the bare JSON literal the hand-off path already sends" {
687test "the haptic and vibrate reply is the bare JSON literal the hand-off path sends" {
692688 // Not `{"success":true}`. The spec serialises with `.fragmentsAllowed`, so
693689 // a page on the hand-off path receives `true` today, and it must keep
694690 // receiving `true`. Truthy also matters: `craft-bridge.js` resolves with
695691 // `payload || {}`, which would turn `false` into an empty object.
696 try testing.expectEqualStrings("true", vibrate_reply);
692 try testing.expectEqualStrings("true", success_reply);
697693
698 const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, vibrate_reply, .{});
694 const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, success_reply, .{});
699695 defer parsed.deinit();
700696 try testing.expectEqual(true, parsed.value.bool);
701697}