@feng3d/watcher
Advanced tools
Comparing version
@@ -284,3 +284,3 @@ class Watcher { | ||
const watchs = host[__watchs__]; | ||
const handlers = watchs[property].handlers; | ||
const handlers = watchs[property].handlers.concat(); | ||
handlers.forEach((element) => { | ||
@@ -287,0 +287,0 @@ if (!element.onlyChanged || newValue !== oldValue) { |
{ | ||
"name": "@feng3d/watcher", | ||
"type": "module", | ||
"version": "0.8.8", | ||
"version": "0.8.9", | ||
"description": "对象属性监听器", | ||
"homepage": "https://feng3d.com/watcher", | ||
"main": "./dist/index.umd.cjs", | ||
@@ -15,3 +16,3 @@ "types": "./lib/index.d.ts", | ||
"require": "./dist/index.umd.cjs", | ||
"types": "./src/index.ts" | ||
"types": "./lib/index.d.ts" | ||
} | ||
@@ -29,2 +30,3 @@ }, | ||
"docs": "typedoc", | ||
"upload_oss": "npm run docs && feng3d-cli oss_upload_dir", | ||
"release": "npm run clean && npm run lint && npm run build && npm run docs && npm run types && npm publish", | ||
@@ -49,2 +51,3 @@ "prepublishOnly": "node scripts/prepublish.js", | ||
"devDependencies": { | ||
"@feng3d/cli": "^0.0.19", | ||
"@typescript-eslint/eslint-plugin": "5.17.0", | ||
@@ -51,0 +54,0 @@ "@typescript-eslint/parser": "5.17.0", |
@@ -5,2 +5,6 @@ # @feng3d/watcher | ||
源码:https://gitee.com/feng3d/watcher | ||
文档:https://feng3d.com/watcher | ||
## 网站 | ||
@@ -91,2 +95,45 @@ | ||
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改变 | ||
``` |
@@ -433,3 +433,3 @@ /** | ||
const watchs: Watchs = host[__watchs__]; | ||
const handlers = watchs[property].handlers; | ||
const handlers = watchs[property].handlers.concat(); // 避免watchs.handlers被修改 | ||
@@ -436,0 +436,0 @@ handlers.forEach((element) => |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
121743
1.47%137
52.22%12
9.09%