@@ -43,34 +43,20 @@ public class ReadableStream: JSBridgedClass {
43
43
}
44
44
}
45
45
46
- @propertyWrapper public struct ClosureHandler < ArgumentType: JSValueCompatible , ReturnType: JSValueCompatible > {
46
+ @propertyWrapper public final class OptionalClosureHandler < ArgumentType, ReturnType>
47
+ where ArgumentType: JSValueCompatible , ReturnType: JSValueCompatible {
47
48
48
49
let jsObject : JSObject
49
50
let name : String
51
+ var closure : JSClosure ?
50
52
51
53
public init ( jsObject: JSObject , name: String ) {
52
54
self . jsObject = jsObject
53
55
self . name = name
54
56
}
55
57
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 ( )
74
60
}
75
61
76
62
public var wrappedValue : ( ( ArgumentType ) -> ReturnType ) ? {
@@ -81,8 +67,13 @@ public class ReadableStream: JSBridgedClass {
81
67
return { function ( $0. jsValue ( ) ) . fromJSValue ( ) ! }
82
68
}
83
69
set {
70
+ if let closure = closure {
71
+ closure. release ( )
72
+ }
84
73
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
86
77
} else {
87
78
jsObject [ name] = . null
88
79
}
0 commit comments