I am building an aggregator class that connects multiple observables and emits a single sequence of emissions from these observables:
5 inputs from sources go in, one input comes out from ReplaySubject
.
Is there a way to manage subscriptions within this aggregator to allow one of the sources to “disconnect” without breaking the whole chain?
Would keeping a dictionary of [Observable: Subscription
] help to be able to cancel a specific subscription?
It sounds like you might benefit from having something like a Store object (reproduced below.)
Your store is bound to your input sources. (as in myInput.bind(to: myStore).disposed(by: bag)
) and the outputs are bound to the store's state. (as in myStore.state.bind(to: myOutput).disposed(by: bag)
)
The inputs can disconnect by completing and the outputs can disconnect by disposing the associated disposable.
The reducer's job is to convert an input into an output.
//
// Store.swift
//
// Created by Daniel Tartaglia on 3/11/17.
// Copyright © 2017 Haneke Design. MIT License
//
import Foundation
import RxSwift
class Store<State, Action> {
init(initialState: State, reducer: @escaping (State, Action) -> State) {
state = actions
.scan(initialState, accumulator: reducer)
.startWith(initialState)
.share(replay: 1)
}
let state: Observable<State>
private let actions = PublishSubject<Action>()
}
extension Store: ObserverType {
typealias E = Action
func on(_ event: Event<E>) {
actions.on(event)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.