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.
- package/README.md +105 -66
- package/android/build.gradle +5 -4
- package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +122 -188
- package/android/src/main/java/io/livevoice/react/__SDK.kt +2 -2
- package/ios/LiveVoiceRNBridge.swift +362 -0
- package/ios/LiveVoiceSdkReactNative.mm +187 -158
- package/ios/__SDK.h +1 -1
- package/ios/livevoice_spm_backport.rb +202 -0
- package/lib/module/components/BrandingView.js +58 -39
- package/lib/module/components/BrandingView.js.map +1 -1
- package/lib/module/components/ChannelView.js +355 -44
- package/lib/module/components/ChannelView.js.map +1 -1
- package/lib/module/components/ErrorView.js +5 -6
- package/lib/module/components/ErrorView.js.map +1 -1
- package/lib/module/components/LiveVoiceSpinner.js +81 -0
- package/lib/module/components/LiveVoiceSpinner.js.map +1 -0
- package/lib/module/components/LiveVoiceView.js +311 -133
- package/lib/module/components/LiveVoiceView.js.map +1 -1
- package/lib/module/components/PasswordPrompt.js +165 -0
- package/lib/module/components/PasswordPrompt.js.map +1 -0
- package/lib/module/components/PlayButtonView.js +180 -244
- package/lib/module/components/PlayButtonView.js.map +1 -1
- package/lib/module/components/ReconnectingView.js +31 -53
- package/lib/module/components/ReconnectingView.js.map +1 -1
- package/lib/module/icons/branding-ios.png +0 -0
- package/lib/module/icons/camera-off-android.png +0 -0
- package/lib/module/icons/camera-off-android@2x.png +0 -0
- package/lib/module/icons/camera-off-android@3x.png +0 -0
- package/lib/module/icons/cpu-android.png +0 -0
- package/lib/module/icons/cpu-android@2x.png +0 -0
- package/lib/module/icons/cpu-android@3x.png +0 -0
- package/lib/module/icons/cpu-ios.png +0 -0
- package/lib/module/icons/cpu-ios@2x.png +0 -0
- package/lib/module/icons/cpu-ios@3x.png +0 -0
- package/lib/module/icons/lock-android.png +0 -0
- package/lib/module/icons/lock-android@2x.png +0 -0
- package/lib/module/icons/lock-android@3x.png +0 -0
- package/lib/module/icons/lock-ios.png +0 -0
- package/lib/module/icons/lock-ios@2x.png +0 -0
- package/lib/module/icons/lock-ios@3x.png +0 -0
- package/lib/module/icons/mic-off-android.png +0 -0
- package/lib/module/icons/mic-off-android@2x.png +0 -0
- package/lib/module/icons/mic-off-android@3x.png +0 -0
- package/lib/module/icons/mic-slash-ios.png +0 -0
- package/lib/module/icons/mic-slash-ios@2x.png +0 -0
- package/lib/module/icons/mic-slash-ios@3x.png +0 -0
- package/lib/module/icons/video-slash-ios.png +0 -0
- package/lib/module/icons/video-slash-ios@2x.png +0 -0
- package/lib/module/icons/video-slash-ios@3x.png +0 -0
- package/lib/module/index.js +11 -6
- package/lib/module/index.js.map +1 -1
- package/lib/module/sdk/Bridge.js +236 -0
- package/lib/module/sdk/Bridge.js.map +1 -0
- package/lib/module/sdk/Event.js +160 -0
- package/lib/module/sdk/Event.js.map +1 -0
- package/lib/module/sdk/Lifecycle.js +331 -0
- package/lib/module/sdk/Lifecycle.js.map +1 -0
- package/lib/module/sdk/LiveVoiceSdk.js +262 -268
- package/lib/module/sdk/LiveVoiceSdk.js.map +1 -1
- package/lib/module/sdk/LiveVoiceState.js +208 -40
- package/lib/module/sdk/LiveVoiceState.js.map +1 -1
- package/lib/module/sdk/LogEntries.js +99 -0
- package/lib/module/sdk/LogEntries.js.map +1 -0
- package/lib/module/sdk/Models.js +2 -0
- package/lib/module/{util/UIState.js.map → sdk/Models.js.map} +1 -1
- package/lib/module/{util/PlayingState.js → sdk/NativeSdkVersions.js} +9 -8
- package/lib/module/sdk/NativeSdkVersions.js.map +1 -0
- package/lib/module/sdk/Observables.js +391 -0
- package/lib/module/sdk/Observables.js.map +1 -0
- package/lib/module/sdk/Passwords.js +298 -0
- package/lib/module/sdk/Passwords.js.map +1 -0
- package/lib/module/sdk/Playback.js +35 -0
- package/lib/module/sdk/Playback.js.map +1 -0
- package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
- package/lib/module/util/Colors.js +2 -1
- package/lib/module/util/Colors.js.map +1 -1
- package/lib/module/util/Translations.js +34 -11
- package/lib/module/util/Translations.js.map +1 -1
- package/lib/module/util/freeze.js +26 -0
- package/lib/module/util/freeze.js.map +1 -0
- package/lib/module/util/logger.js +79 -41
- package/lib/module/util/logger.js.map +1 -1
- package/lib/typescript/src/components/BrandingView.d.ts +4 -0
- package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
- package/lib/typescript/src/components/ChannelView.d.ts +29 -3
- package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
- package/lib/typescript/src/components/ErrorView.d.ts +5 -3
- package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
- package/lib/typescript/src/components/LiveVoiceSpinner.d.ts +16 -0
- package/lib/typescript/src/components/LiveVoiceSpinner.d.ts.map +1 -0
- package/lib/typescript/src/components/LiveVoiceView.d.ts +81 -21
- package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
- package/lib/typescript/src/components/PasswordPrompt.d.ts +13 -0
- package/lib/typescript/src/components/PasswordPrompt.d.ts.map +1 -0
- package/lib/typescript/src/components/PlayButtonView.d.ts +12 -329
- package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
- package/lib/typescript/src/components/ReconnectingView.d.ts +6 -3
- package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +15 -6
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/sdk/Bridge.d.ts +36 -0
- package/lib/typescript/src/sdk/Bridge.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Event.d.ts +97 -0
- package/lib/typescript/src/sdk/Event.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Lifecycle.d.ts +191 -0
- package/lib/typescript/src/sdk/Lifecycle.d.ts.map +1 -0
- package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +195 -190
- package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -1
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts +77 -9
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -1
- package/lib/typescript/src/sdk/LogEntries.d.ts +28 -0
- package/lib/typescript/src/sdk/LogEntries.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Models.d.ts +114 -0
- package/lib/typescript/src/sdk/Models.d.ts.map +1 -0
- package/lib/typescript/src/sdk/NativeSdkVersions.d.ts +6 -0
- package/lib/typescript/src/sdk/NativeSdkVersions.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Observables.d.ts +123 -0
- package/lib/typescript/src/sdk/Observables.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Passwords.d.ts +72 -0
- package/lib/typescript/src/sdk/Passwords.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Playback.d.ts +9 -0
- package/lib/typescript/src/sdk/Playback.d.ts.map +1 -0
- package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +34 -31
- package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
- package/lib/typescript/src/util/Colors.d.ts.map +1 -1
- package/lib/typescript/src/util/Translations.d.ts +30 -8
- package/lib/typescript/src/util/Translations.d.ts.map +1 -1
- package/lib/typescript/src/util/freeze.d.ts +6 -0
- package/lib/typescript/src/util/freeze.d.ts.map +1 -0
- package/lib/typescript/src/util/logger.d.ts +17 -7
- package/lib/typescript/src/util/logger.d.ts.map +1 -1
- package/livevoice-sdk-react-native.podspec +29 -4
- package/package.json +12 -9
- package/src/components/BrandingView.tsx +42 -55
- package/src/components/ChannelView.tsx +329 -38
- package/src/components/ErrorView.tsx +9 -12
- package/src/components/LiveVoiceSpinner.tsx +87 -0
- package/src/components/LiveVoiceView.tsx +319 -201
- package/src/components/PasswordPrompt.tsx +144 -0
- package/src/components/PlayButtonView.tsx +165 -269
- package/src/components/ReconnectingView.tsx +28 -81
- package/src/icons/branding-ios.png +0 -0
- package/src/icons/camera-off-android.png +0 -0
- package/src/icons/camera-off-android@2x.png +0 -0
- package/src/icons/camera-off-android@3x.png +0 -0
- package/src/icons/cpu-android.png +0 -0
- package/src/icons/cpu-android@2x.png +0 -0
- package/src/icons/cpu-android@3x.png +0 -0
- package/src/icons/cpu-ios.png +0 -0
- package/src/icons/cpu-ios@2x.png +0 -0
- package/src/icons/cpu-ios@3x.png +0 -0
- package/src/icons/lock-android.png +0 -0
- package/src/icons/lock-android@2x.png +0 -0
- package/src/icons/lock-android@3x.png +0 -0
- package/src/icons/lock-ios.png +0 -0
- package/src/icons/lock-ios@2x.png +0 -0
- package/src/icons/lock-ios@3x.png +0 -0
- package/src/icons/mic-off-android.png +0 -0
- package/src/icons/mic-off-android@2x.png +0 -0
- package/src/icons/mic-off-android@3x.png +0 -0
- package/src/icons/mic-slash-ios.png +0 -0
- package/src/icons/mic-slash-ios@2x.png +0 -0
- package/src/icons/mic-slash-ios@3x.png +0 -0
- package/src/icons/video-slash-ios.png +0 -0
- package/src/icons/video-slash-ios@2x.png +0 -0
- package/src/icons/video-slash-ios@3x.png +0 -0
- package/src/index.tsx +92 -6
- package/src/sdk/Bridge.ts +259 -0
- package/src/sdk/Event.ts +187 -0
- package/src/sdk/Lifecycle.ts +366 -0
- package/src/sdk/LiveVoiceSdk.ts +285 -309
- package/src/sdk/LiveVoiceState.ts +230 -60
- package/src/sdk/LogEntries.ts +88 -0
- package/src/sdk/Models.ts +129 -0
- package/src/{util/PlayingState.ts → sdk/NativeSdkVersions.ts} +7 -6
- package/src/sdk/Observables.ts +414 -0
- package/src/sdk/Passwords.ts +286 -0
- package/src/sdk/Playback.ts +30 -0
- package/src/specs/NativeLiveVoiceSdk.ts +42 -38
- package/src/util/Colors.ts +2 -1
- package/src/util/Translations.ts +34 -12
- package/src/util/freeze.ts +23 -0
- package/src/util/logger.ts +79 -52
- package/lib/module/components/AudioOutputSwitchView.js +0 -138
- package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
- package/lib/module/components/index.js +0 -24
- package/lib/module/components/index.js.map +0 -1
- package/lib/module/icons/mic.png +0 -0
- package/lib/module/util/ForegroundServiceConfig.js +0 -38
- package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
- package/lib/module/util/PlayingState.js.map +0 -1
- package/lib/module/util/UIState.js +0 -4
- package/lib/typescript/src/components/AudioOutputSwitchView.d.ts +0 -6
- package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +0 -1
- package/lib/typescript/src/components/index.d.ts +0 -9
- package/lib/typescript/src/components/index.d.ts.map +0 -1
- package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +0 -19
- package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +0 -1
- package/lib/typescript/src/util/PlayingState.d.ts +0 -7
- package/lib/typescript/src/util/PlayingState.d.ts.map +0 -1
- package/lib/typescript/src/util/UIState.d.ts +0 -86
- package/lib/typescript/src/util/UIState.d.ts.map +0 -1
- package/src/components/AudioOutputSwitchView.tsx +0 -146
- package/src/components/index.tsx +0 -21
- package/src/icons/mic.png +0 -0
- package/src/util/ForegroundServiceConfig.tsx +0 -42
- 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
|
|
15
|
+
import { View, Text, useColorScheme, StyleSheet } from 'react-native';
|
|
16
16
|
import { COLORS } from '../util/Colors';
|
|
17
17
|
|
|
18
|
-
import { type
|
|
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
|
-
|
|
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
|
|
30
|
-
const isAttemptOngoing = reconnectingState.state.type === 'inProgress';
|
|
33
|
+
const { retry } = props;
|
|
31
34
|
|
|
32
|
-
const [
|
|
35
|
+
const [secondsUntilRetry, setSecondsUntilRetry] = React.useState(() => secondsUntil(retry));
|
|
33
36
|
|
|
34
|
-
//
|
|
37
|
+
// Count down every second.
|
|
35
38
|
useEffect(() => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
55
|
-
|
|
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
|
-
|
|
62
|
-
|
|
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
|
|
95
|
-
if (
|
|
96
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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
|
|
Binary file
|
|
Binary file
|
package/src/index.tsx
CHANGED
|
@@ -11,9 +11,95 @@
|
|
|
11
11
|
//
|
|
12
12
|
//===----------------------------------------------------------------------===//
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
|
|
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
|
+
}
|