// Path: lib/src/main/java/com/yheriatovych/reductor/Cancelable.java
// public interface Cancelable {
//     void cancel();
// }
// 
// Path: lib/src/main/java/com/yheriatovych/reductor/Cursor.java
// public interface Cursor<State> {
//     /**
//      * @return return current state
//      */
//     State getState();
// 
//     /**
//      * Subscribe for state changes
//      * <p>
//      * Note: current state will not be dispatched immediately after subscribe
//      *
//      * @param listener callback which will be notified each time state changes
//      * @return instance of {@link Cancelable} to be used to cancel subscription (remove listener)
//      */
//     Cancelable subscribe(StateChangeListener<State> listener);
// }
// 
// Path: lib/src/main/java/com/yheriatovych/reductor/Cursors.java
// public class Cursors {
//     private Cursors(){}
// 
//     /**
//      * Creates a Cursor by applying a specified function to state.
//      *
//      * @param cursor source Cursor to be mapped
//      * @param mapper the function that will be applied to a state. Must be free of side effects.
//      * @param <State> source state type
//      * @param <R>    the output state type
//      * @return Cursor that holds state of type T produced by mapper function
//      */
//     public static <State, R> Cursor<R> map(Cursor<State> cursor, Function<State, R> mapper) {
//         return new MappedCursor<>(cursor, mapper);
//     }
// 
//     /**
//      * Notify listener for every state in Store.
//      * <p>
//      * Note: equivalent to {@link Cursor#subscribe(StateChangeListener)} but current state will be propagated too
//      *
//      * @param cursor source Cursor
//      * @param listener callback which will be notified
//      * @param <State> source state type
//      * @return instance of {@link Cancelable} to be used to cancel subscription (remove listener)
//      */
//     public static <State> Cancelable forEach(Cursor<State> cursor, StateChangeListener<State> listener) {
//         Cancelable cancelable = cursor.subscribe(listener);
//         listener.onStateChanged(cursor.getState());
//         return cancelable;
//     }
// }
// 
// Path: lib/src/main/java/com/yheriatovych/reductor/Store.java
// public class Store<State> implements Dispatcher, Cursor<State> {
//     public static final String INIT_ACTION = "@@reductor/INIT";
// 
//     private final Reducer<State> reducer;
//     private Dispatcher dispatcher;
//     private final List<StateChangeListener<State>> listeners = new CopyOnWriteArrayList<>();
//     private volatile State state;
// 
//     private Store(Reducer<State> reducer, State initialState, Middleware<State>[] middlewares) {
//         this.reducer = reducer;
//         this.state = initialState;
// 
//         dispatcher = this::dispatchAction;
//         dispatchAction(Action.create(INIT_ACTION));
// 
//         for (int i = middlewares.length - 1; i >= 0; i--) {
//             Middleware<State> middleware = middlewares[i];
//             dispatcher = middleware.create(Store.this, dispatcher);
//         }
//     }
// 
//     private void dispatchAction(final Object actionObject) {
//         if (actionObject instanceof Action) {
//             final Action action = (Action) actionObject;
//             synchronized (this) {
//                 state = reducer.reduce(state, action);
//             }
//             for (StateChangeListener<State> listener : listeners) {
//                 listener.onStateChanged(state);
//             }
//         } else {
//             throw new IllegalArgumentException(String.format("action %s of %s is not instance of %s, use custom Middleware to dispatch another types of actions", actionObject, actionObject.getClass(), Action.class));
//         }
//     }
// 
//     /**
//      * Create store with given {@link Reducer} and optional array of {@link Middleware}
//      *
//      * @param reducer     Reducer of type S which will be used to dispatch actions
//      * @param middlewares array of middlewares to be used to dispatch actions in the same order as provided
//      *                    look {@link Middleware} for more information
//      * @param <S>         type of state to hold and maintain
//      * @return Store initialised with initialState
//      */
//     @SafeVarargs
//     public static <S> Store<S> create(Reducer<S> reducer, Middleware<S>... middlewares) {
//         return create(reducer, null, middlewares);
//     }
// 
//     /**
//      * Create store with given {@link Reducer}, initalState and optional array of {@link Middleware}
//      *
//      * @param reducer      Reducer of type S which will be used to dispatch actions
//      * @param initialState state to be initial state of create store
//      * @param middlewares  array of middlewares to be used to dispatch actions in the same order as provided
//      *                     look {@link Middleware} for more information
//      * @param <S>          type of state to hold and maintain
//      * @return Store initialised with initialState
//      */
//     @SafeVarargs
//     public static <S> Store<S> create(Reducer<S> reducer, S initialState, Middleware<S>... middlewares) {
//         return new Store<>(reducer, initialState, middlewares);
//     }
// 
//     /**
//      * Dispatch action through {@link Reducer} and store the next state
//      *
//      * @param action action to be dispatched, usually instance of {@link Action}
//      *               but custom {@link Middleware} can be used to support other types of actions
//      */
//     public void dispatch(final Object action) {
//         dispatcher.dispatch(action);
//     }
// 
//     /**
//      * {@inheritDoc}
//      */
//     public State getState() {
//         return state;
//     }
// 
//     /**
//      * {@inheritDoc}
//      */
//     public Cancelable subscribe(final StateChangeListener<State> listener) {
//         listeners.add(listener);
//         return () -> listeners.remove(listener);
//     }
// }


