1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| protocol Disposable { func dispose() }
final class AnonymouseDisposable: Disposable { private let _disposeHandler: () -> Void init(_ disposeHandler: @escaping () -> Void) { _disposeHandler = disposeHandler } func dispose() { _disposeHandler() } }
class CompositeDisposable: Disposable { private(set) var isDisposed: Bool = false private var disposables: [Disposable] = [] init() {} func add(disposable: Disposable) { if isDisposed { disposable.dispose() return } disposables.append(disposable) } func dispose() { guard !isDisposed else { return } disposables.forEach { disposable in disposable.dispose() } isDisposed = true } }
|