-
-
Notifications
You must be signed in to change notification settings - Fork 55
Added wrapper types and Codable support #8
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for big efforts 👍
But it's too big changes to review at once. Please split into several PRs to ease review 🙏
And please add test cases for these changes.
@@ -7,10 +7,16 @@ let package = Package( | |||
products: [ | |||
.library(name: "JavaScriptKit", targets: ["JavaScriptKit"]) | |||
], | |||
dependencies: [ | |||
.package(url: "https://github.com/PureSwift/SwiftFoundation.git", .branch("develop")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to keep this library independent from Foundation at this time because this library is the basis of any Swift web app.
@@ -0,0 +1,27 @@ | |||
// swift-tools-version:5.2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This library doesn't support Swift 5.2 compatible toolchain. 🤔
func assert(_ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Wrapped { | ||
switch self { | ||
case .none: | ||
fatalError(message(), file: file, line: line) | ||
case let .some(value): | ||
return value | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name is assert
but it uses fatalError
. Should we use fatalError instead?
internal extension Sequence where Element == CodingKey { | ||
|
||
/// KVC path string for current coding path. | ||
var path: String { | ||
return reduce("", { $0 + "\($0.isEmpty ? "" : ".")" + $1.stringValue }) | ||
} | ||
} | ||
|
||
internal extension CodingKey { | ||
|
||
static var sanitizedName: String { | ||
|
||
let rawName = String(reflecting: self) | ||
var elements = rawName.split(separator: ".") | ||
guard elements.count > 2 | ||
else { return rawName } | ||
elements.removeFirst() | ||
elements.removeAll { $0.hasPrefix("(unknown context") } | ||
return elements.reduce("", { $0 + ($0.isEmpty ? "" : ".") + $1 }) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These helper methods are only used in Encoder implementation, so I think it's better to move them to the JSEncoder.swift and make them fileprivate.
// Created by Alsey Coleman Miller on 6/4/20. | ||
// | ||
|
||
import SwiftFoundation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please allow users to choose whether to use SwiftFoundation or Foundation.
You can use canImport
feature like this. https://github.com/kateinoigakukun/CanImportMagic/blob/master/Packages/LibraryX/Sources/LibraryX/LibraryX.swift
public var state: JSPromiseState { | ||
guard let value = jsObject.state.string.flatMap({ State(rawValue: $0) }) | ||
else { fatalError("Invalid state: \(jsObject.state)") } | ||
return value | ||
} | ||
|
||
/// Promise result value. | ||
public var result: JSValue { | ||
return jsObject.result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are they valid properties? I couldn't find these definitions in spec.
|
||
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future. | ||
*/ | ||
public final class JSPromise<Success>: JSType where Success: JSValueConvertible, Success: JSValueConstructible { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think generic parameter Success
doesn't match JavaScript's Promise use.
JavaScript's Promise allows users to return any type of values, so the requirement of single static type in JSPromise is too hard constraint.
func toString() -> String? { | ||
return jsObject.toString.function?.apply(this: jsObject).string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func toString() -> String? { | |
return jsObject.toString.function?.apply(this: jsObject).string | |
func toString() -> String { | |
return jsObject.toString.function.assert().apply(this: jsObject).string |
} | ||
} | ||
|
||
extension Array: JSValueConvertible where Element == JSValueConvertible { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep this extension for Element == JSValueConvertible to be available for existential
@@ -192,3 +198,15 @@ extension Array where Element: JSValueConvertible { | |||
Swift.Array<JSValueConvertible>.withRawJSValues(self)(body) | |||
} | |||
} | |||
|
|||
extension JSValueConvertible where Self: Encodable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These kinds of internal implementation shouldn't output log without explicit user configuration and I think JSValueConvertiable shouldn't be failable.
JSType
)JSEncoder
for Codable supportJSPromise
)console.log
and assertions