1515import JavaScriptKit
1616import OpenCombine
1717
18- public extension JSPromise where Success : ConstructibleFromJSValue , Failure : JSError {
18+ public extension JSPromise {
1919 final class PromisePublisher : Publisher {
20- public typealias Output = Success
21-
22- /// Reference to a parent promise instance to prevent early deallocation
23- private var parent : JSPromise ?
24-
25- /// Reference to a `then` success callback promise instance to prevent early deallocation
26- private var then : JSPromise < JSValue , Failure > ?
20+ public typealias Output = JSValue
21+ public typealias Failure = JSValue
2722
2823 /// `Future` instance that handles subscriptions to this publisher.
29- private var future : Future < Success , Failure > ?
30-
31- fileprivate init ( parent: JSPromise ) {
32- future = . init { [ weak self] resolver in
33- let then = parent. then { value -> JSValue in
34- resolver ( . success( value) )
35- return . undefined
36- }
37-
38- then. catch {
24+ private var future : Future < JSValue , JSValue >
25+
26+ fileprivate init ( promise: JSPromise ) {
27+ future = . init { resolver in
28+ promise. then ( success: {
29+ resolver ( . success( $0) )
30+ return JSValue . undefined
31+ } , failure: {
3932 resolver ( . failure( $0) )
40- }
41- self ? . then = then
33+ return JSValue . undefined
34+ } )
4235 }
43- self . parent = parent
4436 }
4537
4638 public func receive< Downstream: Subscriber > ( subscriber: Downstream )
47- where Success == Downstream . Input , Failure == Downstream . Failure
39+ where Downstream . Input == JSValue , Downstream . Failure == JSValue
4840 {
49- guard let parent = parent, let then = then, let future = future else { return }
50-
51- future. receive ( subscriber: WrappingSubscriber ( inner: subscriber, parent: parent, then: then) )
41+ future. receive ( subscriber: WrappingSubscriber ( inner: subscriber) )
5242 }
5343 }
5444
5545 /// Creates a new publisher for this `JSPromise` instance.
5646 var publisher : PromisePublisher {
57- . init( parent : self )
47+ . init( promise : self )
5848 }
5949
6050 /** Helper type that wraps a given `inner` subscriber and holds references to both stored promises
@@ -66,8 +56,6 @@ public extension JSPromise where Success: ConstructibleFromJSValue, Failure: JSE
6656 typealias Failure = Inner . Failure
6757
6858 let inner : Inner
69- let parent : JSPromise
70- let then : JSPromise < JSValue , Failure >
7159
7260 var combineIdentifier : CombineIdentifier { inner. combineIdentifier }
7361
0 commit comments