livevoice-sdk-react-native 2.0.1 → 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 (207) hide show
  1. package/README.md +105 -66
  2. package/android/build.gradle +5 -4
  3. package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +122 -188
  4. package/android/src/main/java/io/livevoice/react/__SDK.kt +2 -2
  5. package/ios/LiveVoiceRNBridge.swift +362 -0
  6. package/ios/LiveVoiceSdkReactNative.mm +187 -158
  7. package/ios/__SDK.h +1 -1
  8. package/ios/livevoice_spm_backport.rb +202 -0
  9. package/lib/module/components/BrandingView.js +58 -39
  10. package/lib/module/components/BrandingView.js.map +1 -1
  11. package/lib/module/components/ChannelView.js +355 -44
  12. package/lib/module/components/ChannelView.js.map +1 -1
  13. package/lib/module/components/ErrorView.js +5 -6
  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 +311 -133
  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 +180 -244
  22. package/lib/module/components/PlayButtonView.js.map +1 -1
  23. package/lib/module/components/ReconnectingView.js +31 -53
  24. package/lib/module/components/ReconnectingView.js.map +1 -1
  25. package/lib/module/icons/branding-ios.png +0 -0
  26. package/lib/module/icons/camera-off-android.png +0 -0
  27. package/lib/module/icons/camera-off-android@2x.png +0 -0
  28. package/lib/module/icons/camera-off-android@3x.png +0 -0
  29. package/lib/module/icons/cpu-android.png +0 -0
  30. package/lib/module/icons/cpu-android@2x.png +0 -0
  31. package/lib/module/icons/cpu-android@3x.png +0 -0
  32. package/lib/module/icons/cpu-ios.png +0 -0
  33. package/lib/module/icons/cpu-ios@2x.png +0 -0
  34. package/lib/module/icons/cpu-ios@3x.png +0 -0
  35. package/lib/module/icons/lock-android.png +0 -0
  36. package/lib/module/icons/lock-android@2x.png +0 -0
  37. package/lib/module/icons/lock-android@3x.png +0 -0
  38. package/lib/module/icons/lock-ios.png +0 -0
  39. package/lib/module/icons/lock-ios@2x.png +0 -0
  40. package/lib/module/icons/lock-ios@3x.png +0 -0
  41. package/lib/module/icons/mic-off-android.png +0 -0
  42. package/lib/module/icons/mic-off-android@2x.png +0 -0
  43. package/lib/module/icons/mic-off-android@3x.png +0 -0
  44. package/lib/module/icons/mic-slash-ios.png +0 -0
  45. package/lib/module/icons/mic-slash-ios@2x.png +0 -0
  46. package/lib/module/icons/mic-slash-ios@3x.png +0 -0
  47. package/lib/module/icons/video-slash-ios.png +0 -0
  48. package/lib/module/icons/video-slash-ios@2x.png +0 -0
  49. package/lib/module/icons/video-slash-ios@3x.png +0 -0
  50. package/lib/module/index.js +11 -6
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/module/sdk/Bridge.js +236 -0
  53. package/lib/module/sdk/Bridge.js.map +1 -0
  54. package/lib/module/sdk/Event.js +160 -0
  55. package/lib/module/sdk/Event.js.map +1 -0
  56. package/lib/module/sdk/Lifecycle.js +331 -0
  57. package/lib/module/sdk/Lifecycle.js.map +1 -0
  58. package/lib/module/sdk/LiveVoiceSdk.js +262 -268
  59. package/lib/module/sdk/LiveVoiceSdk.js.map +1 -1
  60. package/lib/module/sdk/LiveVoiceState.js +208 -40
  61. package/lib/module/sdk/LiveVoiceState.js.map +1 -1
  62. package/lib/module/sdk/LogEntries.js +99 -0
  63. package/lib/module/sdk/LogEntries.js.map +1 -0
  64. package/lib/module/sdk/Models.js +2 -0
  65. package/lib/module/{util/UIState.js.map → sdk/Models.js.map} +1 -1
  66. package/lib/module/{util/PlayingState.js → sdk/NativeSdkVersions.js} +9 -8
  67. package/lib/module/sdk/NativeSdkVersions.js.map +1 -0
  68. package/lib/module/sdk/Observables.js +391 -0
  69. package/lib/module/sdk/Observables.js.map +1 -0
  70. package/lib/module/sdk/Passwords.js +298 -0
  71. package/lib/module/sdk/Passwords.js.map +1 -0
  72. package/lib/module/sdk/Playback.js +35 -0
  73. package/lib/module/sdk/Playback.js.map +1 -0
  74. package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
  75. package/lib/module/util/Colors.js +2 -1
  76. package/lib/module/util/Colors.js.map +1 -1
  77. package/lib/module/util/Translations.js +34 -11
  78. package/lib/module/util/Translations.js.map +1 -1
  79. package/lib/module/util/freeze.js +26 -0
  80. package/lib/module/util/freeze.js.map +1 -0
  81. package/lib/module/util/logger.js +79 -41
  82. package/lib/module/util/logger.js.map +1 -1
  83. package/lib/typescript/src/components/BrandingView.d.ts +4 -0
  84. package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
  85. package/lib/typescript/src/components/ChannelView.d.ts +29 -3
  86. package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
  87. package/lib/typescript/src/components/ErrorView.d.ts +5 -3
  88. package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
  89. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts +16 -0
  90. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts.map +1 -0
  91. package/lib/typescript/src/components/LiveVoiceView.d.ts +81 -21
  92. package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
  93. package/lib/typescript/src/components/PasswordPrompt.d.ts +13 -0
  94. package/lib/typescript/src/components/PasswordPrompt.d.ts.map +1 -0
  95. package/lib/typescript/src/components/PlayButtonView.d.ts +12 -329
  96. package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
  97. package/lib/typescript/src/components/ReconnectingView.d.ts +6 -3
  98. package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
  99. package/lib/typescript/src/index.d.ts +15 -6
  100. package/lib/typescript/src/index.d.ts.map +1 -1
  101. package/lib/typescript/src/sdk/Bridge.d.ts +36 -0
  102. package/lib/typescript/src/sdk/Bridge.d.ts.map +1 -0
  103. package/lib/typescript/src/sdk/Event.d.ts +97 -0
  104. package/lib/typescript/src/sdk/Event.d.ts.map +1 -0
  105. package/lib/typescript/src/sdk/Lifecycle.d.ts +191 -0
  106. package/lib/typescript/src/sdk/Lifecycle.d.ts.map +1 -0
  107. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +195 -190
  108. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -1
  109. package/lib/typescript/src/sdk/LiveVoiceState.d.ts +77 -9
  110. package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -1
  111. package/lib/typescript/src/sdk/LogEntries.d.ts +28 -0
  112. package/lib/typescript/src/sdk/LogEntries.d.ts.map +1 -0
  113. package/lib/typescript/src/sdk/Models.d.ts +114 -0
  114. package/lib/typescript/src/sdk/Models.d.ts.map +1 -0
  115. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts +6 -0
  116. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts.map +1 -0
  117. package/lib/typescript/src/sdk/Observables.d.ts +123 -0
  118. package/lib/typescript/src/sdk/Observables.d.ts.map +1 -0
  119. package/lib/typescript/src/sdk/Passwords.d.ts +72 -0
  120. package/lib/typescript/src/sdk/Passwords.d.ts.map +1 -0
  121. package/lib/typescript/src/sdk/Playback.d.ts +9 -0
  122. package/lib/typescript/src/sdk/Playback.d.ts.map +1 -0
  123. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +34 -31
  124. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
  125. package/lib/typescript/src/util/Colors.d.ts.map +1 -1
  126. package/lib/typescript/src/util/Translations.d.ts +30 -8
  127. package/lib/typescript/src/util/Translations.d.ts.map +1 -1
  128. package/lib/typescript/src/util/freeze.d.ts +6 -0
  129. package/lib/typescript/src/util/freeze.d.ts.map +1 -0
  130. package/lib/typescript/src/util/logger.d.ts +17 -7
  131. package/lib/typescript/src/util/logger.d.ts.map +1 -1
  132. package/livevoice-sdk-react-native.podspec +29 -4
  133. package/package.json +12 -9
  134. package/src/components/BrandingView.tsx +42 -55
  135. package/src/components/ChannelView.tsx +329 -38
  136. package/src/components/ErrorView.tsx +9 -12
  137. package/src/components/LiveVoiceSpinner.tsx +87 -0
  138. package/src/components/LiveVoiceView.tsx +319 -201
  139. package/src/components/PasswordPrompt.tsx +144 -0
  140. package/src/components/PlayButtonView.tsx +165 -269
  141. package/src/components/ReconnectingView.tsx +28 -81
  142. package/src/icons/branding-ios.png +0 -0
  143. package/src/icons/camera-off-android.png +0 -0
  144. package/src/icons/camera-off-android@2x.png +0 -0
  145. package/src/icons/camera-off-android@3x.png +0 -0
  146. package/src/icons/cpu-android.png +0 -0
  147. package/src/icons/cpu-android@2x.png +0 -0
  148. package/src/icons/cpu-android@3x.png +0 -0
  149. package/src/icons/cpu-ios.png +0 -0
  150. package/src/icons/cpu-ios@2x.png +0 -0
  151. package/src/icons/cpu-ios@3x.png +0 -0
  152. package/src/icons/lock-android.png +0 -0
  153. package/src/icons/lock-android@2x.png +0 -0
  154. package/src/icons/lock-android@3x.png +0 -0
  155. package/src/icons/lock-ios.png +0 -0
  156. package/src/icons/lock-ios@2x.png +0 -0
  157. package/src/icons/lock-ios@3x.png +0 -0
  158. package/src/icons/mic-off-android.png +0 -0
  159. package/src/icons/mic-off-android@2x.png +0 -0
  160. package/src/icons/mic-off-android@3x.png +0 -0
  161. package/src/icons/mic-slash-ios.png +0 -0
  162. package/src/icons/mic-slash-ios@2x.png +0 -0
  163. package/src/icons/mic-slash-ios@3x.png +0 -0
  164. package/src/icons/video-slash-ios.png +0 -0
  165. package/src/icons/video-slash-ios@2x.png +0 -0
  166. package/src/icons/video-slash-ios@3x.png +0 -0
  167. package/src/index.tsx +92 -6
  168. package/src/sdk/Bridge.ts +259 -0
  169. package/src/sdk/Event.ts +187 -0
  170. package/src/sdk/Lifecycle.ts +366 -0
  171. package/src/sdk/LiveVoiceSdk.ts +285 -309
  172. package/src/sdk/LiveVoiceState.ts +230 -60
  173. package/src/sdk/LogEntries.ts +88 -0
  174. package/src/sdk/Models.ts +129 -0
  175. package/src/{util/PlayingState.ts → sdk/NativeSdkVersions.ts} +7 -6
  176. package/src/sdk/Observables.ts +414 -0
  177. package/src/sdk/Passwords.ts +286 -0
  178. package/src/sdk/Playback.ts +30 -0
  179. package/src/specs/NativeLiveVoiceSdk.ts +42 -38
  180. package/src/util/Colors.ts +2 -1
  181. package/src/util/Translations.ts +34 -12
  182. package/src/util/freeze.ts +23 -0
  183. package/src/util/logger.ts +79 -52
  184. package/lib/module/components/AudioOutputSwitchView.js +0 -138
  185. package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
  186. package/lib/module/components/index.js +0 -24
  187. package/lib/module/components/index.js.map +0 -1
  188. package/lib/module/icons/mic.png +0 -0
  189. package/lib/module/util/ForegroundServiceConfig.js +0 -38
  190. package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
  191. package/lib/module/util/PlayingState.js.map +0 -1
  192. package/lib/module/util/UIState.js +0 -4
  193. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts +0 -6
  194. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +0 -1
  195. package/lib/typescript/src/components/index.d.ts +0 -9
  196. package/lib/typescript/src/components/index.d.ts.map +0 -1
  197. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +0 -19
  198. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +0 -1
  199. package/lib/typescript/src/util/PlayingState.d.ts +0 -7
  200. package/lib/typescript/src/util/PlayingState.d.ts.map +0 -1
  201. package/lib/typescript/src/util/UIState.d.ts +0 -86
  202. package/lib/typescript/src/util/UIState.d.ts.map +0 -1
  203. package/src/components/AudioOutputSwitchView.tsx +0 -146
  204. package/src/components/index.tsx +0 -21
  205. package/src/icons/mic.png +0 -0
  206. package/src/util/ForegroundServiceConfig.tsx +0 -42
  207. package/src/util/UIState.ts +0 -106
