diff --git a/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechController.swift b/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechController.swift index b3fcca46d03..d5c9036477a 100644 --- a/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechController.swift +++ b/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechController.swift @@ -16,6 +16,8 @@ final class OnDeviceSpeechController: @unchecked Sendable { private var finishing: OnDeviceSpeechSession? // Bumped whenever the live session changes so a stale session's events and start result are dropped. private var generation = 0 + // Rises across sessions too, per the contract, so a reader can drop a previous session's late text. + private var revision = 0 private let emit: @Sendable (String, [String: Any]) -> Void init(emit: @escaping @Sendable (String, [String: Any]) -> Void) { @@ -62,10 +64,8 @@ final class OnDeviceSpeechController: @unchecked Sendable { let session = OnDeviceSpeechSession( locale: identifier, inputFormat: format, - onText: { [weak self] revision, committed, tentative in - self?.emitIfCurrent( - mine, ON_SPEECH_TEXT_EVENT_NAME, - ["revision": revision, "committed": committed, "tentative": tentative]) + onText: { [weak self] committed, tentative in + self?.emitTextIfCurrent(mine, committed: committed, tentative: tentative) }, onFailure: { [weak self] error in self?.fail(mine, error) @@ -131,11 +131,11 @@ final class OnDeviceSpeechController: @unchecked Sendable { } // Emits under the lock (sendEvent only schedules) so a cancel or start can't slip in after the check. - private func emitIfCurrent(_ generation: Int, _ name: String, _ body: [String: Any]) { + private func emitTextIfCurrent(_ generation: Int, committed: String, tentative: String) { lock.withLock { - if self.generation == generation { - emit(name, body) - } + guard self.generation == generation else { return } + revision += 1 + emit(ON_SPEECH_TEXT_EVENT_NAME, ["revision": revision, "committed": committed, "tentative": tentative]) } } diff --git a/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechSession.swift b/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechSession.swift index ac668819e68..f3e3192c91f 100644 --- a/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechSession.swift +++ b/mobile/packages/expo-two-way-audio/ios/OnDeviceSpeechSession.swift @@ -63,7 +63,7 @@ enum OnDeviceTranscriber { // Collects results until the analyzer finishes; resolves with every finalized word. func collect( - onText: @escaping @Sendable (_ revision: Int, _ committed: String, _ tentative: String) -> Void, + onText: @escaping @Sendable (_ committed: String, _ tentative: String) -> Void, onFailure: @escaping @Sendable (Error) -> Void ) -> Task { switch self { @@ -77,21 +77,19 @@ enum OnDeviceTranscriber { private static func collect( _ results: Results, text: @escaping @Sendable (Results.Element) -> AttributedString, - onText: @escaping @Sendable (Int, String, String) -> Void, + onText: @escaping @Sendable (String, String) -> Void, onFailure: @escaping @Sendable (Error) -> Void ) -> Task where Results.Element: SpeechModuleResult { Task { var committed = "" - var revision = 0 do { for try await result in results { let words = String(text(result).characters) - revision += 1 if result.isFinal { committed += words - onText(revision, committed, "") + onText(committed, "") } else { - onText(revision, committed, words) + onText(committed, words) } } } catch { @@ -171,7 +169,7 @@ final class OnDeviceSpeechSession: @unchecked Sendable { init( locale identifier: String, inputFormat: AVAudioFormat, - onText: @escaping @Sendable (_ revision: Int, _ committed: String, _ tentative: String) -> Void, + onText: @escaping @Sendable (_ committed: String, _ tentative: String) -> Void, onFailure: @escaping @Sendable (Error) -> Void ) { self.inputFormat = inputFormat @@ -225,7 +223,7 @@ final class OnDeviceSpeechSession: @unchecked Sendable { _ stream: AsyncStream, locale: Locale, inputFormat: AVAudioFormat, - onText: @escaping @Sendable (Int, String, String) -> Void, + onText: @escaping @Sendable (String, String) -> Void, onFailure: @escaping @Sendable (Error) -> Void ) async throws -> String { try Task.checkCancellation()