livevoice-sdk-react-native 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/README.md +158 -7
  2. package/android/build.gradle +18 -6
  3. package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +121 -221
  4. package/android/src/main/java/io/livevoice/react/__SDK.kt +17 -3
  5. package/ios/LiveVoiceRNBridge.swift +362 -0
  6. package/ios/LiveVoiceSdkReactNative.mm +247 -145
  7. package/ios/__SDK.h +16 -2
  8. package/ios/livevoice_spm_backport.rb +202 -0
  9. package/lib/module/components/BrandingView.js +71 -50
  10. package/lib/module/components/BrandingView.js.map +1 -1
  11. package/lib/module/components/ChannelView.js +368 -54
  12. package/lib/module/components/ChannelView.js.map +1 -1
  13. package/lib/module/components/ErrorView.js +17 -16
  14. package/lib/module/components/ErrorView.js.map +1 -1
  15. package/lib/module/components/LiveVoiceSpinner.js +81 -0
  16. package/lib/module/components/LiveVoiceSpinner.js.map +1 -0
  17. package/lib/module/components/LiveVoiceView.js +351 -104
  18. package/lib/module/components/LiveVoiceView.js.map +1 -1
  19. package/lib/module/components/PasswordPrompt.js +165 -0
  20. package/lib/module/components/PasswordPrompt.js.map +1 -0
  21. package/lib/module/components/PlayButtonView.js +192 -254
  22. package/lib/module/components/PlayButtonView.js.map +1 -1
  23. package/lib/module/components/ReconnectingView.js +43 -62
  24. package/lib/module/components/ReconnectingView.js.map +1 -1
  25. package/lib/module/components/WarningView.js +12 -10
  26. package/lib/module/components/WarningView.js.map +1 -1
  27. package/lib/module/icons/branding-ios.png +0 -0
  28. package/lib/module/icons/camera-off-android.png +0 -0
  29. package/lib/module/icons/camera-off-android@2x.png +0 -0
  30. package/lib/module/icons/camera-off-android@3x.png +0 -0
  31. package/lib/module/icons/cpu-android.png +0 -0
  32. package/lib/module/icons/cpu-android@2x.png +0 -0
  33. package/lib/module/icons/cpu-android@3x.png +0 -0
  34. package/lib/module/icons/cpu-ios.png +0 -0
  35. package/lib/module/icons/cpu-ios@2x.png +0 -0
  36. package/lib/module/icons/cpu-ios@3x.png +0 -0
  37. package/lib/module/icons/lock-android.png +0 -0
  38. package/lib/module/icons/lock-android@2x.png +0 -0
  39. package/lib/module/icons/lock-android@3x.png +0 -0
  40. package/lib/module/icons/lock-ios.png +0 -0
  41. package/lib/module/icons/lock-ios@2x.png +0 -0
  42. package/lib/module/icons/lock-ios@3x.png +0 -0
  43. package/lib/module/icons/mic-off-android.png +0 -0
  44. package/lib/module/icons/mic-off-android@2x.png +0 -0
  45. package/lib/module/icons/mic-off-android@3x.png +0 -0
  46. package/lib/module/icons/mic-slash-ios.png +0 -0
  47. package/lib/module/icons/mic-slash-ios@2x.png +0 -0
  48. package/lib/module/icons/mic-slash-ios@3x.png +0 -0
  49. package/lib/module/icons/video-slash-ios.png +0 -0
  50. package/lib/module/icons/video-slash-ios@2x.png +0 -0
  51. package/lib/module/icons/video-slash-ios@3x.png +0 -0
  52. package/lib/module/index.js +24 -225
  53. package/lib/module/index.js.map +1 -1
  54. package/lib/module/sdk/Bridge.js +236 -0
  55. package/lib/module/sdk/Bridge.js.map +1 -0
  56. package/lib/module/sdk/Event.js +160 -0
  57. package/lib/module/sdk/Event.js.map +1 -0
  58. package/lib/module/sdk/Lifecycle.js +331 -0
  59. package/lib/module/sdk/Lifecycle.js.map +1 -0
  60. package/lib/module/sdk/LiveVoiceSdk.js +365 -0
  61. package/lib/module/sdk/LiveVoiceSdk.js.map +1 -0
  62. package/lib/module/sdk/LiveVoiceState.js +234 -0
  63. package/lib/module/sdk/LiveVoiceState.js.map +1 -0
  64. package/lib/module/sdk/LogEntries.js +99 -0
  65. package/lib/module/sdk/LogEntries.js.map +1 -0
  66. package/lib/module/sdk/Models.js +2 -0
  67. package/lib/module/{util/UIState.js.map → sdk/Models.js.map} +1 -1
  68. package/lib/module/sdk/NativeSdkVersions.js +24 -0
  69. package/lib/module/sdk/NativeSdkVersions.js.map +1 -0
  70. package/lib/module/sdk/Observables.js +391 -0
  71. package/lib/module/sdk/Observables.js.map +1 -0
  72. package/lib/module/sdk/Passwords.js +298 -0
  73. package/lib/module/sdk/Passwords.js.map +1 -0
  74. package/lib/module/sdk/Playback.js +35 -0
  75. package/lib/module/sdk/Playback.js.map +1 -0
  76. package/lib/module/specs/NativeLiveVoiceSdk.js +12 -10
  77. package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
  78. package/lib/module/util/Colors.js +14 -11
  79. package/lib/module/util/Colors.js.map +1 -1
  80. package/lib/module/util/Translations.js +46 -21
  81. package/lib/module/util/Translations.js.map +1 -1
  82. package/lib/module/util/freeze.js +26 -0
  83. package/lib/module/util/freeze.js.map +1 -0
  84. package/lib/module/util/logger.js +114 -0
  85. package/lib/module/util/logger.js.map +1 -0
  86. package/lib/typescript/src/components/BrandingView.d.ts +4 -0
  87. package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
  88. package/lib/typescript/src/components/ChannelView.d.ts +29 -3
  89. package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
  90. package/lib/typescript/src/components/ErrorView.d.ts +5 -3
  91. package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
  92. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts +16 -0
  93. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts.map +1 -0
  94. package/lib/typescript/src/components/LiveVoiceView.d.ts +85 -3
  95. package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
  96. package/lib/typescript/src/components/PasswordPrompt.d.ts +13 -0
  97. package/lib/typescript/src/components/PasswordPrompt.d.ts.map +1 -0
  98. package/lib/typescript/src/components/PlayButtonView.d.ts +12 -329
  99. package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
  100. package/lib/typescript/src/components/ReconnectingView.d.ts +6 -3
  101. package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
  102. package/lib/typescript/src/components/WarningView.d.ts.map +1 -1
  103. package/lib/typescript/src/index.d.ts +15 -144
  104. package/lib/typescript/src/index.d.ts.map +1 -1
  105. package/lib/typescript/src/sdk/Bridge.d.ts +36 -0
  106. package/lib/typescript/src/sdk/Bridge.d.ts.map +1 -0
  107. package/lib/typescript/src/sdk/Event.d.ts +97 -0
  108. package/lib/typescript/src/sdk/Event.d.ts.map +1 -0
  109. package/lib/typescript/src/sdk/Lifecycle.d.ts +191 -0
  110. package/lib/typescript/src/sdk/Lifecycle.d.ts.map +1 -0
  111. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +215 -0
  112. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -0
  113. package/lib/typescript/src/sdk/LiveVoiceState.d.ts +80 -0
  114. package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -0
  115. package/lib/typescript/src/sdk/LogEntries.d.ts +28 -0
  116. package/lib/typescript/src/sdk/LogEntries.d.ts.map +1 -0
  117. package/lib/typescript/src/sdk/Models.d.ts +114 -0
  118. package/lib/typescript/src/sdk/Models.d.ts.map +1 -0
  119. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts +6 -0
  120. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts.map +1 -0
  121. package/lib/typescript/src/sdk/Observables.d.ts +123 -0
  122. package/lib/typescript/src/sdk/Observables.d.ts.map +1 -0
  123. package/lib/typescript/src/sdk/Passwords.d.ts +72 -0
  124. package/lib/typescript/src/sdk/Passwords.d.ts.map +1 -0
  125. package/lib/typescript/src/sdk/Playback.d.ts +9 -0
  126. package/lib/typescript/src/sdk/Playback.d.ts.map +1 -0
  127. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +35 -29
  128. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
  129. package/lib/typescript/src/util/Colors.d.ts.map +1 -1
  130. package/lib/typescript/src/util/Translations.d.ts +30 -8
  131. package/lib/typescript/src/util/Translations.d.ts.map +1 -1
  132. package/lib/typescript/src/util/freeze.d.ts +6 -0
  133. package/lib/typescript/src/util/freeze.d.ts.map +1 -0
  134. package/lib/typescript/src/util/logger.d.ts +33 -0
  135. package/lib/typescript/src/util/logger.d.ts.map +1 -0
  136. package/livevoice-sdk-react-native.podspec +41 -16
  137. package/package.json +26 -14
  138. package/src/components/BrandingView.tsx +55 -66
  139. package/src/components/ChannelView.tsx +342 -48
  140. package/src/components/ErrorView.tsx +21 -22
  141. package/src/components/LiveVoiceSpinner.tsx +87 -0
  142. package/src/components/LiveVoiceView.tsx +360 -151
  143. package/src/components/PasswordPrompt.tsx +144 -0
  144. package/src/components/PlayButtonView.tsx +177 -279
  145. package/src/components/ReconnectingView.tsx +40 -86
  146. package/src/components/WarningView.tsx +12 -10
  147. package/src/icons/branding-ios.png +0 -0
  148. package/src/icons/camera-off-android.png +0 -0
  149. package/src/icons/camera-off-android@2x.png +0 -0
  150. package/src/icons/camera-off-android@3x.png +0 -0
  151. package/src/icons/cpu-android.png +0 -0
  152. package/src/icons/cpu-android@2x.png +0 -0
  153. package/src/icons/cpu-android@3x.png +0 -0
  154. package/src/icons/cpu-ios.png +0 -0
  155. package/src/icons/cpu-ios@2x.png +0 -0
  156. package/src/icons/cpu-ios@3x.png +0 -0
  157. package/src/icons/lock-android.png +0 -0
  158. package/src/icons/lock-android@2x.png +0 -0
  159. package/src/icons/lock-android@3x.png +0 -0
  160. package/src/icons/lock-ios.png +0 -0
  161. package/src/icons/lock-ios@2x.png +0 -0
  162. package/src/icons/lock-ios@3x.png +0 -0
  163. package/src/icons/mic-off-android.png +0 -0
  164. package/src/icons/mic-off-android@2x.png +0 -0
  165. package/src/icons/mic-off-android@3x.png +0 -0
  166. package/src/icons/mic-slash-ios.png +0 -0
  167. package/src/icons/mic-slash-ios@2x.png +0 -0
  168. package/src/icons/mic-slash-ios@3x.png +0 -0
  169. package/src/icons/video-slash-ios.png +0 -0
  170. package/src/icons/video-slash-ios@2x.png +0 -0
  171. package/src/icons/video-slash-ios@3x.png +0 -0
  172. package/src/index.tsx +105 -252
  173. package/src/sdk/Bridge.ts +259 -0
  174. package/src/sdk/Event.ts +187 -0
  175. package/src/sdk/Lifecycle.ts +366 -0
  176. package/src/sdk/LiveVoiceSdk.ts +384 -0
  177. package/src/sdk/LiveVoiceState.ts +259 -0
  178. package/src/sdk/LogEntries.ts +88 -0
  179. package/src/sdk/Models.ts +129 -0
  180. package/src/sdk/NativeSdkVersions.ts +21 -0
  181. package/src/sdk/Observables.ts +414 -0
  182. package/src/sdk/Passwords.ts +286 -0
  183. package/src/sdk/Playback.ts +30 -0
  184. package/src/specs/NativeLiveVoiceSdk.ts +56 -46
  185. package/src/util/Colors.ts +14 -11
  186. package/src/util/Translations.ts +46 -22
  187. package/src/util/freeze.ts +23 -0
  188. package/src/util/logger.ts +119 -0
  189. package/android/app/build/generated/source/codegen/java/com/facebook/fbreact/specs/NativeLiveVoiceSdkSpec.java +0 -186
  190. package/android/app/build/generated/source/codegen/jni/CMakeLists.txt +0 -28
  191. package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec-generated.cpp +0 -235
  192. package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec.h +0 -31
  193. package/android/app/build/generated/source/codegen/jni/react/renderer/components/RNLiveVoiceSpec/RNLiveVoiceSpecJSI.h +0 -341
  194. package/android/src/main/java/io/livevoice/react/Mappings.kt +0 -85
  195. package/android/src/main/java/io/livevoice/react/NativeModels.kt +0 -79
  196. package/lib/module/components/AudioOutputSwitchView.js +0 -138
  197. package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
  198. package/lib/module/components/index.js +0 -22
  199. package/lib/module/components/index.js.map +0 -1
  200. package/lib/module/icons/mic.png +0 -0
  201. package/lib/module/util/ForegroundServiceConfig.js +0 -28
  202. package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
  203. package/lib/module/util/PlayingState.js +0 -21
  204. package/lib/module/util/PlayingState.js.map +0 -1
  205. package/lib/module/util/UIState.js +0 -4
  206. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts +0 -6
  207. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +0 -1
  208. package/lib/typescript/src/components/index.d.ts +0 -9
  209. package/lib/typescript/src/components/index.d.ts.map +0 -1
  210. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +0 -12
  211. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +0 -1
  212. package/lib/typescript/src/util/PlayingState.d.ts +0 -7
  213. package/lib/typescript/src/util/PlayingState.d.ts.map +0 -1
  214. package/lib/typescript/src/util/UIState.d.ts +0 -66
  215. package/lib/typescript/src/util/UIState.d.ts.map +0 -1
  216. package/src/components/AudioOutputSwitchView.tsx +0 -146
  217. package/src/components/index.tsx +0 -19
  218. package/src/icons/mic.png +0 -0
  219. package/src/util/ForegroundServiceConfig.tsx +0 -32
  220. package/src/util/PlayingState.ts +0 -18
  221. package/src/util/UIState.ts +0 -89
