Created
March 6, 2023 14:30
-
-
Save gordonbrander/83663bd1bf1676ab018023de73fe1d73 to your computer and use it in GitHub Desktop.
Versioned Proxy - using proxy to increment a version on object mutation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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