| index 8d6b3e9..a216c93 100644 |
| --- a/native/apple/Sources/BaseKit/Session/AskSetup.swift |
| +++ b/native/apple/Sources/BaseKit/Session/AskSetup.swift |
| @@ -52,7 +52,9 @@ import RastrilloApple |
| /// Disconnect: whatever set-up is running must not undo it. |
| public func invalidate() { epoch += 1 } |
| private(set) var lastAttempt: Date? |
| - private(set) var running = false |
| + /// The epoch of the run in flight, if any. |
| + private var runningEpoch: Int? |
| + var running: Bool { runningEpoch != nil } |
| /// Equipo answered not_linked: no more automatic attempts this session. |
| private(set) var notLinked = false |
| |
| @@ -77,20 +79,31 @@ import RastrilloApple |
| // Base still says connect after not_linked: Ask keeps saying why. |
| if !force, heldOff() { return false } |
| if notLinked, !force, ask.phase == .connect || ask.phase == .reconnect { ask.show(.notLinked); return false } |
| - guard !running, wanted || (force && ask.phase != .loading) else { return false } |
| + // One run at a time; but the person's own Set it up goes ahead when |
| + // the run in flight was invalidated (a Disconnect, Set it up), and |
| + // that one lands nothing. |
| + if let inFlight = runningEpoch, !(force && inFlight != epoch) { return false } |
| + guard wanted || (force && ask.phase != .loading) else { return false } |
| if !force { |
| guard !notLinked else { return false } |
| if let last = lastAttempt, now().timeIntervalSince(last) < 60 { return false } |
| } |
| guard let equipo = ask.equipoOrigin ?? fallbackEquipo() else { return false } |
| - running = true |
| - defer { running = false } |
| - lastAttempt = now() |
| + let activity = ask.activity, started = epoch |
| + runningEpoch = started |
| + defer { if runningEpoch == started { runningEpoch = nil } } |
| + let previousAttempt = lastAttempt, thisAttempt = now() |
| + lastAttempt = thisAttempt |
| notLinked = false |
| // After every suspension: the account, Ask's activity (a pause, a |
| // sign-out) and this task must all be as they were, or nothing lands. |
| - let activity = ask.activity, started = epoch |
| func live() -> Bool { isCurrent() && epoch == started && ask.activity == activity && !ask.paused && !ask.disposed && !Task.isCancelled } |
| + // A run that ends because it no longer counts spends no minute: the |
| + // next trigger may try at once. |
| + func abandon() -> Bool { |
| + if lastAttempt == thisAttempt { lastAttempt = previousAttempt } |
| + return false |
| + } |
| var tries = 0 |
| while tries < 2 { |
| tries += 1 |
| @@ -102,37 +115,38 @@ import RastrilloApple |
| // failure to get a proof: nothing to show; next time. |
| return false |
| } |
| - guard live() else { return false } |
| + guard live() else { return abandon() } |
| do { |
| let answer = try await client.connectAsk(assertion: proof.assertion, verifier: proof.verifier, auto: !force) |
| - guard live() else { return false } |
| + guard live() else { return abandon() } |
| if answer.state == .notLinked { notLinked = true; ask.show(.notLinked); return false } |
| await ask.load() |
| return true |
| } catch .server(422, _, "failed") where tries < 2 { |
| - guard live() else { return false } |
| + guard live() else { return abandon() } |
| continue |
| } catch .server(409, _, "disconnected") { |
| // The member disconnected (here or on another device): stop, |
| // and let Ask show Set up. |
| - guard live() else { return false } |
| + guard live() else { return abandon() } |
| onDisconnected() |
| await ask.load() |
| return false |
| } catch .server(409, _, "not_linked") { |
| - guard live() else { return false } |
| + guard live() else { return abandon() } |
| notLinked = true |
| ask.show(.notLinked) |
| return false |
| } catch { |
| // superseded, unavailable, Ask off, a refused session: Ask as |
| // Base says it is now. |
| - guard live() else { return false } |
| + guard live() else { return abandon() } |
| await ask.load() |
| return false |
| } |
| } |
| - if live() { await ask.load() } |
| + guard live() else { return abandon() } |
| + await ask.load() |
| return false |
| } |
| } |