Generators.observe(initialize) adapts a push-based data source (like an Observable, EventEmitter, or EventTarget) into a pull-based generator.
Usage:
Pass an initialize function that receives a change function. Calling change(value) triggers the resolution of the current promise. The initialize function can optionally return a dispose function to clean up resources (e.g., removing event listeners) when the generator is disposed.
Note: This generator is lossy. If change is called multiple times before the next promise is pulled, intermediate values may be skipped. Use Generators.queue if you need to ensure no values are lost.
Generators.observe(change => {
// An event listener to yield the element’s new value.
const inputted = () => change(element.value);
// Attach the event listener.
element.addEventListener("input", inputted);
// Yield the element’s initial value.
change(element.value);
// Detach the event listener when the generator is disposed.
return () => element.removeEventListener("input", inputted);
})