@@ -12,94 +12,57 @@
12
12
  //===----------------------------------------------------------------------===//
13
13
 
14
14
  import React, { useEffect } from 'react';
15
- import { View, Text, useColorScheme, StyleSheet, TouchableOpacity, ActivityIndicator } from 'react-native';
15
+ import { View, Text, useColorScheme, StyleSheet } from 'react-native';
16
16
  import { COLORS } from '../util/Colors';
17
17
 
18
- import { type Reconnecting } from '../util/UIState';
18
+ import { type RetryValue } from '../sdk/Lifecycle';
19
19
  import { Translations } from '../util/Translations';
20
+ import { LiveVoiceSpinner } from './LiveVoiceSpinner';
20
21
 
21
22
  export interface ReconnectingProps {
22
23
  isDarkMode?: boolean
23
- state: Reconnecting
24
- onPress: () => void
24
+ retry: RetryValue
25
25
  }
26
26
 
27
+ /**
28
+ * The default reconnecting part. The reconnect runs by itself, so it has no retry-now button; the
29
+ * error part has the retry.
30
+ */
27
31
  export const ReconnectingView: React.FC<ReconnectingProps> = (props) => {
28
32
  const isDarkMode = props.isDarkMode ?? useColorScheme() === 'dark';
29
- const reconnectingState = props.state;
30
- const isAttemptOngoing = reconnectingState.state.type === 'inProgress';
33
+ const { retry } = props;
31
34
 
32
- const [secUntilRetry, setSecUntilRetry] = React.useState(0);
35
+ const [secondsUntilRetry, setSecondsUntilRetry] = React.useState(() => secondsUntil(retry));
33
36
 
34
- //updated the countdown every second!
37
+ // Count down every second.
35
38
  useEffect(() => {
36
- const newValue = calculateSecondsUntilRetry(reconnectingState)
37
- setSecUntilRetry(newValue);
38
-
39
- const interval = setInterval(() => {
40
- const newValue = calculateSecondsUntilRetry(reconnectingState)
41
- setSecUntilRetry(newValue);
42
- }, 1000);
43
-
44
- return () => clearInterval(interval); // Clean up when unmounted
45
- }, [reconnectingState]);
39
+ setSecondsUntilRetry(secondsUntil(retry));
40
+ const interval = setInterval(() => setSecondsUntilRetry(secondsUntil(retry)), 1000);
41
+ return () => clearInterval(interval);
42
+ }, [retry]);
46
43
 