import com.yheriatovych.reductor.Cancelable;
import com.yheriatovych.reductor.Cursor;
import com.yheriatovych.reductor.Cursors;
import com.yheriatovych.reductor.Store;
import rx.Emitter;
import rx.Observable;
package com.yheriatovych.reductor.rx;


public final class RxStore {

    /**
     * Create observable of state changes from specified {@link Cursor}
     * <p>
     * Note: This method will emit current sate immediately after subscribe
     */
    public static <State> Observable<State> asObservable(final Cursor<State> cursor) {
        return Observable.fromEmitter(emitter -> {
// Path: lib/src/main/java/com/yheriatovych/reductor/Cancelable.java
// public interface Cancelable {
//     void cancel();
// }
// 
// Path: lib/src/main/java/com/yheriatovych/reductor/Cursor.java
// public interface Cursor<State> {
//     /**
//      * @return return current state
//      */
//     State getState();
// 
//     /**
//      * Subscribe for state changes
//      * <p>
//      * Note: current state will not be dispatched immediately after subscribe
//      *
//      * @param listener callback which will be notified each time state changes
//      * @return instance of {@link Cancelable} to be used to cancel subscription (remove listener)
//      */
//     Cancelable subscribe(StateChangeListener<State> listener);
// }
// 
// Path: lib/src/main/java/com/yheriatovych/reductor/Cursors.java
// public class Cursors {
//     private Cursors(){}
// 
//     /**
//      * Creates a Cursor by applying a specified function to state.
//      *
//      * @param cursor source Cursor to be mapped
//      * @param mapper the function that will be applied to a state. Must be free of side effects.
//      * @param <State> source state type
//      * @param <R>    the output state type
//      * @return Cursor that holds state of type T produced by mapper function
//      */
//     public static <State, R> Cursor<R> map(Cursor<State> cursor, Function<State, R> mapper) {
//         return new MappedCursor<>(cursor, mapper);
//     }
// 
//     /**
//      * Notify listener for every state in Store.
//      * <p>
//      * Note: equivalent to {@link Cursor#subscribe(StateChangeListener)} but current state will be propagated too
//      *
//      * @param cursor source Cursor
//      * @param listener callback which will be notified
//      * @param <State> source state type
//      * @return instance of {@link Cancelable} to be used to cancel subscription (remove listener)
//      */
//     public static <State> Cancelable forEach(Cursor<State> cursor, StateChangeListener<State> listener) {
//         Cancelable cancelable = cursor.subscribe(listener);
//         listener.onStateChanged(cursor.getState());
//         return cancelable;
//     }
// }
// 
// Path: lib/src/main/java/com/yheriatovych/reductor/Store.java
// public class Store<State> implements Dispatcher, Cursor<State> {
//     public static final String INIT_ACTION = "@@reductor/INIT";
// 
//     private final Reducer<State> reducer;
//     private Dispatcher dispatcher;
//     private final List<StateChangeListener<State>> listeners = new CopyOnWriteArrayList<>();
//     private volatile State state;
// 
//     private Store(Reducer<State> reducer, State initialState, Middleware<State>[] middlewares) {
//         this.reducer = reducer;
//         this.state = initialState;
// 
//         dispatcher = this::dispatchAction;
//         dispatchAction(Action.create(INIT_ACTION));
// 
//         for (int i = middlewares.length - 1; i >= 0; i--) {
//             Middleware<State> middleware = middlewares[i];
//             dispatcher = middleware.create(Store.this, dispatcher);
//         }
//     }
// 
//     private void dispatchAction(final Object actionObject) {
//         if (actionObject instanceof Action) {
//             final Action action = (Action) actionObject;
//             synchronized (this) {
//                 state = reducer.reduce(state, action);
//             }
//             for (StateChangeListener<State> listener : listeners) {
//                 listener.onStateChanged(state);
//             }
//         } else {
//             throw new IllegalArgumentException(String.format("action %s of %s is not instance of %s, use custom Middleware to dispatch another types of actions", actionObject, actionObject.getClass(), Action.class));
//         }
//     }
// 
//     /**
//      * Create store with given {@link Reducer} and optional array of {@link Middleware}
//      *
//      * @param reducer     Reducer of type S which will be used to dispatch actions
//      * @param middlewares array of middlewares to be used to dispatch actions in the same order as provided
//      *                    look {@link Middleware} for more information
//      * @param <S>         type of state to hold and maintain
//      * @return Store initialised with initialState
//      */
//     @SafeVarargs
//     public static <S> Store<S> create(Reducer<S> reducer, Middleware<S>... middlewares) {
//         return create(reducer, null, middlewares);
//     }
// 
//     /**
//      * Create store with given {@link Reducer}, initalState and optional array of {@link Middleware}
//      *
//      * @param reducer      Reducer of type S which will be used to dispatch actions
//      * @param initialState state to be initial state of create store
//      * @param middlewares  array of middlewares to be used to dispatch actions in the same order as provided
//      *                     look {@link Middleware} for more information
//      * @param <S>          type of state to hold and maintain
//      * @return Store initialised with initialState
//      */
//     @SafeVarargs
//     public static <S> Store<S> create(Reducer<S> reducer, S initialState, Middleware<S>... middlewares) {
//         return new Store<>(reducer, initialState, middlewares);
//     }
// 
//     /**
//      * Dispatch action through {@link Reducer} and store the next state
//      *
//      * @param action action to be dispatched, usually instance of {@link Action}
//      *               but custom {@link Middleware} can be used to support other types of actions
//      */
//     public void dispatch(final Object action) {
//         dispatcher.dispatch(action);
//     }
// 
//     /**
//      * {@inheritDoc}
//      */
//     public State getState() {
//         return state;
//     }
// 
//     /**
//      * {@inheritDoc}
//      */
//     public Cancelable subscribe(final StateChangeListener<State> listener) {
//         listeners.add(listener);
//         return () -> listeners.remove(listener);
//     }
// }


// Path: reductor-rxjava/src/main/java/com/yheriatovych/reductor/rx/RxStore.java
import com.yheriatovych.reductor.Cancelable;
import com.yheriatovych.reductor.Cursor;
import com.yheriatovych.reductor.Cursors;
import com.yheriatovych.reductor.Store;
import rx.Emitter;
import rx.Observable;

package com.yheriatovych.reductor.rx;


public final class RxStore {

    /**
     * Create observable of state changes from specified {@link Cursor}
     * <p>
     * Note: This method will emit current sate immediately after subscribe
     */
    public static <State> Observable<State> asObservable(final Cursor<State> cursor) {
        return Observable.fromEmitter(emitter -> {
            final Cancelable cancelable = Cursors.forEach(cursor, emitter::onNext);