refactor(core): moved storage load&save to methods to allow overriding logic in subclasses
This commit is contained in:
parent
95069d0120
commit
0b361d0b8d
1 changed files with 14 additions and 6 deletions
|
@ -286,6 +286,14 @@ export class BaseTelegramClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async _loadStorage(): Promise<void> {
|
||||||
|
await this.storage.load?.()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async _saveStorage(): Promise<void> {
|
||||||
|
await this.storage.save?.()
|
||||||
|
}
|
||||||
|
|
||||||
private _cleanupPrimaryConnection(forever = false): void {
|
private _cleanupPrimaryConnection(forever = false): void {
|
||||||
if (forever && this.primaryConnection) this.primaryConnection.destroy()
|
if (forever && this.primaryConnection) this.primaryConnection.destroy()
|
||||||
if (this._keepAliveInterval) clearInterval(this._keepAliveInterval)
|
if (this._keepAliveInterval) clearInterval(this._keepAliveInterval)
|
||||||
|
@ -303,7 +311,7 @@ export class BaseTelegramClient {
|
||||||
})
|
})
|
||||||
this.primaryConnection.on('usable', async () => {
|
this.primaryConnection.on('usable', async () => {
|
||||||
this._keepAliveInterval = setInterval(async () => {
|
this._keepAliveInterval = setInterval(async () => {
|
||||||
await this.storage.save?.()
|
await this._saveStorage()
|
||||||
|
|
||||||
// according to telethon, "We need to send some content-related request at least hourly
|
// according to telethon, "We need to send some content-related request at least hourly
|
||||||
// for Telegram to keep delivering updates, otherwise they will just stop even if we're connected.
|
// for Telegram to keep delivering updates, otherwise they will just stop even if we're connected.
|
||||||
|
@ -346,7 +354,7 @@ export class BaseTelegramClient {
|
||||||
)
|
)
|
||||||
this.primaryConnection.on('key-change', async (key) => {
|
this.primaryConnection.on('key-change', async (key) => {
|
||||||
this.storage.setAuthKeyFor(this._primaryDc.id, key)
|
this.storage.setAuthKeyFor(this._primaryDc.id, key)
|
||||||
await this.storage.save?.()
|
await this._saveStorage()
|
||||||
})
|
})
|
||||||
this.primaryConnection.on('error', (err) => this._emitError(err))
|
this.primaryConnection.on('error', (err) => this._emitError(err))
|
||||||
}
|
}
|
||||||
|
@ -358,7 +366,7 @@ export class BaseTelegramClient {
|
||||||
* implicitly the first time you call {@link call}.
|
* implicitly the first time you call {@link call}.
|
||||||
*/
|
*/
|
||||||
async connect(): Promise<void> {
|
async connect(): Promise<void> {
|
||||||
await this.storage.load?.()
|
await this._loadStorage()
|
||||||
const primaryDc = await this.storage.getDefaultDc()
|
const primaryDc = await this.storage.getDefaultDc()
|
||||||
if (primaryDc !== null) this._primaryDc = primaryDc
|
if (primaryDc !== null) this._primaryDc = primaryDc
|
||||||
|
|
||||||
|
@ -379,7 +387,7 @@ export class BaseTelegramClient {
|
||||||
// close additional connections
|
// close additional connections
|
||||||
this._additionalConnections.forEach((conn) => conn.destroy())
|
this._additionalConnections.forEach((conn) => conn.destroy())
|
||||||
|
|
||||||
await this.storage.save?.()
|
await this._saveStorage()
|
||||||
await this.storage.destroy?.()
|
await this.storage.destroy?.()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -430,7 +438,7 @@ export class BaseTelegramClient {
|
||||||
|
|
||||||
this._primaryDc = newDc
|
this._primaryDc = newDc
|
||||||
await this.storage.setDefaultDc(newDc)
|
await this.storage.setDefaultDc(newDc)
|
||||||
await this.storage.save?.()
|
await this._saveStorage()
|
||||||
this.primaryConnection.changeDc(newDc)
|
this.primaryConnection.changeDc(newDc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -593,7 +601,7 @@ export class BaseTelegramClient {
|
||||||
|
|
||||||
// connection.authKey was already generated at this point
|
// connection.authKey was already generated at this point
|
||||||
this.storage.setAuthKeyFor(dc.id, connection.authKey)
|
this.storage.setAuthKeyFor(dc.id, connection.authKey)
|
||||||
await this.storage.save?.()
|
await this._saveStorage()
|
||||||
}
|
}
|
||||||
|
|
||||||
this._additionalConnections.push(connection)
|
this._additionalConnections.push(connection)
|
||||||
|
|
Loading…
Reference in a new issue