With RxJS, I want to subscribe to events. The problem is: I want to setup the Observable BEFORE the event listener is set up.
Think of this like two buttons: Button1 does something when clicked, but only after button2 attached an event listener to button1. The observer should subscribe to the click event of button1. All of this should be wired together when the app starts. Button2 shouldn't setup the observer! Some code without RxJS:
button2.addEventListener('click', function() {
// [...] some code to prevent that button1 has more than one event listener
// attach event listener to button1
button1.addEventListener('click', clickHandler);
// now let observer subscribe itself to clickHandler somehow
// -> HOW?
});
function clickHandler() {
console.log("clicked");
}
Edit: Maybe, is it possible to observe some kind of dummy event handler that never emits anything, but turns into the real event handler (clickHandler) after button2 is clicked or something like that?!
Edit2: I noticed that the 2 buttons aren't the best example. Let's make it different and have the event listener for button2 be attached through some unknown mechanism. For example:
// is called by unkown means, i. e. the observer doesn't know how and when its attached
function attach() {
button1.addEventListener('click', clickHandler);
}
But when attach() is called, the Observer should subscribe to the clickHandler. Or maybe the Observer is subscribed all the time to some function and attach() swaps out the function with the actual event listener. Dunno.