Skip to content

Commit 3f47ae9

Browse files
committed
Added JSConsole
1 parent b75da55 commit 3f47ae9

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

Example/JavaScriptKitExample/Sources/JavaScriptKitExample/main.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ _ = body.appendChild!(divElement)
1212
let buttonElement = document.createElement!("button").object!
1313
buttonElement.innerText = "Click me!"
1414
buttonElement.onclick = .function { _ in
15-
JSObjectRef.global.console.object?.log.function?("\(#file) \(#function) \(#line)")
15+
JSConsole.debug("\(#file) \(#function) \(#line)")
1616
alert("Swift is running on browser!")
17+
JSConsole.log("Requesting device")
1718
bluetooth.requestDevice().then {
18-
JSObjectRef.global.console.object?.log.function?("\($0)")
19+
JSConsole.info("\($0)")
1920
alert("Got device \($0)")
2021
}
21-
JSObjectRef.global.console.object?.log.function?("\(#file) \(#function) \(#line)")
22+
JSConsole.debug("\(#file) \(#function) \(#line)")
2223
return .undefined
2324
}
2425

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// JSConsole.swift
3+
//
4+
//
5+
// Created by Alsey Coleman Miller on 6/3/20.
6+
//
7+
8+
public final class JSConsole: JSType {
9+
10+
// MARK: - Properties
11+
12+
public let jsObject: JSObjectRef
13+
14+
// MARK: - Initialization
15+
16+
public init(_ jsObject: JSObjectRef) {
17+
self.jsObject = jsObject
18+
}
19+
20+
// MARK: - Methods
21+
22+
/**
23+
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.
24+
*/
25+
public static func log(_ message: String) {
26+
logFunction(message)
27+
}
28+
29+
/**
30+
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.
31+
*/
32+
public static func info(_ message: String) {
33+
infoFunction(message)
34+
}
35+
36+
/**
37+
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.
38+
*/
39+
public static func debug(_ message: String) {
40+
debugFunction(message)
41+
}
42+
43+
/**
44+
Outputs an error message to the Web Console.
45+
*/
46+
public static func error(_ message: String) {
47+
errorFunction(message)
48+
}
49+
50+
/**
51+
The `console.assert()` method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
52+
*/
53+
public static func assert(_ condition: @autoclosure () -> (Bool), _ message: String) {
54+
assertFunction(condition(), message)
55+
}
56+
}
57+
58+
internal extension JSConsole {
59+
60+
static let classObject = JSObjectRef.global.console.object!
61+
62+
static let logFunction = classObject.log.function!
63+
64+
static let infoFunction = classObject.info.function!
65+
66+
static let debugFunction = classObject.debug.function!
67+
68+
static let errorFunction = classObject.error.function!
69+
70+
static let assertFunction = classObject.assert.function!
71+
}

0 commit comments

Comments
 (0)