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.
- package/README.md +158 -7
- package/android/build.gradle +18 -6
- package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +121 -221
- package/android/src/main/java/io/livevoice/react/__SDK.kt +17 -3
- package/ios/LiveVoiceRNBridge.swift +362 -0
- package/ios/LiveVoiceSdkReactNative.mm +247 -145
- package/ios/__SDK.h +16 -2
- package/ios/livevoice_spm_backport.rb +202 -0
- package/lib/module/components/BrandingView.js +71 -50
- package/lib/module/components/BrandingView.js.map +1 -1
- package/lib/module/components/ChannelView.js +368 -54
- package/lib/module/components/ChannelView.js.map +1 -1
- package/lib/module/components/ErrorView.js +17 -16
- 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 +351 -104
- 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 +192 -254
- package/lib/module/components/PlayButtonView.js.map +1 -1
- package/lib/module/components/ReconnectingView.js +43 -62
- package/lib/module/components/ReconnectingView.js.map +1 -1
- package/lib/module/components/WarningView.js +12 -10
- package/lib/module/components/WarningView.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 +24 -225
- 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 +365 -0
- package/lib/module/sdk/LiveVoiceSdk.js.map +1 -0
- package/lib/module/sdk/LiveVoiceState.js +234 -0
- package/lib/module/sdk/LiveVoiceState.js.map +1 -0
- 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/sdk/NativeSdkVersions.js +24 -0
- 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 +12 -10
- package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
- package/lib/module/util/Colors.js +14 -11
- package/lib/module/util/Colors.js.map +1 -1
- package/lib/module/util/Translations.js +46 -21
- 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 +114 -0
- package/lib/module/util/logger.js.map +1 -0
- 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 +85 -3
- 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/components/WarningView.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +15 -144
- 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 +215 -0
- package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -0
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts +80 -0
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -0
- 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 +35 -29
- 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 +33 -0
- package/lib/typescript/src/util/logger.d.ts.map +1 -0
- package/livevoice-sdk-react-native.podspec +41 -16
- package/package.json +26 -14
- package/src/components/BrandingView.tsx +55 -66
- package/src/components/ChannelView.tsx +342 -48
- package/src/components/ErrorView.tsx +21 -22
- package/src/components/LiveVoiceSpinner.tsx +87 -0
- package/src/components/LiveVoiceView.tsx +360 -151
- package/src/components/PasswordPrompt.tsx +144 -0
- package/src/components/PlayButtonView.tsx +177 -279
- package/src/components/ReconnectingView.tsx +40 -86
- package/src/components/WarningView.tsx +12 -10
- 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 +105 -252
- 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 +384 -0
- package/src/sdk/LiveVoiceState.ts +259 -0
- package/src/sdk/LogEntries.ts +88 -0
- package/src/sdk/Models.ts +129 -0
- package/src/sdk/NativeSdkVersions.ts +21 -0
- 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 +56 -46
- package/src/util/Colors.ts +14 -11
- package/src/util/Translations.ts +46 -22
- package/src/util/freeze.ts +23 -0
- package/src/util/logger.ts +119 -0
- package/android/app/build/generated/source/codegen/java/com/facebook/fbreact/specs/NativeLiveVoiceSdkSpec.java +0 -186
- package/android/app/build/generated/source/codegen/jni/CMakeLists.txt +0 -28
- package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec-generated.cpp +0 -235
- package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec.h +0 -31
- package/android/app/build/generated/source/codegen/jni/react/renderer/components/RNLiveVoiceSpec/RNLiveVoiceSpecJSI.h +0 -341
- package/android/src/main/java/io/livevoice/react/Mappings.kt +0 -85
- package/android/src/main/java/io/livevoice/react/NativeModels.kt +0 -79
- package/lib/module/components/AudioOutputSwitchView.js +0 -138
- package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
- package/lib/module/components/index.js +0 -22
- package/lib/module/components/index.js.map +0 -1
- package/lib/module/icons/mic.png +0 -0
- package/lib/module/util/ForegroundServiceConfig.js +0 -28
- package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
- package/lib/module/util/PlayingState.js +0 -21
- 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 -12
- 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 -66
- package/lib/typescript/src/util/UIState.d.ts.map +0 -1
- package/src/components/AudioOutputSwitchView.tsx +0 -146
- package/src/components/index.tsx +0 -19
- package/src/icons/mic.png +0 -0
- package/src/util/ForegroundServiceConfig.tsx +0 -32
- package/src/util/PlayingState.ts +0 -18
- package/src/util/UIState.ts +0 -89
|
@@ -0,0 +1,366 @@
|
|
|
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
|
+
import Native from '../specs/NativeLiveVoiceSdk';
|
|
15
|
+
import { logDecodeError, logInfo, LiveVoiceLogEvent, writeLogEntry } from '../util/logger';
|
|
16
|
+
import { awaitRevisions, errorMessage, isInitialized, record as object } from './Bridge';
|
|
17
|
+
|
|
18
|
+
/** Every {@link LiveVoiceErrorCode}, for example to check a value at runtime. */
|
|
19
|
+
export const liveVoiceErrorCodes = [
|
|
20
|
+
'not_initialized',
|
|
21
|
+
'not_joined',
|
|
22
|
+
'invalid_argument',
|
|
23
|
+
'superseded',
|
|
24
|
+
'cancelled',
|
|
25
|
+
'timed_out',
|
|
26
|
+
'leave_incomplete',
|
|
27
|
+
'command_in_progress',
|
|
28
|
+
'api_key_invalid',
|
|
29
|
+
'event_not_found',
|
|
30
|
+
'event_inactive',
|
|
31
|
+
'wrong_password',
|
|
32
|
+
'listener_limit_reached',
|
|
33
|
+
'network_failed',
|
|
34
|
+
'no_network',
|
|
35
|
+
'generic',
|
|
36
|
+
'password_required',
|
|
37
|
+
'unsupported_sdk',
|
|
38
|
+
'channel_password_required',
|
|
39
|
+
'channel_wrong_password',
|
|
40
|
+
'channel_limit_reached',
|
|
41
|
+
'channel_unavailable',
|
|
42
|
+
'channel_forbidden',
|
|
43
|
+
'channel_offline',
|
|
44
|
+
] as const;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* What went wrong, the same codes on every platform. Each call names the codes specific to it.
|
|
48
|
+
*
|
|
49
|
+
* Commands:
|
|
50
|
+
* - `not_initialized`: a call that needs `initialize()` was made before it resolved. Reading
|
|
51
|
+
* values, adding listeners and some setters work before it.
|
|
52
|
+
* - `invalid_argument`: an argument is malformed, or not allowed in the current state.
|
|
53
|
+
* - `not_joined`: the connection is not in a state the call allows; most calls need `ready`.
|
|
54
|
+
* - `superseded`: a newer call replaced a `joinEvent`, `reconnect` or `switchToLinkedEvent`. No
|
|
55
|
+
* other call fails with it.
|
|
56
|
+
* - `cancelled`: a leave, a newer command or a new join ended the call. Like `superseded` it
|
|
57
|
+
* needs no message to the user.
|
|
58
|
+
* - `timed_out`: no result within 30 seconds (join, switch, reconnect) or 10 seconds (a start or
|
|
59
|
+
* stop of playback, a subtitle start, the language calls).
|
|
60
|
+
* - `leave_incomplete`: local cleanup or an earlier stop has not finished within its deadline of
|
|
61
|
+
* 5 seconds. It comes from a leave (the event is left all the same), from a stop of playback
|
|
62
|
+
* while joining or failed, and from a start waiting behind an unfinished stop, which can be a
|
|
63
|
+
* stop in the current event. The cleanup is never abandoned; it continues.
|
|
64
|
+
* - `command_in_progress`: the same command is still running: an identical subtitle start, or
|
|
65
|
+
* the request a cancellation is meant to withdraw.
|
|
66
|
+
*
|
|
67
|
+
* Joining: `api_key_invalid` (also when the key does not permit custom UI, for a custom cell,
|
|
68
|
+
* `switchToLinkedEvent` and the language calls), `event_not_found`, `event_inactive`,
|
|
69
|
+
* `password_required` (no password was given), `wrong_password` (one was given and rejected),
|
|
70
|
+
* `listener_limit_reached`, `network_failed` (the connection failed or was lost, also before a
|
|
71
|
+
* call could reach the server), `no_network`, `unsupported_sdk` (the server requires a newer SDK,
|
|
72
|
+
* or the installed native SDK is not the version this package was built for), `generic` (see
|
|
73
|
+
* `detail.reason`).
|
|
74
|
+
*
|
|
75
|
+
* Channels and subtitles: `channel_password_required`, `channel_wrong_password`,
|
|
76
|
+
* `channel_limit_reached` (`detail.limitReason` is `participant` or `viewer`), `channel_offline`
|
|
77
|
+
* (a start of playback only; an offline channel's subtitles can be started),
|
|
78
|
+
* `channel_unavailable` (not part of the event, without audio, or a start of playback while
|
|
79
|
+
* reconnecting), `channel_forbidden`.
|
|
80
|
+
*/
|
|
81
|
+
export type LiveVoiceErrorCode = typeof liveVoiceErrorCodes[number];
|
|
82
|
+
|
|
83
|
+
/** Optional details of an error. */
|
|
84
|
+
export type LiveVoiceErrorDetail = {
|
|
85
|
+
/** The HTTP status of a failed request. */
|
|
86
|
+
httpStatus?: number;
|
|
87
|
+
/** The platform's own error code. */
|
|
88
|
+
platformCode?: number;
|
|
89
|
+
/** Which limit was reached. */
|
|
90
|
+
limitReason?: string;
|
|
91
|
+
/** A finer reason within the code, such as `stop_failed`. */
|
|
92
|
+
reason?: string;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** The codes worth trying again: the cause can go away by itself or with the user's help. */
|
|
96
|
+
const retryableCodes: ReadonlySet<LiveVoiceErrorCode> = new Set<LiveVoiceErrorCode>([
|
|
97
|
+
'timed_out',
|
|
98
|
+
'event_inactive',
|
|
99
|
+
'wrong_password',
|
|
100
|
+
'password_required',
|
|
101
|
+
'listener_limit_reached',
|
|
102
|
+
'network_failed',
|
|
103
|
+
'no_network',
|
|
104
|
+
'channel_password_required',
|
|
105
|
+
'channel_wrong_password',
|
|
106
|
+
'channel_limit_reached',
|
|
107
|
+
'channel_offline',
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
function knownCode(value: unknown): value is LiveVoiceErrorCode {
|
|
111
|
+
return typeof value === 'string' && (liveVoiceErrorCodes as readonly string[]).includes(value);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function typedDetail(value: unknown): LiveVoiceErrorDetail {
|
|
115
|
+
if (!object(value)) return {};
|
|
116
|
+
const detail: LiveVoiceErrorDetail = {};
|
|
117
|
+
if (typeof value.httpStatus === 'number') detail.httpStatus = value.httpStatus;
|
|
118
|
+
if (typeof value.platformCode === 'number') detail.platformCode = value.platformCode;
|
|
119
|
+
if (typeof value.limitReason === 'string') detail.limitReason = value.limitReason;
|
|
120
|
+
if (typeof value.reason === 'string') detail.reason = value.reason;
|
|
121
|
+
return detail;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function wireError(value: unknown): LiveVoiceError {
|
|
125
|
+
if (!object(value) || !knownCode(value.code)) {
|
|
126
|
+
return new LiveVoiceError('generic', { detail: { reason: 'unknown_native_error' } });
|
|
127
|
+
}
|
|
128
|
+
return new LiveVoiceError(value.code, {
|
|
129
|
+
detail: typedDetail(value.detail),
|
|
130
|
+
message: typeof value.message === 'string' ? value.message : null,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The error every LiveVoice call rejects with, and the one a `failed` connection carries. Its
|
|
136
|
+
* `message` is localised text to show as is. Codes only a developer can fix (`invalid_argument`,
|
|
137
|
+
* `not_initialized`, `api_key_invalid`, `unsupported_sdk`) share a generic text; the SDK log
|
|
138
|
+
* explains them.
|
|
139
|
+
*/
|
|
140
|
+
export class LiveVoiceError extends Error {
|
|
141
|
+
/** What went wrong; stable, for your own handling. */
|
|
142
|
+
readonly code: LiveVoiceErrorCode;
|
|
143
|
+
/** Whether trying again can succeed; it follows from the code. */
|
|
144
|
+
readonly isRetryable: boolean;
|
|
145
|
+
/** Optional details, `{}` when there are none. */
|
|
146
|
+
readonly detail: LiveVoiceErrorDetail;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Creates an error, for tests and previews.
|
|
150
|
+
*
|
|
151
|
+
* @param code - What went wrong; `isRetryable` follows from it.
|
|
152
|
+
* @param options - `detail`: extra information for some codes. `message`: the text to show;
|
|
153
|
+
* leave it out or pass `null` for the code's localised text.
|
|
154
|
+
*/
|
|
155
|
+
constructor(
|
|
156
|
+
code: LiveVoiceErrorCode,
|
|
157
|
+
options: { detail?: LiveVoiceErrorDetail; message?: string | null } = {},
|
|
158
|
+
) {
|
|
159
|
+
super(options?.message ?? (errorMessage(code) || code));
|
|
160
|
+
this.name = 'LiveVoiceError';
|
|
161
|
+
this.code = code;
|
|
162
|
+
this.isRetryable = retryableCodes.has(code);
|
|
163
|
+
this.detail = Object.freeze(typedDetail(options?.detail));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** When the next reconnect attempt runs. */
|
|
168
|
+
export type LiveVoiceRetry =
|
|
169
|
+
| { kind: 'inProgress' }
|
|
170
|
+
| { kind: 'scheduled'; at: Date };
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The connection to the event:
|
|
174
|
+
* - `idle`: nothing is joined or joining; also before `initialize()`.
|
|
175
|
+
* - `joining`: a join, a switch to a linked event, or the retry of a failed join is in progress.
|
|
176
|
+
* - `ready`: the event `eventId` is joined.
|
|
177
|
+
* - `reconnecting`: the connection was lost and is recovering; see `retry`. A `reconnect()` that
|
|
178
|
+
* times out leaves it here, and recovery continues.
|
|
179
|
+
* - `leaving`: a leave is running.
|
|
180
|
+
* - `failed`: a join, a switch or the retry of a join failed, or the joined event ended, with
|
|
181
|
+
* `error`. The join is kept: `reconnect()` retries it if the error is retryable, and
|
|
182
|
+
* `stopChannel` and `stopSubtitles` still work. It stays until that retry, a new join or a
|
|
183
|
+
* leave.
|
|
184
|
+
*
|
|
185
|
+
* Use it for display. To know that a call completed, await the call.
|
|
186
|
+
*/
|
|
187
|
+
export type LiveVoiceConnection =
|
|
188
|
+
| { status: 'idle' | 'joining' | 'leaving' }
|
|
189
|
+
| { status: 'ready'; eventId: number }
|
|
190
|
+
| { status: 'reconnecting'; eventId: number; retry: LiveVoiceRetry }
|
|
191
|
+
| { status: 'failed'; error: LiveVoiceError };
|
|
192
|
+
|
|
193
|
+
/** What `joinEvent` joins with. */
|
|
194
|
+
export type LiveVoiceJoinRequest = {
|
|
195
|
+
/**
|
|
196
|
+
* The code listeners join the event with: six characters, digits and the letters
|
|
197
|
+
* `ABCDEFGHJKLMNPQRST`, in either case.
|
|
198
|
+
*/
|
|
199
|
+
joinCode: string;
|
|
200
|
+
/**
|
|
201
|
+
* The event's password, if it has one. A password you pass is final: a wrong one fails with
|
|
202
|
+
* `wrong_password` without a prompt. Without one, a protected event's password comes from the
|
|
203
|
+
* SDK's memory, the password store, then the password provider or the built-in prompt.
|
|
204
|
+
*/
|
|
205
|
+
password?: string | null;
|
|
206
|
+
/** Your API key, from your LiveVoice account page. */
|
|
207
|
+
apiKey: string;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The joined event `joinEvent` returns. It follows a completed switch to a linked event.
|
|
212
|
+
*
|
|
213
|
+
* Its `leave()` leaves only the join this came from, including a linked event it switched to; it
|
|
214
|
+
* can never leave a newer join. If a newer join replaced its join, it does nothing and resolves at
|
|
215
|
+
* once. Otherwise it resolves as `leaveEvent` does: the event's local cleanup has ended, and the
|
|
216
|
+
* observed connection is `idle` unless a newer join exists. If its join was already left, it
|
|
217
|
+
* waits for that leave's cleanup.
|
|
218
|
+
*
|
|
219
|
+
* `leave()` rejects with a {@link LiveVoiceError} `leave_incomplete` when the cleanup has not
|
|
220
|
+
* finished within 5 seconds; the event is left all the same and the cleanup continues. Another
|
|
221
|
+
* leave then waits for the same cleanup and can fail with `leave_incomplete` again.
|
|
222
|
+
*/
|
|
223
|
+
export type LiveVoiceJoinedEvent = { leave(): Promise<void> };
|
|
224
|
+
|
|
225
|
+
export function normalizeError(error: unknown): LiveVoiceError {
|
|
226
|
+
if (error instanceof LiveVoiceError) return error;
|
|
227
|
+
const native = object(error) ? error : {};
|
|
228
|
+
try {
|
|
229
|
+
const wire: unknown = JSON.parse(typeof native.message === 'string' ? native.message : '');
|
|
230
|
+
if (object(wire) && typeof wire.code === 'string') return wireError(wire);
|
|
231
|
+
} catch { /* Non-SDK failures retain a stable fallback. */ }
|
|
232
|
+
return wireError(native);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A developer-only error the wrapper throws itself (bridge.md 3.6): its message is the generic
|
|
237
|
+
* text, and its explanation, a fixed sentence without argument values, goes to the SDK log as
|
|
238
|
+
* `LV_USAGE_ERROR`.
|
|
239
|
+
*/
|
|
240
|
+
export function usageError(
|
|
241
|
+
code: 'invalid_argument' | 'not_initialized',
|
|
242
|
+
operation: string,
|
|
243
|
+
explanation: string,
|
|
244
|
+
): LiveVoiceError {
|
|
245
|
+
writeLogEntry('LV_USAGE_ERROR', 'error', { code, operation, explanation });
|
|
246
|
+
return new LiveVoiceError(code);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** The public call behind each command, for the log. */
|
|
250
|
+
const operations: Record<string, string> = { join: 'joinEvent', leave: 'leaveEvent' };
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Runs `command` natively. Before `initialize()` it fails with `not_initialized` without reaching
|
|
254
|
+
* native, the only error the wrapper owns (bridge.md 3.1). A reply that is not JSON resolves as
|
|
255
|
+
* `undefined`: native succeeded, so the call does too.
|
|
256
|
+
*/
|
|
257
|
+
export async function invoke(command: string, args: Record<string, unknown> = {}): Promise<unknown> {
|
|
258
|
+
if (!isInitialized()) {
|
|
259
|
+
throw usageError('not_initialized', operations[command] ?? command, 'Call initialize() and await it first.');
|
|
260
|
+
}
|
|
261
|
+
let reply: string;
|
|
262
|
+
try { reply = await Native.execute(command, JSON.stringify(args)); }
|
|
263
|
+
catch (error) { throw normalizeError(error); }
|
|
264
|
+
try { return JSON.parse(reply); }
|
|
265
|
+
catch {
|
|
266
|
+
logDecodeError('reply', command);
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export async function join(request: LiveVoiceJoinRequest): Promise<LiveVoiceJoinedEvent> {
|
|
272
|
+
if (!request || typeof request.joinCode !== 'string' || !request.joinCode.trim() ||
|
|
273
|
+
typeof request.apiKey !== 'string' || !request.apiKey.trim() ||
|
|
274
|
+
(request.password != null && typeof request.password !== 'string')) {
|
|
275
|
+
throw usageError('invalid_argument', 'joinEvent',
|
|
276
|
+
'The join request needs a non-empty join code and API key, and a password that is a string or null.');
|
|
277
|
+
}
|
|
278
|
+
logInfo(LiveVoiceLogEvent.joinRequest, { joinCode: request.joinCode, hasPassword: request.password != null });
|
|
279
|
+
const result = await invoke('join', { joinCode: request.joinCode, password: request.password, apiKey: request.apiKey });
|
|
280
|
+
if (!object(result) || typeof result.entryId !== 'string' || !result.entryId.trim()) {
|
|
281
|
+
logDecodeError('reply', 'join');
|
|
282
|
+
throw new LiveVoiceError('generic', { detail: { reason: 'invalid_join_receipt' } });
|
|
283
|
+
}
|
|
284
|
+
const { entryId } = result;
|
|
285
|
+
// When joinEvent() resolves, the values already show the joined event (18 item 110).
|
|
286
|
+
await awaitRevisions(result.revisions);
|
|
287
|
+
let leaving: Promise<void> | undefined;
|
|
288
|
+
return Object.freeze({
|
|
289
|
+
// When leave() resolves, the values already show what it left behind, as after a join.
|
|
290
|
+
leave: () => leaving ??= invoke('leave', { entryId }).then(
|
|
291
|
+
reply => awaitRevisions(object(reply) ? reply.revisions : undefined),
|
|
292
|
+
error => {
|
|
293
|
+
leaving = undefined;
|
|
294
|
+
throw error;
|
|
295
|
+
},
|
|
296
|
+
),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function channelId(id: number, operation: string): number {
|
|
301
|
+
if (!Number.isSafeInteger(id) || id <= 0) {
|
|
302
|
+
throw usageError('invalid_argument', operation, 'A channel id is a positive safe integer.');
|
|
303
|
+
}
|
|
304
|
+
return id;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** A retry as the cache keeps it: the time in milliseconds, so every delivery gets its own `Date`. */
|
|
308
|
+
export type RetryValue = { kind: 'inProgress' } | { kind: 'scheduled'; at: number };
|
|
309
|
+
|
|
310
|
+
/** The connection as the cache keeps it, with the retry time in milliseconds. */
|
|
311
|
+
export type ConnectionValue =
|
|
312
|
+
| { status: 'idle' | 'joining' | 'leaving' }
|
|
313
|
+
| { status: 'ready'; eventId: number }
|
|
314
|
+
| { status: 'reconnecting'; eventId: number; retry: RetryValue }
|
|
315
|
+
| { status: 'failed'; error: LiveVoiceError };
|
|
316
|
+
|
|
317
|
+
/** The wire's retry, or `undefined` when it is malformed. */
|
|
318
|
+
export function decodeRetry(wire: unknown): RetryValue | undefined {
|
|
319
|
+
if (object(wire) && wire.kind === 'inProgress') return { kind: 'inProgress' };
|
|
320
|
+
if (object(wire) && wire.kind === 'scheduled' && typeof wire.at === 'number' && Number.isSafeInteger(wire.at)) {
|
|
321
|
+
return { kind: 'scheduled', at: wire.at };
|
|
322
|
+
}
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** A public retry, with a `Date` of its own. */
|
|
327
|
+
export function publicRetry(retry: RetryValue): LiveVoiceRetry {
|
|
328
|
+
return retry.kind === 'scheduled' ? { kind: 'scheduled', at: new Date(retry.at) } : { kind: 'inProgress' };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** The wire's error as a `LiveVoiceError`; anything else becomes `generic`. */
|
|
332
|
+
export function decodeError(wire: unknown): LiveVoiceError {
|
|
333
|
+
return wireError(wire);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** The wire's connection (bridge.md 2.3); throws for one it cannot read. */
|
|
337
|
+
export function decodeConnection(wire: unknown): ConnectionValue {
|
|
338
|
+
if (!object(wire)) throw new LiveVoiceError('generic');
|
|
339
|
+
switch (wire.status) {
|
|
340
|
+
case 'idle': case 'joining': case 'leaving':
|
|
341
|
+
return { status: wire.status };
|
|
342
|
+
case 'ready': case 'reconnecting': {
|
|
343
|
+
const eventId = wire.eventId;
|
|
344
|
+
if (typeof eventId !== 'number' || !Number.isSafeInteger(eventId) || eventId <= 0) throw new LiveVoiceError('generic');
|
|
345
|
+
if (wire.status === 'ready') return { status: 'ready', eventId };
|
|
346
|
+
const retry = decodeRetry(wire.retry);
|
|
347
|
+
if (retry === undefined) throw new LiveVoiceError('generic');
|
|
348
|
+
return { status: 'reconnecting', eventId, retry };
|
|
349
|
+
}
|
|
350
|
+
case 'failed':
|
|
351
|
+
return { status: 'failed', error: wireError(wire.error) };
|
|
352
|
+
default:
|
|
353
|
+
throw new LiveVoiceError('generic');
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* The public connection. A scheduled retry gets a `Date` of its own on every call, so changing one
|
|
359
|
+
* changes no other; every other connection is the cached value itself.
|
|
360
|
+
*/
|
|
361
|
+
export function publicConnection(value: ConnectionValue): LiveVoiceConnection {
|
|
362
|
+
if (value.status === 'reconnecting' && value.retry.kind === 'scheduled') {
|
|
363
|
+
return Object.freeze({ ...value, retry: Object.freeze(publicRetry(value.retry)) });
|
|
364
|
+
}
|
|
365
|
+
return value as LiveVoiceConnection;
|
|
366
|
+
}
|