superjson
superjson copied to clipboard
Synchronize Utility
The synchronize utility would make working with state synchronization easier by preserving unchanged instances while replacing updated ones. It would follow the pure update style causing parents to be updated as well.
Proposed usage:
import { createSynchronize } from '@onedeadpixel/superjson-synchronize'
const updateState = createSynchronize()
export async function fetchState() {
const response = await fetch('/api/state')
const jsonState = await response.text()
return updateState(jsonState)
}
const firstState = await fetchState() // => { a: { b: 1 }, c: { d: 2 } }
// update `c.d` to 3 on server
const secondState = await fetchState() // => { a: { b: 1 }, c: { d: 3 } }
firstState === secondSate // => false
firstState.a === secondSate.a // => true
firstState.b === secondSate.b // => false