@feng3d/watcher
Advanced tools
Comparing version 0.8.3 to 0.8.7
@@ -14,4 +14,5 @@ class Watcher { | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watch(object, property, handler, thisObject) { | ||
watch(object, property, handler, thisObject, onlyChanged = true) { | ||
if (!Object.getOwnPropertyDescriptor(object, __watchs__)) { | ||
@@ -36,6 +37,4 @@ Object.defineProperty(object, __watchs__, { | ||
const oldValue = this[_property]; | ||
if (oldValue !== value) { | ||
orgSet && orgSet.call(this, value); | ||
notifyListener(value, oldValue, this, _property); | ||
} | ||
orgSet && orgSet.call(this, value); | ||
notifyListener(value, oldValue, this, _property); | ||
}; | ||
@@ -49,6 +48,4 @@ } else if (!data || !data.get && !data.set) { | ||
const oldValue = this[__watchs__][_property].value; | ||
if (oldValue !== value) { | ||
this[__watchs__][_property].value = value; | ||
notifyListener(value, oldValue, this, _property); | ||
} | ||
this[__watchs__][_property].value = value; | ||
notifyListener(value, oldValue, this, _property); | ||
}; | ||
@@ -64,3 +61,3 @@ } else { | ||
if (!has) { | ||
propertywatchs.handlers.push({ handler, thisObject }); | ||
propertywatchs.handlers.push({ handler, thisObject, onlyChanged }); | ||
} | ||
@@ -114,6 +111,7 @@ } | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchs(object, propertys, handler, thisObject) { | ||
watchs(object, propertys, handler, thisObject, onlyChanged = true) { | ||
propertys.forEach((v) => { | ||
this.watch(object, v, handler, thisObject); | ||
this.watch(object, v, handler, thisObject, onlyChanged); | ||
}); | ||
@@ -182,7 +180,8 @@ } | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchchain(object, property, handler, thisObject) { | ||
watchchain(object, property, handler, thisObject, onlyChanged = true) { | ||
const notIndex = property.indexOf("."); | ||
if (notIndex === -1) { | ||
this.watch(object, property, handler, thisObject); | ||
this.watch(object, property, handler, thisObject, onlyChanged); | ||
return; | ||
@@ -203,3 +202,3 @@ } | ||
if (object[currentp]) { | ||
this.watchchain(object[currentp], nextp, handler, thisObject); | ||
this.watchchain(object[currentp], nextp, handler, thisObject, onlyChanged); | ||
} | ||
@@ -210,10 +209,10 @@ const watchchainFun = (newValue, oldValue) => { | ||
if (newValue) | ||
this.watchchain(newValue, nextp, handler, thisObject); | ||
this.watchchain(newValue, nextp, handler, thisObject, onlyChanged); | ||
const ov = getObjectPropertyValue(oldValue, nextp); | ||
const nv = getObjectPropertyValue(newValue, nextp); | ||
if (ov !== nv) { | ||
if (!onlyChanged || ov !== nv) { | ||
handler.call(thisObject, nv, ov, newValue, nextp); | ||
} | ||
}; | ||
this.watch(object, currentp, watchchainFun); | ||
this.watch(object, currentp, watchchainFun, void 0, onlyChanged); | ||
propertywatchs.push({ handler, thisObject, watchchainFun }); | ||
@@ -265,7 +264,8 @@ } | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchobject(object, property, handler, thisObject) { | ||
watchobject(object, property, handler, thisObject, onlyChanged = true) { | ||
const chains = getObjectPropertyChains(property); | ||
chains.forEach((v) => { | ||
this.watchchain(object, v, handler, thisObject); | ||
this.watchchain(object, v, handler, thisObject, onlyChanged); | ||
}); | ||
@@ -295,3 +295,5 @@ } | ||
handlers.forEach((element) => { | ||
element.handler.call(element.thisObject, newValue, oldValue, host, property); | ||
if (!element.onlyChanged || newValue !== oldValue) { | ||
element.handler.call(element.thisObject, newValue, oldValue, host, property); | ||
} | ||
}); | ||
@@ -298,0 +300,0 @@ } |
@@ -44,4 +44,5 @@ /** | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watch<T, K extends PropertyNames<T>, V extends T[K]>(object: T, property: K, handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any): void; | ||
watch<T, K extends PropertyNames<T>, V extends T[K]>(object: T, property: K, handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any, onlyChanged?: boolean): void; | ||
/** | ||
@@ -65,4 +66,5 @@ * 取消监听对象属性的变化 | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchs<T, K extends PropertyNames<T>, V extends T[K]>(object: T, propertys: K[], handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any): void; | ||
watchs<T, K extends PropertyNames<T>, V extends T[K]>(object: T, propertys: K[], handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any, onlyChanged?: boolean): void; | ||
/** | ||
@@ -103,4 +105,5 @@ * 取消监听对象属性的变化 | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchchain(object: any, property: string, handler: (newValue: any, oldValue: any, object: any, property: string) => void, thisObject?: any): void; | ||
watchchain(object: any, property: string, handler: (newValue: any, oldValue: any, object: any, property: string) => void, thisObject?: any, onlyChanged?: boolean): void; | ||
/** | ||
@@ -122,4 +125,5 @@ * 取消监听对象属性链值变化 | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchobject<T>(object: T, property: gPartial<T>, handler: (newValue: any, oldValue: any, host: any, property: string) => void, thisObject?: any): void; | ||
watchobject<T>(object: T, property: gPartial<T>, handler: (newValue: any, oldValue: any, host: any, property: string) => void, thisObject?: any, onlyChanged?: boolean): void; | ||
/** | ||
@@ -126,0 +130,0 @@ * 取消监听对象属性链值变化 |
{ | ||
"name": "@feng3d/watcher", | ||
"version": "0.8.3", | ||
"type": "module", | ||
"version": "0.8.7", | ||
"description": "对象属性监听器", | ||
"author": "feng", | ||
"license": "MIT", | ||
"type": "module", | ||
"main": "./dist/index.umd.cjs", | ||
"types": "./lib/index.d.ts", | ||
"module": "./dist/index.js", | ||
"author": "feng", | ||
"license": "MIT", | ||
"exports": { | ||
@@ -33,3 +33,3 @@ ".": { | ||
"type": "git", | ||
"url": "https://gitlab.com/feng3d/watcher.git" | ||
"url": "https://gitee.com/feng3d/watcher.git" | ||
}, | ||
@@ -36,0 +36,0 @@ "publishConfig": { |
@@ -46,4 +46,5 @@ /** | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watch<T, K extends PropertyNames<T>, V extends T[K]>(object: T, property: K, handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any) | ||
watch<T, K extends PropertyNames<T>, V extends T[K]>(object: T, property: K, handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any, onlyChanged = true) | ||
{ | ||
@@ -79,7 +80,4 @@ if (!Object.getOwnPropertyDescriptor(object, __watchs__)) | ||
if (oldValue !== value) | ||
{ | ||
orgSet && orgSet.call(this, value); | ||
notifyListener(value, oldValue, this, _property); | ||
} | ||
orgSet && orgSet.call(this, value); | ||
notifyListener(value, oldValue, this, _property); | ||
}; | ||
@@ -98,7 +96,4 @@ } | ||
if (oldValue !== value) | ||
{ | ||
this[__watchs__][_property].value = value; | ||
notifyListener(value, oldValue, this, _property); | ||
} | ||
this[__watchs__][_property].value = value; | ||
notifyListener(value, oldValue, this, _property); | ||
}; | ||
@@ -120,3 +115,3 @@ } | ||
{ | ||
propertywatchs.handlers.push({ handler, thisObject }); | ||
propertywatchs.handlers.push({ handler, thisObject, onlyChanged }); | ||
} | ||
@@ -183,8 +178,9 @@ } | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchs<T, K extends PropertyNames<T>, V extends T[K]>(object: T, propertys: K[], handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any) | ||
watchs<T, K extends PropertyNames<T>, V extends T[K]>(object: T, propertys: K[], handler: (newValue: V, oldValue: V, object: T, property: string) => void, thisObject?: any, onlyChanged = true) | ||
{ | ||
propertys.forEach((v) => | ||
{ | ||
this.watch(object, v, handler, thisObject); | ||
this.watch(object, v, handler, thisObject, onlyChanged); | ||
}); | ||
@@ -272,4 +268,5 @@ } | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchchain(object: any, property: string, handler: (newValue: any, oldValue: any, object: any, property: string) => void, thisObject?: any) | ||
watchchain(object: any, property: string, handler: (newValue: any, oldValue: any, object: any, property: string) => void, thisObject?: any, onlyChanged = true) | ||
{ | ||
@@ -280,3 +277,3 @@ const notIndex = property.indexOf('.'); | ||
{ | ||
this.watch(object, property, handler, thisObject); | ||
this.watch(object, property, handler, thisObject, onlyChanged); | ||
@@ -307,3 +304,3 @@ return; | ||
{ | ||
this.watchchain(object[currentp], nextp, handler, thisObject); | ||
this.watchchain(object[currentp], nextp, handler, thisObject, onlyChanged); | ||
} | ||
@@ -315,3 +312,3 @@ | ||
if (oldValue) this.unwatchchain(oldValue, nextp, handler, thisObject); | ||
if (newValue) this.watchchain(newValue, nextp, handler, thisObject); | ||
if (newValue) this.watchchain(newValue, nextp, handler, thisObject, onlyChanged); | ||
// 当更换对象且监听值发生改变时触发处理函数 | ||
@@ -321,3 +318,3 @@ const ov = getObjectPropertyValue(oldValue, nextp); | ||
if (ov !== nv) | ||
if (!onlyChanged || ov !== nv) | ||
{ | ||
@@ -328,3 +325,3 @@ handler.call(thisObject, nv, ov, newValue, nextp); | ||
this.watch(object, currentp, watchchainFun); | ||
this.watch(object, currentp, watchchainFun, undefined, onlyChanged); | ||
@@ -398,4 +395,5 @@ // 记录链监听函数 | ||
* @param thisObject 变化回调函数 this值 | ||
* @param onlyChanged 值为 true 时表示只在变化时才触发回调函数,否则只要被赋值就触发回调函数。默认为 true 。 | ||
*/ | ||
watchobject<T>(object: T, property: gPartial<T>, handler: (newValue: any, oldValue: any, host: any, property: string) => void, thisObject?: any) | ||
watchobject<T>(object: T, property: gPartial<T>, handler: (newValue: any, oldValue: any, host: any, property: string) => void, thisObject?: any, onlyChanged = true) | ||
{ | ||
@@ -406,3 +404,3 @@ const chains = getObjectPropertyChains(property); | ||
{ | ||
this.watchchain(object, v, handler, thisObject); | ||
this.watchchain(object, v, handler, thisObject, onlyChanged); | ||
}); | ||
@@ -434,3 +432,3 @@ } | ||
{ | ||
[property: string]: { value: any, oldPropertyDescriptor: any, handlers: { handler: (newValue: any, oldValue: any, host: any, property: string) => void, thisObject: any }[] }; | ||
[property: string]: { value: any, oldPropertyDescriptor: any, handlers: { handler: (newValue: any, oldValue: any, host: any, property: string) => void, thisObject: any, onlyChanged: boolean }[] }; | ||
} | ||
@@ -453,3 +451,6 @@ | ||
{ | ||
element.handler.call(element.thisObject, newValue, oldValue, host, property); | ||
if (!element.onlyChanged || newValue !== oldValue) | ||
{ | ||
element.handler.call(element.thisObject, newValue, oldValue, host, property); | ||
} | ||
}); | ||
@@ -456,0 +457,0 @@ } |
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
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
119830
1385
0