44
+ const textColor = isDarkMode ? COLORS.white : COLORS.black;
47
45
  return (
48
46
  <View
49
47
  style={[
50
48
  styles.container,
51
49
  isDarkMode ? { backgroundColor: COLORS.cardBgDark } : { backgroundColor: COLORS.cardBg }
52
50
  ]}>
53
-
54
- <Text
55
- style={[styles.text, isDarkMode ? styles.darkText : styles.lightText]}>
56
- {reconnectingState.state.type === 'awaitingRetry'
57
- ? Translations.errorScreenRetryingIn(secUntilRetry)
51
+ <Text style={[styles.text, { color: textColor }]}>
52
+ {retry.kind === 'scheduled'
53
+ ? Translations.errorScreenRetryingIn(secondsUntilRetry)
58
54
  : Translations.errorScreenRetrying()}
59
55
  </Text>
60
-
61
- {/* Retry-Button */}
62
- <TouchableOpacity
63
- onPress={props.onPress}
64
- activeOpacity={0.9}
65
- style={[
66
- styles.button,
67
- {
68
- minWidth: 100, // fixed width, so it doesn't change size when showing the ActivityIndicator
69
- backgroundColor: isDarkMode ? '#fff' : '#191919',
70
- },
71
- ]}
72
- >
73
- {isAttemptOngoing
74
- ? (
75
- <ActivityIndicator
76
- size='small'
77
- color={isDarkMode ? '#191919' : '#fff'}
78
- />
79
- )
80
- : (
81
- <Text
82
- style={[
83
- styles.buttonText,
84
- isDarkMode ? styles.lightText : styles.darkText,
85
- ]}>
86
- {Translations.errorRetryNowButton()}
87
- </Text>
88
- )}
89
- </TouchableOpacity>
56
+ <View style={styles.spinner}>
57
+ <LiveVoiceSpinner size={36} color={textColor} />
58
+ </View>
90
59
  </View>
91
60
  );
92
61
  };
