Skip to content

Commit 4850942

Browse files
committed
Simplify
1 parent ebfb0c0 commit 4850942

11 files changed

+92
-752
lines changed

Sources/NTLBridge/JSInboundMessage.swift

Lines changed: 0 additions & 47 deletions
This file was deleted.

Sources/NTLBridge/JSMethodHandlerContainer.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import Foundation
44
public typealias JSMethodHandler = (JSONValue) throws -> JSONValue?
55

66
/// JavaScript异步方法处理器类型别名
7-
public typealias JSAsyncMethodHandler = (JSONValue, @escaping (Result<JSONValue?, Error>) -> Void) -> Void
7+
public typealias JSAsyncMethodHandler = (JSONValue, @escaping (Result<JSONValue?, Error>) -> Void) throws -> Void
88

99
/// JavaScript方法处理器存储结构
10-
internal struct JSMethodHandlerContainer {
10+
struct JSMethodHandlerContainer {
1111
let handler: JSMethodHandler
1212
let asyncHandler: JSAsyncMethodHandler?
1313
weak var target: AnyObject?
1414
private let hasTarget: Bool
1515
let isAsync: Bool
16-
16+
1717
init(handler: @escaping JSMethodHandler, target: AnyObject? = nil) {
1818
self.handler = handler
1919
self.asyncHandler = nil
2020
self.target = target
2121
self.hasTarget = target != nil
2222
self.isAsync = false
2323
}
24-
24+
2525
/// 每次调用只能回调一次。
2626
init(asyncHandler: @escaping JSAsyncMethodHandler, target: AnyObject? = nil) {
2727
self.handler = { _ in throw NSError(domain: "NTLBridge", code: -1, userInfo: [NSLocalizedDescriptionKey: "Async handler used in sync context"]) }
@@ -30,11 +30,11 @@ internal struct JSMethodHandlerContainer {
3030
self.hasTarget = target != nil
3131
self.isAsync = true
3232
}
33-
33+
3434
/// 检查目标对象是否仍然有效(对于实例方法)
3535
var isValid: Bool {
3636
// 如果初始化时没有目标对象(静态方法),始终有效
3737
// 如果有目标对象,检查弱引用是否仍然指向有效对象
3838
hasTarget ? target != nil : true
3939
}
40-
}
40+
}

Sources/NTLBridge/JSStructuredError.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

Sources/NTLBridge/NTLBridgeUtil.swift

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import Foundation
22

3+
func jsStructuredError(jsonValue: JSONValue) -> NSError? {
4+
guard case let .dictionary(dictionary) = jsonValue,
5+
let name = dictionary["name"]?.stringValue,
6+
let message = dictionary["message"]?.stringValue,
7+
let stack = dictionary["stack"]?.stringValue
8+
else {
9+
return nil
10+
}
11+
let description = "name: \(name)\nmessage: \(message)\nstack: \(stack)"
12+
let userInfo: [String: Any] = [
13+
NSLocalizedDescriptionKey: description,
14+
]
15+
return NSError(domain: "NTLBridge", code: -1, userInfo: userInfo)
16+
}
17+
318
/// Bridge错误类型
419
public enum NTLBridgeError: Error, LocalizedError {
520
case invalidValue
@@ -9,15 +24,14 @@ public enum NTLBridgeError: Error, LocalizedError {
924
switch self {
1025
case .invalidValue:
1126
return "Invalid value for conversion"
12-
case .typeConversionFailed(let error):
27+
case let .typeConversionFailed(error):
1328
return "Type conversion failed: \(error.localizedDescription)"
1429
}
1530
}
1631
}
1732

1833
/// Bridge工具类,提供JSON序列化/反序列化功能
19-
public final class NTLBridgeUtil {
20-
34+
public enum NTLBridgeUtil {
2135
// MARK: - JSON Encoder/Decoder
2236

2337
/// 创建JSONEncoder实例,可被子类重写
@@ -76,46 +90,8 @@ public final class NTLBridgeUtil {
7690
return JSONValue(any: any) ?? .null
7791
}
7892

79-
/// 将JSONValue转换为Any类型
80-
/// - Parameter value: 要转换的JSONValue
81-
/// - Returns: 转换后的Any值
82-
public static func anyValue(from value: JSONValue) -> Any? {
83-
return value.rawValue
84-
}
85-
86-
// MARK: - Array Conversion
87-
88-
/// 将参数数组转换为JSONValue数组
89-
/// - Parameter args: 参数数组
90-
/// - Returns: JSONValue数组
91-
public static func jsonArguments(from args: [Any?]) -> [JSONValue] {
92-
return args.map { jsonValue(from: $0) }
93-
}
94-
95-
/// 将JSONValue数组转换为Any数组
96-
/// - Parameter values: JSONValue数组
97-
/// - Returns: Any数组
98-
public static func anyArguments(from values: [JSONValue]) -> [Any?] {
99-
return values.map { anyValue(from: $0) }
100-
}
101-
10293
// MARK: - Message Parsing
10394

104-
/// 解析来自JavaScript的消息
105-
/// - Parameter message: 消息字符串
106-
/// - Returns: 解析后的JSInboundMessage,解析失败返回nil
107-
public static func parseInboundMessage(_ message: String) -> JSInboundMessage? {
108-
guard let data = message.data(using: .utf8) else {
109-
return nil
110-
}
111-
112-
do {
113-
return try decoder.decode(JSInboundMessage.self, from: data)
114-
} catch {
115-
return nil
116-
}
117-
}
118-
11995
/// 编码发往JavaScript的调用信息
12096
/// - Parameter callInfo: 调用信息
12197
/// - Returns: 编码后的JSON字符串,编码失败返回nil
@@ -130,20 +106,6 @@ public final class NTLBridgeUtil {
130106

131107
// MARK: - Type Conversion
132108

133-
/// 将JSONValue转换为指定类型
134-
/// - Parameter value: 要转换的JSONValue
135-
/// - Returns: 转换后的指定类型,转换失败返回nil
136-
public static func convertValue<T: Decodable>(_ value: JSONValue?) -> T? {
137-
guard let value = value else { return nil }
138-
139-
do {
140-
let data = try encoder.encode(value)
141-
return try decoder.decode(T.self, from: data)
142-
} catch {
143-
return nil
144-
}
145-
}
146-
147109
/// 将JSONValue转换为指定类型,抛出错误
148110
/// - Parameter value: 要转换的JSONValue
149111
/// - Returns: 转换后的指定类型
@@ -165,8 +127,8 @@ public final class NTLBridgeUtil {
165127
/// - Parameter methodName: 方法名
166128
/// - Returns: 是否有效
167129
public static func isValidMethodName(_ methodName: String) -> Bool {
168-
return !methodName.isEmpty &&
169-
!methodName.contains(" ") &&
170-
!methodName.hasPrefix("_")
130+
return !methodName.isEmpty &&
131+
!methodName.contains(" ") &&
132+
!methodName.hasPrefix("_")
171133
}
172134
}

Sources/NTLBridge/NTLWebView+CallHandler.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Foundation
22

3-
extension NTLWebView {
3+
public extension NTLWebView {
44
/// 调用 js bridge 方法,支持直接传入 Codable 参数数组
5-
public func callHandler<T: Encodable>(
5+
func callHandler<T: Encodable>(
66
_ method: String,
77
arguments: [T],
88
completion: ((Result<JSONValue?, Error>) -> Void)? = nil
@@ -16,7 +16,7 @@ extension NTLWebView {
1616
}
1717

1818
/// 调用 js bridge 方法(无参数版本)
19-
public func callHandler(
19+
func callHandler(
2020
_ method: String,
2121
completion: ((Result<JSONValue?, Error>) -> Void)? = nil
2222
) {
@@ -29,7 +29,7 @@ extension NTLWebView {
2929
}
3030

3131
/// 调用 js bridge 方法,支持直接传入任意类型参数数组
32-
public func callHandler(
32+
func callHandler(
3333
_ method: String,
3434
arguments: [Any],
3535
completion: ((Result<JSONValue?, Error>) -> Void)? = nil
@@ -43,7 +43,7 @@ extension NTLWebView {
4343
}
4444

4545
/// 调用 js bridge 方法,支持直接传入 Codable 参数数组并返回指定类型
46-
public func callTypedHandler<T: Encodable, U: Decodable>(
46+
func callTypedHandler<T: Encodable, U: Decodable>(
4747
_ method: String,
4848
arguments: [T],
4949
expecting type: U.Type,
@@ -65,7 +65,7 @@ extension NTLWebView {
6565
}
6666

6767
/// 调用 js bridge 方法并返回指定类型(无参数版本)
68-
public func callTypedHandler<U: Decodable>(
68+
func callTypedHandler<U: Decodable>(
6969
_ method: String,
7070
expecting type: U.Type,
7171
completion: @escaping (Result<U, Error>) -> Void

0 commit comments

Comments
 (0)