snowflakify
Version:
The most complete Snowflake ID generator in TypeScript
42 lines • 1.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const FragmentBase_js_1 = __importDefault(require("../FragmentBase.js"));
/**
* SequenceFragment class for sequence (increment/counter) IDs.
* @public
*/
class SequenceFragment extends FragmentBase_js_1.default {
getValue() {
this.value = (this.value + BigInt(1)) & this.maxValue;
return this.value;
}
destructure(snowflake) {
const bits = BigInt(snowflake) & this.bitMask;
return {
identifier: this.identifier,
value: Number(bits >> this.bitShift),
};
}
/**
* Returns a boolean indicating whether the sequence will reset on the next call.
*
* @returns `true` if the sequence will reset when the next getValue() is called.
* Otherwise, returns `false`.
* @internal
*/
willReset() {
return ((this.value + BigInt(1)) & this.maxValue) === BigInt(0);
}
/**
* Resets the sequence to 0
* @internal
*/
resetSequence() {
this.value = BigInt(0);
}
}
exports.default = SequenceFragment;
//# sourceMappingURL=SequenceFragment.js.map