Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
Created March 6, 2023 14:30
Show Gist options
  • Save gordonbrander/83663bd1bf1676ab018023de73fe1d73 to your computer and use it in GitHub Desktop.
Save gordonbrander/83663bd1bf1676ab018023de73fe1d73 to your computer and use it in GitHub Desktop.
Versioned Proxy - using proxy to increment a version on object mutation
export const $version = Symbol('version')
const VersionedProxy = {
set(target, prop, value) {
target[$version] = target[$version] + 1
target[prop] = value
},
deleteProperty(target, prop) {
target[$version] = target[$version] + 1
delete target[prop]
}
}
export const versioned = target => {
target[$version] = 0
return new Proxy(target, VersionedProxy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment