nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
41 lines (30 loc) • 1.16 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/cluster/shared_handle.js
import * as assert from "nstdlib/lib/internal/assert";
import * as dgram from "nstdlib/lib/internal/dgram";
import * as net from "nstdlib/lib/net";
export default SharedHandle;
function SharedHandle(key, address, { port, addressType, fd, flags }) {
this.key = key;
this.workers = new Map();
this.handle = null;
this.errno = 0;
let rval;
if (addressType === "udp4" || addressType === "udp6")
rval = dgram._createSocketHandle(address, port, addressType, fd, flags);
else rval = net._createServerHandle(address, port, addressType, fd, flags);
if (typeof rval === "number") this.errno = rval;
else this.handle = rval;
}
SharedHandle.prototype.add = function (worker, send) {
assert(!this.workers.has(worker.id));
this.workers.set(worker.id, worker);
send(this.errno, null, this.handle);
};
SharedHandle.prototype.remove = function (worker) {
if (!this.workers.has(worker.id)) return false;
this.workers.delete(worker.id);
if (this.workers.size !== 0) return false;
this.handle.close();
this.handle = null;
return true;
};