superjson icon indicating copy to clipboard operation
superjson copied to clipboard

Synchronize Utility

Open nrdobie opened this issue 5 years ago • 0 comments

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

nrdobie avatar Jul 19 '19 16:07 nrdobie