Skip to content

Commit

Permalink
change: do not cancel return of then from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Apr 17, 2024
1 parent c85286b commit 5861fec
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 107 deletions.
22 changes: 10 additions & 12 deletions Source/Concurrency/Contract/Chain/ContractFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension Contract {

subscribe(
queue: queue,
onResolved: schedule { [weak self] value, finish in
onResolved: schedule { value, finish in
let promise = block(value)
promise.subscribe(
queue: queue,
Expand All @@ -77,11 +77,10 @@ extension Contract {
contract.resolve(result)
},
onRejected: { _ in },
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
onCanceled: { [weak contract] in
defer { finish() }
contract?.cancel()
}
)
},
onRejected: { error in contract.reject(error) },
Expand Down Expand Up @@ -109,7 +108,7 @@ extension Contract {

subscribe(
queue: queue,
onResolved: schedule { [weak self] value, finish in
onResolved: schedule { value, finish in
guard let promise = block(value) else { return }
promise.subscribe(
queue: queue,
Expand All @@ -118,11 +117,10 @@ extension Contract {
contract.resolve($0)
},
onRejected: { _ in },
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
onCanceled: { [weak contract] in
defer { finish() }
contract?.cancel()
}
)
},
onRejected: { error in contract.reject(error) },
Expand Down
22 changes: 10 additions & 12 deletions Source/Concurrency/Contract/Chain/ContractRecover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension Contract {
subscribe(
queue: queue,
onResolved: { value in contract.resolve(value) },
onRejected: schedule { [weak self] error, finish in
onRejected: schedule { error, finish in
do {
let promise = try block(error)
promise.subscribe(
Expand All @@ -69,11 +69,10 @@ extension Contract {
defer { finish() }
contract.reject($0)
},
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
onCanceled: { [weak contract] in
defer { finish() }
contract?.cancel()
}
)
}
catch let error {
Expand Down Expand Up @@ -132,7 +131,7 @@ extension Contract {
subscribe(
queue: queue,
onResolved: { value in contract.resolve(value) },
onRejected: schedule { [weak self] error, finish in
onRejected: schedule { error, finish in
let promise = block(error)
promise.subscribe(
queue: queue,
Expand All @@ -144,11 +143,10 @@ extension Contract {
defer { finish() }
contract.reject($0)
},
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
onCanceled: { [weak contract] in
defer { finish() }
contract?.cancel()
}
)
},
onCanceled: { [weak contract] in contract?.cancel() }
Expand Down
22 changes: 10 additions & 12 deletions Source/Concurrency/Contract/Chain/ContractThen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extension Contract {

subscribe(
queue: queue,
onResolved: schedule { [weak self] value, finish in
onResolved: schedule { value, finish in
do {
let promise = try block(value)
promise.subscribe(
Expand All @@ -68,11 +68,10 @@ extension Contract {
defer { finish() }
contract.reject($0)
},
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
onCanceled: { [weak contract] in
defer { finish() }
contract?.cancel()
}
)
}
catch let error {
Expand Down Expand Up @@ -131,7 +130,7 @@ extension Contract where Failure == Never {

subscribe(
queue: queue,
onResolved: schedule { [weak self] value, finish in
onResolved: schedule { value, finish in
let promise = block(value)
promise.subscribe(
queue: queue,
Expand All @@ -143,11 +142,10 @@ extension Contract where Failure == Never {
defer { finish() }
contract.reject($0)
},
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
onCanceled: { [weak contract] in
defer { finish() }
contract?.cancel()
}
)
},
onRejected: { _ in },
Expand Down
18 changes: 6 additions & 12 deletions Source/Concurrency/Promise/Chain/PromiseRecover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ extension Promise {
do {
let value = try block($0)
promiseReturn.resolve(value)
} catch let error {
}
catch let error {
promiseReturn.reject(error)
}
},
Expand All @@ -46,7 +47,7 @@ extension Promise {
subscribe(
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { [weak self] in
onRejected: {
do {
let promise = try block($0)
promise.subscribe(
Expand All @@ -55,11 +56,8 @@ extension Promise {
onRejected: { promiseReturn.reject($0) },
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
)
} catch let error {
}
catch let error {
promiseReturn.reject(error)
}
},
Expand Down Expand Up @@ -105,18 +103,14 @@ extension Promise {
subscribe(
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { [weak self] in
onRejected: {
let promise = block($0)
promise.subscribe(
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
)
},
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)
Expand Down
18 changes: 6 additions & 12 deletions Source/Concurrency/Promise/Chain/PromiseThen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ extension Promise {
do {
let value = try block($0)
promiseReturn.resolve(value)
} catch let error {
}
catch let error {
promiseReturn.reject(error)
}
},
Expand All @@ -45,7 +46,7 @@ extension Promise {

subscribe(
queue: queue,
onResolved: { [weak self] in
onResolved: {
do {
let promise = try block($0)
promise.subscribe(
Expand All @@ -54,11 +55,8 @@ extension Promise {
onRejected: { promiseReturn.reject($0) },
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
)
} catch let error {
}
catch let error {
promiseReturn.reject(error)
}
},
Expand Down Expand Up @@ -104,18 +102,14 @@ extension Promise where Failure == Never {

subscribe(
queue: queue,
onResolved: { [weak self] in
onResolved: {
let promise = block($0)
promise.subscribe(
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { [weak promise] in promise?.cancel() }
)
},
onRejected: { _ in },
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
Expand Down
45 changes: 9 additions & 36 deletions Source/Concurrency/Promise/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ public final class Promise<Value, Failure: Error> {
let queue: DispatchQueue
let pendingGroup: DispatchGroup

var cancelSubscribers: [PromiseCancelSubscriber]

init(queue: DispatchQueue = .global()) {
self.state = Atomic(.pending)

self.queue = queue
self.pendingGroup = DispatchGroup()

self.cancelSubscribers = []

pendingGroup.enter()
}

Expand Down Expand Up @@ -241,16 +237,8 @@ extension Promise {

func cancel() {
state.mutate { state in
if case .canceled = state {} else {
if case .pending = state {
pendingGroup.leave()
}
for cancelSubscriber in self.cancelSubscribers {
pendingGroup.notify(queue: queue) {
cancelSubscriber.onCanceled()
}
}
cancelSubscribers = []
if case .pending = state {
pendingGroup.leave()
return .canceled
}

Expand All @@ -274,18 +262,8 @@ extension Promise {
else if case .rejected(let error) = state {
onRejected(error)
}
}
state.capture { state in
if case .canceled = state {
pendingGroup.notify(queue: queue) {
onCanceled()
}
}
else {
cancelSubscribers.append(PromiseCancelSubscriber(
queue: queue,
onCanceled: onCanceled
))
else if case .canceled = state {
onCanceled()
}
}
}
Expand All @@ -294,17 +272,12 @@ extension Promise {
queue: DispatchQueue,
onCanceled: @escaping () -> Void
) {
state.capture { state in
let state = self.state
pendingGroup.notify(queue: queue) {
let state = state.capture { $0 }

if case .canceled = state {
pendingGroup.notify(queue: queue) {
onCanceled()
}
}
else {
cancelSubscribers.append(PromiseCancelSubscriber(
queue: queue,
onCanceled: onCanceled
))
onCanceled()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Test/Concurrency/Contract/Chain/ContractFilterTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class ContractFilterTest: XCTestCase {
contract0.resolve("10")
}
PromiseTest.expect(semaphore: end, timeout: .seconds(1))
PromiseTest.expect(promise: filterPromise, state: .canceled, timeout: .seconds(1))
PromiseTest.expect(promise: filterPromise, state: .pending, timeout: .seconds(1))
}

func test__never_filter_bool() {
Expand Down Expand Up @@ -142,7 +142,7 @@ final class ContractFilterTest: XCTestCase {
contract0.resolve("10")
}
PromiseTest.expect(semaphore: end, timeout: .seconds(1))
PromiseTest.expect(promise: filterPromise, state: .canceled, timeout: .seconds(1))
PromiseTest.expect(promise: filterPromise, state: .pending, timeout: .seconds(1))
}

func test__filter_schedule_sync() throws {
Expand Down
2 changes: 1 addition & 1 deletion Test/Concurrency/Contract/Chain/ContractRecoverTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ final class ContractRecoverTest: XCTestCase {
contract0.reject(ContractTest.SampleError.one)
}
PromiseTest.expect(semaphore: end, timeout: .seconds(1))
PromiseTest.expect(promise: recoverPromise, state: .canceled, timeout: .seconds(1))
PromiseTest.expect(promise: recoverPromise, state: .pending, timeout: .seconds(1))
}

func test__recover_schedule_sync() throws {
Expand Down
2 changes: 1 addition & 1 deletion Test/Concurrency/Contract/Chain/ContractThenTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ final class ContractThenTest: XCTestCase {
contract0.resolve(10)
}
PromiseTest.expect(semaphore: end, timeout: .seconds(1))
PromiseTest.expect(promise: thenPromise, state: .canceled, timeout: .seconds(1))
PromiseTest.expect(promise: thenPromise, state: .pending, timeout: .seconds(1))
}

func test__never_then_return_value() {
Expand Down
2 changes: 1 addition & 1 deletion Test/Concurrency/Promise/Attribute/PromiseDelayTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class PromiseDelayTest: XCTestCase {
}

PromiseTest.expect(semaphore: end, timeout: .seconds(1))
PromiseTest.expect(promise: promise2, state: .canceled, timeout: .seconds(1))
PromiseTest.expect(promise: promise2, state: .resolved({ $0 == () }), timeout: .seconds(1))
}

func test__never_delay_create() {
Expand Down
2 changes: 1 addition & 1 deletion Test/Concurrency/Promise/Chain/PromiseCatchTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ final class PromiseCatchTest: XCTestCase {
}

PromiseTest.expect(semaphore: end, timeout: .seconds(1))
PromiseTest.expect(promise: promise1, state: .canceled, timeout: .seconds(1))
PromiseTest.expect(promise: promise1, state: .rejected(PromiseTest.SampleError.one), timeout: .seconds(1))
}
}
2 changes: 1 addition & 1 deletion Test/Concurrency/Promise/Chain/PromiseFinallyTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ final class PromiseFinallyTest: XCTestCase {
}

PromiseTest.expect(semaphore: end, timeout: .seconds(1))
PromiseTest.expect(promise: promise1, state: .canceled, timeout: .seconds(1))
PromiseTest.expect(promise: promise1, state: .rejected(PromiseTest.SampleError.one), timeout: .seconds(1))
}
}
Loading

0 comments on commit 5861fec

Please sign in to comment.