简体   繁体   中英

RxJS 5 fromEvent observable subscribe called twice

I'm trying to respond to a merged observable of touch and mousedown events to toggle a menu open/close. The problem is that the code to handle the toggle is running twice. I've confirmed this by logging the event to the console, and it's the same event that is logged:

export const toggleMenu = (openButton, closeButton) => {
  return Observable.merge(
    Observable.fromEvent(openButton, 'mousedown'),
    Observable.fromEvent(closeButton, 'mousedown'))
      .subscribe((event) => {
        console.log(event);
        if (elementIsVisible(nav)) {
          hideElement(nav);
        } else {
          showElement(nav);
        }
      });
};

So this ends up opening then immediately closing the menu.

Ok, this was my mistake. The actual posted code is fine, but I was instantiating the class that called this function twice, so there were two subscriptions.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM