UNPKG

ram64

Version:

Multi-threaded 64bit memory cache database inspired by redis-like features

39 lines (38 loc) 1.38 kB
"use strict"; exports.connect = connect; var _workerThreads = require("worker_threads"); var _ = require("."); var _rand = require("../util/rand"); var _startup = require("./startup"); async function connect(connectKey, { concurrency =_startup.DEFAULT_CONCURRENCY } = {}) { if (_workerThreads.isMainThread) return Promise.reject(new Error('connect() must be called from a worker thread. Use startup() instead.')); const req = { ram64: true, requestId: (0, _rand).randomString(), command: 'connect', args: { connectKey } }; // TODO: support for timeout probably isn't a terrible idea const promise = new Promise((resolve, reject)=>{ const handler = (res)=>{ if (res?.requestId !== req.requestId) return; // ignore unintended responses _workerThreads.parentPort?.off('message', handler); // remove handler if (res.error) return void reject(res.error); resolve(res.value); }; _workerThreads.parentPort?.on('message', handler); _workerThreads.parentPort?.postMessage(req); }); const res1 = await promise; const ports = res1.ports; const shardCount = res1.shardCount; return new _.RAM64({ connectKey, ports, shardCount, concurrency }); } //# sourceMappingURL=connect.js.map