mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 05:25:40 +09:00
refactor: remove dead code (#3337)
This commit is contained in:
1 parent
a19002652b
commit
93d1f8af46
1527 files changed
+21170
-73846
No files matched your search
@@ -132,10 +132,6 @@ export class SnowflakeGenerator {
|
||||
return snowflake;
|
||||
}
|
||||
|
||||
getWorkerId(): number {
|
||||
return Number(this.workerId);
|
||||
}
|
||||
|
||||
private waitUntilNextTimestamp(): bigint {
|
||||
let timestamp = getTimestampFromNow(this.now);
|
||||
while (timestamp <= this.lastTimestamp) {
|
||||
|
||||
@@ -12,38 +12,7 @@ function getRelativeTimestampForBucket(snowflake: bigint | null): bigint {
|
||||
return snowflake >> TIMESTAMP_SHIFT;
|
||||
}
|
||||
|
||||
function createBucketRange(startBucket: number, endBucket: number): Array<number> {
|
||||
if (endBucket < startBucket) {
|
||||
return [];
|
||||
}
|
||||
const size = endBucket - startBucket + 1;
|
||||
const range = new Array<number>(size);
|
||||
for (let index = 0; index < size; index += 1) {
|
||||
range[index] = startBucket + index;
|
||||
}
|
||||
return range;
|
||||
}
|
||||
|
||||
export function makeBucket(snowflake: bigint | null): number {
|
||||
const timestamp = getRelativeTimestampForBucket(snowflake);
|
||||
return Number(timestamp / SNOWFLAKE_BUCKET_SIZE_MS);
|
||||
}
|
||||
|
||||
export function makeBucketString(snowflake: string | null): number {
|
||||
if (snowflake == null) {
|
||||
return makeBucket(null);
|
||||
}
|
||||
return makeBucket(BigInt(snowflake));
|
||||
}
|
||||
|
||||
export function makeBuckets(startId: bigint | null, endId: bigint | null = null): Array<number> {
|
||||
const startBucket = makeBucket(startId);
|
||||
const endBucket = makeBucket(endId);
|
||||
return createBucketRange(startBucket, endBucket);
|
||||
}
|
||||
|
||||
export function makeBucketsString(startId: string | null, endId: string | null = null): Array<number> {
|
||||
const startBigInt = startId != null ? BigInt(startId) : null;
|
||||
const endBigInt = endId != null ? BigInt(endId) : null;
|
||||
return makeBuckets(startBigInt, endBigInt);
|
||||
}
|
||||
@@ -1,12 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {
|
||||
createSnowflake,
|
||||
FLUXER_EPOCH,
|
||||
MAX_SEQUENCE,
|
||||
MAX_WORKER_ID,
|
||||
TIMESTAMP_SHIFT,
|
||||
} from '@fluxer/snowflake/src/Snowflake';
|
||||
import {createSnowflake, FLUXER_EPOCH, TIMESTAMP_SHIFT} from '@fluxer/snowflake/src/Snowflake';
|
||||
|
||||
const FLUXER_EPOCH_NUMBER = Number(FLUXER_EPOCH);
|
||||
|
||||
@@ -21,25 +15,6 @@ function toClampedTimestamp(timestamp: number): number {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
function assertValidWorkerId(workerId: number): void {
|
||||
if (!Number.isInteger(workerId)) {
|
||||
throw new Error(`Worker ID must be between 0 and ${MAX_WORKER_ID}`);
|
||||
}
|
||||
const workerIdBigInt = BigInt(workerId);
|
||||
if (workerIdBigInt < 0n || workerIdBigInt > MAX_WORKER_ID) {
|
||||
throw new Error(`Worker ID must be between 0 and ${MAX_WORKER_ID}`);
|
||||
}
|
||||
}
|
||||
|
||||
function assertValidSequenceValue(sequence: number): void {
|
||||
if (!Number.isInteger(sequence)) {
|
||||
throw new Error(`Snowflake sequence number overflow: ${sequence}`);
|
||||
}
|
||||
if (sequence < 0 || sequence > Number(MAX_SEQUENCE)) {
|
||||
throw new Error(`Snowflake sequence number overflow: ${sequence}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function extractTimestamp(snowflake: string): number {
|
||||
return extractTimestampFromSnowflake(snowflake);
|
||||
}
|
||||
@@ -60,40 +35,10 @@ export function fromTimestampBigInt(timestamp: number): bigint {
|
||||
return createSnowflake({timestamp: clampedTimestamp});
|
||||
}
|
||||
|
||||
export function fromTimestampWithSequence(timestamp: number, sequence: SnowflakeSequence): string {
|
||||
const clampedTimestamp = toClampedTimestamp(timestamp);
|
||||
return createSnowflake({
|
||||
timestamp: clampedTimestamp,
|
||||
sequence: sequence.next(),
|
||||
}).toString();
|
||||
}
|
||||
|
||||
export function fromTimestampWithSequenceBigInt(timestamp: number, sequence: SnowflakeSequence, workerId = 0): bigint {
|
||||
assertValidWorkerId(workerId);
|
||||
const clampedTimestamp = toClampedTimestamp(timestamp);
|
||||
return createSnowflake({
|
||||
timestamp: clampedTimestamp,
|
||||
workerId,
|
||||
sequence: sequence.next(),
|
||||
});
|
||||
}
|
||||
|
||||
export function atPreviousMillisecond(snowflake: string): string {
|
||||
return fromTimestamp(extractTimestamp(snowflake) - 1);
|
||||
}
|
||||
|
||||
export function atPreviousMillisecondBigInt(snowflake: bigint): bigint {
|
||||
return fromTimestampBigInt(extractTimestampBigInt(snowflake) - 1);
|
||||
}
|
||||
|
||||
export function atNextMillisecond(snowflake: string): string {
|
||||
return fromTimestamp(extractTimestamp(snowflake) + 1);
|
||||
}
|
||||
|
||||
export function atNextMillisecondBigInt(snowflake: bigint): bigint {
|
||||
return fromTimestampBigInt(extractTimestampBigInt(snowflake) + 1);
|
||||
}
|
||||
|
||||
export function compare(snowflake1: string | null, snowflake2: string | null): number {
|
||||
if (snowflake1 === snowflake2) {
|
||||
return 0;
|
||||
@@ -113,19 +58,6 @@ export function compare(snowflake1: string | null, snowflake2: string | null): n
|
||||
return snowflake1 > snowflake2 ? 1 : -1;
|
||||
}
|
||||
|
||||
export function compareBigInt(snowflake1: bigint | null, snowflake2: bigint | null): number {
|
||||
if (snowflake1 === snowflake2) {
|
||||
return 0;
|
||||
}
|
||||
if (snowflake2 == null) {
|
||||
return 1;
|
||||
}
|
||||
if (snowflake1 == null) {
|
||||
return -1;
|
||||
}
|
||||
return snowflake1 > snowflake2 ? 1 : -1;
|
||||
}
|
||||
|
||||
export function isProbablyAValidSnowflake(value: string | null | undefined): boolean {
|
||||
if (value == null) {
|
||||
return false;
|
||||
@@ -146,14 +78,6 @@ export function sortBySnowflakeDesc<
|
||||
return [...items].sort((a, b) => compare(b.id, a.id));
|
||||
}
|
||||
|
||||
export function sortBySnowflakeDescBigInt<
|
||||
T extends {
|
||||
id: bigint;
|
||||
},
|
||||
>(items: ReadonlyArray<T>): Array<T> {
|
||||
return [...items].sort((a, b) => compareBigInt(b.id, a.id));
|
||||
}
|
||||
|
||||
export function age(snowflake: string): number {
|
||||
const timestamp = extractTimestamp(snowflake);
|
||||
if (Number.isNaN(timestamp)) {
|
||||
@@ -162,11 +86,6 @@ export function age(snowflake: string): number {
|
||||
return Date.now() - timestamp;
|
||||
}
|
||||
|
||||
export function ageBigInt(snowflake: bigint): number {
|
||||
const timestamp = extractTimestampBigInt(snowflake);
|
||||
return Date.now() - timestamp;
|
||||
}
|
||||
|
||||
export function extractTimestampFromSnowflake(snowflake: string, epoch?: string | bigint): number {
|
||||
try {
|
||||
const epochBigInt = epoch != null ? (typeof epoch === 'string' ? BigInt(epoch) : epoch) : FLUXER_EPOCH;
|
||||
@@ -183,43 +102,3 @@ export function extractTimestampFromSnowflakeAsDate(snowflake: string, epoch?: s
|
||||
}
|
||||
return new Date(timestamp);
|
||||
}
|
||||
|
||||
export function extractTimestampFromSnowflakeAsDateBigInt(snowflake: bigint): Date {
|
||||
return new Date(extractTimestampBigInt(snowflake));
|
||||
}
|
||||
|
||||
export interface SnowflakeSequenceOptions {
|
||||
initialValue?: number;
|
||||
}
|
||||
|
||||
export class SnowflakeSequence {
|
||||
private seq: number;
|
||||
|
||||
constructor(options: SnowflakeSequenceOptions = {}) {
|
||||
const initialValue = options.initialValue ?? 0;
|
||||
assertValidSequenceValue(initialValue);
|
||||
this.seq = initialValue;
|
||||
}
|
||||
|
||||
next(): number {
|
||||
if (this.seq > Number(MAX_SEQUENCE)) {
|
||||
throw new Error(`Snowflake sequence number overflow: ${this.seq}`);
|
||||
}
|
||||
const current = this.seq;
|
||||
this.seq += 1;
|
||||
return current;
|
||||
}
|
||||
|
||||
willOverflowNext(): boolean {
|
||||
return this.seq > Number(MAX_SEQUENCE);
|
||||
}
|
||||
|
||||
peek(): number {
|
||||
return this.seq;
|
||||
}
|
||||
|
||||
reset(nextValue = 0): void {
|
||||
assertValidSequenceValue(nextValue);
|
||||
this.seq = nextValue;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user