refactor: use ??= for memoization

This commit is contained in:
teidesu 2022-10-30 22:24:07 +03:00
parent 70b09903f1
commit bc2ed98b14
27 changed files with 188 additions and 358 deletions

View file

@ -36,13 +36,9 @@ export class TermsOfService {
* Terms of Service entities text * Terms of Service entities text
*/ */
get entities(): ReadonlyArray<MessageEntity> { get entities(): ReadonlyArray<MessageEntity> {
if (!this._entities) { return (this._entities ??= this.tos.entities
this._entities = this.tos.entities .map((it) => MessageEntity._parse(it))
.map((it) => MessageEntity._parse(it)) .filter((it) => it !== null) as MessageEntity[])
.filter((it) => it !== null) as MessageEntity[]
}
return this._entities
} }
} }

View file

@ -33,14 +33,10 @@ export class CallbackQuery {
* User who has pressed the button * User who has pressed the button
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
/** /**
@ -155,11 +151,7 @@ export class CallbackQuery {
get dataStr(): string | null { get dataStr(): string | null {
if (!this.raw.data) return null if (!this.raw.data) return null
if (!this._dataStr) { return (this._dataStr ??= this.raw.data.toString('utf8'))
this._dataStr = this.raw.data.toString('utf8')
}
return this._dataStr
} }
/** /**

View file

@ -19,14 +19,10 @@ export class GameHighScore {
* User who has scored this score * User who has scored this score
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
/** /**

View file

@ -33,14 +33,10 @@ export class InlineQuery {
* User who sent this query * User who sent this query
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
/** /**
@ -59,11 +55,7 @@ export class InlineQuery {
get location(): Location | null { get location(): Location | null {
if (this.raw.geo?._ !== 'geoPoint') return null if (this.raw.geo?._ !== 'geoPoint') return null
if (!this._location) { return (this._location ??= new Location(this.client, this.raw.geo))
this._location = new Location(this.client, this.raw.geo)
}
return this._location
} }
/** /**

View file

@ -131,20 +131,16 @@ export class RawDocument extends FileLocation {
* representing this document. * representing this document.
*/ */
get fileId(): string { get fileId(): string {
if (!this._fileId) { return (this._fileId ??= toFileId({
this._fileId = toFileId({ type: this._fileIdType(),
type: this._fileIdType(), dcId: this.raw.dcId,
dcId: this.raw.dcId, fileReference: this.raw.fileReference,
fileReference: this.raw.fileReference, location: {
location: { _: 'common',
_: 'common', id: this.raw.id,
id: this.raw.id, accessHash: this.raw.accessHash,
accessHash: this.raw.accessHash, },
}, }))
})
}
return this._fileId
} }
protected _uniqueFileId?: string protected _uniqueFileId?: string
@ -152,14 +148,10 @@ export class RawDocument extends FileLocation {
* Get a unique File ID representing this document. * Get a unique File ID representing this document.
*/ */
get uniqueFileId(): string { get uniqueFileId(): string {
if (!this._uniqueFileId) { return (this._uniqueFileId ??= toUniqueFileId(td.FileType.Document, {
this._uniqueFileId = toUniqueFileId(td.FileType.Document, { _: 'common',
_: 'common', id: this.raw.id,
id: this.raw.id, }))
})
}
return this._uniqueFileId
} }
} }

View file

