UNPKG

@panyam/tsutils

Version:

Some basic TS utils for personal use

92 lines 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Send = exports.Reply = exports.Message = exports.Actor = void 0; const types_1 = require("../types"); class Actor { processMessage(msg) { (0, types_1.assert)(msg.target == this, "Cannot handle messages whose target is not this."); if (msg.isReply) { const reply = msg; (0, types_1.assert)(reply.responseTo.spawnedFrom != null, "Cannot spawn a reply for a send that itself was not spawned from another send"); this.processReply(reply); } else { this.processSend(msg); } } processSend(send) { } processReply(_reply) { } } exports.Actor = Actor; class Message { constructor(name, source, target) { this.uuid = Message.counter++; this.sourceData = null; this.payload = null; (0, types_1.assert)(source != null, "Source cannot be null"); (0, types_1.assert)(target != null, "Target cannot be null"); this.name = name; this.source = source; this.target = target; } } exports.Message = Message; Message.counter = 0; class ForwardableBase extends Message { constructor() { super(...arguments); this._spawnedFrom = null; } get spawnedFrom() { return this._spawnedFrom; } setSpawnedFrom(msg) { this._spawnedFrom = msg; if (msg == null) this._rootMessage = this; else this._rootMessage = msg.rootMessage; } get rootMessage() { return this._rootMessage; } } class Reply extends ForwardableBase { constructor(responseTo, error) { super(responseTo.name, responseTo.target, responseTo.source); this.isReply = true; (0, types_1.assert)(responseTo.reply == null, "Send's reply has already been set"); this.responseTo = responseTo; this.error = error || null; this.isError = this.error != null; responseTo.reply = this; } spawn(responseTo, error) { const child = new Reply(responseTo, error); child.setSpawnedFrom(this); return child; } } exports.Reply = Reply; class Send extends ForwardableBase { constructor() { super(...arguments); this.isReply = false; this.children = []; this.reply = null; this.cancelledAt = null; } spawn(nextTarget) { const child = new Send(this.name, this.target, nextTarget); child.setSpawnedFrom(this); this.children.push(child); return child; } spawnReply(error) { return new Reply(this, error); } } exports.Send = Send; //# sourceMappingURL=actors.js.map