You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
import Foundation |
|
import RxSwift |
|
import ObjectiveC |
|
|
|
fileprivate var disposeBagContext: UInt8 = 0 |
|
|
|
/// each HasDisposeBag offers a unique RxSwift DisposeBag instance |
|
public protocol HasDisposeBag: class { |
|
|
|
/// a unique RxSwift DisposeBag instance |
|
var disposeBag: DisposeBag { get set } |
|
} |
|
|
|
extension HasDisposeBag { |
|
|
|
func synchronizedBag<T>( _ action: () -> T) -> T { |
|
objc_sync_enter(self) |
|
let result = action() |
|
objc_sync_exit(self) |
|
return result |
|
} |
|
|
|
public var disposeBag: DisposeBag { |
|
get { |
|
return synchronizedBag { |
|
if let disposeObject = objc_getAssociatedObject(self, &disposeBagContext) as? DisposeBag { |
|
return disposeObject |
|
} |
|
let disposeObject = DisposeBag() |
|
objc_setAssociatedObject(self, &disposeBagContext, disposeObject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
|
return disposeObject |
|
} |
|
} |
|
|
|
set { |
|
synchronizedBag { |
|
objc_setAssociatedObject(self, &disposeBagContext, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|