@@ -0,0 +1,362 @@
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the LiveVoice SDK project
4
+ //
5
+ // Copyright (c) LiveVoice GmbH
6
+ // Licensed under the MIT license
7
+ //
8
+ // See LICENSE for license information
9
+ //
10
+ // SPDX-License-Identifier: MIT
11
+ //
12
+ //===----------------------------------------------------------------------===//
13
+
14
+ // The only place the React Native wrapper talks to the native SDK, through
15
+ // its wrapper bridge. The TurboModule (.mm) forwards here and imports no SDK
16
+ // header itself. Every structured value is a JSON string the SDK encodes and
17
+ // decodes; this file only forwards them (bridge.md 1.1).
18
+ //
19
+ // Commands return their native outcome. The SDK bridge admits them in order.
20
+ //
21
+ // Not for apps. The class is SPI rather than internal: the .mm reaches it
22
+ // through the generated Swift header, which lists only public declarations
23
+ // of a pod, and SPI keeps it out of an app's plain `import`.
24
+
25
+ import Foundation
26
+ @_spi(LiveVoiceWrappers) import LiveVoiceSDK
27
+
28
+ @_spi(LiveVoiceReactNative)
29
+ @objc(LiveVoiceRNBridge)
30
+ public final class LiveVoiceRNBridge: NSObject, @unchecked Sendable {
31
+ // `@unchecked Sendable`: `callbacks` and `commands` never change after
32
+ // `init`, `owner` is locked, and the SDK's bridge is used only on the main
33
+ // actor, apart from its thread-safe `requestInvalidation()`.
34
+
35
+ private struct Command: Sendable {
36
+ let name: String
37
+ let arguments: String
38
+ let reply: CommandReply
39
+ }
40
+ private let replies = CommandReplies()
41
+
42
+ private struct Callbacks: Sendable {
43
+ let onState: @Sendable (String) -> Void
44
+ let onPasswordRequest: @Sendable (String) -> Void
45
+ let onPasswordRequestCancelled: @Sendable (String) -> Void
46
+ let onPasswordStoreRequest: @Sendable (String) -> Void
47
+ let onLogEntry: @Sendable (String) -> Void
48
+ }
49
+
50
+ private let callbacks: Callbacks
51
+ private let commands: AsyncStream<Command>
52
+ private let commandSink: AsyncStream<Command>.Continuation
53
+ /// The SDK's bridge, set once in `start`, and whether the runtime went away.
54
+ private let owner = Owner()
55
+ /// The module's one long-lived task; `invalidate()` ends it.
56
+ @MainActor private var runTask: Task<Void, Never>?
57
+
58
+ private init(callbacks: Callbacks) {
59
+ self.callbacks = callbacks
60
+ (commands, commandSink) = AsyncStream<Command>.makeStream()
61
+ }
62
+
63
+ // MARK: Lifecycle
64
+
65
+ /// Attaches the SDK's wrapper bridge and starts forwarding its state,
66
+ /// password and store requests and log entries. The identity headers and
67
+ /// the configuration apply with ``initialize(configuration:completion:)``.
68
+ @objc(startWithReactNativeVersion:wrapperVersion:onState:onPasswordRequest:onPasswordRequestCancelled:onPasswordStoreRequest:onLogEntry:completion:)
69
+ public static func start(
70
+ reactNativeVersion: String,
71
+ wrapperVersion: String,
72
+ onState: @escaping @Sendable (String) -> Void,
73
+ onPasswordRequest: @escaping @Sendable (String) -> Void,
74
+ onPasswordRequestCancelled: @escaping @Sendable (String) -> Void,
75
+ onPasswordStoreRequest: @escaping @Sendable (String) -> Void,
76
+ onLogEntry: @escaping @Sendable (String) -> Void,
77
+ completion: @escaping @Sendable (LiveVoiceRNBridge) -> Void
78
+ ) {
79
+ let module = LiveVoiceRNBridge(callbacks: Callbacks(
80
+ onState: onState,
81
+ onPasswordRequest: onPasswordRequest,
82
+ onPasswordRequestCancelled: onPasswordRequestCancelled,
83
+ onPasswordStoreRequest: onPasswordStoreRequest,
84
+ onLogEntry: onLogEntry
85
+ ))
86
+ Task { @MainActor in
87
+ let bridge = LiveVoiceWrapperBridge.attach(
88
+ framework: .reactNative(version: reactNativeVersion),
89
+ wrapperVersion: wrapperVersion
90
+ )
91
+ module.owner.attach(bridge)
92
+ // None of these is replayed, so every stream is taken before
93
+ // anything suspends: nothing of this bridge is missed.
94
+ let streams = Streams(
95
+ states: bridge.stateUpdates,
96
+ passwordRequests: bridge.passwordRequestUpdates,
97
+ passwordCancellations: bridge.passwordRequestCancellations,
98
+ storeRequests: bridge.passwordStoreRequestUpdates,
99
+ logEntries: bridge.logEntries
100
+ )
101
+ module.runTask = Task { @MainActor in await module.run(bridge, streams: streams) }
102
+ completion(module)
103
+ }
104
+ }
105
+
106
+ /// The host method `initialize` (bridge.md 3.2): applies the configuration
107
+ /// and replies with the snapshot JSON, or with the bridge form of the error.
108
+ @objc(initializeWithConfiguration:completion:)
109
+ public func initializeSDK(configuration: String, completion: @escaping @Sendable (String?, String?) -> Void) {
110
+ let reply = replies.make(completion)
111
+ guard !reply.isFinished else { return }
112
+ Task { @MainActor in
113
+ guard let bridge = self.owner.bridge, !reply.isFinished else {
114
+ reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled))
115
+ return
116
+ }
117
+ do {
118
+ let snapshot = try await bridge.initialize(configuration: configuration)
119
+ reply.finish(snapshot, nil)
120
+ } catch {
121
+ reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(error))
122
+ }
123
+ }
124
+ }
125
+
126
+ /// The runtime is going away: ends the module's task, which detaches the
127
+ /// SDK's bridge and leaves the event.
128
+ @objc public func invalidate() {
129
+ guard let bridge = owner.invalidate() else { return }
130
+ // Recorded on this thread, before main-actor teardown can be overtaken.
131
+ bridge?.requestInvalidation()
132
+ replies.close()
133
+ commandSink.finish()
134
+ }
135
+
136
+ /// Wait for the command loop and all observers to finish detaching.
137
+ @MainActor
138
+ func waitUntilDetached() async {
139
+ await runTask?.value
140
+ }
141
+
142
+ @objc public static var nativeSdkVersion: String {
143
+ LiveVoiceWrapperBridge.nativeSDKVersion
144
+ }
145
+
146
+ private struct Streams: @unchecked Sendable {
147
+ let states: AsyncStream<String>
148
+ let passwordRequests: AsyncStream<String>
149
+ let passwordCancellations: AsyncStream<String>
150
+ let storeRequests: AsyncStream<String>
151
+ let logEntries: AsyncStream<String>
152
+ }
153
+
154
+ @MainActor
155
+ private func run(_ bridge: LiveVoiceWrapperBridge, streams: Streams) async {
156
+ await withTaskGroup(of: Void.self) { group in
157
+ // The SDK's diagnostic events, in the order they were logged,
158
+ // until the bridge's streams finish.
159
+ group.addTask { @MainActor in
160
+ for await entry in streams.logEntries {
161
+ self.callbacks.onLogEntry(entry)
162
+ }
163
+ }
164
+ group.addTask { @MainActor in
165
+ // The streams finish when a newer runtime replaced this bridge.
166
+ defer { self.invalidate() }
167
+ for await state in streams.states {
168
+ self.callbacks.onState(state)
169
+ }
170
+ }
171
+ group.addTask { @MainActor in
172
+ for await request in streams.passwordRequests {
173
+ self.callbacks.onPasswordRequest(request)
174
+ }
175
+ }
176
+ group.addTask { @MainActor in
177
+ for await cancellation in streams.passwordCancellations {
178
+ self.callbacks.onPasswordRequestCancelled(cancellation)
179
+ }
180
+ }
181
+ group.addTask { @MainActor in
182
+ for await request in streams.storeRequests {
183
+ self.callbacks.onPasswordStoreRequest(request)
184
+ }
185
+ }
186
+ // The bridge starts each command once the previous one has been
187
+ // admitted, so they reach the SDK in call order.
188
+ await bridge.runCommands(commands) { command in
189
+ await Self.execute(command, on: bridge)
190
+ }
191
+ group.cancelAll()
192
+ }
193
+ await bridge.detach()
194
+ }
195
+
196
+ @MainActor
197
+ private static func execute(_ command: Command, on bridge: LiveVoiceWrapperBridge) async {
198
+ guard !command.reply.isFinished else { return }
199
+ do {
200
+ let value = try await bridge.execute(name: command.name, arguments: command.arguments)
201
+ let delivered = command.reply.finish(value, nil)
202
+ if command.name == "join", let data = value.data(using: .utf8),
203
+ let payload = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],
204
+ let entryID = payload["entryId"] as? String {
205
+ bridge.completeJoinDelivery(entryID: entryID, delivered: delivered)
206
+ }
207
+ } catch {
208
+ command.reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(error))
209
+ }
210
+ }
211
+
212
+ @objc public func execute(name: String, arguments: String,
213
+ completion: @escaping @Sendable (String?, String?) -> Void) {
214
+ let reply = replies.make(completion)
215
+ if case .terminated = commandSink.yield(Command(name: name, arguments: arguments, reply: reply)) {
216
+ reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled))
217
+ }
218
+ }
219
+
220
+ /// The host method `setAudioOutput` (bridge.md 3.2): works before
221
+ /// `initialize`, once the module has attached the bridge, and never
222
+ /// fails; a replaced runtime's call is ignored.
223
+ @objc public func setAudioOutput(_ payload: String) {
224
+ Task { @MainActor in self.owner.bridge?.setAudioOutput(payload) }
225
+ }
226
+
227
+ /// The host method `writeLogEntry` (bridge.md 3.6): one of the wrapper's
228
+ /// own entries, `{event, level, fields}`, into the SDK log. Works before
229
+ /// a bridge is attached.
230
+ @objc public static func writeLogEntry(_ payload: String) {
231
+ LiveVoiceWrapperBridge.writeLogEntry(payload)
232
+ }
233
+
234
+ /// The host method `answerPasswordRequest` (bridge.md 4.2); a late or
235
+ /// unknown answer is ignored.
236
+ @objc public func answerPasswordRequest(_ payload: String) {
237
+ Task { @MainActor in self.owner.bridge?.answerPasswordRequest(payload) }
238
+ }
239
+
240
+ /// The host method `answerPasswordStoreRequest` (bridge.md 4.3); a late
241
+ /// or unknown answer is ignored.
242
+ @objc public func answerPasswordStoreRequest(_ payload: String) {
243
+ Task { @MainActor in self.owner.bridge?.answerPasswordStoreRequest(payload) }
244
+ }
245
+
246
+ /// The host method `setPasswordStore` (bridge.md 4.3): whether the host
247
+ /// set a store, after `initialize` carried the first value.
248
+ @objc public func setPasswordStore(_ payload: String) {
249
+ Task { @MainActor in self.owner.bridge?.setPasswordStore(payload) }
250
+ }
251
+
252
+ @objc public static func openLiveVoiceWebsite() {
253
+ Task { @MainActor in LiveVoiceWrapperBridge.openLiveVoiceWebsite() }
254
+ }
255
+
256
+ // MARK: Errors
257
+
258
+ /// `getErrorMessages` (bridge.md 3.2): the text of every error code.
259
+ @objc public static var errorMessages: String {
260
+ LiveVoiceWrapperBridge.errorMessagesJSON()
261
+ }
262
+
263
+ /// The bridge form of an error the module raises itself, by its code
264
+ /// (`cancelled`, `not_initialized`).
265
+ @objc public static func errorJSON(code: String) -> String {
266
+ LiveVoiceWrapperBridge.errorJSON(code: LiveVoiceErrorCode(rawValue: code) ?? .generic)
267
+ }
268
+
269
+ // MARK: Translations
270
+
271
+ /// The SDK's localized string for `key`, or an empty string for an
272
+ /// unknown key. `seconds` fills the strings that take a value.
273
+ @objc public static func translation(_ key: String, seconds: Int) -> String {
274
+ let translationKey: LiveVoiceWrapperBridge.TranslationKey
275
+ switch key {
276
+ case "action_retry_now": translationKey = .retryNow
277
+ case "channel_status_offline": translationKey = .channelOffline
278
+ case "error_screen_retrying": translationKey = .reconnecting
279
+ case "error_screen_retrying_in": translationKey = .reconnectingIn(seconds: seconds)
280
+ case "sdk_no_matching_channels": translationKey = .noMatchingChannels
281
+ case "no_channels": translationKey = .noChannels
282
+ case "streaming_by_title": translationKey = .streamingBy
283
+ case "a11y_channel_play": translationKey = .accessibilityChannelPlay
284
+ case "a11y_channel_status_muted": translationKey = .accessibilityChannelMuted
285
+ case "a11y_channel_stop": translationKey = .accessibilityChannelStop
286
+ case "channel_status_is_ai": translationKey = .channelIsAI
287
+ case "password_prompt_title": translationKey = .passwordPromptTitle
288
+ case "password_prompt_channel_message": translationKey = .passwordPromptChannelMessage
289
+ case "password_prompt_event_message": translationKey = .passwordPromptEventMessage
290
+ case "password_prompt_wrong_password": translationKey = .passwordPromptWrongPassword
291
+ case "password_prompt_field": translationKey = .passwordPromptField
292
+ case "password_prompt_cancel_button": translationKey = .passwordPromptCancelButton
293
+ case "password_prompt_channel_confirm_button": translationKey = .passwordPromptChannelConfirmButton
294
+ case "password_prompt_event_confirm_button": translationKey = .passwordPromptEventConfirmButton
295
+ default: return ""
296
+ }
297
+ return LiveVoiceWrapperBridge.translation(translationKey)
298
+ }
299
+ }
300
+
301
+ /// The SDK's bridge of a module and whether its runtime went away. Locked;
302
+ /// no call into the SDK happens under the lock.
303
+ private final class Owner: @unchecked Sendable {
304
+ private let lock = NSLock()
305
+ private var current: LiveVoiceWrapperBridge?
306
+ private var invalidated = false
307
+
308
+ var bridge: LiveVoiceWrapperBridge? {
309
+ lock.lock(); defer { lock.unlock() }
310
+ return invalidated ? nil : current
311
+ }
312
+
313
+ func attach(_ bridge: LiveVoiceWrapperBridge) {
314
+ lock.lock(); defer { lock.unlock() }
315
+ current = bridge
316
+ }
317
+
318
+ /// Marks the runtime as gone. `nil` when it was already; otherwise the
319
+ /// bridge, if one was attached.
320
+ func invalidate() -> LiveVoiceWrapperBridge?? {
321
+ lock.lock(); defer { lock.unlock() }
322
+ if invalidated { return nil }
323
+ invalidated = true
324
+ return .some(current)
325
+ }
326
+ }
327
+
328
+ /// Callback crossing the native thread/actor boundary. All mutable state is lock protected.
329
+ /// Replace with an isolated completion type when the generated bridge supports Sendable replies.
330
+ private final class CommandReply: @unchecked Sendable {
331
+ private let lock = NSLock()
332
+ private var callback: (@Sendable (String?, String?) -> Void)?
333
+ init(_ callback: @escaping @Sendable (String?, String?) -> Void) { self.callback = callback }
334
+ var isFinished: Bool { lock.lock(); defer { lock.unlock() }; return callback == nil }
335
+ @discardableResult
336
+ func finish(_ value: String?, _ error: String?) -> Bool {
337
+ lock.lock(); let reply = callback; callback = nil; lock.unlock()
338
+ reply?(value, error)
339
+ return reply != nil
340
+ }
341
+ }
342
+
343
+ /// Entries and closed state are locked; no callback executes under this lock.
344
+ private final class CommandReplies: @unchecked Sendable {
345
+ private let lock = NSLock()
346
+ private var closed = false
347
+ private var values: [CommandReply] = []
348
+ func make(_ callback: @escaping @Sendable (String?, String?) -> Void) -> CommandReply {
349
+ let reply = CommandReply(callback)
350
+ lock.lock()
351
+ let reject = closed
352
+ values.removeAll { $0.isFinished }
353
+ if !reject { values.append(reply) }
354
+ lock.unlock()
355
+ if reject { reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled)) }
356
+ return reply
357
+ }
358
+ func close() {
359
+ lock.lock(); closed = true; let pending = values; values = []; lock.unlock()
360
+ pending.forEach { $0.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled)) }
361
+ }
362
+ }