vasille
Version:
The first Developer eXperience Orientated front-end framework (core library).
71 lines (70 loc) • 1.61 kB
JavaScript
import { IValue } from "../core/ivalue.js";
import { Reference } from "./reference.js";
/**
* r/w pointer to a value
* @class Pointer
* @extends IValue
*/
export class Pointer extends IValue {
/**
* Constructs a notifiable bind to a value
* @param value {IValue} is initial valu
*/
constructor(value) {
super();
this.handler = (v) => {
this.reference.$ = v;
};
this.target = value;
this.reference = new Reference(value.$);
value.on(this.handler);
}
get $() {
return this.reference.$;
}
set $(v) {
this.target?.off(this.handler);
this.target = null;
this.reference.$ = v;
}
get $$() {
return this.reference.$;
}
set $$(v) {
/* istanbul ignore else */
if (this.target !== v) {
this.disconnectTarget();
}
if (v instanceof IValue) {
this.target = v;
this.target.on(this.handler);
this.reference.$ = v.$;
}
else {
this.$ = v;
}
}
on(handler) {
this.reference.on(handler);
}
off(handler) {
this.reference.off(handler);
}
destroy() {
this.target?.off(this.handler);
this.reference.destroy();
super.destroy();
}
disconnectTarget() {
this.target?.off(this.handler);
}
}
export class OwningPointer extends Pointer {
destroy() {
this.target?.destroy();
super.destroy();
}
disconnectTarget() {
this.target?.destroy();
}
}