mirror of
https://github.com/ichmagmaus111/ghostgram.git
synced 2026-11-02 22:04:28 +01:00
Ghostgram for iOS, rebuilt from scratch on Telegram iOS 13.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.2 KiB
Swift
105 lines
3.2 KiB
Swift
import Foundation
|
|
|
|
let doNothing: () -> Void = { }
|
|
|
|
public enum NoValue {
|
|
}
|
|
|
|
public enum NoError {
|
|
}
|
|
|
|
public func identity<A>(a: A) -> A {
|
|
return a
|
|
}
|
|
|
|
precedencegroup PipeRight {
|
|
associativity: left
|
|
higherThan: DefaultPrecedence
|
|
}
|
|
|
|
infix operator |> : PipeRight
|
|
|
|
@inlinable
|
|
public func |> <T, U>(value: T, function: ((T) -> U)) -> U {
|
|
return function(value)
|
|
}
|
|
|
|
public final class Signal<T, E> {
|
|
private let generator: (Subscriber<T, E>) -> Disposable
|
|
|
|
public init(_ generator: @escaping(Subscriber<T, E>) -> Disposable) {
|
|
self.generator = generator
|
|
}
|
|
|
|
public func start(next: ((T) -> Void)! = nil, error: ((E) -> Void)! = nil, completed: (() -> Void)! = nil) -> Disposable {
|
|
let subscriber = Subscriber<T, E>(next: next, error: error, completed: completed)
|
|
let disposable = self.generator(subscriber)
|
|
let wrappedDisposable = subscriber.core.assignDisposable(disposable)
|
|
withExtendedLifetime(subscriber, {})
|
|
return wrappedDisposable
|
|
}
|
|
|
|
public func startStandalone(next: ((T) -> Void)! = nil, error: ((E) -> Void)! = nil, completed: (() -> Void)! = nil) -> Disposable {
|
|
let subscriber = Subscriber<T, E>(next: next, error: error, completed: completed)
|
|
let disposable = self.generator(subscriber)
|
|
let wrappedDisposable = subscriber.core.assignDisposable(disposable)
|
|
withExtendedLifetime(subscriber, {})
|
|
return wrappedDisposable
|
|
}
|
|
|
|
public func startStrict(next: ((T) -> Void)! = nil, error: ((E) -> Void)! = nil, completed: (() -> Void)! = nil, file: String = #file, line: Int = #line) -> Disposable {
|
|
let subscriber = Subscriber<T, E>(next: next, error: error, completed: completed)
|
|
let disposable = self.generator(subscriber)
|
|
let wrappedDisposable = subscriber.core.assignDisposable(disposable)
|
|
withExtendedLifetime(subscriber, {})
|
|
return wrappedDisposable.strict(file: file, line: line)
|
|
}
|
|
|
|
public static func single(_ value: T) -> Signal<T, E> {
|
|
return Signal<T, E> { subscriber in
|
|
subscriber.putNext(value)
|
|
subscriber.putCompletion()
|
|
|
|
return EmptyDisposable
|
|
}
|
|
}
|
|
|
|
public static func complete() -> Signal<T, E> {
|
|
return Signal<T, E> { subscriber in
|
|
subscriber.putCompletion()
|
|
|
|
return EmptyDisposable
|
|
}
|
|
}
|
|
|
|
public static func fail(_ error: E) -> Signal<T, E> {
|
|
return Signal<T, E> { subscriber in
|
|
subscriber.putError(error)
|
|
|
|
return EmptyDisposable
|
|
}
|
|
}
|
|
|
|
public static func never() -> Signal<T, E> {
|
|
return Signal<T, E> { _ in
|
|
return EmptyDisposable
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(iOS 13.0, macOS 10.15, *)
|
|
public extension Signal where E == NoError {
|
|
func get() async -> T {
|
|
let disposable = MetaDisposable()
|
|
return await withTaskCancellationHandler(operation: {
|
|
return await withCheckedContinuation { continuation in
|
|
disposable.set((self |> take(1)).startStandalone(next: { value in
|
|
continuation.resume(returning: value)
|
|
}))
|
|
}
|
|
}, onCancel: {
|
|
disposable.dispose()
|
|
})
|
|
}
|
|
}
|