93
62
 
94
- function calculateSecondsUntilRetry(reconnectingState: Reconnecting): number {
95
- if (reconnectingState.state.type !== 'awaitingRetry') {
96
- return 0;
97
- }
98
-
99
- return Math.max(
100
- 0,
101
- (reconnectingState.state.retryAt.getTime() / 1000) - (Date.now() / 1000),
102
- );
63
+ function secondsUntil(retry: RetryValue): number {
64
+ if (retry.kind !== 'scheduled') return 0;
65
+ return Math.max(0, Math.ceil((retry.at - Date.now()) / 1000));
103
66
  }
104
67
 
105
68
  const styles = StyleSheet.create({
@@ -116,25 +79,9 @@ const styles = StyleSheet.create({
116
79
  fontWeight: '500',
117
80
  textAlign: 'center',
118
81
  },
119
- dangerText: {
120
- color: '#F1521B',
121
- },
122
- darkText: {
123
- color: '#fff',
124
- },
125
- lightText: {
126
- color: '#191919',
127
- },
128
- button: {
129
- paddingHorizontal: 10,
130
- paddingVertical: 8,
131
- borderRadius: 8,
82
+ // Muted, as on the native SDKs: it says the SDK is working, the text says what on.
83
+ spinner: {
132
84
  marginTop: 10,
133
- },
134
- buttonText: {
135
- color: '#fff',
136
- fontSize: 14,
137
- fontWeight: '500',
138
- textAlign: 'center',
85
+ opacity: 0.5,
139
86
  },
140
87
  });
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/src/index.tsx CHANGED
@@ -11,9 +11,95 @@
11
11
  //
12
12
  //===----------------------------------------------------------------------===//
13
13
 
14
- export * from './sdk/LiveVoiceSdk';
15
- export * from './components';
16
- export * from './util/Translations';
17
- export * from './util/ForegroundServiceConfig';
18
- export * from './util/UIState';
19
- export * from './util/PlayingState';
14
+ // The public API, by name. Everything else is internal: the parts of the default view, the
15
+ // texts and the bridge plumbing.
16
+
17
+ export {
18
+ initialize,
19
+ joinEvent,
20
+ leaveEvent,
21
+ reconnect,
22
+ stopChannel,
23
+ startSubtitles,
24
+ stopSubtitles,
25
+ setAudioOutput,
26
+ setNowPlayingArtwork,
27
+ openLiveVoiceWebsite,
28
+ getSdkVersion,
29
+ getNativeSdkVersion,
30
+ } from './sdk/LiveVoiceSdk';
31
+ export type {
32
+ LiveVoiceAudioFocus,
33
+ LiveVoiceForegroundServiceConfig,
34
+ LiveVoiceInitializeOptions,
35
+ } from './sdk/LiveVoiceSdk';
36
+ export {
37
+ getCurrentConnection,
38
+ addConnectionListener,
39
+ useLiveVoiceConnection,
40
+ getCurrentSubtitles,
41
+ addSubtitlesListener,
42
+ useLiveVoiceSubtitles,
43
+ getCurrentAvailableSubtitles,
44
+ addAvailableSubtitlesListener,
45
+ useLiveVoiceAvailableSubtitles,
46
+ getCurrentAudioOutput,
47
+ addAudioOutputListener,
48
+ useLiveVoiceAudioOutput,
49
+ getCurrentFeatures,
50
+ addFeaturesListener,
51
+ useLiveVoiceFeatures,
52
+ getCurrentEvent,
53
+ addCurrentEventListener,
54
+ useLiveVoiceCurrentEvent,
55
+ getCurrentRequestableLanguages,
56
+ addRequestableLanguagesListener,
57
+ useLiveVoiceRequestableLanguages,
58
+ getCurrentLinkedEvents,
59
+ addLinkedEventsListener,
60
+ useLiveVoiceLinkedEvents,
61
+ } from './sdk/Observables';
62
+ export { LiveVoiceError, liveVoiceErrorCodes } from './sdk/Lifecycle';
63
+ export type {
64
+ LiveVoiceConnection,
65
+ LiveVoiceErrorCode,
66
+ LiveVoiceErrorDetail,
67
+ LiveVoiceJoinedEvent,
68
+ LiveVoiceJoinRequest,
69
+ LiveVoiceRetry,
70
+ } from './sdk/Lifecycle';
71
+ export type {
72
+ LiveVoiceAiTranslationInfo,
73
+ LiveVoiceAudioOutput,
74
+ LiveVoiceAvailableSubtitle,
75
+ LiveVoiceChannel,
76
+ LiveVoiceDependentChannel,
77
+ LiveVoiceFeatures,
78
+ LiveVoicePlayingState,
79
+ LiveVoiceSubscription,
80
+ LiveVoiceSubtitleState,
81
+ } from './sdk/Models';
82
+ export { requestLanguage, cancelLanguageRequest, switchToLinkedEvent } from './sdk/Event';
83
+ export type {
84
+ LiveVoiceBrandColors,
85
+ LiveVoiceEvent,
86
+ LiveVoiceLanguageRequestState,
87
+ LiveVoiceLinkedEvent,
88
+ LiveVoiceRequestableLanguage,
89
+ } from './sdk/Event';
90
+ export { setPasswordProvider, setPasswordStore } from './sdk/Passwords';
91
+ export type {
92
+ LiveVoicePasswordProvider,
93
+ LiveVoicePasswordRequest,
94
+ LiveVoicePasswordStore,
95
+ LiveVoicePasswordSubject,
96
+ } from './sdk/Passwords';
97
+ export { addLogListener } from './sdk/LogEntries';
98
+ export type { LiveVoiceLogEntry, LiveVoiceLogLevel, LiveVoiceLogListener } from './sdk/LogEntries';
99
+ export { LiveVoiceView } from './components/LiveVoiceView';
100
+ export { Translations } from './util/Translations';
101
+ export type {
102
+ LiveVoiceChannelCell,
103
+ LiveVoiceEmptyReason,
104
+ LiveVoiceViewProps,
105
+ } from './components/LiveVoiceView';
@@ -0,0 +1,259 @@
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
+ // Internal: the wrapper's side of the native wrapper bridge (bridge.md). It hydrates a cache of
15
+ // every state from the snapshot `initialize` returns, keeps it current from the one `onState`
16
+ // callback, and drops any delivery whose revision is not newer than the one it holds. Nothing here
17
+ // is exported from the package entry point.
18
+
19
+ import Native from '../specs/NativeLiveVoiceSdk';
20
+ import { logDecodeError, logListenerError } from '../util/logger';
21
+ import {
22
+ bridgeDidInitialize,
23
+ receivePasswordRequest,
24
+ receivePasswordRequestCancellation,
25
+ receivePasswordStoreRequest,
26
+ } from './Passwords';
27
+
28
+ /** The states on the wire (bridge.md 2.1). */
29
+ export type StateName =
30
+ | 'connection'
31
+ | 'defaultView'
32
+ | 'features'
33
+ | 'audioOutput'
34
+ | 'subtitles'
35
+ | 'availableSubtitles'
36
+ | 'currentEvent'
37
+ | 'requestableLanguages'
38
+ | 'linkedEvents';
39
+
40
+ const stateNames: readonly StateName[] = [
41
+ 'connection',
42
+ 'defaultView',
43
+ 'features',
44
+ 'audioOutput',
45
+ 'subtitles',
46
+ 'availableSubtitles',
47
+ 'currentEvent',
48
+ 'requestableLanguages',
49
+ 'linkedEvents',
50
+ ];
51
+
52
+ /** A delivered value; `decoded` is whether the state's decoder, if it has one, could read it. */
53
+ type Envelope = { revision: number; value: unknown; encoded: string; decoded: boolean };
54
+
55
+ const states = new Map<StateName, Envelope>();
56
+ /** The latest revision of each state whose value was decoded and applied (bridge.md 1.3). */
57
+ const appliedRevisions = new Map<StateName, number>();
58
+ /** The one decoder of each state that has one: a public store, or the default view's model. */
59
+ const decoders = new Map<StateName, (value: unknown) => boolean>();
60
+ const observers = new Map<StateName, Set<(value: unknown) => void>>();
61
+ let initialized = false;
62
+ let subscribed = false;
63
+ let errorMessages: Record<string, string> | undefined;
64
+
65
+ /** Created once per JavaScript runtime; a different one tells native that the runtime is new. */
66
+ export const runtimeId = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
67
+
68
+ /** Whether `initialize` has hydrated the cache. */
69
+ export function isInitialized(): boolean {
70
+ return initialized;
71
+ }
72
+
73
+ /** The latest value of `name` as JSON, which changes exactly when the value does. */
74
+ export function currentEncoded(name: StateName): string | undefined {
75
+ return states.get(name)?.encoded;
76
+ }
77
+
78
+ type RevisionWaiter = { targets: [StateName, number][]; done: () => void };
79
+ const revisionWaiters = new Set<RevisionWaiter>();
80
+
81
+ /** How long a command's reply waits for its states before resolving anyway (18 item 133). */
82
+ export const revisionWaitMillis = 5_000;
83
+
84
+ /**
85
+ * Resolves once every state named in a reply's `revisions` (bridge.md 3.3) has applied that
86
+ * revision or a newer one, so `getCurrent…()` shows the command's result right after `await`. Only
87
+ * a value its decoder read counts; a state without a decoder counts on arrival. A state that never
88
+ * applies, for example after an undecodable delivery, stops the wait after five seconds: the
89
+ * command succeeded, so it never fails because of it.
90
+ */
91
+ export function awaitRevisions(revisions: unknown): Promise<void> {
92
+ if (!record(revisions)) return Promise.resolve();
93
+ const targets = Object.entries(revisions).filter(
94
+ (entry): entry is [StateName, number] =>
95
+ stateNames.includes(entry[0] as StateName) && typeof entry[1] === 'number',
96
+ );
97
+ const pending = () => targets.filter(([name, revision]) => !hasApplied(name, revision));
98
+ if (pending().length === 0) return Promise.resolve();
99
+ return new Promise(resolve => {
100
+ const waiter: RevisionWaiter = {
101
+ targets,
102
+ done: () => {
103
+ clearTimeout(timer);
104
+ revisionWaiters.delete(waiter);
105
+ resolve();
106
+ },
107
+ };
108
+ const timer = setTimeout(() => {
109
+ for (const [name] of pending()) logDecodeError('state', name);
110
+ waiter.done();
111
+ }, revisionWaitMillis);
112
+ revisionWaiters.add(waiter);
113
+ });
114
+ }
115
+
116
+ function hasApplied(name: StateName, revision: number): boolean {
117
+ return (appliedRevisions.get(name) ?? -1) >= revision;
118
+ }
119
+
120
+ function settleRevisionWaiters(): void {
121
+ for (const waiter of [...revisionWaiters]) {
122
+ if (waiter.targets.every(([name, revision]) => hasApplied(name, revision))) waiter.done();
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Makes `decoder` the one decoder of `name`, and runs it on the value already held. The bridge
128
+ * calls it with every changed value before any observer; it returns whether it decoded and applied
129
+ * the value, and only then may that value's revision, or a newer one of the same value, satisfy a
130
+ * command's revisions.
131
+ */
132
+ export function decodeState(name: StateName, decoder: (value: unknown) => boolean): void {
133
+ decoders.set(name, decoder);
134
+ const held = states.get(name);
135
+ if (held === undefined) return;
136
+ held.decoded = runDecoder(name, held.value);
137
+ if (held.decoded) markApplied(name, held.revision);
138
+ // It counted on arrival, before the state had a decoder; now it is known to be undecodable.
139
+ else appliedRevisions.delete(name);
140
+ }
141
+
142
+ function runDecoder(name: StateName, value: unknown): boolean {
143
+ const decoder = decoders.get(name);
144
+ if (decoder === undefined) return true;
145
+ try { return decoder(value); }
146
+ catch {
147
+ logDecodeError('state', name);
148
+ return false;
149
+ }
150
+ }
151
+
152
+ function markApplied(name: StateName, revision: number): void {
153
+ appliedRevisions.set(name, revision);
154
+ settleRevisionWaiters();
155
+ }
156
+
157
+ /** Calls `observer` with every new value of `name`, from now on. */
158
+ export function observeState(name: StateName, observer: (value: unknown) => void): () => void {
159
+ let set = observers.get(name);
160
+ if (set === undefined) {
161
+ set = new Set();
162
+ observers.set(name, set);
163
+ }
164
+ const entry = (value: unknown) => observer(value);
165
+ set.add(entry);
166
+ return () => { set.delete(entry); };
167
+ }
168
+
169
+ /**
170
+ * Initialises the native SDK with `configuration` (bridge.md 3.2) and applies the snapshot it
171
+ * replies with before resolving. A native failure rejects with the native error as is.
172
+ */
173
+ export async function initializeBridge(configuration: Record<string, unknown>): Promise<void> {
174
+ subscribe();
175
+ const reply = await Native.initialize(JSON.stringify(configuration));
176
+ const snapshot = parse(reply);
177
+ initialized = true;
178
+ bridgeDidInitialize();
179
+ if (!record(snapshot)) {
180
+ logDecodeError('reply', 'initialize');
181
+ return;
182
+ }
183
+ for (const name of stateNames) {
184
+ const envelope = snapshot[name];
185
+ if (record(envelope)) apply(name, envelope.revision, envelope.value);
186
+ }
187
+ if (Array.isArray(snapshot.passwordRequests)) {
188
+ // A request already answered or cancelled is not taken again.
189
+ for (const request of snapshot.passwordRequests) receivePasswordRequest(request);
190
+ }
191
+ }
192
+
193
+ /** The localised text of an error code, from the native table (works before `initialize`). */
194
+ export function errorMessage(code: string): string {
195
+ if (errorMessages === undefined) {
196
+ const table = parse(Native.getErrorMessages());
197
+ errorMessages = {};
198
+ if (record(table)) {
199
+ for (const [key, value] of Object.entries(table)) {
200
+ if (typeof value === 'string') errorMessages[key] = value;
201
+ }
202
+ }
203
+ }
204
+ return errorMessages[code] ?? '';
205
+ }
206
+
207
+ function subscribe(): void {
208
+ if (subscribed) return;
209
+ subscribed = true;
210
+ Native.onState(receiveState);
211
+ Native.onPasswordRequest(data => receivePasswordRequest(callback('onPasswordRequest', data)));
212
+ Native.onPasswordRequestCancelled(data => receivePasswordRequestCancellation(callback('onPasswordRequestCancelled', data)));
213
+ Native.onPasswordStoreRequest(data => receivePasswordStoreRequest(callback('onPasswordStoreRequest', data)));
214
+ }
215
+
216
+ /** A callback's JSON object; one that is not is logged and handed on as `undefined`. */
217
+ function callback(name: string, data: string): unknown {
218
+ const value = parse(data);
219
+ if (!record(value)) logDecodeError('callback', name);
220
+ return value;
221
+ }
222
+
223
+ function receiveState(data: string): void {
224
+ const update = callback('onState', data);
225
+ if (!record(update) || !stateNames.includes(update.state as StateName)) return;
226
+ apply(update.state as StateName, update.revision, update.value);
227
+ }
228
+
229
+ function apply(name: StateName, revision: unknown, value: unknown): void {
230
+ if (typeof revision !== 'number' || !Number.isSafeInteger(revision) || revision < 0) return;
231
+ const held = states.get(name);
232
+ if (held !== undefined && revision <= held.revision) return;
233
+ const encoded = JSON.stringify(value ?? null);
234
+ const changed = held === undefined || held.encoded !== encoded;
235
+ // A newer revision of the same value is as decoded as the value it repeats.
236
+ const envelope: Envelope = { revision, value: value ?? null, encoded, decoded: changed || held.decoded };
237
+ states.set(name, envelope);
238
+ // Only an actual change reaches the decoder and the observers; a newer revision of the same
239
+ // value does not.
240
+ if (changed) {
241
+ envelope.decoded = runDecoder(name, envelope.value);
242
+ for (const observer of [...(observers.get(name) ?? [])]) {
243
+ try { observer(envelope.value); }
244
+ catch (error) { logListenerError('LV_LISTENER_ERROR', error); }
245
+ }
246
+ }
247
+ // After the observers, so a waiting command resolves with the values already published.
248
+ if (envelope.decoded) markApplied(name, revision);
249
+ }
250
+
251
+ function parse(data: unknown): unknown {
252
+ if (typeof data !== 'string') return undefined;
253
+ try { return JSON.parse(data); }
254
+ catch { return undefined; }
255
+ }
256
+
257
+ export function record(value: unknown): value is Record<string, unknown> {
258
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
259
+ }