Within the following code I’m making an attempt to create a customized topic that may emit solely even numbers. For some purpose It isn’t emitting any values. I used to be utilizing CurrentValueSubject earlier than however that was selecting the final worth. I need to emit any worth so long as it’s even quantity.

class EvenSubject<Failure: Error>: Topic {
    
    typealias Output = Int
    
    non-public let wrapped: PassthroughSubject<Int, Failure>
    
    init(initialValue: Int) {
        self.wrapped = PassthroughSubject()
        let evenInitialValue = EvenSubject.isEven(worth: initialValue) ? initialValue: 0
        ship(evenInitialValue)
    }
    
    non-public static func isEven(worth: Int) -> Bool {
        worth % 2 == 0
    }
    
    func ship(_ worth: Output) {
        print("ship")
        
        if worth % 2 == 0 {
            wrapped.ship(worth)
        }
        
    }
    
    func ship(completion: Subscribers.Completion<Failure>) {
        print("ship completion")
        wrapped.ship(completion: completion)
    }
    
    func ship(subscription: Subscription) {
        print("ship subscription")
        wrapped.ship(subscription: subscription)
    }
    
    func obtain<S>(subscriber: S) the place S : Subscriber, Failure == S.Failure, Output == S.Enter {
        wrapped.obtain(subscriber: subscriber)
    }
    
}


let topic = EvenSubject<By no means>(initialValue: 4)

topic.ship(10)
topic.ship(7)
topic.ship(14)


let cancellable = topic.sink { _ in
    
} receiveValue: { worth in
    print(worth)
}

0