UNPKG

snowflakify

Version:

The most complete Snowflake ID generator in TypeScript

81 lines 3.37 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const worker_threads_1 = require("worker_threads"); const index_js_1 = __importStar(require("../index.js")); const CircularBuffer_js_1 = __importDefault(require("./CircularBuffer.js")); const { buffer, generatorOptions, isRefillingSAB } = worker_threads_1.workerData; const fragmentProtypeMap = { TimestampFragment: index_js_1.TimestampFragment.prototype, WorkerFragment: index_js_1.WorkerFragment.prototype, ProcessFragment: index_js_1.ProcessFragment.prototype, NetworkFragment: index_js_1.NetworkFragment.prototype, RandomFragment: index_js_1.RandomFragment.prototype, SequenceFragment: index_js_1.SequenceFragment.prototype, }; Object.setPrototypeOf(buffer, CircularBuffer_js_1.default.prototype); // When passing a fragment array, the prototypes of the fragments must be set again, // and the ID values for the workers, processes, and network must be updated to // reflect those of the workers that generated the snowflake IDs. if (generatorOptions?.fragmentArray) { generatorOptions.fragmentArray.forEach((fragment) => { Object.setPrototypeOf(fragment, fragmentProtypeMap[fragment.identifier]); if (fragment.identifier === 'WorkerFragment') { fragment.updateId(); } else if (fragment.identifier === 'ProcessFragment') { fragment.updateId(); } else if (fragment.identifier === 'NetworkFragment') { fragment.updateId(); } }); } const snowflake = new index_js_1.default(generatorOptions); const isRefillingTa = new Int32Array(isRefillingSAB); const refillBuffer = () => { let isFull = false; // There was an issue with while(true) which would cause // one worker to block out all others... // Will have to check again in the future while (!isFull) { try { buffer.push(snowflake.nextId()); } catch (e) { Atomics.store(isRefillingTa, 0, 0); isFull = true; } } }; worker_threads_1.parentPort?.on('message', (msg) => { if (msg === 'refill') { refillBuffer(); } }); //# sourceMappingURL=worker.js.map