Skip to content

Commit 3fc3416

Browse files
committed
Remove ClosureHandler, fix OptionalClosureHandler
1 parent 0977a93 commit 3fc3416

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

Sources/DOMKit/ECMAScript/Support.swift

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,20 @@ public class ReadableStream: JSBridgedClass {
4343
}
4444
}
4545

46-
@propertyWrapper public struct ClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {
46+
@propertyWrapper public final class OptionalClosureHandler<ArgumentType, ReturnType>
47+
where ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible {
4748

4849
let jsObject: JSObject
4950
let name: String
51+
var closure: JSClosure?
5052

5153
public init(jsObject: JSObject, name: String) {
5254
self.jsObject = jsObject
5355
self.name = name
5456
}
5557

56-
public var wrappedValue: (ArgumentType) -> ReturnType {
57-
get {
58-
{ arg in jsObject[name]!(arg).fromJSValue()! }
59-
}
60-
set {
61-
jsObject[name] = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }.jsValue()
62-
}
63-
}
64-
}
65-
66-
@propertyWrapper public struct OptionalClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {
67-
68-
let jsObject: JSObject
69-
let name: String
70-
71-
public init(jsObject: JSObject, name: String) {
72-
self.jsObject = jsObject
73-
self.name = name
58+
deinit {
59+
closure?.release()
7460
}
7561

7662
public var wrappedValue: ((ArgumentType) -> ReturnType)? {
@@ -81,8 +67,13 @@ public class ReadableStream: JSBridgedClass {
8167
return { function($0.jsValue()).fromJSValue()! }
8268
}
8369
set {
70+
if let closure = closure {
71+
closure.release()
72+
}
8473
if let newValue = newValue {
85-
jsObject[name] = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }.jsValue()
74+
let closure = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }
75+
jsObject[name] = closure.jsValue()
76+
self.closure = closure
8677
} else {
8778
jsObject[name] = .null
8879
}

Sources/DOMKit/WebIDL/MutationObserver.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ public class MutationObserver: JSBridgedClass {
99
public class var constructor: JSFunction { JSObject.global.MutationObserver.function! }
1010

1111
public let jsObject: JSObject
12+
var closure: JSClosure?
1213

1314
public required init(unsafelyWrapping jsObject: JSObject) {
1415
self.jsObject = jsObject
1516
}
1617

1718
public convenience init(callback: @escaping MutationCallback) {
18-
self.init(unsafelyWrapping: MutationObserver.constructor.new(JSClosure { callback($0[0].fromJSValue()!, $0[1].fromJSValue()!) }))
19+
let closure = JSClosure { callback($0[0].fromJSValue()!, $0[1].fromJSValue()!) }
20+
self.closure = closure
21+
self.init(unsafelyWrapping: MutationObserver.constructor.new(closure))
22+
}
23+
24+
deinit {
25+
closure?.release()
1926
}
2027

2128
public func observe(target: Node, options: MutationObserverInit = [:]) {

0 commit comments

Comments
 (0)