36 lines
873 B
TypeScript
36 lines
873 B
TypeScript
|
|
||
|
|
||
|
export class ObProperty<T> {
|
||
|
private value:T; // 值
|
||
|
private onChanges:((newValue:T, oldValue:T)=>void)[]=[];
|
||
|
|
||
|
constructor(initValue:T){
|
||
|
this.value = initValue;
|
||
|
}
|
||
|
|
||
|
public get Value():T{ return this.value;}
|
||
|
|
||
|
public set Value(newValue:T) {
|
||
|
if(newValue != this.value){
|
||
|
const oldValue = this.value;
|
||
|
this.value = newValue;
|
||
|
this.onChanges.forEach(callback=>callback(newValue, oldValue));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 注册变更回调
|
||
|
public RegisterOnChange(callback:(newValue:T, oldValue:T)=>void):void{
|
||
|
this.onChanges.push(callback);
|
||
|
}
|
||
|
|
||
|
// 移除变更回调
|
||
|
public UnregisterOnChange(callback:(newValue:T, oldValue:T)=>void):void {
|
||
|
const idx = this.onChanges.indexOf(callback);
|
||
|
if (idx !== -1){
|
||
|
this.onChanges.splice(idx, 1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|