Trying to implement an rxjs observable pipeline in React.
Codesandbox example -> https://codesandbox.io/s/optimistic-fog-xtdi4
Creating a html element (div) in React, and binding mouseover and mouseout events.
React Code to initialize this:
useEffect(() => {
const elMouseOut$ = fromEvent(elRef.current, "mouseout").subscribe(() =>
setState({ hover: "mouseout", eventType: "mouseout" })
);
const elMouseOver$ = fromEvent(elRef.current, "mouseover")
.pipe(debounceTime(2000))
.subscribe(() =>
setState({ hover: "mouseover", eventType: "mouseover" })
);
return () => {
elMouseOver$.unsubscribe();
elMouseOut$.unsubscribe();
};
}, []);
Acceptance criteria Steps.
- When I mouse over, after a delay of 2 seconds i need to make a state update.
- If I mouse out at any time BEFORE the 2 second delay I need to cancel the mouseover stream, and set some state. (THIS STEP IS BROKEN)
- If I mouse out any time AFTER the 2 second delay, I need to set some state.
- This should be repeatable, in the sense that if i mouse over, mouse out and mouse over again we start from Step 1.
I have tried several different combinations of operators, but cannot get things working as expected.
Note: If the mouse over debounce time is met, an ajax request is made.
Any help/advice would be appreciated, i simply do not have the rxjs chops to sort this one out.