0.8.14 • Published 10 months ago
@feng3d/watcher v0.8.14
@feng3d/watcher
用于监听对象属性的变化以及同步两个对象的属性值。
源码: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改变0.8.9
12 months ago
0.8.8
12 months ago
0.8.7
12 months ago
0.8.12
10 months ago
0.8.11
11 months ago
0.8.14
10 months ago
0.8.13
10 months ago
0.8.10
11 months ago
0.8.3
2 years ago
0.8.1
3 years ago
0.8.2
3 years ago
0.8.0
3 years ago
0.7.1
3 years ago
0.5.0
4 years ago
0.7.0
3 years ago
0.6.0
3 years ago
0.4.11
4 years ago
0.4.10
4 years ago
0.4.8
4 years ago
0.4.5
4 years ago
0.4.6
4 years ago
0.4.1
4 years ago
0.4.3
4 years ago
0.4.2
4 years ago
0.3.9
4 years ago
0.3.8
4 years ago
0.3.6
4 years ago
0.3.7
4 years ago
0.3.5
5 years ago
0.3.0
5 years ago
0.3.1
5 years ago
0.3.3
5 years ago
0.2.4
5 years ago
0.0.19
7 years ago
0.0.2
7 years ago