Peter 971273e8f8 Add 'submodules/SSignalKit/' from commit '359b2ee7c9f20f99f221f78e307369ef5ad0ece2'
git-subtree-dir: submodules/SSignalKit
git-subtree-mainline: 4459dc5b47e7db4ea1adb3a43a4324d1c2f9feab
git-subtree-split: 359b2ee7c9f20f99f221f78e307369ef5ad0ece2
2019-06-11 18:57:57 +01:00

40 lines
1.1 KiB
Swift

import Foundation
public final class ValuePipe<T> {
private let subscribers = Atomic(value: Bag<(T) -> Void>())
public init() {
}
public func signal() -> Signal<T, NoError> {
return Signal { [weak self] subscriber in
if let strongSelf = self {
let index = strongSelf.subscribers.with { value -> Bag<T>.Index in
return value.add { next in
subscriber.putNext(next)
}
}
return ActionDisposable { [weak strongSelf] in
if let strongSelf = strongSelf {
strongSelf.subscribers.with { value -> Void in
value.remove(index)
}
}
}
} else {
return EmptyDisposable
}
}
}
public func putNext(_ next: T) {
let items = self.subscribers.with { value -> [(T) -> Void] in
return value.copyItems()
}
for f in items {
f(next)
}
}
}