@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
25 lines (24 loc) • 802 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MsgId = void 0;
class MsgId {
referenceClock = BigInt(0);
lastTime = BigInt(0);
msgIdOffset = BigInt(0);
serverTime = BigInt(0);
constructor() { }
getMsgId() {
const now = BigInt(Math.floor(Date.now() / 1000)) - this.referenceClock + this.serverTime;
this.msgIdOffset = now === this.lastTime ? this.msgIdOffset + BigInt(4) : BigInt(0);
const msgId = now * BigInt(2 ** 32) + this.msgIdOffset;
this.lastTime = now;
return msgId;
}
setServerTime(serverTime) {
if (!this.serverTime) {
this.referenceClock = BigInt(Math.floor(Date.now() / 1000));
this.serverTime = serverTime;
}
}
}
exports.MsgId = MsgId;