drborges / arbor-store

Seamless state management made with ❤️
https://github.com/drborges/arbor-store
8 stars 0 forks source link

Allow restoring a tree path to a previous value #43

Open drborges opened 6 years ago

drborges commented 6 years ago

This would essentially allow one to "rollback" mutations to a certain path. For optimistic UIs this means we'd be able to easily rollback UI changes upon API request failures:

store.subscribe("/users/:index", async (newUser, oldUser, restore) => {
  const response = await api.users.update(newUser)
  if (response.status >= 400) {
    restore(oldUser).errors.push(response.message)
  }
})

Subscribers would receive a restore function as the third argument, to be called with the previous value of the mutated node, returning the restored node allowing further method chaining.