-
Notifications
You must be signed in to change notification settings - Fork 2
Description
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]
=> falseSimilarly, Array#includes would return unexpected results, for example:
store.state.includes(todo1)
=> falseEven though todo1 points to something valid within the OST.
I believe we can provide a better DX if we extend Array#includes as part of the overwrites in ArrayNodeHandler so that it automatically compares the Seed value of the values being compared, this would enabled users to rely on Array#includes for this type of check.
Prototype
export class ArrayNodeHandler<T extends object = object> extends NodeHandler<
T[]
> {
...
includes(value: T) {
for (const item of this) {
if (Seed.from(item) === Seed.from(value)) return true
}
return false
}
...
}