userbase icon indicating copy to clipboard operation
userbase copied to clipboard

Feature request: changeHandler should receive information on which items changed

Open shamblesides opened this issue 5 years ago • 2 comments

Right now the changeHandler receives just one argument, which is the entire list of items in a database. I would like to receive information on exactly which items were changed so I don't have to iterate over them myself to figure it out. This could be a second argument passed to the changeHandler.

shamblesides avatar Sep 10 '20 04:09 shamblesides

I am playing around with it at the moment and I think I can provide a PR. I have a couple of questions about this:

  1. What should the argument be, if an entry was deleted? An empty array or the deleted item?
  2. Should an object for the argument be used to be able to seemlessly integrate additional arguments in the future?

Fubinator avatar Oct 28 '20 10:10 Fubinator

Regarding (1), I usually see change handler callbacks implemented as cb: <T>(previous: T | null, current: T | null). So a new item is (null, T), a changed item is (T, T), and a deleted item is (T, null). Sometimes the callback is (current, previous) to make it easy to capture just the new value.

From #227:

This could be done on the application side, rather than inside userbase-js. This is our preference for now because we'd rather keep the SDK extremely simple

For anyone looking to solve this, you can use a tool like deep-diff to compare old/new copies of the database values.

spiffytech avatar Jan 24 '21 14:01 spiffytech