def test_ignore_values_basic(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
                on_next(180, 1), on_next(210, 2), on_next(250, 3),
                on_next(270, 4), on_next(310, 5), on_next(360, 6),
                on_next(380, 7), on_next(410, 8), on_next(590, 9))
        results = scheduler.start(create=lambda: xs.pipe(ops.ignore_elements()))

        assert results.messages == []
        assert xs.subscriptions == [subscribe(200, 1000)]
Exemplo n.º 2
0
    def test_ignore_values_basic(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(on_next(180, 1), on_next(210, 2),
                                             on_next(250, 3), on_next(270, 4),
                                             on_next(310, 5), on_next(360, 6),
                                             on_next(380, 7), on_next(410, 8),
                                             on_next(590, 9))
        results = scheduler.start(
            create=lambda: xs.pipe(ops.ignore_elements()))

        assert results.messages == []
        assert xs.subscriptions == [subscribe(200, 1000)]
Exemplo n.º 3
0
def create_sample_feature() -> ReduxFeatureModule:
    """
        Constructs a new sample feature
    """
    def handle_sample_action(state: Any, action: Action) -> Any:
        return select_action_payload(action)

    sample_reducer = handle_actions({ADD_SAMPLE_ACTION: handle_sample_action})

    add_epic = pipe(
        of_type(ADD_SAMPLE_ACTION),
        ignore_elements(),
    )

    sample_epic = combine_epics(add_epic)

    return create_feature_module(SAMPLE_FEATURE, sample_reducer, sample_epic)
Exemplo n.º 4
0
def create_init_feature() -> ReduxFeatureModule:
    """
        Constructs a new sample feature
    """
    def handle_init_action(state: Any, action: Action) -> Any:
        return select_action_payload(action)

    sample_reducer = handle_actions({ADD_INIT_ACTION: handle_init_action})

    add_epic = pipe(
        of_type(ADD_INIT_ACTION),
        ignore_elements(),
    )

    init_epic = pipe(
        of_init_feature(INIT_FEATURE),
        map(lambda x: add_init_action("init")),
    )

    sample_epic = combine_epics(add_epic, init_epic)

    return create_feature_module(INIT_FEATURE, sample_reducer, sample_epic)
import rx
import rx.operators as ops

numbers = rx.from_([1, 2, 3, 4, 5, 6])
numbers.pipe(ops.ignore_elements()).subscribe(
    on_next=lambda i: print("on_next {}".format(i)),
    on_error=lambda e: print("on_error: {}".format(e)),
    on_completed=lambda: print("on_completed"))
op.sum()

"""Transformation"""
op.buffer()
op.group_by()
op.map()
op.scan()
# ...

"""Filtering"""
op.debounce()
op.distinct()
op.filter()
op.element_at()
op.first()
op.ignore_elements()
op.last()
op.skip()
op.skip_last()
op.take()
op.take_last()
# ...

"""Error Handling"""
op.catch()
op.retry()

"""Utility"""
op.delay()
op.materialize()
op.time_interval()
Exemplo n.º 7
0
def create_store(initial_state: Optional[ReduxRootState] = None) -> ReduxRootStore:  # pylint: disable=too-many-locals
    """ Constructs a new store that can handle feature modules.

        Args:
            initial_state: optional initial state of the store, will typically be the empty dict

        Returns:
            An implementation of the store
    """

    # current reducer
    reducer: Reducer = identity_reducer

    def replace_reducer(new_reducer: Reducer) -> None:
        """ Callback that replaces the current reducer

            Args:
                new_reducer: the new reducer

        """
        nonlocal reducer
        reducer = new_reducer

    # subject used to dispatch actions
    actions = Subject()

    # the shared action observable
    actions_ = actions.pipe(op.share())

    _dispatch = actions.on_next

    # our current state
    state = BehaviorSubject(initial_state if initial_state else {})

    # shutdown trigger
    done_ = Subject()

    # The set of known modules, to avoid cycles and duplicate registration
    modules: MutableMapping[str, ReduxFeatureModule] = {}

    # Sequence of added modules
    module_subject = Subject()

    # Subscribe to the resolved modules
    module_ = module_subject.pipe(op.distinct(select_id), op.share())

    # Build the reducers
    reducer_ = module_.pipe(
        op.filter(has_reducer),
        op.scan(reduce_reducers, {}),
        op.map(combine_reducers),
        op.map(replace_reducer),
    )

    # Build the epic
    epic_ = module_.pipe(
        op.map(select_epic),
        op.filter(bool),
        op.map(normalize_epic)
    )

    # Root epic that combines all of the incoming epics
    def root_epic(
        action_: Observable, state_: Observable
    ) -> Observable:
        """ Implementation of the root epic. If listens for new epics
            to come in and automatically subscribes.

            Args:
                action_: the action observable
                state_: the state observable

            Returns
                The observable of resulting actions
        """
        return epic_.pipe(
            op.flat_map(run_epic(action_, state_)),
            op.map(_dispatch)
        )

    # notifications about new feature states
    new_module_ = module_.pipe(
        op.map(select_id),
        op.map(create_action(INIT_ACTION)),
        op.map(_dispatch),
    )

    def _add_feature_module(module: ReduxFeatureModule):
        """ Registers a new feature module

            Args:
                module: the new feature module

        """
        module_id = select_id(module)
        if not module_id in modules:
            modules[module_id] = module
            for dep in select_dependencies(module):
                _add_feature_module(dep)
            module_subject.on_next(module)

    # all state
    internal_ = merge(root_epic(actions_, state), reducer_, new_module_).pipe(
        op.ignore_elements()
    )

    def _as_observable() -> Observable:
        """ Returns the state as an observable

            Returns:
                the observable
        """
        return state

    def _on_completed() -> None:
        """ Triggers the done event """
        done_.on_next(None)

    merge(actions_, internal_).pipe(
        op.map(lambda action: reducer(state.value, action)),
        op.take_until(done_),
    ).subscribe(state, logger.error)

    return ReduxRootStore(
        _as_observable, _dispatch, _add_feature_module, _dispatch, _on_completed
    )