
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
@feng3d/watcher
Advanced tools
用于监听对象属性的变化以及同步两个对象的属性值。
源码:https://gitee.com/feng3d/watcher
npm install @feng3d/watcher
const { watcher } = require('@feng3d/watcher');
const o = { a: 1 };
let out = '';
const f = (_h, _p, _o) => { out += 'f'; };
const f1 = (_h, _p, _o) => { out += 'f1'; };
watcher.watch(o, 'a', f);
watcher.watch(o, 'a', f1);
o.a = 2;
watcher.unwatch(o, 'a', f);
o.a = 3;
console.assert(out === 'ff1f1', out);
const { watcher } = require('@feng3d/watcher');
const o1 = { a: 1 };
const o2 = { a: 1 };
watcher.bind(o1, 'a', o2, 'a');
o1.a = 2;
console.assert(o1.a == o2.a && o2.a === 2);
o2.a = 5;
console.assert(o1.a == o2.a && o1.a === 5);
const { watcher } = require('@feng3d/watcher');
const o = { a: { b: { c: 1 } } };
let out = '';
const f = (_h, _p, _o) => { out += 'f'; };
const f1 = (_h, _p, _o) => { out += 'f1'; };
watcher.watchchain(o, 'a.b.c', f);
watcher.watchchain(o, 'a.b.c', f1);
o.a.b.c = 2;
watcher.unwatchchain(o, 'a.b.c', f);
o.a.b.c = 3;
console.assert(out === 'ff1f1', out);
//
out = '';
watcher.unwatchchain(o, 'a.b.c', f1);
o.a.b.c = 4;
console.assert(out === '', out);
//
out = '';
watcher.watchchain(o, 'a.b.c', f);
o.a.b.c = 4;
o.a.b.c = 5;
console.assert(out === 'f', out);
//
out = '';
o.a = { b: { c: 1 } };
o.a.b.c = 3;
console.assert(out === 'ff', `out:${out}`);
//
out = '';
watcher.unwatchchain(o, 'a.b.c', f);
o.a.b.c = 4;
console.assert(out === '', `out:${out}`);
//
out = '';
watcher.watchchain(o, 'a.b.c', f);
o.a = null;
o.a = { b: { c: 1 } };
o.a.b.c = 5;
console.assert(out === 'fff', out);
// 变换
const transform = {
position: { x: 0, y: 0, z: 0 },
angle: { x: 0, y: 0, z: 0 },
scale: { x: 1, y: 1, z: 1 },
};
let changeCount = 0;
// 变化回调
function onChanged(_newValue: any, _oldValue: any, _host: any, _property: string)
{
changeCount++;
}
// 监听变化
watcher.watchobject(transform, { position: { x: 0, y: 0, z: 0 }, angle: { x: 0, y: 0, z: 0 }, scale: { x: 0, y: 0, z: 0 } }, onChanged);
//
changeCount = 0;
transform.position.x = Math.random();
equal(changeCount, 1); // 触发改变一次
changeCount = 0;
transform.position.x = transform.position.x + 0;
equal(changeCount, 0); // 赋予相同的值不会触发改变
changeCount = 0;
transform.position = { x: Math.random(), y: Math.random(), z: Math.random() };
equal(changeCount, 3); // x、y、z均改变
changeCount = 0;
transform.position = { x: transform.position.x, y: transform.position.y, z: transform.position.z };
equal(changeCount, 0); // x、y、z均未改变
// 移除监听变化
watcher.unwatchobject(transform, { position: { x: 0, y: 0, z: 0 }, angle: { x: 0, y: 0, z: 0 }, scale: { x: 0, y: 0, z: 0 } }, onChanged);
changeCount = 0;
transform.position = { x: Math.random(), y: Math.random(), z: Math.random() };
equal(changeCount, 0); // 无法监听到x、y、z改变
FAQs
The npm package @feng3d/watcher receives a total of 70 weekly downloads. As such, @feng3d/watcher popularity was classified as not popular.
We found that @feng3d/watcher demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.