
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Watch an object or array for changes
It works recursively, so it will even detect if you modify a deep property like obj.a.b[0].c = true
.
Uses the Proxy
API.
$ npm install on-change
const onChange = require('on-change');
const object = {
foo: false,
a: {
b: [
{
c: false
}
]
}
};
let i = 0;
const watchedObject = onChange(object, () => {
console.log('Object changed:', ++i);
});
watchedObject.foo = true;
//=> 'Object changed: 1'
watchedObject.a.b[0].c = true;
//=> 'Object changed: 2'
Returns a version of object
that is watched. It's the exact same object, just with some Proxy
traps.
Type: Object
Object to watch for changes.
Type: Function
Function that gets called anytime the object changes.
I had some code that was like:
const foo = {
a: 0,
b: 0
};
// …
foo.a = 3;
save(foo);
// …
foo.b = 7;
save(foo);
// …
foo.a = 10;
save(foo);
Now it can be simplified to:
const foo = onChange({
a: 0,
b: 0
}, () => save(foo));
// …
foo.a = 3;
// …
foo.b = 7;
// …
foo.a = 10;
Proxy
too)array[-1]
(Uses Proxy
too)Proxy
too)Proxy
too)MIT © Sindre Sorhus
FAQs
Watch an object or array for changes
The npm package on-change receives a total of 67,739 weekly downloads. As such, on-change popularity was classified as popular.
We found that on-change demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.