yieldmachine
yieldmachine copied to clipboard
Support subscribing to an EventSource
- https://www.html5rocks.com/tutorials/eventsource/basics/
const machine = start(Machine);
const eventSource = new EventSource();
machine.receiveFrom(eventSource);
const messagesKey = Symbol("messages");
function* Machine() {
yield listenTo(eventSource, "open");
yield on(new Map([["type", "error"], ["readyState", EventSource.CLOSED]]), Closed);
function* Open() {
yield listenTo(eventSource, "message");
yield accumulate("message", messagesKey);
}
function* Closed() {}
return function* Initial() {
yield listenTo(eventSource, "open");
yield on("open", Open);
}
}
This is implemented. It does seem to run in an infinite loop, re-opening after it closes.