Skip to content

Commit

Permalink
feat!: make getArg into an Event method
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Jun 15, 2024
1 parent 647d82b commit 00366f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
18 changes: 9 additions & 9 deletions Examples/CallSwiftFromJS/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,34 @@ let doc = """
</html>
"""

func handleStr(_ e: Event) {
let str1: String = try! getArg(e)
let str2: String = try! getArg(e, 1)
func handleStr(e: Event) {
let str1: String = try! e.getArg()
let str2: String = try! e.getArg(1)

print("handleStr 1: \(str1)") // Hello
print("handleStr 2: \(str2)") // World
}

func handleInt(e: Event) {
let num1: Int = try! getArg(e)
let num2: Int = try! getArg(e, 1)
let num3: Int = try! getArg(e, 2)
let num1: Int = try! e.getArg()
let num2: Int = try! e.getArg(1)
let num3: Int = try! e.getArg(2)

print("handleInt 1: \(num1)") // 123
print("handleInt 2: \(num2)") // 456
print("handleInt 3: \(num3)") // 789
}

func handleBool(e: Event) {
let status1: Bool = try! getArg(e)
let status2: Bool = try! getArg(e, 1)
let status1: Bool = try! e.getArg()
let status2: Bool = try! e.getArg(1)

print("handleBool 1: \(status1)") // true
print("handleBool 2: \(status2)") // false
}

func handleResp(e: Event) {
let count: Int = try! getArg(e)
let count: Int = try! e.getArg()
response(e, count * 2)
}

Expand Down
48 changes: 24 additions & 24 deletions Sources/SwiftWebUI/SwiftWebUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ public struct Event {
self.element = String(cString: element!)
self.bindId = bindId
}

/// Gets an argument passed from JavaScript.
/// - Parameters:
/// - event: The event object.
/// - idx: The argument position starting from 0.
public func getArg<T>(_ idx: Int = 0) throws -> T {
var cEvent = cStruct
let arg_count = webui_get_count(&cEvent)
if idx >= arg_count {
throw WebUIError.runtimeError("error: argument index out of range (index: \(idx), argument count: \(arg_count))")
}
if T.self == String.self {
let str = webui_get_string_at(&cEvent, idx)!
return String(cString: str) as! T
} else if T.self == Int.self {
return Int(webui_get_int_at(&cEvent, idx)) as! T
} else if T.self == Bool.self {
return webui_get_bool_at(&cEvent, idx) as! T
} else if T.self == Double.self {
return webui_get_float_at(&cEvent, idx) as! T
}
// TODO: automatically decode other types.
throw WebUIError.runtimeError("error: failed to get argument at index `\(idx)`")
}
}

/// Creates a new window object.
Expand Down Expand Up @@ -138,30 +162,6 @@ public func clean() {
webui_clean()
}

/// Gets an argument passed from JavaScript.
/// - Parameters:
/// - event: The event object.
/// - idx: The argument position starting from 0.
public func getArg<T>(_ event: Event, _ idx: Int = 0) throws -> T {
var cEvent = event.cStruct
let arg_count = webui_get_count(&cEvent)
if idx >= arg_count {
throw WebUIError.runtimeError("error: argument index out of range (index: \(idx), argument count: \(arg_count))")
}
if T.self == String.self {
let str = webui_get_string_at(&cEvent, idx)!
return String(cString: str) as! T
} else if T.self == Int.self {
return Int(webui_get_int_at(&cEvent, idx)) as! T
} else if T.self == Bool.self {
return webui_get_bool_at(&cEvent, idx) as! T
} else if T.self == Double.self {
return webui_get_float_at(&cEvent, idx) as! T
}
// TODO: automatically decode other types.
throw WebUIError.runtimeError("error: failed to get argument at index `\(idx)`")
}

/// Returns a response to JavaScript.
/// - Parameters:
/// - event: The event object.
Expand Down

0 comments on commit 00366f1

Please sign in to comment.