mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 14:45:21 +00:00
Add 'submodules/SSignalKit/' from commit '359b2ee7c9f20f99f221f78e307369ef5ad0ece2'
git-subtree-dir: submodules/SSignalKit git-subtree-mainline:4459dc5b47git-subtree-split:359b2ee7c9
This commit is contained in:
73
submodules/SSignalKit/SwiftSignalKit/Signal_Take.swift
Normal file
73
submodules/SSignalKit/SwiftSignalKit/Signal_Take.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
import Foundation
|
||||
|
||||
public func take<T, E>(_ count: Int) -> (Signal<T, E>) -> Signal<T, E> {
|
||||
return { signal in
|
||||
return Signal { subscriber in
|
||||
let counter = Atomic(value: 0)
|
||||
return signal.start(next: { next in
|
||||
var passthrough = false
|
||||
var complete = false
|
||||
let _ = counter.modify { value in
|
||||
let updatedCount = value + 1
|
||||
passthrough = updatedCount <= count
|
||||
complete = updatedCount == count
|
||||
return updatedCount
|
||||
}
|
||||
if passthrough {
|
||||
subscriber.putNext(next)
|
||||
}
|
||||
if complete {
|
||||
subscriber.putCompletion()
|
||||
}
|
||||
}, error: { error in
|
||||
subscriber.putError(error)
|
||||
}, completed: {
|
||||
subscriber.putCompletion()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct SignalTakeAction {
|
||||
public let passthrough: Bool
|
||||
public let complete: Bool
|
||||
|
||||
public init(passthrough: Bool, complete: Bool) {
|
||||
self.passthrough = passthrough
|
||||
self.complete = complete
|
||||
}
|
||||
}
|
||||
|
||||
public func take<T, E>(until: @escaping (T) -> SignalTakeAction) -> (Signal<T, E>) -> Signal<T, E> {
|
||||
return { signal in
|
||||
return Signal { subscriber in
|
||||
return signal.start(next: { next in
|
||||
let action = until(next)
|
||||
if action.passthrough {
|
||||
subscriber.putNext(next)
|
||||
}
|
||||
if action.complete {
|
||||
subscriber.putCompletion()
|
||||
}
|
||||
}, error: { error in
|
||||
subscriber.putError(error)
|
||||
}, completed: {
|
||||
subscriber.putCompletion()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func last<T, E>(signal: Signal<T, E>) -> Signal<T?, E> {
|
||||
return Signal { subscriber in
|
||||
let value = Atomic<T?>(value: nil)
|
||||
return signal.start(next: { next in
|
||||
let _ = value.swap(next)
|
||||
}, error: { error in
|
||||
subscriber.putError(error)
|
||||
}, completed: {
|
||||
subscriber.putNext(value.with({ $0 }))
|
||||
subscriber.putCompletion()
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user