import Combine protocol OptionalType { associatedtype Wrapped var optional: Wrapped? { get } } extension Optional: OptionalType { public var optional: Wrapped? { self } } extension Publisher where Output: OptionalType { func ignoreNil() -> AnyPublisher { flatMap { output -> AnyPublisher in guard let output = output.optional else { return Empty(completeImmediately: false).eraseToAnyPublisher() } return Just(output).setFailureType(to: Failure.self).eraseToAnyPublisher() } .eraseToAnyPublisher() } } extension Publisher { func combineWithPrevious() -> AnyPublisher<(Output, Output), Failure> { zip(dropFirst()).eraseToAnyPublisher() } } extension Publisher { func cancellable() -> some Cancellable { sink { _ in } receiveValue: { _ in } } } extension Publisher where Failure == Never { func cancellable() -> some Cancellable { sink { _ in } } } typealias Lifetime = Set extension Publisher where Failure == Never { func weakAssign( to keyPath: ReferenceWritableKeyPath, on object: T ) -> AnyCancellable { sink { [weak object] value in object?[keyPath: keyPath] = value } } }