also looking at this
fix(ios): answer the shapes the spec answers, now that a bare false survives
#105
7 files
+423
-82
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -1093,11 +1093,11 @@ struct CraftWebView: UIViewRepresentable { renamed throughout | ||
| 1093 | 1093 | // MARK: - Siri Shortcuts |
| 1094 | 1094 | case "registerSiriShortcut": |
| 1095 | 1095 | if let phrase = body["phrase"] as? String, |
| 1096 | let action = body["action"] as? String { | |
| 1096 | let action = body["shortcutAction"] as? String { | |
| 1097 | 1097 | registerSiriShortcut(phrase: phrase, action: action, callbackId: callbackId) |
| 1098 | 1098 | } |
| 1099 | 1099 | case "removeSiriShortcut": |
| 1100 | if let action = body["action"] as? String { | |
| 1100 | if let action = body["shortcutAction"] as? String { | |
| 1101 | 1101 | removeSiriShortcut(action: action, callbackId: callbackId) |
| 1102 | 1102 | } |
| 1103 | 1103 | |
| @@ -2194,7 +2194,15 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2194 | 2194 | register: function(phrase, action) { |
| 2195 | 2195 | var self = window.craft; |
| 2196 | 2196 | var id = 'cb_' + (++self._callbackId); |
| 2197 | window.webkit.messageHandlers.craft.postMessage({action: 'registerSiriShortcut', phrase: phrase, action: action, callbackId: id}); | |
| 2197 | // `shortcutAction`, not `action`. This object had two | |
| 2198 | // keys called `action` — the message's own, and the | |
| 2199 | // shortcut's — and the later one wins, so every call | |
| 2200 | // arrived labelled with the shortcut's identifier | |
| 2201 | // instead of 'registerSiriShortcut'. No switch arm | |
| 2202 | // matched, nothing answered, and these wrappers park in | |
| 2203 | // `_callbacks` with no timeout: the promise never | |
| 2204 | // settled at all. | |
| 2205 | window.webkit.messageHandlers.craft.postMessage({action: 'registerSiriShortcut', phrase: phrase, shortcutAction: action, callbackId: id}); | |
| 2198 | 2206 | return new Promise(function(resolve, reject) { |
| 2199 | 2207 | self._callbacks[id] = {resolve: resolve, reject: reject}; |
| 2200 | 2208 | }); |
| @@ -2202,7 +2210,8 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2202 | 2210 | remove: function(action) { |
| 2203 | 2211 | var self = window.craft; |
| 2204 | 2212 | var id = 'cb_' + (++self._callbackId); |
| 2205 | window.webkit.messageHandlers.craft.postMessage({action: 'removeSiriShortcut', action: action, callbackId: id}); | |
| 2213 | // The same duplicate key as `register` above. | |
| 2214 | window.webkit.messageHandlers.craft.postMessage({action: 'removeSiriShortcut', shortcutAction: action, callbackId: id}); | |
| 2206 | 2215 | return new Promise(function(resolve, reject) { |
| 2207 | 2216 | self._callbacks[id] = {resolve: resolve, reject: reject}; |
| 2208 | 2217 | }); |
| @@ -105,12 +105,18 @@ pub const DisplayBridge = struct { | ||
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | /// `{"count":N}` in, `{"count":<what UIKit now reports>}` out. | |
| 108 | /// `{"count":N}` in, bare `true` out — what the spec resolves. | |
| 109 | 109 | /// |
| 110 | /// The reply is an object, not the bare `true` Swift resolves: | |
| 111 | /// `craft-bridge.js` settles with `payload || {}`, so any falsy scalar | |
| 112 | /// arrives at the page as `{}` and carries nothing. An object also lets the | |
| 113 | /// value be checked, which a `true` never could. | |
| 110 | /// This answered `{"count":<observed>}` until now, to work around | |
| 111 | /// `craft-bridge.js` settling with `payload || {}`: a falsy scalar reached | |
| 112 | /// the page as `{}` and carried nothing. That is fixed at its source | |
| 113 | /// (`_orEmpty`), so the wrapper is gone and pages reading `=== true` work | |
| 114 | /// again. | |
| 115 | /// | |
| 116 | /// The observed count goes to the log rather than the page. It was the one | |
| 117 | /// thing the object shape bought, and the spec never offered it — a page | |
| 118 | /// that wants the badge back has no API for it in either implementation, | |
| 119 | /// so inventing one here would be a second divergence rather than a fix. | |
| 114 | 120 | fn setBadge(self: *Self, data: []const u8) !void { |
| 115 | 121 | const requested = try badgeCountFrom(self.allocator, data); |
| 116 | 122 | try self.replyBadge(A.set_badge, try applyBadge(requested)); |
| @@ -125,11 +131,15 @@ pub const DisplayBridge = struct { | ||
| 125 | 131 | } |
| 126 | 132 | |
| 127 | 133 | fn replyBadge(self: *Self, action: []const u8, observed: i64) !void { |
| 128 | var buf: [48]u8 = undefined; | |
| 129 | bridge_error.sendResultToJS(self.allocator, action, try renderBadgeReply(&buf, observed)); | |
| 134 | std.log.info("{s}: badge is now {d}", .{ action, observed }); | |
| 135 | bridge_error.sendResultToJS(self.allocator, action, badge_applied); | |
| 130 | 136 | } |
| 131 | 137 | |
| 132 | /// `{"enabled":<bool>}` in, `{"enabled":<what UIKit now reports>}` out. | |
| 138 | /// `{"enabled":<bool>}` in, the bare observed boolean out. | |
| 139 | /// | |
| 140 | /// The spec resolves the `enabled` the page sent; this resolves what UIKit | |
| 141 | /// reports, which is the same value whenever the write took. Bare rather | |
| 142 | /// than `{"enabled":…}` for the reason recorded on `replyBadge`. | |
| 133 | 143 | /// |
| 134 | 144 | /// Swift also stores `isKeepingAwake`, which nothing in the template ever |
| 135 | 145 | /// reads. It is not ported: a variable with no reader cannot be wrong, and |
| @@ -140,11 +150,10 @@ pub const DisplayBridge = struct { | ||
| 140 | 150 | const requested = try keepAwakeFrom(self.allocator, data); |
| 141 | 151 | const observed = try applyKeepAwake(requested); |
| 142 | 152 | |
| 143 | var buf: [32]u8 = undefined; | |
| 144 | 153 | bridge_error.sendResultToJS( |
| 145 | 154 | self.allocator, |
| 146 | 155 | A.set_keep_awake, |
| 147 | try renderKeepAwakeReply(&buf, observed), | |
| 156 | if (observed) "true" else "false", | |
| 148 | 157 | ); |
| 149 | 158 | } |
| 150 | 159 | |
| @@ -185,13 +194,8 @@ pub const DisplayBridge = struct { | ||
| 185 | 194 | /// Split out because a test that re-typed the format string would pass no |
| 186 | 195 | /// matter what the handler sent — it would be asserting that `std.fmt` works. |
| 187 | 196 | /// These are the strings the page actually receives. |
| 188 | fn renderBadgeReply(buf: []u8, observed: i64) ![]const u8 { | |
| 189 | return std.fmt.bufPrint(buf, "{{\"count\":{d}}}", .{observed}); | |
| 190 | } | |
| 191 | ||
| 192 | fn renderKeepAwakeReply(buf: []u8, observed: bool) ![]const u8 { | |
| 193 | return std.fmt.bufPrint(buf, "{{\"enabled\":{}}}", .{observed}); | |
| 194 | } | |
| 197 | /// What `setBadge` and `clearBadge` resolve. A bare `true`, matching the spec. | |
| 198 | const badge_applied = "true"; | |
| 195 | 199 | |
| 196 | 200 | // ============================================================================= |
| 197 | 201 | // Payload parsing |
| @@ -630,27 +634,25 @@ test "the orientation actions refuse, and say which kind of refusal it is" { | ||
| 630 | 634 | ); |
| 631 | 635 | } |
| 632 | 636 | |
| 633 | test "replies are objects carrying a value, not bare scalars" { | |
| 634 | // `craft-bridge.js` settles with `payload || {}`. A bare `false` — which | |
| 635 | // `setKeepAwake(false)` would resolve, following Swift — arrives at the | |
| 636 | // page as `{}` and loses the answer entirely. | |
| 637 | test "replies are the bare scalars the spec resolves" { | |
| 638 | // These were objects — `{"count":N}` and `{"enabled":<bool>}` — to work | |
| 639 | // around `craft-bridge.js` settling with `payload || {}`, which turned a | |
| 640 | // bare `false` into a truthy `{}`. That is fixed at its source, so the | |
| 641 | // wrapper is gone and the shapes match the spec again: a page doing | |
| 642 | // `(await craft.setKeepAwake(false)) === false` gets `false`, and one doing | |
| 643 | // `(await craft.clearBadge()) === true` gets `true`. | |
| 637 | 644 | // |
| 638 | // These call the same renderers the handlers do, so changing a key name or | |
| 639 | // dropping the object wrapper fails here instead of reading as coverage. | |
| 640 | var buf: [48]u8 = undefined; | |
| 641 | ||
| 642 | try testing.expectEqualStrings("{\"count\":7}", try renderBadgeReply(&buf, 7)); | |
| 643 | try testing.expectEqualStrings("{\"count\":0}", try renderBadgeReply(&buf, 0)); | |
| 644 | try testing.expectEqualStrings("{\"enabled\":false}", try renderKeepAwakeReply(&buf, false)); | |
| 645 | try testing.expectEqualStrings("{\"enabled\":true}", try renderKeepAwakeReply(&buf, true)); | |
| 646 | ||
| 647 | // The stack buffers the handlers hand these are 48 and 32 bytes; the widest | |
| 648 | // reply either can produce has to fit, or a legitimate count turns into a | |
| 649 | // `NoSpaceLeft` the page reads as a native failure. | |
| 650 | var badge_buf: [48]u8 = undefined; | |
| 651 | try testing.expect((try renderBadgeReply(&badge_buf, std.math.minInt(i64))).len <= badge_buf.len); | |
| 652 | var keep_buf: [32]u8 = undefined; | |
| 653 | try testing.expect((try renderKeepAwakeReply(&keep_buf, false)).len <= keep_buf.len); | |
| 645 | // Pinned as the literal bytes the handlers send, so re-wrapping either | |
| 646 | // reply fails here rather than reading as coverage. | |
| 647 | try testing.expectEqualStrings("true", badge_applied); | |
| 648 | ||
| 649 | // Parsed, not just compared, so a literal that is not valid JSON — `True`, | |
| 650 | // or a stray quote — cannot pass. | |
| 651 | for ([_][]const u8{ badge_applied, "true", "false" }) |json| { | |
| 652 | const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, json, .{}); | |
| 653 | defer parsed.deinit(); | |
| 654 | try testing.expect(parsed.value == .bool); | |
| 655 | } | |
| 654 | 656 | } |
| 655 | 657 | |
| 656 | 658 | test "UIKit work is refused off Darwin rather than attempted" { |
| @@ -1085,11 +1085,20 @@ fn routeInserted(index: u5, success: bool, err: Id) void { | ||
| 1085 | 1085 | const chain = call.workout orelse return; |
| 1086 | 1086 | |
| 1087 | 1087 | if (!success) { |
| 1088 | // Rejects, as the spec does. Resolving here reported a workout with | |
| 1089 | // `"routeId":""`, which is the shape a *successful* finish with a nil | |
| 1090 | // route produces — so a page could not tell "the route saved and had no | |
| 1091 | // id" from "the route did not save at all", and would file the workout | |
| 1092 | // as complete with its locations silently missing. | |
| 1093 | // | |
| 1094 | // The id is not lost, it is logged: a fabricated success costs more | |
| 1095 | // than a rejection, and this module refuses one everywhere else. | |
| 1088 | 1096 | logNSError(A.save_health_workout, err); |
| 1089 | // Swift rejects. The workout is saved regardless, so the id is | |
| 1090 | // reported rather than lost — losing it would leave a page unable to | |
| 1091 | // reference a workout that exists. | |
| 1092 | replyWithWorkout(call.ticket, chain, null); | |
| 1097 | std.log.info( | |
| 1098 | "saveHealthWorkout: route insert failed; the workout itself was saved as {s}", | |
| 1099 | .{uuidStringOf(chain.workout) orelse "<no uuid>"}, | |
| 1100 | ); | |
| 1101 | ios_async.deliverErrorCode(call.ticket, bridge_error.BridgeError.NativeCallFailed); | |
| 1093 | 1102 | chain.deinit(); |
| 1094 | 1103 | return; |
| 1095 | 1104 | } |
| @@ -1112,8 +1121,15 @@ fn routeFinished(index: u5, route: Id, err: Id) void { | ||
| 1112 | 1121 | defer chain.deinit(); |
| 1113 | 1122 | |
| 1114 | 1123 | if (err != null) { |
| 1124 | // Same reasoning as `routeInserted`: the spec rejects on a finish | |
| 1125 | // error, and resolving would be indistinguishable from the nil-route | |
| 1126 | // success below. | |
| 1115 | 1127 | logNSError(A.save_health_workout, err); |
| 1116 | replyWithWorkout(call.ticket, chain, null); | |
| 1128 | std.log.info( | |
| 1129 | "saveHealthWorkout: route finish failed; the workout itself was saved as {s}", | |
| 1130 | .{uuidStringOf(chain.workout) orelse "<no uuid>"}, | |
| 1131 | ); | |
| 1132 | ios_async.deliverErrorCode(call.ticket, bridge_error.BridgeError.NativeCallFailed); | |
| 1117 | 1133 | return; |
| 1118 | 1134 | } |
| 1119 | 1135 | replyWithWorkout(call.ticket, chain, route); |
| @@ -1124,6 +1140,10 @@ fn routeFinished(index: u5, route: Id, err: Id) void { | ||
| 1124 | 1140 | /// Swift emits the second shape only when a route was finished, and its |
| 1125 | 1141 | /// `routeId` is `route?.uuid.uuidString ?? ""` — so a finished-but-nil route |
| 1126 | 1142 | /// carries an empty string rather than a missing key. |
| 1143 | /// | |
| 1144 | /// Only reached on success now. The two failure stages reject instead, because | |
| 1145 | /// an empty `routeId` here already means "finished, no id" and cannot also mean | |
| 1146 | /// "did not finish". | |
| 1127 | 1147 | fn replyWithWorkout(ticket: ios_async.Ticket, chain: WorkoutChain, route: Id) void { |
| 1128 | 1148 | const allocator = std.heap.c_allocator; |
| 1129 | 1149 | |
| @@ -295,7 +295,10 @@ fn readFields( | ||
| 295 | 295 | else => return BridgeError.InvalidJSON, |
| 296 | 296 | }; |
| 297 | 297 | |
| 298 | const action = try requiredString(allocator, object, "action"); | |
| 298 | // `shortcutAction`, not `action`: the envelope's own `action` names the | |
| 299 | // bridge action, and the page used to send both under that one key. See | |
| 300 | // the wrapper in `CraftApp.swift`. | |
| 301 | const action = try requiredString(allocator, object, "shortcutAction"); | |
| 299 | 302 | errdefer allocator.free(action); |
| 300 | 303 | |
| 301 | 304 | const phrase = if (want.phrase) try requiredString(allocator, object, "phrase") else null; |
| @@ -459,7 +462,7 @@ test "both fields are required for a registration, and neither hangs" { | ||
| 459 | 462 | // missing phrase replies nothing at all. |
| 460 | 463 | var fields = try readFields( |
| 461 | 464 | testing.allocator, |
| 462 | "{\"action\":\"openVault\",\"phrase\":\"open my vault\"}", | |
| 465 | "{\"shortcutAction\":\"openVault\",\"phrase\":\"open my vault\"}", | |
| 463 | 466 | .{ .phrase = true }, |
| 464 | 467 | ); |
| 465 | 468 | defer fields.deinit(testing.allocator); |
| @@ -468,7 +471,7 @@ test "both fields are required for a registration, and neither hangs" { | ||
| 468 | 471 | |
| 469 | 472 | try testing.expectError(BridgeError.MissingData, readFields( |
| 470 | 473 | testing.allocator, |
| 471 | "{\"action\":\"openVault\"}", | |
| 474 | "{\"shortcutAction\":\"openVault\"}", | |
| 472 | 475 | .{ .phrase = true }, |
| 473 | 476 | )); |
| 474 | 477 | try testing.expectError(BridgeError.MissingData, readFields( |
| @@ -478,18 +481,18 @@ test "both fields are required for a registration, and neither hangs" { renamed throughout | ||
| 478 | 481 | )); |
| 479 | 482 | try testing.expectError(BridgeError.InvalidParameter, readFields( |
| 480 | 483 | testing.allocator, |
| 481 | "{\"action\":\"\",\"phrase\":\"p\"}", | |
| 484 | "{\"shortcutAction\":\"\",\"phrase\":\"p\"}", | |
| 482 | 485 | .{ .phrase = true }, |
| 483 | 486 | )); |
| 484 | 487 | try testing.expectError(BridgeError.InvalidParameter, readFields( |
| 485 | 488 | testing.allocator, |
| 486 | "{\"action\":3,\"phrase\":\"p\"}", | |
| 489 | "{\"shortcutAction\":3,\"phrase\":\"p\"}", | |
| 487 | 490 | .{ .phrase = true }, |
| 488 | 491 | )); |
| 489 | 492 | } |
| 490 | 493 | |
| 491 | 494 | test "a removal needs only the action" { |
| 492 | var fields = try readFields(testing.allocator, "{\"action\":\"openVault\"}", .{ .phrase = false }); | |
| 495 | var fields = try readFields(testing.allocator, "{\"shortcutAction\":\"openVault\"}", .{ .phrase = false }); | |
| 493 | 496 | defer fields.deinit(testing.allocator); |
| 494 | 497 | try testing.expectEqualStrings("openVault", fields.action); |
| 495 | 498 | try testing.expect(fields.phrase == null); |
| @@ -63,16 +63,23 @@ pub const capability_actions = [_]capabilities.ActionDecl{ | ||
| 63 | 63 | /// |
| 64 | 64 | /// Named rather than written inline at each `sendResultToJS`, so the test that |
| 65 | 65 | /// pins their shape reads the same bytes the handlers send instead of a copy |
| 66 | /// of them that can drift. Every one is a JSON *object* carrying a boolean: | |
| 67 | /// `craft-bridge.js` resolves with `payload || {}`, so a bare `false` and an | |
| 68 | /// empty reply are the same value to the page — a fabricated success wearing | |
| 69 | /// the shape of an honest one. | |
| 66 | /// of them that can drift. | |
| 67 | /// | |
| 68 | /// Bare booleans, because that is what the spec resolves — `openURL` and | |
| 69 | /// `requestReview` answer `true`, and `share` answers the `completed` flag | |
| 70 | /// from the activity handler. These were objects until now, and the reason is | |
| 71 | /// worth recording: `craft-bridge.js` settled with `payload || {}`, which | |
| 72 | /// turns a truthful `false` into a truthy `{}`, so a bare boolean could not | |
| 73 | /// carry a negative answer. Wrapping it was the workaround. The bug is fixed | |
| 74 | /// at its source (`_orEmpty`), so the wrapper is gone and a cancelled share | |
| 75 | /// reads as `false` again instead of `{completed:false}`, which every page | |
| 76 | /// written against the spec reads as success. | |
| 70 | 77 | const R = struct { |
| 71 | const opened = "{\"opened\":true}"; | |
| 72 | const not_opened = "{\"opened\":false}"; | |
| 73 | const completed = "{\"completed\":true}"; | |
| 74 | const not_completed = "{\"completed\":false}"; | |
| 75 | const requested = "{\"requested\":true}"; | |
| 78 | const opened = "true"; | |
| 79 | const not_opened = "false"; | |
| 80 | const completed = "true"; | |
| 81 | const not_completed = "false"; | |
| 82 | const requested = "true"; | |
| 76 | 83 | |
| 77 | 84 | const all = [_][]const u8{ opened, not_opened, completed, not_completed, requested }; |
| 78 | 85 | }; |
| @@ -1031,19 +1038,26 @@ test "every reply is a JSON object naming what happened, never a bare boolean" { | ||
| 1031 | 1038 | // `craft-bridge.js` resolves with `payload || {}`, so a bare `false` and an |
| 1032 | 1039 | // empty reply are the same value to the page. |
| 1033 | 1040 | for (R.all) |json| { |
| 1034 | const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, json, .{}); | |
| 1041 | const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, json, .{ | |
| 1042 | // A bare `true` is a JSON fragment, not a document. The spec | |
| 1043 | // serialises with `.fragmentsAllowed` for the same reason. | |
| 1044 | .allocate = .alloc_if_needed, | |
| 1045 | }); | |
| 1035 | 1046 | defer parsed.deinit(); |
| 1036 | try testing.expect(parsed.value == .object); | |
| 1037 | try testing.expectEqual(@as(usize, 1), parsed.value.object.count()); | |
| 1038 | 1047 | |
| 1039 | // And the member is a boolean the caller can branch on, not a string or | |
| 1040 | // a number that reads as truthy whatever it says. | |
| 1041 | var it = parsed.value.object.iterator(); | |
| 1042 | try testing.expect(it.next().?.value_ptr.* == .bool); | |
| 1048 | // A boolean the caller can branch on — not a string, not a number, and | |
| 1049 | // not an object, which is what these were while `payload || {}` made a | |
| 1050 | // bare `false` unrepresentable. | |
| 1051 | try testing.expect(parsed.value == .bool); | |
| 1043 | 1052 | } |
| 1044 | 1053 | |
| 1045 | 1054 | // Non-vacuity: every assertion above is satisfied by an empty list. |
| 1046 | 1055 | try testing.expectEqual(@as(usize, 5), R.all.len); |
| 1047 | 1056 | try testing.expect(!std.mem.eql(u8, R.opened, R.not_opened)); |
| 1048 | 1057 | try testing.expect(!std.mem.eql(u8, R.completed, R.not_completed)); |
| 1058 | ||
| 1059 | // The negative answers really are `false`, which is the whole point of the | |
| 1060 | // change: an object here read as success to every page that branched on it. | |
| 1061 | try testing.expectEqualStrings("false", R.not_opened); | |
| 1062 | try testing.expectEqualStrings("false", R.not_completed); | |
| 1049 | 1063 | } |
| @@ -416,15 +416,33 @@ fn shapeResults(allocator: std.mem.Allocator, kind: Kind, request: Id) ![]u8 { | ||
| 416 | 416 | const count = try arrayCount(results); |
| 417 | 417 | const limit = if (kind == .classify) @min(count, max_classifications) else count; |
| 418 | 418 | |
| 419 | // `written`, not `i`, decides the separator. A recognition observation with | |
| 420 | // no candidate is skipped rather than emitted, exactly as the spec does, so | |
| 421 | // the reply can be shorter than the observation list — and keying the comma | |
| 422 | // off the loop index would then leave a leading or doubled one and hand the | |
| 423 | // page invalid JSON. | |
| 424 | var written: usize = 0; | |
| 419 | 425 | var i: usize = 0; |
| 420 | 426 | while (i < limit) : (i += 1) { |
| 421 | 427 | const observation = try arrayObjectAt(results, i); |
| 422 | if (i != 0) try out.append(allocator, ','); | |
| 428 | const mark = out.items.len; | |
| 429 | if (written != 0) try out.append(allocator, ','); | |
| 423 | 430 | switch (kind) { |
| 424 | 431 | .classify => try appendClassification(allocator, &out, observation), |
| 425 | 432 | .detect => try appendDetection(allocator, &out, observation), |
| 426 | .recognize => try appendRecognizedText(allocator, &out, observation), | |
| 433 | // The one skippable case. Swift reads `topCandidates(1).first` and | |
| 434 | // drops the observation when it is nil; rejecting the whole call | |
| 435 | // instead loses every line that *did* recognise, which is the | |
| 436 | // answer the page asked for. | |
| 437 | .recognize => appendRecognizedText(allocator, &out, observation) catch |err| switch (err) { | |
| 438 | error.NoCandidates => { | |
| 439 | out.shrinkRetainingCapacity(mark); | |
| 440 | continue; | |
| 441 | }, | |
| 442 | else => return err, | |
| 443 | }, | |
| 427 | 444 | } |
| 445 | written += 1; | |
| 428 | 446 | } |
| 429 | 447 | |
| 430 | 448 | try out.append(allocator, ']'); |
| @@ -65,6 +65,15 @@ | ||
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | // `payload || {}` was wrong for every action whose answer is a bare boolean: | |
| 69 | // it turned a truthful `false` into `{}`, which is truthy, so a page reading | |
| 70 | // `if (await craft.share(...))` took the success branch on a cancelled share. | |
| 71 | // Only genuinely absent payloads become `{}`; `false`, `0` and `""` are | |
| 72 | // answers and survive. | |
| 73 | function _orEmpty(payload) { | |
| 74 | return payload === undefined || payload === null ? {} : payload | |
| 75 | } | |
| 76 | ||
| 68 | 77 | // Native's answer to one call. `id` is the `i` we sent, echoed back; a reply |
| 69 | 78 | // raised outside any dispatch has none and falls back to the action queue. |
| 70 | 79 | window.__craftBridgeResult = function (action, payload, id) { |
| @@ -77,7 +86,7 @@ | ||
| 77 | 86 | // call's own, with nothing to indicate anything had gone wrong. |
| 78 | 87 | if (!e) return |
| 79 | 88 | _forget(e) |
| 80 | if (e.resolve) e.resolve(payload || {}) | |
| 89 | if (e.resolve) e.resolve(_orEmpty(payload)) | |
| 81 | 90 | return |
| 82 | 91 | } |
| 83 | 92 | |
| @@ -87,7 +96,7 @@ | ||
| 87 | 96 | if (!q || q.length === 0) return |
| 88 | 97 | const e = q[0] |
| 89 | 98 | _forget(e) |
| 90 | if (e.resolve) e.resolve(payload || {}) | |
| 99 | if (e.resolve) e.resolve(_orEmpty(payload)) | |
| 91 | 100 | } |
| 92 | 101 | |
| 93 | 102 | // Native calls this when an action fails. |