Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with JavaScriptKit 0.7 #281

Merged
merged 5 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wasm-5.3-SNAPSHOT-2020-07-27-a
wasm-5.3-SNAPSHOT-2020-09-25-a
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"pins": [
{
"package": "JavaScriptKit",
"repositoryURL": "https://github.com/kateinoigakukun/JavaScriptKit.git",
"repositoryURL": "https://github.com/swiftwasm/JavaScriptKit.git",
"state": {
"branch": null,
"revision": "c90e82fe1d576a2ccd1aae798380bf80be7885fb",
"version": "0.5.0"
"revision": "6e84a7003071ed3e9fa7f8d8266a272a36b84608",
"version": "0.7.2"
}
},
{
Expand Down
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/kateinoigakukun/JavaScriptKit.git", from: "0.5.0"),
.package(
url: "https://github.com/swiftwasm/JavaScriptKit.git",
.upToNextMinor(from: "0.7.2")
),
.package(url: "https://github.com/MaxDesiatov/Runtime.git", from: "2.1.2"),
.package(url: "https://github.com/MaxDesiatov/OpenCombine.git", from: "0.0.1"),
],
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ app.
## Requirements for app developers

- macOS 10.15 and Xcode 11.4 or later.
- [Swift 5.2 or later](https://swift.org/download/) for Linux.
- [Swift 5.2 or later](https://swift.org/download/) and Ubuntu 18.04 if you'd like to use Linux.
Other Linux distributions are currently not supported.

## Requirements for app users

Expand All @@ -134,7 +135,7 @@ app by following these steps:
brew install swiftwasm/tap/carton
```

If you had `carton` installed before this, make sure you have version 0.5.0 or greater:
If you had `carton` installed before this, make sure you have version 0.6.0 or greater:

```
carton --version
Expand Down
2 changes: 2 additions & 0 deletions Sources/TokamakCore/Shapes/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#endif

/// The outline of a 2D shape.
Expand Down
10 changes: 4 additions & 6 deletions Sources/TokamakCore/Stubs/CGStubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import Glibc
#elseif canImport(Darwin)
import Darwin
#elseif os(WASI)
import WASILibc
#endif

public typealias CGFloat = Double
Expand Down Expand Up @@ -230,19 +232,15 @@ public struct CGAffineTransform: Equatable {
/// - Parameters:
/// - tx: The value by which to move x values with the affine transform.
/// - ty: The value by which to move y values with the affine transform.
public func translatedBy(x tx: CGFloat,
y ty: CGFloat) -> Self
{
public func translatedBy(x tx: CGFloat, y ty: CGFloat) -> Self {
.init(a: a, b: b, c: c, d: d, tx: self.tx + tx, ty: self.ty + ty)
}

/// Returns an affine transformation matrix constructed by scaling an existing affine transform.
/// - Parameters:
/// - sx: The value by which to scale x values of the affine transform.
/// - sy: The value by which to scale y values of the affine transform.
public func scaledBy(x sx: CGFloat,
y sy: CGFloat) -> Self
{
public func scaledBy(x sx: CGFloat, y sy: CGFloat) -> Self {
.init(a: a + sx, b: b, c: c, d: d + sy, tx: tx, ty: ty)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakDOM/App/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension App {
public static func _launch(
_ app: Self,
_ rootEnvironment: EnvironmentValues,
_ body: JSObjectRef
_ body: JSObject
) {
if body.style.object!.all == "" {
body.style = "margin: 0;"
Expand Down
3 changes: 1 addition & 2 deletions Sources/TokamakDOM/App/ColorSchemeObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ enum ColorSchemeObserver {
private static var closure: JSClosure?
private static var cancellable: AnyCancellable?

static func observe(_ rootElement: JSObjectRef) {
static func observe(_ rootElement: JSObject) {
let closure = JSClosure {
publisher.value = .init(matchMediaDarkScheme: $0[0].object!)
return .undefined
}
_ = matchMediaDarkScheme.addListener!(closure)
Self.closure = closure
Expand Down
3 changes: 1 addition & 2 deletions Sources/TokamakDOM/App/ScenePhaseObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ enum ScenePhaseObserver {
private static var closure: JSClosure?

static func observe() {
let closure = JSClosure { _ in
let closure = JSClosure { _ -> () in
let visibilityState = document.visibilityState.string
if visibilityState == "visible" {
publisher.send(.active)
} else if visibilityState == "hidden" {
publisher.send(.background)
}
return .undefined
}
_ = document.addEventListener!("visibilitychange", closure)
Self.closure = closure
Expand Down
8 changes: 4 additions & 4 deletions Sources/TokamakDOM/DOMNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ extension AnyHTML {
}

final class DOMNode: Target {
let ref: JSObjectRef
let ref: JSObject
private var listeners: [String: JSClosure]
var view: AnyView

init<V: View>(_ view: V, _ ref: JSObjectRef, _ listeners: [String: Listener] = [:]) {
init<V: View>(_ view: V, _ ref: JSObject, _ listeners: [String: Listener] = [:]) {
self.ref = ref
self.listeners = [:]
self.view = AnyView(view)
reinstall(listeners)
}

init(_ ref: JSObjectRef) {
init(_ ref: JSObject) {
self.ref = ref
view = AnyView(EmptyView())
listeners = [:]
Expand All @@ -64,13 +64,13 @@ final class DOMNode: Target {
func reinstall(_ listeners: [String: Listener]) {
for (event, jsClosure) in self.listeners {
_ = ref.removeEventListener!(event, jsClosure)
jsClosure.release()
}
self.listeners = [:]

for (event, listener) in listeners {
let jsClosure = JSClosure {
listener($0[0].object!)
return .undefined
}
_ = ref.addEventListener!(event, jsClosure)
self.listeners[event] = jsClosure
Expand Down
4 changes: 2 additions & 2 deletions Sources/TokamakDOM/DOMRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ extension View {
/** Allows capturing DOM references of host views. The resulting reference is written
to a given `binding`.
*/
public func _domRef(_ binding: Binding<JSObjectRef?>) -> some View {
// Convert `Binding<JSObjectRef?>` to `Binding<DOMNode?>` first.
public func _domRef(_ binding: Binding<JSObject?>) -> some View {
// Convert `Binding<JSObject?>` to `Binding<DOMNode?>` first.
let targetBinding = Binding(
get: { binding.wrappedValue.map(DOMNode.init) },
set: { binding.wrappedValue = $0?.ref }
Expand Down
25 changes: 10 additions & 15 deletions Sources/TokamakDOM/DOMRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,15 @@ private extension AnyView {
}
}

let global = JSObjectRef.global
let global = JSObject.global
let window = global.window.object!
let matchMediaDarkScheme = window.matchMedia!("(prefers-color-scheme: dark)").object!
let log = global.console.object!.log.function!
let document = global.document.object!
let body = document.body.object!
let head = document.head.object!

private let timeoutScheduler = { (closure: @escaping () -> ()) in
let fn = JSClosure { _ in
closure()
return .undefined
}
_ = JSObjectRef.global.setTimeout!(fn, 0)
}

func appendRootStyle(_ rootNode: JSObjectRef) {
func appendRootStyle(_ rootNode: JSObject) {
rootNode.style = .string(rootNodeStyles)
let rootStyle = document.createElement!("style").object!
rootStyle.innerHTML = .string(tokamakStyles)
Expand All @@ -81,19 +73,22 @@ func appendRootStyle(_ rootNode: JSObjectRef) {
final class DOMRenderer: Renderer {
private(set) var reconciler: StackReconciler<DOMRenderer>?

private let rootRef: JSObjectRef
private let rootRef: JSObject

private let scheduler: JSScheduler

init<A: App>(_ app: A, _ ref: JSObjectRef, _ rootEnvironment: EnvironmentValues? = nil) {
init<A: App>(_ app: A, _ ref: JSObject, _ rootEnvironment: EnvironmentValues? = nil) {
rootRef = ref
appendRootStyle(ref)

let scheduler = JSScheduler()
self.scheduler = scheduler
reconciler = StackReconciler(
app: app,
target: DOMNode(ref),
environment: .defaultEnvironment,
renderer: self,
scheduler: timeoutScheduler
)
renderer: self
) { scheduler.schedule(options: nil, $0) }
}

public func mountTarget(to parent: DOMNode, with host: MountedHost) -> DOMNode? {
Expand Down
Loading