also looking at this
fix(ios): hand the page the answers native was already sending
#222
18 files
+597
-164
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | //! The haptics half of the `mobile` namespace: `haptic` and `vibrate`. |
| 2 | 2 | //! |
| 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. | |
| 6 | 6 | //! |
| 7 | 7 | //! ## The refusal that used to live here |
| 8 | 8 | //! |
| @@ -83,23 +83,21 @@ fn hapticTypeIn(allocator: std.mem.Allocator, data: []const u8) !HapticType { | ||
| 83 | 83 | /// `craft.vibrate([100])` fires impacts on an app with haptics "disabled". |
| 84 | 84 | /// Adding a gate here would be a divergence dressed as a tidy-up. |
| 85 | 85 | pub 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 }, | |
| 92 | 90 | // `.result` carrying the JSON literal `true`. Not a shape worth improving: |
| 93 | 91 | // the hand-off path already delivers exactly this today, and `.none` would |
| 94 | 92 | // park any `_req`-based caller for the full 30s timeout. |
| 95 | 93 | .{ .name = A.vibrate, .reply = .result }, |
| 96 | 94 | }; |
| 97 | 95 | |
| 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 | |
| 100 | 98 | /// `.fragmentsAllowed`. It stays truthy on purpose: `craft-bridge.js` resolves |
| 101 | 99 | /// with `payload || {}`, so `false` or `0` would arrive as `{}`. |
| 102 | const vibrate_reply = "true"; | |
| 100 | const success_reply = "true"; | |
| 103 | 101 | |
| 104 | 102 | pub const HapticsBridge = struct { |
| 105 | 103 | allocator: std.mem.Allocator, |
| @@ -136,12 +134,13 @@ pub const HapticsBridge = struct { | ||
| 136 | 134 | /// unrecognised style to `.impact_medium` as well, matching the spec's |
| 137 | 135 | /// `default:` arm, and this path exercises the same branch. |
| 138 | 136 | /// |
| 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. | |
| 143 | 141 | fn haptic(self: *Self, data: []const u8) !void { |
| 144 | 142 | try triggerHapticChecked(try hapticTypeIn(self.allocator, data)); |
| 143 | bridge_error.sendResultToJS(self.allocator, A.haptic, success_reply); | |
| 145 | 144 | } |
| 146 | 145 | |
| 147 | 146 | /// `case "vibrate"` (`CraftApp.swift:685-691`). |
| @@ -176,7 +175,7 @@ pub const HapticsBridge = struct { | ||
| 176 | 175 | try triggerHapticChecked(.impact_medium); |
| 177 | 176 | } |
| 178 | 177 | |
| 179 | bridge_error.sendResultToJS(self.allocator, A.vibrate, vibrate_reply); | |
| 178 | bridge_error.sendResultToJS(self.allocator, A.vibrate, success_reply); | |
| 180 | 179 | } |
| 181 | 180 | }; |
| 182 | 181 | |
| @@ -282,8 +281,7 @@ fn schedulePattern(items: []const std.json.Value) !usize { | ||
| 282 | 281 | /// Engine is absent on the simulator and on iPad, and System Haptics can be off |
| 283 | 282 | /// in Settings — all three are silent no-ops with no API that reports them. So |
| 284 | 283 | /// "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. | |
| 287 | 285 | /// |
| 288 | 286 | /// `pub` for one caller outside this file. `bridge_mobile_speech.zig` fires a |
| 289 | 287 | /// light impact on start and on stop because `CraftApp.swift:2544` and `:2558` |
| @@ -384,8 +382,8 @@ pub fn triggerHapticChecked(haptic_type: HapticType) !void { | ||
| 384 | 382 | /// tag, so honouring `'soft'` as `UIImpactFeedbackStyleSoft` would mean |
| 385 | 383 | /// widening the enum *and* changing what the shipped API does. |
| 386 | 384 | /// - **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. | |
| 389 | 387 | /// |
| 390 | 388 | /// The field is `style`. Not `type` — `examples/web_to_native/app.html` posts |
| 391 | 389 | /// `{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" { | ||
| 482 | 480 | try testing.expectEqualStrings(A.vibrate, capability_actions[1].name); |
| 483 | 481 | |
| 484 | 482 | // 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); | |
| 488 | 485 | try testing.expectEqual(capabilities.Reply.result, capability_actions[1].reply); |
| 489 | 486 | } |
| 490 | 487 | |
| @@ -548,7 +545,7 @@ test "haptic reads the style the way the spec's cast reads it" { | ||
| 548 | 545 | |
| 549 | 546 | // `as? String` fails for each of these, and `?? "medium"` catches them all. |
| 550 | 547 | // 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. | |
| 552 | 549 | try testing.expectEqual(HapticType.impact_medium, try hapticTypeIn(alloc, "{}")); |
| 553 | 550 | try testing.expectEqual(HapticType.impact_medium, try hapticTypeIn(alloc, "{\"style\":null}")); |
| 554 | 551 | 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'" { | ||
| 584 | 581 | // because fixing it is a behaviour change, not a port. |
| 585 | 582 | try testing.expectEqual(HapticType.impact_medium, hapticTypeForStyle("soft")); |
| 586 | 583 | 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. | |
| 589 | 585 | try testing.expectEqual(HapticType.impact_medium, hapticTypeForStyle("mediuim")); |
| 590 | 586 | try testing.expectEqual(HapticType.impact_medium, hapticTypeForStyle("")); |
| 591 | 587 | } |
| @@ -688,14 +684,14 @@ test "an absurd duration saturates instead of panicking" { | ||
| 688 | 684 | ); |
| 689 | 685 | } |
| 690 | 686 | |
| 691 | test "the vibrate reply is the bare JSON literal the hand-off path already sends" { | |
| 687 | test "the haptic and vibrate reply is the bare JSON literal the hand-off path sends" { | |
| 692 | 688 | // Not `{"success":true}`. The spec serialises with `.fragmentsAllowed`, so |
| 693 | 689 | // a page on the hand-off path receives `true` today, and it must keep |
| 694 | 690 | // receiving `true`. Truthy also matters: `craft-bridge.js` resolves with |
| 695 | 691 | // `payload || {}`, which would turn `false` into an empty object. |
| 696 | try testing.expectEqualStrings("true", vibrate_reply); | |
| 692 | try testing.expectEqualStrings("true", success_reply); | |
| 697 | 693 | |
| 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, .{}); | |
| 699 | 695 | defer parsed.deinit(); |
| 700 | 696 | try testing.expectEqual(true, parsed.value.bool); |
| 701 | 697 | } |