Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a isEqual utility for seamless seed comparison #133

Open
drborges opened this issue Jul 13, 2024 · 0 comments
Open

Provide a isEqual utility for seamless seed comparison #133

drborges opened this issue Jul 13, 2024 · 0 comments

Comments

@drborges
Copy link
Owner

Because Arbor nodes are proxies around data and even though for instances of Arbor, the underlying data is mutable the proxies wrapping nodes in the OST (observable state tree) get "refreshed" every time the underlying data is mutated. This is done so that we can easily compute diffs on the OST, by simply comparing the identity of nodes, e.g. ===.

However, this behavior can create some unexpected behavior. For instance, take the following store holding an array of todos:

const store = new Arbor([
  { text: "todo 1" },
  { text: "todo 2" },
])

Should we get a reference of say, todo 1 and mutate it like so:

const todo1 = store.state[0]
todo1.text = "todo 1 updated"

Then the identity of the variable todo1 will no longer be the same as store.state[0] since that node in the OST will be refreshed to indicate it has been mutated, so the following will be false:

todo1 === store.state[0]
=> false

Providing a utility function to allow users to compare nodes by their seeds can provide a similar behavior as the identity check mentioned above:

isEqual(todo1, store.state[0])
=> true

Prototype

function isEqual(v1: object, v2: object) {
  return Seed.from(v1) === Seed.from(v2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant