UNPKG

snowflakify

Version:

The most complete Snowflake ID generator in TypeScript

37 lines (36 loc) 951 B
import FragmentBase from '../FragmentBase.js'; /** * SequenceFragment class for sequence (increment/counter) IDs. * @public */ export default class SequenceFragment extends FragmentBase { 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); } } //# sourceMappingURL=SequenceFragment.js.map