UNPKG

mokka

Version:

Mokka Consensus Algorithm implementation in Javascript

45 lines 1.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestProcessorService = void 0; const MessageApi_1 = require("../api/MessageApi"); const NodeApi_1 = require("../api/NodeApi"); const VoteApi_1 = require("../api/VoteApi"); const MessageTypes_1 = __importDefault(require("../constants/MessageTypes")); class RequestProcessorService { constructor(mokka) { this.voteApi = new VoteApi_1.VoteApi(mokka); this.messageApi = new MessageApi_1.MessageApi(mokka); this.nodeApi = new NodeApi_1.NodeApi(mokka); this.mokka = mokka; this.actionMap = new Map(); this.actionMap.set(MessageTypes_1.default.VOTE, [ this.voteApi.vote.bind(this.voteApi) ]); this.actionMap.set(MessageTypes_1.default.VOTED, [ this.voteApi.voted.bind(this.voteApi) ]); this.actionMap.set(MessageTypes_1.default.ACK, [ this.voteApi.validateAndApplyLeader.bind(this.voteApi), this.nodeApi.pingFromLeader.bind(this.nodeApi) ]); } async process(packet) { const node = this.mokka.nodes.get(packet.publicKey); if (!node || !this.actionMap.has(packet.type)) return; let reply = false; for (const action of this.actionMap.get(packet.type)) { if (reply === null) { continue; } reply = await action(reply === false ? packet : reply); } if (reply) await this.messageApi.message(reply, packet.publicKey); } } exports.RequestProcessorService = RequestProcessorService; //# sourceMappingURL=RequestProcessorService.js.map