JavaScriptRegenerated / yieldmachine

Components for State Machines, using Generator Functions
https://regenerated.dev/yieldmachine
MIT License
33 stars 0 forks source link

Support subscribing to an EventSource #4

Open RoyalIcing opened 3 years ago

RoyalIcing commented 3 years ago
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);
  }
}
RoyalIcing commented 3 years ago

This is implemented. It does seem to run in an infinite loop, re-opening after it closes.