/** State: State type of the system. Request: Subset of state used to control the feedback loop. For every uniquely identifiable request `effects` closure is invoked with the initial value of the request and future requests corresponding to the same identifier. Subsequent equal values of request are not emitted from the effects state parameter. - parameter requests: Requests to perform some effects. - parameter effects: The request effects. - parameter initial: Initial request. - parameter state: Latest request state. - returns: The feedback loop performing the effects. */ publicfuncreact<State, Request: Equatable, RequestID, Event>( requests: @escaping (State) -> [RequestID: Request], effects: @escaping (_ initial: Request, _ state: Observable<Request>) -> Observable<Event> ) -> (ObservableSchedulerContext<State>) -> Observable<Event> { return { stateContext in Observable.create { observer in let state =RequestLifetimeTracking<Request, RequestID, Event>( effects: effects, scheduler: stateContext.scheduler, observer: observer )
let subscription = stateContext.source .map(requests) .subscribe { event in switch event { case .next(let requests): state.forwardRequests(requests) case .error(let error): observer.on(.error(error)) case .completed: observer.on(.completed) } }
/** Bi-directional binding of a system State to external state machine and events from it. Strongify owner. */ publicfuncbind<State, Event, WeakOwner>(_owner: WeakOwner, _bindings: @escaping (WeakOwner, ObservableSchedulerContext<State>) -> (Bindings<Event>)) -> (ObservableSchedulerContext<State>) -> Observable<Event> whereWeakOwner: AnyObject { return bind(bindingsStrongify(owner, bindings)) }