Skip to content

Commit 1f42ace

Browse files
committed
chore: simplification
1 parent ad7acf4 commit 1f42ace

2 files changed

Lines changed: 53 additions & 56 deletions

File tree

ios/LKAudioProcessingManager.m

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,22 @@ - (void)clearProcessors {
6161
// TODO
6262
}
6363

64+
- (BOOL)requireAudioDeviceModule:(NSError * _Nullable * _Nullable)error {
65+
if (self.audioDeviceModule != nil) {
66+
return YES;
67+
}
68+
if (error != nil) {
69+
*error = [NSError errorWithDomain:LKAudioProcessingManagerErrorDomain
70+
code:-1
71+
userInfo:@{
72+
NSLocalizedDescriptionKey : @"Audio device module is unavailable",
73+
}];
74+
}
75+
return NO;
76+
}
77+
6478
- (BOOL)startLocalRecording:(NSError * _Nullable * _Nullable)error {
65-
if (self.audioDeviceModule == nil) {
66-
if (error != nil) {
67-
*error = [NSError errorWithDomain:LKAudioProcessingManagerErrorDomain
68-
code:-1
69-
userInfo:@{
70-
NSLocalizedDescriptionKey : @"Audio device module is unavailable",
71-
}];
72-
}
79+
if (![self requireAudioDeviceModule:error]) {
7380
return NO;
7481
}
7582

@@ -97,14 +104,7 @@ - (BOOL)startLocalRecording:(NSError * _Nullable * _Nullable)error {
97104
}
98105

99106
- (BOOL)stopLocalRecording:(NSError * _Nullable * _Nullable)error {
100-
if (self.audioDeviceModule == nil) {
101-
if (error != nil) {
102-
*error = [NSError errorWithDomain:LKAudioProcessingManagerErrorDomain
103-
code:-1
104-
userInfo:@{
105-
NSLocalizedDescriptionKey : @"Audio device module is unavailable",
106-
}];
107-
}
107+
if (![self requireAudioDeviceModule:error]) {
108108
return NO;
109109
}
110110

ios/LiveKitReactNativeModule.swift

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public class LivekitReactNativeModule: RCTEventEmitter {
104104
}
105105
}
106106

107+
private static let kModuleErrorDomain = "LivekitReactNativeModule"
108+
107109
private func syncAudioDeviceModule() -> Bool {
108110
guard let webRTCModule = self.bridge.module(for: WebRTCModule.self) as? WebRTCModule else {
109111
return false
@@ -114,44 +116,45 @@ public class LivekitReactNativeModule: RCTEventEmitter {
114116
return true
115117
}
116118

119+
private func sendRecordingStateEvent(_ stage: String, error: Error? = nil) {
120+
var body: [String: Any] = [
121+
"stage": stage,
122+
"timestampMs": Int(Date().timeIntervalSince1970 * 1000),
123+
]
124+
if let error = error {
125+
body["error"] = error.localizedDescription
126+
}
127+
self.sendEvent(withName: LKEvents.kEventAudioRecordingState, body: body)
128+
}
129+
130+
private func bridgeUnavailableError(context: String) -> NSError {
131+
NSError(
132+
domain: Self.kModuleErrorDomain,
133+
code: -1,
134+
userInfo: [
135+
NSLocalizedDescriptionKey:
136+
"WebRTCModule is unavailable while \(context) local recording",
137+
]
138+
)
139+
}
140+
117141
@objc(startLocalRecording:withRejecter:)
118142
public func startLocalRecording(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
119-
self.sendEvent(withName: LKEvents.kEventAudioRecordingState, body: [
120-
"stage": "local_recording_start_requested",
121-
"timestampMs": Int(Date().timeIntervalSince1970 * 1000),
122-
])
143+
sendRecordingStateEvent("local_recording_start_requested")
123144

124145
guard syncAudioDeviceModule() else {
125-
let bridgeError = NSError(
126-
domain: "LivekitReactNativeModule",
127-
code: -1,
128-
userInfo: [
129-
NSLocalizedDescriptionKey:
130-
"WebRTCModule is unavailable while starting local recording",
131-
]
132-
)
133-
self.sendEvent(withName: LKEvents.kEventAudioRecordingState, body: [
134-
"stage": "local_recording_start_failed",
135-
"timestampMs": Int(Date().timeIntervalSince1970 * 1000),
136-
"error": bridgeError.localizedDescription,
137-
])
138-
reject("startLocalRecording", bridgeError.localizedDescription, bridgeError)
146+
let err = bridgeUnavailableError(context: "starting")
147+
sendRecordingStateEvent("local_recording_start_failed", error: err)
148+
reject("startLocalRecording", err.localizedDescription, err)
139149
return
140150
}
141151

142152
do {
143153
try LKAudioProcessingManager.sharedInstance().startLocalRecording()
144-
self.sendEvent(withName: LKEvents.kEventAudioRecordingState, body: [
145-
"stage": "local_recording_started",
146-
"timestampMs": Int(Date().timeIntervalSince1970 * 1000),
147-
])
154+
sendRecordingStateEvent("local_recording_started")
148155
resolve(nil)
149156
} catch {
150-
self.sendEvent(withName: LKEvents.kEventAudioRecordingState, body: [
151-
"stage": "local_recording_start_failed",
152-
"timestampMs": Int(Date().timeIntervalSince1970 * 1000),
153-
"error": error.localizedDescription,
154-
])
157+
sendRecordingStateEvent("local_recording_start_failed", error: error)
155158
reject(
156159
"startLocalRecording",
157160
"Error starting local recording: \(error.localizedDescription)",
@@ -162,27 +165,21 @@ public class LivekitReactNativeModule: RCTEventEmitter {
162165

163166
@objc(stopLocalRecording:withRejecter:)
164167
public func stopLocalRecording(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
168+
sendRecordingStateEvent("local_recording_stop_requested")
169+
165170
guard syncAudioDeviceModule() else {
166-
let bridgeError = NSError(
167-
domain: "LivekitReactNativeModule",
168-
code: -1,
169-
userInfo: [
170-
NSLocalizedDescriptionKey:
171-
"WebRTCModule is unavailable while stopping local recording",
172-
]
173-
)
174-
reject("stopLocalRecording", bridgeError.localizedDescription, bridgeError)
171+
let err = bridgeUnavailableError(context: "stopping")
172+
sendRecordingStateEvent("local_recording_stop_failed", error: err)
173+
reject("stopLocalRecording", err.localizedDescription, err)
175174
return
176175
}
177176

178177
do {
179178
try LKAudioProcessingManager.sharedInstance().stopLocalRecording()
180-
self.sendEvent(withName: LKEvents.kEventAudioRecordingState, body: [
181-
"stage": "local_recording_stopped",
182-
"timestampMs": Int(Date().timeIntervalSince1970 * 1000),
183-
])
179+
sendRecordingStateEvent("local_recording_stopped")
184180
resolve(nil)
185181
} catch {
182+
sendRecordingStateEvent("local_recording_stop_failed", error: error)
186183
reject(
187184
"stopLocalRecording",
188185
"Error stopping local recording: \(error.localizedDescription)",

0 commit comments

Comments
 (0)