@ -45,11 +45,7 @@ export class Game {
get photo(): Photo | null { get photo(): Photo | null {
if (this.game.photo._ === 'photoEmpty') return null if (this.game.photo._ === 'photoEmpty') return null
if (!this._photo) { return (this._photo ??= new Photo(this.client, this.game.photo))
this._photo = new Photo(this.client, this.game.photo)
}
return this._photo
} }
private _animation?: Video | null private _animation?: Video | null

View file

@ -41,15 +41,11 @@ export class InvoiceExtendedMediaPreview {
return null return null
} }
if (!this._thumbnail) { return (this._thumbnail ??= new Thumbnail(
this._thumbnail = new Thumbnail( this.client,
this.client, this.raw,
this.raw, this.raw.thumb
this.raw.thumb ))
)
}
return this._thumbnail
} }
/** /**
@ -107,11 +103,7 @@ export class Invoice {
get photo(): WebDocument | null { get photo(): WebDocument | null {
if (!this.raw.photo) return null if (!this.raw.photo) return null
if (!this._photo) { return (this._photo ??= new WebDocument(this.client, this.raw.photo))
this._photo = new WebDocument(this.client, this.raw.photo)
}
return this._photo
} }
/** /**
@ -168,14 +160,10 @@ export class Invoice {
if (this.raw.extendedMedia?._ !== 'messageExtendedMediaPreview') if (this.raw.extendedMedia?._ !== 'messageExtendedMediaPreview')
throw new MtArgumentError('No extended media preview available') throw new MtArgumentError('No extended media preview available')
if (!this._extendedMediaPreview) { return (this._extendedMediaPreview ??= new InvoiceExtendedMediaPreview(
this._extendedMediaPreview = new InvoiceExtendedMediaPreview( this.client,
this.client, this.raw.extendedMedia
this.raw.extendedMedia ))
)
}
return this._extendedMediaPreview
} }
private _extendedMedia?: MessageMedia private _extendedMedia?: MessageMedia
@ -185,19 +173,15 @@ export class Invoice {
* Otherwise, throws an error. * Otherwise, throws an error.
*/ */
get extendedMedia(): MessageMedia { get extendedMedia(): MessageMedia {
if (this.raw.extendedMedia?._ !== "messageExtendedMedia") { if (this.raw.extendedMedia?._ !== 'messageExtendedMedia') {
throw new MtArgumentError('No extended media available') throw new MtArgumentError('No extended media available')
} }
if (!this._extendedMedia) { return (this._extendedMedia ??= _messageMediaFromTl(
this._extendedMedia = _messageMediaFromTl( this.client,
this.client, null,
null, this.raw.extendedMedia.media
this.raw.extendedMedia.media ))
)
}
return this._extendedMedia
} }
/** /**

View file

@ -204,8 +204,8 @@ export class Sticker extends RawDocument {
if (this.attr._ !== 'documentAttributeSticker' || !this.attr.maskCoords) if (this.attr._ !== 'documentAttributeSticker' || !this.attr.maskCoords)
return null return null
const raw = this.attr.maskCoords
if (!this._maskPosition) { if (!this._maskPosition) {
const raw = this.attr.maskCoords
this._maskPosition = { this._maskPosition = {
point: MASK_POS[raw.n], point: MASK_POS[raw.n],
x: raw.x, x: raw.x,

View file

@ -60,15 +60,11 @@ export class Video extends RawDocument {
* (represented either by actual GIF or a silent MP4 video) * (represented either by actual GIF or a silent MP4 video)
*/ */
get isAnimation(): boolean { get isAnimation(): boolean {
if (!this._isAnimation) { return (this._isAnimation ??=
this._isAnimation = this.attr._ === 'documentAttributeImageSize' ||
this.attr._ === 'documentAttributeImageSize' || this.raw.attributes.some(
this.raw.attributes.some( (it) => it._ === 'documentAttributeAnimated'
(it) => it._ === 'documentAttributeAnimated' ))
)
}
return this._isAnimation
} }
/** /**

View file

@ -403,7 +403,7 @@ export class Message {
* *
* For unsupported events, use `.raw.action` directly. * For unsupported events, use `.raw.action` directly.
*/ */
get action(): MessageAction { get action(): MessageAction | null {
if (!this._action) { if (!this._action) {
if (this.raw._ === 'message') { if (this.raw._ === 'message') {
this._action = null this._action = null
@ -412,7 +412,7 @@ export class Message {
} }
} }
return this._action! return this._action
} }
private _media?: MessageMedia private _media?: MessageMedia
@ -431,7 +431,11 @@ export class Message {
) { ) {
this._media = null this._media = null
} else { } else {
this._media = _messageMediaFromTl(this.client, this._peers, this.raw.media) this._media = _messageMediaFromTl(
this.client,
this._peers,
this.raw.media
)
} }
} }

View file

