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, User,
UsersIndex, UsersIndex,
} from './types' } from './types'
import { MaybeArray, MaybeAsync, TelegramConnection } from '@mtcute/core' import {
import { Lock } from './utils/lock' AsyncLock,
MaybeArray,
MaybeAsync,
TelegramConnection,
} from '@mtcute/core'
import { tdFileId } from '@mtcute/file-id' import { tdFileId } from '@mtcute/file-id'
export interface TelegramClient extends BaseTelegramClient { export interface TelegramClient extends BaseTelegramClient {
@ -2974,7 +2978,7 @@ export class TelegramClient extends BaseTelegramClient {
protected _connectionsForInline: Record<number, TelegramConnection> protected _connectionsForInline: Record<number, TelegramConnection>
protected _parseModes: Record<string, IMessageEntityParser> protected _parseModes: Record<string, IMessageEntityParser>
protected _defaultParseMode: string | null protected _defaultParseMode: string | null
protected _updLock: Lock protected _updLock: AsyncLock
protected _pts: number protected _pts: number
protected _date: number protected _date: number
protected _cpts: Record<number, number> protected _cpts: Record<number, number>
@ -2986,7 +2990,7 @@ export class TelegramClient extends BaseTelegramClient {
this._connectionsForInline = {} this._connectionsForInline = {}
this._parseModes = {} this._parseModes = {}
this._defaultParseMode = null this._defaultParseMode = null
this._updLock = new Lock() this._updLock = new AsyncLock()
// we dont need to initialize state fields since // we dont need to initialize state fields since
// they are always loaded either from the server, or from storage. // they are always loaded either from the server, or from storage.

View file

@ -38,14 +38,16 @@ import {
ChatInviteLink, ChatInviteLink,
UsersIndex, UsersIndex,
ChatsIndex, ChatsIndex,
GameHighScore GameHighScore,
} from '../types' } from '../types'
// @copy // @copy
import { MaybeArray, MaybeAsync, TelegramConnection } from '@mtcute/core' import {
MaybeArray,
// @copy MaybeAsync,
import { Lock } from '../utils/lock' TelegramConnection,
AsyncLock,
} from '@mtcute/core'
// @copy // @copy
import { tdFileId } from '@mtcute/file-id' import { tdFileId } from '@mtcute/file-id'

View file

@ -7,9 +7,8 @@ import {
peerToInputPeer, peerToInputPeer,
} from '../utils/peer-utils' } from '../utils/peer-utils'
import { extractChannelIdFromUpdate } from '../utils/misc-utils' import { extractChannelIdFromUpdate } from '../utils/misc-utils'
import { Lock } from '../utils/lock'
import bigInt from 'big-integer' 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 { isDummyUpdate, isDummyUpdates } from '../utils/updates-utils'
import { ChatsIndex, UsersIndex } from '../types' import { ChatsIndex, UsersIndex } from '../types'
@ -24,7 +23,7 @@ const debug = require('debug')('mtcute:upds')
// @extension // @extension
interface UpdatesState { interface UpdatesState {
_updLock: Lock _updLock: AsyncLock
// accessing storage every time might be expensive, // accessing storage every time might be expensive,
// so store everything here, and load & save // so store everything here, and load & save
@ -37,7 +36,7 @@ interface UpdatesState {
// @initialize // @initialize
function _initializeUpdates(this: TelegramClient) { function _initializeUpdates(this: TelegramClient) {
this._updLock = new Lock() this._updLock = new AsyncLock()
// we dont need to initialize state fields since // we dont need to initialize state fields since
// they are always loaded either from the server, or from storage. // 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/crypto'
export * from './utils/peer-utils' export * from './utils/peer-utils'
export * from './utils/tl-json' export * from './utils/tl-json'
export * from './utils/async-lock'
export { BinaryReader } from './utils/binary/binary-reader' export { BinaryReader } from './utils/binary/binary-reader'
export { BinaryWriter } from './utils/binary/binary-writer' export { BinaryWriter } from './utils/binary/binary-writer'

View file

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