vasille
Version:
The same framework which is designed to build bulletproof frontends (core library).
62 lines (61 loc) • 1.27 kB
JavaScript
import { Reference } from "./reference.js";
/**
* Forward only link type
* @class Forward
* @extends Reference
*/
export class Forward extends Reference {
/**
* forwarded value
* @type IValue
*/
target;
/**
* Handler to receive updates from forwarded value
*/
handler;
/**
* Constructs a value forwarder
* @param value {IValue} is source of forwarded data
* @param ctx lifetime context
*/
constructor(value, ctx) {
super(value.V);
this.handler = (v) => {
this.V = v;
};
this.target = value;
value.on(this.handler);
ctx?.bind(this);
}
destroy() {
this.target.off(this.handler);
}
}
/**
* Backward only link type
* @class Backward
* @extends Reference
*/
export class Backward extends Reference {
/**
* target, which receive the updates
* @type IValue
*/
target;
/**
* Constructs a value backward stream
* @param value {IValue} target, which receive the updates
*/
constructor(value) {
super(value.V);
this.target = value;
}
get V() {
return super.V;
}
set V(value) {
super.V = value;
this.target.V = value;
}
}