@ -113,14 +113,9 @@ export class MessageReactions {
return [] return []
} }
if (!this._recentReactions) { return (this._recentReactions ??= this.raw.recentReactions.map(
this._recentReactions = this.raw.recentReactions.map( (reaction) => new PeerReaction(this.client, reaction, this._peers)
(reaction) => ))
new PeerReaction(this.client, reaction, this._peers)
)
}
return this._recentReactions
} }
/** /**

View file

@ -205,14 +205,10 @@ export class StickerSet {
* (i.e. first sticker should be used as thumbnail) * (i.e. first sticker should be used as thumbnail)
*/ */
get thumbnails(): ReadonlyArray<Thumbnail> { get thumbnails(): ReadonlyArray<Thumbnail> {
if (!this._thumbnails) { return (this._thumbnails ??=
this._thumbnails = this.brief.thumbs?.map(
this.brief.thumbs?.map( (sz) => new Thumbnail(this.client, this.brief, sz)
(sz) => new Thumbnail(this.client, this.brief, sz) ) ?? [])
) ?? []
}
return this._thumbnails
} }
/** /**

View file

@ -538,23 +538,15 @@ export class ChatEvent {
* Actor of the event * Actor of the event
*/ */
get actor(): User { get actor(): User {
if (!this._actor) { return (this._actor ??= new User(
this._actor = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._actor
} }
private _action?: ChatEvent.Action private _action?: ChatEvent.Action
get action(): ChatEvent.Action { get action(): ChatEvent.Action {
if (!this._action) { return (this._action ??= _actionFromTl.call(this, this.raw.action))
this._action = _actionFromTl.call(this, this.raw.action)
}
return this._action!
} }
} }

View file

@ -83,14 +83,10 @@ export class ChatInviteLink {
get creator(): User | null { get creator(): User | null {
if (!this._peers) return null if (!this._peers) return null
if (!this._creator) { return (this._creator ??= new User(
this._creator = new User( this.client,
this.client, this._peers.user(this.raw.adminId)
this._peers.user(this.raw.adminId) ))
)
}
return this._creator
} }
/** /**

View file

@ -18,14 +18,10 @@ export class ChatLocation {
* Location of the chat * Location of the chat
*/ */
get location(): Location { get location(): Location {
if (!this._location) { return (this._location ??= new Location(
this._location = new Location( this.client,
this.client, this.raw.geoPoint as tl.RawGeoPoint
this.raw.geoPoint as tl.RawGeoPoint ))
)
}
return this._location
} }
/** /**

View file

@ -200,11 +200,9 @@ export class ChatMember {
get restrictions(): ChatPermissions | null { get restrictions(): ChatPermissions | null {
if (this.raw._ !== 'channelParticipantBanned') return null if (this.raw._ !== 'channelParticipantBanned') return null
if (!this._restrictions) { return (this._restrictions ??= new ChatPermissions(
this._restrictions = new ChatPermissions(this.raw.bannedRights) this.raw.bannedRights
} ))
return this._restrictions
} }
/** /**

View file

@ -91,21 +91,17 @@ export class ChatPhotoSize extends FileLocation {
* TDLib and Bot API compatible unique File ID representing this size * TDLib and Bot API compatible unique File ID representing this size
*/ */
get uniqueFileId(): string { get uniqueFileId(): string {
if (!this._uniqueFileId) { return (this._uniqueFileId ??= toUniqueFileId(
this._uniqueFileId = toUniqueFileId( tdFileId.FileType.ProfilePhoto,
tdFileId.FileType.ProfilePhoto, {
{ _: 'photo',
_: 'photo', id: this.obj.photoId,
id: this.obj.photoId, source: {
source: { _: 'dialogPhoto',
_: 'dialogPhoto', big: this.big,
big: this.big, } as any,
} as any, }
} ))
)
}
return this._uniqueFileId
} }
} }
@ -133,31 +129,24 @@ export class ChatPhoto {
/** Chat photo file location in small resolution (160x160) */ /** Chat photo file location in small resolution (160x160) */
get small(): ChatPhotoSize { get small(): ChatPhotoSize {
if (!this._smallFile) { return (this._smallFile ??= new ChatPhotoSize(
this._smallFile = new ChatPhotoSize( this.client,
this.client, this.peer,
this.peer, this.obj,
this.obj, false
false ))
)
}
return this._smallFile
} }
private _bigFile?: ChatPhotoSize private _bigFile?: ChatPhotoSize
/** Chat photo file location in big resolution (640x640) */ /** Chat photo file location in big resolution (640x640) */
get big(): ChatPhotoSize { get big(): ChatPhotoSize {
if (!this._bigFile) { return (this._bigFile ??= new ChatPhotoSize(
this._bigFile = new ChatPhotoSize( this.client,
this.client, this.peer,
this.peer, this.obj,
this.obj, true
true ))
)
}
return this._bigFile
} }
private _thumb?: Buffer private _thumb?: Buffer
@ -168,11 +157,7 @@ export class ChatPhoto {
get thumb(): Buffer | null { get thumb(): Buffer | null {
if (!this.obj.strippedThumb) return null if (!this.obj.strippedThumb) return null
if (!this._thumb) { return (this._thumb ??= strippedPhotoToJpg(this.obj.strippedThumb))
this._thumb = strippedPhotoToJpg(this.obj.strippedThumb)
}
return this._thumb
} }
} }

