@lf-lang/reactor-ts
Version:
A reactor-oriented programming framework in TypeScript
98 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduledTrigger = exports.Trigger = void 0;
const internal_1 = require("./internal");
/**
* Abstract class for a trigger. A trigger may be an action, port, or timer.
*/
class Trigger extends internal_1.Component {
/**
* Reactions to trigger.
*/
reactions = new Set();
getContainer() {
return this._getContainer();
}
}
exports.Trigger = Trigger;
/**
*
*/
class ScheduledTrigger extends Trigger {
value = undefined;
tag;
runtime;
constructor(container) {
super(container);
this._linkToRuntimeObject();
}
/**
* Update the current value of this timer in accordance with the given
* event, and trigger any reactions that list this timer as their trigger.
* @param e Timestamped event.
*/
update(e) {
if (!e.tag.isSimultaneousWith(this.runtime.util.getCurrentTag())) {
throw new Error("Time of event does not match current logical time.");
}
if (e.trigger === this) {
this.value = e.value;
this.tag = e.tag;
for (const r of this.reactions) {
this.runtime.stage(r);
}
}
else {
throw new Error("Attempt to update action using incompatible event.");
}
}
getManager(key) {
if (this._key === key) {
return this.manager;
}
throw Error("Unable to grant access to manager.");
}
/**
* Returns true if this action was scheduled for the current
* logical time. This result is not affected by whether it
* has a value.
*/
isPresent() {
if (this.tag === undefined) {
// This action has never been scheduled before.
return false;
}
if (this.tag.isSimultaneousWith(this.runtime.util.getCurrentTag())) {
return true;
}
else {
return false;
}
}
manager = new (class {
trigger;
constructor(trigger) {
this.trigger = trigger;
}
getContainer() {
return this.trigger._getContainer();
}
addReaction(reaction) {
this.trigger.reactions.add(reaction);
}
delReaction(reaction) {
this.trigger.reactions.delete(reaction);
}
})(this);
_receiveRuntimeObject(runtime) {
if (this.runtime == null) {
this.runtime = runtime;
}
else {
throw new Error("Can only establish link to runtime once.");
}
}
}
exports.ScheduledTrigger = ScheduledTrigger;
// FIXME(marten): move these to trigger.ts and let them extend trigger
//# sourceMappingURL=trigger.js.map