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

38 lines
869 B
JavaScript

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}`);
}
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;
}
}