refactor: moved async lock to core and exported it

This commit is contained in:
teidesu 2021-05-16 23:05:53 +03:00
parent 9d8b09c5ff
commit 82fb3d7e44
5 changed files with 24 additions and 15 deletions

View file

@ -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<number, TelegramConnection>
protected _parseModes: Record<string, IMessageEntityParser>
protected _defaultParseMode: string | null
protected _updLock: Lock
protected _updLock: AsyncLock
protected _pts: number
protected _date: number
protected _cpts: Record<number, number>
@ -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.

View file

@ -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'

View file

@ -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.

View file

@ -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'

View file

@ -4,8 +4,11 @@ interface OneWayLinkedList<T> {
n?: OneWayLinkedList<T>
}
/** @internal */
export class Lock {
/**
* Simple class implementing a semaphore like
* behaviour.
*/
export class AsyncLock {
private _first?: OneWayLinkedList<LockInfo>
private _last?: OneWayLinkedList<LockInfo>