Skip to content

Commit

Permalink
fix: solve memory issue of cancelBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Apr 16, 2024
1 parent 951f594 commit 980726b
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 345 deletions.
4 changes: 2 additions & 2 deletions Source/Concurrency/Contract/Chain/ContractCatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Contract {
contract.reject(error)
}
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand All @@ -51,7 +51,7 @@ extension Contract {
block(error)
contract.reject(error)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down
16 changes: 8 additions & 8 deletions Source/Concurrency/Contract/Chain/ContractFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension Contract {
if block(value) { contract.resolve(value) }
},
onRejected: { error in contract.reject(error) },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand All @@ -43,7 +43,7 @@ extension Contract {
contract.resolve(result)
},
onRejected: { error in contract.reject(error) },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down Expand Up @@ -77,15 +77,15 @@ extension Contract {
contract.resolve(result)
},
onRejected: { _ in },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
},
onRejected: { error in contract.reject(error) },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down Expand Up @@ -118,15 +118,15 @@ extension Contract {
contract.resolve($0)
},
onRejected: { _ in },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
},
onRejected: { error in contract.reject(error) },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down
2 changes: 1 addition & 1 deletion Source/Concurrency/Contract/Chain/ContractFinally.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension Contract {
block()
contract.reject(error)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down
16 changes: 8 additions & 8 deletions Source/Concurrency/Contract/Chain/ContractRecover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Contract {
contract.reject(error)
}
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down Expand Up @@ -69,19 +69,19 @@ extension Contract {
defer { finish() }
contract.reject($0)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
}
catch let error {
defer { finish() }
contract.reject(error)
}
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand All @@ -105,7 +105,7 @@ extension Contract {
let result = block(error)
contract.resolve(result)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down Expand Up @@ -144,14 +144,14 @@ extension Contract {
defer { finish() }
contract.reject($0)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down
16 changes: 8 additions & 8 deletions Source/Concurrency/Contract/Chain/ContractThen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Contract {
}
},
onRejected: { error in contract.reject(error) },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down Expand Up @@ -68,11 +68,11 @@ extension Contract {
defer { finish() }
contract.reject($0)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
}
catch let error {
Expand All @@ -81,7 +81,7 @@ extension Contract {
}
},
onRejected: { error in contract.reject(error) },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand All @@ -105,7 +105,7 @@ extension Contract where Failure == Never {
contract.resolve(result)
},
onRejected: { _ in },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down Expand Up @@ -143,15 +143,15 @@ extension Contract where Failure == Never {
defer { finish() }
contract.reject($0)
},
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
},
onRejected: { _ in },
onCanceled: { contract.cancel() }
onCanceled: { [weak contract] in contract?.cancel() }
)

return contract
Expand Down
14 changes: 7 additions & 7 deletions Source/Concurrency/Promise/Attribute/PromiseConvert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Promise {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -44,7 +44,7 @@ extension Promise {
queue: queue,
onResolved: { _ in promiseReturn.resolve(()) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -57,7 +57,7 @@ extension Promise {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -72,7 +72,7 @@ extension Promise where Failure == Never {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { _ in },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -85,7 +85,7 @@ extension Promise where Failure == Never {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { _ in },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -98,7 +98,7 @@ extension Promise where Failure == Never {
queue: queue,
onResolved: { _ in promiseReturn.resolve(()) },
onRejected: { _ in },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -111,7 +111,7 @@ extension Promise where Failure == Never {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { _ in },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand Down
2 changes: 1 addition & 1 deletion Source/Concurrency/Promise/Attribute/PromiseDelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension Promise {
}
},
onRejected: { promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand Down
2 changes: 1 addition & 1 deletion Source/Concurrency/Promise/Attribute/PromiseTimeout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension Promise {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

queue.asyncAfter(deadline: .now() + interval) {
Expand Down
4 changes: 2 additions & 2 deletions Source/Concurrency/Promise/Chain/PromiseCatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Promise {
promiseReturn.reject(error)
}
},
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -48,7 +48,7 @@ extension Promise {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { block($0); promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand Down
2 changes: 1 addition & 1 deletion Source/Concurrency/Promise/Chain/PromiseFinally.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension Promise {
queue: queue,
onResolved: { block(); promiseReturn.resolve($0) },
onRejected: { block(); promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand Down
16 changes: 8 additions & 8 deletions Source/Concurrency/Promise/Chain/PromiseRecover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension Promise {
promiseReturn.reject(error)
}
},
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -53,17 +53,17 @@ extension Promise {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
} catch let error {
promiseReturn.reject(error)
}
},
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -87,7 +87,7 @@ extension Promise {
let value = block($0)
promiseReturn.resolve(value)
},
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand All @@ -111,14 +111,14 @@ extension Promise {
queue: queue,
onResolved: { promiseReturn.resolve($0) },
onRejected: { promiseReturn.reject($0) },
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)
self?.subscribe(
queue: queue,
onCanceled: { promise.cancel() }
onCanceled: { [weak promise] in promise?.cancel() }
)
},
onCanceled: { promiseReturn.cancel() }
onCanceled: { [weak promiseReturn] in promiseReturn?.cancel() }
)

return promiseReturn
Expand Down
Loading

0 comments on commit 980726b

Please sign in to comment.