nanostores / nanostores

A tiny (286 bytes) state manager for React/RN/Preact/Vue/Svelte with many atomic tree-shakable stores
MIT License
5.39k stars 109 forks source link

Exception handling in events #175

Open jcalfee opened 1 year ago

jcalfee commented 1 year ago

What about an exception to notify the caller that is trying to make the action succeed? Are their any parameters for abort and what happens if an exception is passed to abort or thrown?

https://github.com/nanostores/nanostores#store-events

import { onSet } from 'nanostores'

onSet(store, ({ newValue, abort }) => {
  if (!validate(newValue)) {
    abort()
  }
})
ai commented 1 year ago

No right now. The current plan is to keep event system simple and create complex logic on top of it (but we can change this plan).

What you try to do and why?

jcalfee commented 1 year ago

Nothing specific, I'm evaluating to maybe add Svelte into a Vue environment.. I can't imagine the caller not needing to know the change did not take effect.

ai commented 1 year ago

What API do you think of? If you want and it will be simple, you can send PR.

ai commented 1 year ago

In recent 0.8 release we added onAction event with error callback. Not sure that it will cover all onSet events. Maybe we should add onError listener.