View file

@ -63,11 +63,7 @@ export class ChatPreview {
get photo(): Photo | null { get photo(): Photo | null {
if (this.invite.photo._ === 'photoEmpty') return null if (this.invite.photo._ === 'photoEmpty') return null
if (!this._photo) { return (this._photo ??= new Photo(this.client, this.invite.photo))
this._photo = new Photo(this.client, this.invite.photo)
}
return this._photo
} }
private _someMembers?: User[] private _someMembers?: User[]
@ -79,15 +75,9 @@ export class ChatPreview {
* ordered before others. * ordered before others.
*/ */
get someMembers(): ReadonlyArray<User> { get someMembers(): ReadonlyArray<User> {
if (!this._someMembers) { return (this._someMembers ??= this.invite.participants
this._someMembers = this.invite.participants ? this.invite.participants.map((it) => new User(this.client, it))
? this.invite.participants.map( : [])
(it) => new User(this.client, it)
)
: []
}
return this._someMembers
} }
/** /**

View file

@ -281,15 +281,11 @@ export class Chat {
) )
return null return null
if (!this._photo) { return (this._photo ??= new ChatPhoto(
this._photo = new ChatPhoto( this.client,
this.client, this.inputPeer,
this.inputPeer, this.peer.photo
this.peer.photo ))
)
}
return this._photo
} }
/** /**
@ -386,11 +382,9 @@ export class Chat {
if (!('bannedRights' in this.peer && this.peer.bannedRights)) if (!('bannedRights' in this.peer && this.peer.bannedRights))
return null return null
if (!this._permissions) { return (this._permissions ??= new ChatPermissions(
this._permissions = new ChatPermissions(this.peer.bannedRights) this.peer.bannedRights
} ))
return this._permissions
} }
/** /**
@ -403,13 +397,9 @@ export class Chat {
) )
return null return null
if (!this._permissions) { return (this._permissions ??= new ChatPermissions(
this._permissions = new ChatPermissions( this.peer.defaultBannedRights
this.peer.defaultBannedRights ))
)
}
return this._permissions
} }
/** /**
@ -439,14 +429,10 @@ export class Chat {
) )
return null return null
if (!this._location) { return (this._location ??= new ChatLocation(
this._location = new ChatLocation( this.client,
this.client, this.fullPeer.location
this.fullPeer.location ))
)
}
return this._location
} }
private _linkedChat?: Chat private _linkedChat?: Chat
@ -469,8 +455,7 @@ export class Chat {
get user(): User | null { get user(): User | null {
if (this.peer._ !== 'user') return null if (this.peer._ !== 'user') return null
if (!this._user) this._user = new User(this.client, this.peer) return (this._user ??= new User(this.client, this.peer))
return this._user
} }
/** @internal */ /** @internal */

View file

@ -252,15 +252,11 @@ export class User {
get photo(): ChatPhoto | null { get photo(): ChatPhoto | null {
if (this.raw.photo?._ !== 'userProfilePhoto') return null if (this.raw.photo?._ !== 'userProfilePhoto') return null
if (!this._photo) { return (this._photo ??= new ChatPhoto(
this._photo = new ChatPhoto( this.client,
this.client, this.inputPeer,
this.inputPeer, this.raw.photo
this.raw.photo ))
)
}
return this._photo
} }
/** /**

View file

@ -31,14 +31,10 @@ export class BotChatJoinRequestUpdate {
* Object containing the chat information. * Object containing the chat information.
*/ */
get chat(): Chat { get chat(): Chat {
if (!this._chat) { return (this._chat ??= new Chat(
this._chat = new Chat( this.client,
this.client, this._peers.chat(getBarePeerId(this.raw.peer))
this._peers.chat(getBarePeerId(this.raw.peer)) ))
)
}
return this._chat
} }
/** /**
@ -53,14 +49,10 @@ export class BotChatJoinRequestUpdate {
* Object containing the user information. * Object containing the user information.
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
/** /**
@ -83,11 +75,10 @@ export class BotChatJoinRequestUpdate {
* Invite link used to request joining. * Invite link used to request joining.
*/ */
get invite(): ChatInviteLink { get invite(): ChatInviteLink {
if (!this._invite) { return (this._invite ??= new ChatInviteLink(
this._invite = new ChatInviteLink(this.client, this.raw.invite) this.client,
} this.raw.invite
))
return this._invite
} }
/** /**

View file

@ -30,14 +30,10 @@ export class BotStoppedUpdate {
* User who stopped or restarted the bot * User who stopped or restarted the bot
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
/** /**

View file

@ -41,13 +41,9 @@ export class ChatJoinRequestUpdate {
* Users who recently requested to join the chat * Users who recently requested to join the chat
*/ */
get recentRequesters(): User[] { get recentRequesters(): User[] {
if (!this._recentRequesters) { return (this._recentRequesters ??= this.raw.recentRequesters.map(
this._recentRequesters = this.raw.recentRequesters.map( (id) => new User(this.client, this._peers.user(id))
(id) => new User(this.client, this._peers.user(id)) ))
)
}
return this._recentRequesters
} }
/** /**

View file

@ -177,14 +177,10 @@ export class ChatMemberUpdate {
* Can be chat/channel administrator or the {@link user} themself. * Can be chat/channel administrator or the {@link user} themself.
*/ */
get actor(): User { get actor(): User {
if (!this._actor) { return (this._actor ??= new User(
this._actor = new User( this.client,
this.client, this._peers.user(this.raw.actorId)
this._peers.user(this.raw.actorId) ))
)
}
return this._actor
} }
private _user?: User private _user?: User
@ -192,14 +188,10 @@ export class ChatMemberUpdate {
* User representing the chat member whose status was changed. * User representing the chat member whose status was changed.
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
private _oldMember?: ChatMember private _oldMember?: ChatMember
@ -209,15 +201,11 @@ export class ChatMemberUpdate {
get oldMember(): ChatMember | null { get oldMember(): ChatMember | null {
if (!this.raw.prevParticipant) return null if (!this.raw.prevParticipant) return null
if (!this._oldMember) { return (this._oldMember ??= new ChatMember(
this._oldMember = new ChatMember( this.client,
this.client, this.raw.prevParticipant,
this.raw.prevParticipant, this._peers
this._peers ))
)
}
return this._oldMember
} }
private _newMember?: ChatMember private _newMember?: ChatMember
@ -227,15 +215,11 @@ export class ChatMemberUpdate {
get newMember(): ChatMember | null { get newMember(): ChatMember | null {
if (!this.raw.newParticipant) return null if (!this.raw.newParticipant) return null
if (!this._newMember) { return (this._newMember ??= new ChatMember(
this._newMember = new ChatMember( this.client,
this.client, this.raw.newParticipant,
this.raw.newParticipant, this._peers
this._peers ))
)
}
return this._newMember
} }
private _inviteLink?: ChatInviteLink private _inviteLink?: ChatInviteLink
@ -245,11 +229,10 @@ export class ChatMemberUpdate {
get inviteLink(): ChatInviteLink | null { get inviteLink(): ChatInviteLink | null {
if (!this.raw.invite) return null if (!this.raw.invite) return null
if (!this._inviteLink) { return (this._inviteLink ??= new ChatInviteLink(
this._inviteLink = new ChatInviteLink(this.client, this.raw.invite) this.client,
} this.raw.invite
))
return this._inviteLink
} }
} }

View file

@ -31,14 +31,10 @@ export class ChosenInlineResult {
* User who has chosen the query * User who has chosen the query
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
/** /**
@ -56,11 +52,7 @@ export class ChosenInlineResult {
get location(): Location | null { get location(): Location | null {
if (this.raw.geo?._ !== 'geoPoint') return null if (this.raw.geo?._ !== 'geoPoint') return null
if (!this._location) { return (this._location ??= new Location(this.client, this.raw.geo))
this._location = new Location(this.client, this.raw.geo)
}
return this._location
} }
/** /**

View file

@ -28,14 +28,10 @@ export class PollVoteUpdate {
* User who has voted * User who has voted
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User( this.client,
this.client, this._peers.user(this.raw.userId)
this._peers.user(this.raw.userId) ))
)
}
return this._user
} }
/** /**

View file

@ -30,11 +30,10 @@ export class PreCheckoutQuery {
* User who sent the query * User who sent the query
*/ */
get user(): User { get user(): User {
if (!this._user) { return (this._user ??= new User(
this._user = new User(this.client, this._peers.user(this.userId)) this.client,
} this._peers.user(this.userId)
))
return this._user
} }
/** /**