-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallback.swift
More file actions
49 lines (42 loc) · 1.16 KB
/
Callback.swift
File metadata and controls
49 lines (42 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Callback.swift
// RCTAgora
//
// Created by LXH on 2020/4/9.
// Copyright © 2020 Syan. All rights reserved.
//
import Foundation
import AgoraRtcKit
@objc
protocol Callback: class {
func success(_ data: Any?)
func failure(_ code: String, _ message: String)
}
extension Callback {
func code(_ code: Int32?, _ runnable: ((Int32?) -> Any?)? = nil) {
if code == nil || code! < 0 {
let newCode = abs(Int(code ?? Int32(AgoraErrorCode.notInitialized.rawValue)))
failure(String(newCode), AgoraRtcEngineKit.getErrorDescription(newCode) ?? "")
return
}
let res = runnable?(code)
if res is Void {
success(nil)
} else {
success(res)
}
}
func resolve<T>(_ source: T?, _ runnable: (T) -> Any?) {
guard let `source` = source else {
let code = AgoraErrorCode.notInitialized.rawValue
failure(String(code), AgoraRtcEngineKit.getErrorDescription(code) ?? "")
return
}
let res = runnable(source)
if res is Void {
success(nil)
} else {
success(res)
}
}
}