mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.8 KiB
Swift
64 lines
1.8 KiB
Swift
5 years ago
|
//
|
||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import PromiseKit
|
||
|
|
||
|
@objc
|
||
|
public extension AnyPromise {
|
||
|
/**
|
||
|
* Sometimes there isn't a straight forward candidate to retain a promise, in that case we tell the
|
||
|
* promise to self retain, until it completes to avoid the risk it's GC'd before completion.
|
||
|
*/
|
||
|
@objc
|
||
|
func retainUntilComplete() {
|
||
|
var retainCycle: AnyPromise? = self
|
||
|
_ = self.ensure {
|
||
|
assert(retainCycle != nil)
|
||
|
retainCycle = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public extension PMKFinalizer {
|
||
|
/**
|
||
|
* Sometimes there isn't a straight forward candidate to retain a promise, in that case we tell the
|
||
|
* promise to self retain, until it completes to avoid the risk it's GC'd before completion.
|
||
|
*/
|
||
|
func retainUntilComplete() {
|
||
|
var retainCycle: PMKFinalizer? = self
|
||
|
_ = self.finally {
|
||
|
assert(retainCycle != nil)
|
||
|
retainCycle = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public extension Promise {
|
||
|
/**
|
||
|
* Sometimes there isn't a straight forward candidate to retain a promise, in that case we tell the
|
||
|
* promise to self retain, until it completes to avoid the risk it's GC'd before completion.
|
||
|
*/
|
||
|
func retainUntilComplete() {
|
||
|
var retainCycle: Promise<T>? = self
|
||
|
_ = self.ensure {
|
||
|
assert(retainCycle != nil)
|
||
|
retainCycle = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public extension Guarantee {
|
||
|
/**
|
||
|
* Sometimes there isn't a straight forward candidate to retain a promise, in that case we tell the
|
||
|
* promise to self retain, until it completes to avoid the risk it's GC'd before completion.
|
||
|
*/
|
||
|
func retainUntilComplete() {
|
||
|
var retainCycle: Guarantee<T>? = self
|
||
|
_ = self.done { _ in
|
||
|
assert(retainCycle != nil)
|
||
|
retainCycle = nil
|
||
|
}
|
||
|
}
|
||
|
}
|