Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How implement toggle with Rxjs

I am learning rxjs. I create decorator "toggleable" for Dropdown component. All work fine, but I don't like it. How can I remove condition "toggle/hide".

Uses rxjs, react.js, recompose. It's toogleable decorator for Dropdown component.

export const toggleable = Wrapped => componentFromStream((props$) => {
// toogleHandler  called  with onClick
  const { handler: toogleHandler, stream: toogle$ } = createEventHandler();
// hideHandler called with code below
  const { handler: hideHandler, stream: hide$ } = createEventHandler();

  const show$ = Observable.merge(
    toogle$.mapTo('toogle'),
    hide$.mapTo('hide'))
          .startWith(false)
          .scan((state, type) => {
            if (type === 'toogle') {
              return !state;
            }
            if (type === 'hide') {
              return false;
            }

            return state;
          });


  return props$
    .combineLatest(
      show$,
      (props, show) => (
        <Wrapped
          {...props}
          show={show}
          onToggle={toogleHandler}
          onHide={hideHandler}
        />
      ));
});

It's decorator for Dropdown button

  // hideHandler caller
        class Foo extends Component {
            constructor(props) {
              super(props);
              this.refButton.bind(this);
              this.documentClick$ = Observable.fromEvent(global.document, 'click')
                .filter(event => this.button !== event.target)
                .do((event) => { this.props.onHide(event); });
            }

            componentDidMount() {
              this.documentClick$.subscribe();
            }
            componentWillUnmount() {
              this.documentClick$.unsubscribe();
            }
            refButton = (ref) => {
              this.button = ref;
            }
        }
like image 672
nonlux Avatar asked May 13 '17 06:05

nonlux


1 Answers

You can implement show$ with no conditions by mapping the toggle$/hide$ to functions on the previous state:

const show$ = Observable.merge(
    toggle$.mapTo(prev => !prev),
    hide$.mapTo(prev => false))
        .startWith(false)
        .scan((state, changeState) => changeState(state));

Another improvement you can do is with your toggleable implementation. Instead of using recompose componentFromStream, you can use recompose mapPropsStream:

export const toggleable = mapPropsStream(props$ => {
    const { handler: toogleHandler, stream: toogle$ } = createEventHandler();
    const { handler: hideHandler, stream: hide$ } = createEventHandler();
    const show$ = Observable.merge(
        toggle$.map(() => prev => !prev),
        hide$.map(() => prev => false))
            .startWith(false)
            .scan((state, changeState) => changeState(state));

    return props$
        .combineLatest(
            show$,
            (props, show) => ({
                ...props,
                show
                onToggle: toogleHandler
                onHide: hideHandler
            })
        );
});
like image 165
ZahiC Avatar answered Oct 23 '22 15:10

ZahiC