diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index d0b0b714..3123b0b6 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -186,8 +186,12 @@ import { User, UsersIndex, } from './types' -import { MaybeArray, MaybeAsync, TelegramConnection } from '@mtcute/core' -import { Lock } from './utils/lock' +import { + AsyncLock, + MaybeArray, + MaybeAsync, + TelegramConnection, +} from '@mtcute/core' import { tdFileId } from '@mtcute/file-id' export interface TelegramClient extends BaseTelegramClient { @@ -2974,7 +2978,7 @@ export class TelegramClient extends BaseTelegramClient { protected _connectionsForInline: Record protected _parseModes: Record protected _defaultParseMode: string | null - protected _updLock: Lock + protected _updLock: AsyncLock protected _pts: number protected _date: number protected _cpts: Record @@ -2986,7 +2990,7 @@ export class TelegramClient extends BaseTelegramClient { this._connectionsForInline = {} this._parseModes = {} this._defaultParseMode = null - this._updLock = new Lock() + this._updLock = new AsyncLock() // we dont need to initialize state fields since // they are always loaded either from the server, or from storage. diff --git a/packages/client/src/methods/_imports.ts b/packages/client/src/methods/_imports.ts index 06cebd30..06eb168e 100644 --- a/packages/client/src/methods/_imports.ts +++ b/packages/client/src/methods/_imports.ts @@ -38,14 +38,16 @@ import { ChatInviteLink, UsersIndex, ChatsIndex, - GameHighScore + GameHighScore, } from '../types' // @copy -import { MaybeArray, MaybeAsync, TelegramConnection } from '@mtcute/core' - -// @copy -import { Lock } from '../utils/lock' +import { + MaybeArray, + MaybeAsync, + TelegramConnection, + AsyncLock, +} from '@mtcute/core' // @copy import { tdFileId } from '@mtcute/file-id' diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index b3a7d5f6..c285d08d 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -7,9 +7,8 @@ import { peerToInputPeer, } from '../utils/peer-utils' import { extractChannelIdFromUpdate } from '../utils/misc-utils' -import { Lock } from '../utils/lock' import bigInt from 'big-integer' -import { MAX_CHANNEL_ID } from '@mtcute/core' +import { AsyncLock, MAX_CHANNEL_ID } from '@mtcute/core' import { isDummyUpdate, isDummyUpdates } from '../utils/updates-utils' import { ChatsIndex, UsersIndex } from '../types' @@ -24,7 +23,7 @@ const debug = require('debug')('mtcute:upds') // @extension interface UpdatesState { - _updLock: Lock + _updLock: AsyncLock // accessing storage every time might be expensive, // so store everything here, and load & save @@ -37,7 +36,7 @@ interface UpdatesState { // @initialize function _initializeUpdates(this: TelegramClient) { - this._updLock = new Lock() + this._updLock = new AsyncLock() // we dont need to initialize state fields since // they are always loaded either from the server, or from storage. diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index a237545f..9517af95 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -6,6 +6,7 @@ export * from './types' export * from './utils/crypto' export * from './utils/peer-utils' export * from './utils/tl-json' +export * from './utils/async-lock' export { BinaryReader } from './utils/binary/binary-reader' export { BinaryWriter } from './utils/binary/binary-writer' diff --git a/packages/client/src/utils/lock.ts b/packages/core/src/utils/async-lock.ts similarity index 90% rename from packages/client/src/utils/lock.ts rename to packages/core/src/utils/async-lock.ts index 18330ba0..abe3e8d2 100644 --- a/packages/client/src/utils/lock.ts +++ b/packages/core/src/utils/async-lock.ts @@ -4,8 +4,11 @@ interface OneWayLinkedList { n?: OneWayLinkedList } -/** @internal */ -export class Lock { +/** + * Simple class implementing a semaphore like + * behaviour. + */ +export class AsyncLock { private _first?: OneWayLinkedList private _last?: OneWayLinkedList