2020-10-22 06:10:51 +00:00
|
|
|
export class Snowflake {
|
2020-10-22 06:48:43 +00:00
|
|
|
id: string
|
2020-10-22 15:50:47 +00:00
|
|
|
constructor (id: string) {
|
2020-10-22 06:48:43 +00:00
|
|
|
this.id = id
|
2020-10-22 06:10:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:50:47 +00:00
|
|
|
deconstruct () {
|
|
|
|
const snowflake = BigInt.asUintN(64, BigInt(this.id))
|
2020-10-22 06:10:51 +00:00
|
|
|
const res = {
|
2020-10-22 15:50:47 +00:00
|
|
|
timestamp: ((snowflake << BigInt(22)) + BigInt(1420070400000)).toString(),
|
|
|
|
workerId: ((snowflake & BigInt(0x3e0000)) >> BigInt(17)).toString(),
|
|
|
|
processId: ((snowflake & BigInt(0x1f000)) >> BigInt(12)).toString(),
|
|
|
|
increment: (snowflake & BigInt(0xfff)).toString()
|
2020-10-22 06:48:43 +00:00
|
|
|
}
|
|
|
|
return res
|
2020-10-22 06:10:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-22 15:50:47 +00:00
|
|
|
|
|
|
|
// BigInt라서 이걸 어케 할까 고심끝에 나온게 toString 읍
|
|
|
|
// 엄...
|
|
|
|
|
|
|
|
// deconstruct가 소멸자임? 색 봐서는 아닌거같은데
|