also looking at this
fix(ios): hand the page the answers native was already sending
#222
18 files
+597
-164
| @@ -1,13 +1,12 @@ | ||
| 1 | 1 | //! The `mobile` namespace's speech pair: `startListening` and `stopListening`. |
| 2 | 2 | //! |
| 3 | //! Neither action replies. Both are raw `postMessage` calls with no | |
| 4 | //! `callbackId` (`CraftApp.swift:1562-1568`), so nothing on the page is | |
| 5 | //! awaiting an answer and every outcome — success, refusal, transcript, end — | |
| 6 | //! travels as a `craftSpeech*` event. That is why both are declared | |
| 7 | //! `.reply = .none` below, and why no path in this file returns an error to | |
| 8 | //! `ios_dispatch`: an error there would call `sendErrorToJS` under a request | |
| 9 | //! id no page is holding, which is console noise standing in for the event a | |
| 10 | //! page is actually listening for. | |
| 3 | //! Both actions answer `true` as soon as the request is taken, and nothing | |
| 4 | //! more. The page awaits that answer (#207), but everything that happens after | |
| 5 | //! it — a declined prompt, no recognizer, a transcript, the end — travels as a | |
| 6 | //! `craftSpeech*` event, because none of it is known while the dispatch frame | |
| 7 | //! is still open. No path in this file returns an error to `ios_dispatch` | |
| 8 | //! either: a failure is reported as the event a page is listening for, and | |
| 9 | //! the promise has already been answered. | |
| 11 | 10 | //! |
| 12 | 11 | //! ## What this round changed |
| 13 | 12 | //! |
| @@ -188,25 +187,28 @@ pub const A = struct { | ||
| 188 | 187 | pub const stop_listening = "stopListening"; |
| 189 | 188 | }; |
| 190 | 189 | |
| 191 | /// `.none` for both, and the distinction matters. | |
| 190 | /// `.result` for both: `true`, sent from `handleMessage` while the dispatch | |
| 191 | /// frame still holds the page's request id. | |
| 192 | 192 | /// |
| 193 | /// `.result` would tell an app to await a reply that never comes: neither | |
| 194 | /// action calls `sendResultToJS` on any path, because neither has a page-side | |
| 195 | /// promise to settle. Everything observable leaves through `ios_events`. | |
| 193 | /// Settling on the outcome instead would leave the promise open. The | |
| 194 | /// authorization answer arrives after `ios_dispatch` has popped the request | |
| 195 | /// context, so a reply sent from there carries no id and the page drops it, | |
| 196 | /// and a start while authorization is already in flight has no outcome of its | |
| 197 | /// own. Everything after the `true` leaves through `ios_events`. | |
| 196 | 198 | /// |
| 197 | 199 | /// `.live` for both. A refusal here is a specific condition — speech not |
| 198 | 200 | /// configured, authorization declined, no input route — and never the normal |
| 199 | 201 | /// answer, which is what `.unavailable` would claim. |
| 200 | 202 | pub const capability_actions = [_]capabilities.ActionDecl{ |
| 201 | .{ .name = A.start_listening, .reply = .none }, | |
| 202 | .{ .name = A.stop_listening, .reply = .none }, | |
| 203 | .{ .name = A.start_listening, .reply = .result }, | |
| 204 | .{ .name = A.stop_listening, .reply = .result }, | |
| 203 | 205 | }; |
| 204 | 206 | |
| 205 | 207 | pub const SpeechBridge = struct { |
| 206 | /// Held for the interface `ios_dispatch` builds every mobile bridge with. | |
| 207 | /// Unused: nothing in a dispatch allocates, and the two paths that do — | |
| 208 | /// shaping a transcript and shaping an error — run from callbacks where | |
| 209 | /// this bridge is long gone, so they take the C allocator instead. | |
| 208 | /// Shapes the `true` each action answers with. The two other paths that | |
| 209 | /// allocate — shaping a transcript and shaping an error — run from | |
| 210 | /// callbacks where this bridge is long gone, so they take the C allocator | |
| 211 | /// instead. | |
| 210 | 212 | allocator: std.mem.Allocator, |
| 211 | 213 | |
| 212 | 214 | const Self = @This(); |
| @@ -218,15 +220,17 @@ pub const SpeechBridge = struct { | ||
| 218 | 220 | pub fn deinit(_: *Self) void {} |
| 219 | 221 | |
| 220 | 222 | /// The payload is ignored, deliberately: Swift reads no field from either |
| 221 | /// action, and the injected JS posts neither a body nor a `callbackId`. | |
| 222 | /// Parsing it would only create a way to fail that the spec has not got. | |
| 223 | pub fn handleMessage(_: *Self, action: []const u8, _: []const u8) !void { | |
| 223 | /// action, and the injected JS posts only a `callbackId`. Parsing it would | |
| 224 | /// only create a way to fail that the spec has not got. | |
| 225 | pub fn handleMessage(self: *Self, action: []const u8, _: []const u8) !void { | |
| 224 | 226 | if (std.mem.eql(u8, action, A.start_listening)) { |
| 225 | 227 | startListening(); |
| 228 | bridge_error.sendResultToJS(self.allocator, A.start_listening, "true"); | |
| 226 | 229 | return; |
| 227 | 230 | } |
| 228 | 231 | if (std.mem.eql(u8, action, A.stop_listening)) { |
| 229 | 232 | stopListening(); |
| 233 | bridge_error.sendResultToJS(self.allocator, A.stop_listening, "true"); | |
| 230 | 234 | return; |
| 231 | 235 | } |
| 232 | 236 | return BridgeError.UnknownAction; |
| @@ -1184,11 +1188,12 @@ test "the action names match the Swift case labels exactly" { | ||
| 1184 | 1188 | try testing.expectEqualStrings("stopListening", A.stop_listening); |
| 1185 | 1189 | } |
| 1186 | 1190 | |
| 1187 | test "both actions are declared, and neither promises a reply" { | |
| 1191 | test "both actions are declared, and both answer the promise the page awaits" { | |
| 1188 | 1192 | try testing.expectEqual(@as(usize, 2), capability_actions.len); |
| 1189 | 1193 | for (capability_actions) |decl| { |
| 1190 | // `.result` would tell an app to await something no path here sends. | |
| 1191 | try testing.expectEqual(capabilities.Reply.none, decl.reply); | |
| 1194 | // The page posts a callbackId for both (#207), so `.none` would be a | |
| 1195 | // promise that never settles. | |
| 1196 | try testing.expectEqual(capabilities.Reply.result, decl.reply); | |
| 1192 | 1197 | try testing.expectEqual(capabilities.ActionStatus.live, decl.status); |
| 1193 | 1198 | } |
| 1194 | 1199 | } |
| @@ -1207,8 +1212,7 @@ test "an unrelated action falls through rather than being refused" { | ||
| 1207 | 1212 | |
| 1208 | 1213 | test "a malformed payload is not a reason to refuse either action" { |
| 1209 | 1214 | // Neither action reads a field, so neither parses one. A parser here would |
| 1210 | // invent a failure the spec has not got — and it would surface as a | |
| 1211 | // dispatcher error under a request id no page holds. | |
| 1215 | // invent a failure the spec has not got. | |
| 1212 | 1216 | var bridge = SpeechBridge.init(testing.allocator); |
| 1213 | 1217 | defer bridge.deinit(); |
| 1214 | 1218 | |