-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add JSBridgedType and JSBridgedClass #26
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
Changes from 16 commits
b123600
e6b6705
5982bef
a5ce5e4
0f79f43
fbf9f76
e31a989
e4d657e
c07db3d
be1f916
ba3316e
5fe4b96
b62b78e
8b181c7
de5f52e
479ad20
b4edc19
cabda58
d0da61a
e19788a
ff1f4ac
c74e3d1
ed9f8d4
29a30e7
4429d88
767d05d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/// Use this protocol when your type has no single JavaScript class. | ||
/// For example, a union type of multiple classes or primitive values. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this comment give an example of such type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Would it be OK to link to DOMKit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please do 👍 |
||
public protocol JSBridgedType: JSValueCodable, CustomStringConvertible { | ||
/// This is the value your class wraps. | ||
var value: JSValue { get } | ||
|
||
/// If your class is incompatible with the provided value, return `nil`. | ||
init?(from value: JSValue) | ||
} | ||
|
||
extension JSBridgedType { | ||
public static func construct(from value: JSValue) -> Self? { | ||
return Self.init(from: value) | ||
} | ||
|
||
public func jsValue() -> JSValue { value } | ||
|
||
public var description: String { value.description } | ||
} | ||
|
||
/// Conform to this protocol when your Swift class wraps a JavaScript class. | ||
public protocol JSBridgedClass: JSBridgedType { | ||
/// The constructor function for the JavaScript class | ||
static var constructor: JSFunction { get } | ||
|
||
/// The JavaScript object wrapped by this instance. | ||
/// You may assume that `jsObject instanceof Self.constructor` | ||
j-f1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var jsObject: JSObject { get } | ||
|
||
/// Create an instannce wrapping the given JavaScript object. | ||
/// You may assume that `jsObject instanceof Self.constructor` | ||
init(withCompatibleObject jsObject: JSObject) | ||
j-f1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
extension JSBridgedClass { | ||
public var value: JSValue { jsObject.jsValue() } | ||
public init?(from value: JSValue) { | ||
guard let object = value.object, object.isInstanceOf(Self.constructor) else { return nil } | ||
self.init(withCompatibleObject: object) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.