gordonbrander / spellcaster

Reactive signals UI library
MIT License
57 stars 2 forks source link

Experiment: Implement saga-like effects through generators #37

Closed gordonbrander closed 6 months ago

gordonbrander commented 7 months ago

This PR takes a different approach to store effects, using generators to implement simple feed-forward sagas.

Essentially effects are managed separately from state updates. The top-level fx generator function is typically written with a switch statement, just like the update function:

async function* fx(state, msg) {
  switch (msg.type) {
  case 'a':
    yield* aFx(state, msg)
    return
  case 'b':
    yield* bFx(state, msg)
    return
  default:
    return
  }
}
gordonbrander commented 7 months ago

Note that this PR switch to feed-forward async generators. I believe implementing cooperative generators that can communicate bi-directionally and fold with state/msg would require a considerably more complex cooperative multitasking runtime.

gordonbrander commented 6 months ago

Further investigation has revealed that nearly all useful use-cases for generator-style effects requires communication with the messages sent to the store.

I believe doing this correctly will require something like Communicating Sequential Processes so that we can ensure we don't miss relevant messages while a saga is busy doing something else.

Closing for now.