dao-governance-framework/forum-network/public/classes/actor.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

export class Actor {
constructor(name, scene) {
this.name = name;
this.scene = scene;
this.callbacks = new Map();
this.status = this.scene.addDisplayValue(`${this.name} status`);
this.status.set('New');
this.scene.log(`participant ${this.name}`);
2022-11-12 16:20:42 -06:00
this.values = new Map();
}
send(dest, action, detail) {
action.log(this, dest, detail ? JSON.stringify(detail) : '');
dest.recv(this, action, detail);
return this;
}
recv(src, action, detail) {
const cb = this.callbacks.get(action.name);
if (!cb) {
throw new Error(
`[${this.scene.name} actor ${this.name} does not have a callback registered for ${action.name}`,
);
}
cb(src, detail);
return this;
}
on(action, cb) {
this.callbacks.set(action.name, cb);
return this;
}
setStatus(status) {
this.status.set(status);
return this;
}
2022-11-12 16:20:42 -06:00
addValue(label) {
this.values.set(label, this.scene.addDisplayValue(`${this.name} ${label}`));
return this;
}
setValue(label, value) {
let displayValue = this.values.get(label);
if (!displayValue) {
displayValue = this.scene.addDisplayValue(`${this.name} ${label}`);
this.values.set(label, displayValue);
}
displayValue.set(value);
return this;
}
}