Reconcile not recognising equality in Date() objects

It seems like reconcile() doesn't recognise equality in Date() objects. It recognises strings, numbers, booleans, arrays, objects as equal. It doesn't recognise Date() objects as such. Is it possible to somehow overwrite the equality operation for Date() in reconcile? I mean, it should be comparing by timestamp seconds and recognise when they are equal.
1 Reply
hyperknot
hyperknotOP22h ago
Created a helper function for it. It also does partial reconcile.
export function setStateSmart(store: any, data: Record<string, any>): void {
batch(() => {
for (const [key, newValue] of Object.entries(data)) {
// smart Date() comparison
if (newValue instanceof Date) {
const oldValue = store.state[key]
if (oldValue instanceof Date && oldValue.getTime() === newValue.getTime()) continue
store.setState(key, newValue)
continue
}

// partial reconcile for the rest
store.setState(key, reconcile(newValue))
}
})
}
export function setStateSmart(store: any, data: Record<string, any>): void {
batch(() => {
for (const [key, newValue] of Object.entries(data)) {
// smart Date() comparison
if (newValue instanceof Date) {
const oldValue = store.state[key]
if (oldValue instanceof Date && oldValue.getTime() === newValue.getTime()) continue
store.setState(key, newValue)
continue
}

// partial reconcile for the rest
store.setState(key, reconcile(newValue))
}
})
}

Did you find this page helpful?