UNPKG

swarpc

Version:

Full type-safe RPC library for service worker -- move things off of the UI thread with ease!

33 lines (32 loc) 1.13 kB
import { scopeIsDedicated, scopeIsShared } from "./scopes.js"; export function whoToSendTo(nodes, requests) { if (!nodes) return undefined; let chosen = Object.keys(nodes)[0]; const requestsPerNode = Map.groupBy(requests.values(), (req) => req.nodeId); for (const node of Object.keys(nodes)) { if (!requestsPerNode.has(node)) requestsPerNode.set(node, []); } for (const [node, reqs] of requestsPerNode.entries()) { if (!node) continue; if (reqs.length < requestsPerNode.get(chosen).length) chosen = node; } console.debug("[SWARPC Load balancer] Choosing", chosen, "load map is", requestsPerNode); return chosen; } export function nodeIdFromScope(scope, _scopeType) { if (scopeIsDedicated(scope, _scopeType) || scopeIsShared(scope, _scopeType)) { return scope.name; } return "(SW)"; } export function makeNodeId() { return "N" + Math.random().toString(16).substring(2, 5).toUpperCase(); } const serviceWorkerNodeId = "(SW)"; export function nodeIdOrSW(id) { return id ?? serviceWorkerNodeId; }