Skip to content

Commit e4f4025

Browse files
committed
Updated JSConsole
1 parent e14b6ac commit e4f4025

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

Example/JavaScriptKitExample/Sources/JavaScriptKitExample/main.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ buttonElement.onclick = .function { _ in
1616
JSConsole.debug("\(#file) \(#function) \(#line)")
1717
alert("Swift is running on browser!")
1818
JSConsole.log("Requesting any Bluetooth Device...")
19-
bluetooth.requestDevice().then {
20-
JSConsole.info($0)
21-
JSConsole.debug("\(#file) \(#function) \(#line)")
22-
alert("Got device \($0)")
19+
bluetooth.requestDevice().then { (device: JSBluetoothDevice) -> (JSPromise<JSBluetoothRemoteGATTServer>) in
20+
JSConsole.info(device)
21+
JSConsole.debug("\(#file) \(#function) \(#line) \(device)")
22+
alert("Got device \(device)")
2323
JSConsole.log("Connecting to GATT Server...")
24-
$0.gatt.connect()
25-
}.then {
26-
JSConsole.info($0)
27-
JSConsole.debug("\(#file) \(#function) \(#line)")
24+
return device.gatt.connect()
25+
}.then { (server: JSBluetoothRemoteGATTServer) -> () in
26+
JSConsole.info(server)
27+
JSConsole.debug("\(#file) \(#function) \(#line) \(server)")
2828
alert("Connected")
29+
}.catch { (error: JSError) in
30+
alert("Error: \(error)")
2931
}
3032
JSConsole.debug("\(#file) \(#function) \(#line)")
3133
return .undefined

Sources/JavaScriptKit/JS Types/JSConsole.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,36 @@ public final class JSConsole: JSType {
2222
/**
2323
The Console method `log()` outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.
2424
*/
25-
public static func log(_ arguments: JSValueConvertible...) {
26-
logFunction.dynamicallyCall(withArguments: arguments)
25+
public static func log(_ arguments: Any...) {
26+
logFunction.dynamicallyCall(withArguments: arguments.map(print))
2727
}
2828

2929
/**
3030
The `console.info()` method outputs an informational message to the Web Console. In Firefox, a small "i" icon is displayed next to these items in the Web Console's log.
3131
*/
32-
public static func info(_ arguments: JSValueConvertible...) {
33-
infoFunction.dynamicallyCall(withArguments: arguments)
32+
public static func info(_ arguments: Any...) {
33+
infoFunction.dynamicallyCall(withArguments: arguments.map(print))
3434
}
3535

3636
/**
3737
The console method `debug()` outputs a message to the web console at the "debug" log level. The message is only displayed to the user if the console is configured to display debug output.
3838
*/
39-
public static func debug(_ arguments: JSValueConvertible...) {
40-
debugFunction.dynamicallyCall(withArguments: arguments)
39+
public static func debug(_ arguments: Any...) {
40+
debugFunction.dynamicallyCall(withArguments: arguments.map(print))
4141
}
4242

4343
/**
4444
Outputs an error message to the Web Console.
4545
*/
46-
public static func error(_ arguments: JSValueConvertible...) {
47-
errorFunction.dynamicallyCall(withArguments: arguments)
46+
public static func error(_ arguments: Any...) {
47+
errorFunction.dynamicallyCall(withArguments: arguments.map(print))
4848
}
4949

5050
/**
5151
The `console.assert()` method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
5252
*/
5353
public static func assert(_ condition: @autoclosure () -> (Bool), _ arguments: JSValueConvertible...) {
54-
assertFunction.dynamicallyCall(withArguments: [condition()] + arguments)
54+
assertFunction.dynamicallyCall(withArguments: [condition()] + arguments.map(print))
5555
}
5656
}
5757

@@ -69,16 +69,19 @@ internal extension JSConsole {
6969

7070
static let assertFunction = classObject.assert.function!
7171
}
72-
/*
72+
7373
private extension JSConsole {
7474

7575
/// Console should print objects as their description and not
76-
static func print(_ value: JSValueConvertible) -> JSValueConvertible {
77-
if let value = value as? CustomStringConvertible {
76+
static func print(_ value: Any) -> JSValueConvertible {
77+
if let value = value as? JSType {
78+
return value
79+
} else if let value = value as? JSValueConvertible {
80+
return value
81+
} else if let value = value as? CustomStringConvertible {
7882
return value.description
7983
} else {
80-
return value
84+
return "\(value)"
8185
}
8286
}
8387
}
84-
*/

0 commit comments

Comments
 (0)