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
*/
get entities(): ReadonlyArray<MessageEntity> {
if (!this._entities) {
this._entities = this.tos.entities
return (this._entities ??= this.tos.entities
.map((it) => MessageEntity._parse(it))
.filter((it) => it !== null) as MessageEntity[]
}
return this._entities
.filter((it) => it !== null) as MessageEntity[])
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -403,7 +403,7 @@ export class Message {
*
* For unsupported events, use `.raw.action` directly.
*/
get action(): MessageAction {
get action(): MessageAction | null {
if (!this._action) {
if (this.raw._ === 'message') {
this._action = null
@ -412,7 +412,7 @@ export class Message {
}
}
return this._action!
return this._action
}
private _media?: MessageMedia
@ -431,7 +431,11 @@ export class Message {
) {
this._media = null
} 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 []
}
if (!this._recentReactions) {
this._recentReactions = this.raw.recentReactions.map(
(reaction) =>
new PeerReaction(this.client, reaction, this._peers)
)
}
return this._recentReactions
return (this._recentReactions ??= this.raw.recentReactions.map(
(reaction) => new PeerReaction(this.client, reaction, this._peers)
))
}
/**

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -30,14 +30,10 @@ export class BotStoppedUpdate {
* User who stopped or restarted the bot
*/
get user(): User {
if (!this._user) {
this._user = new User(
return (this._user ??= new User(
this.client,
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
*/
get recentRequesters(): User[] {
if (!this._recentRequesters) {
this._recentRequesters = this.raw.recentRequesters.map(
return (this._recentRequesters ??= this.raw.recentRequesters.map(
(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.
*/
get actor(): User {
if (!this._actor) {
this._actor = new User(
return (this._actor ??= new User(
this.client,
this._peers.user(this.raw.actorId)
)
}
return this._actor
))
}
private _user?: User
@ -192,14 +188,10 @@ export class ChatMemberUpdate {
* User representing the chat member whose status was changed.
*/
get user(): User {
if (!this._user) {
this._user = new User(
return (this._user ??= new User(
this.client,
this._peers.user(this.raw.userId)
)
}
return this._user
))
}
private _oldMember?: ChatMember
@ -209,15 +201,11 @@ export class ChatMemberUpdate {
get oldMember(): ChatMember | null {
if (!this.raw.prevParticipant) return null
if (!this._oldMember) {
this._oldMember = new ChatMember(
return (this._oldMember ??= new ChatMember(
this.client,
this.raw.prevParticipant,
this._peers
)
}
return this._oldMember
))
}
private _newMember?: ChatMember
@ -227,15 +215,11 @@ export class ChatMemberUpdate {
get newMember(): ChatMember | null {
if (!this.raw.newParticipant) return null
if (!this._newMember) {
this._newMember = new ChatMember(
return (this._newMember ??= new ChatMember(
this.client,
this.raw.newParticipant,
this._peers
)
}
return this._newMember
))
}
private _inviteLink?: ChatInviteLink
@ -245,11 +229,10 @@ export class ChatMemberUpdate {
get inviteLink(): ChatInviteLink | null {
if (!this.raw.invite) return null
if (!this._inviteLink) {
this._inviteLink = new ChatInviteLink(this.client, this.raw.invite)
}
return this._inviteLink
return (this._inviteLink ??= new ChatInviteLink(
this.client,
this.raw.invite
))
}
}

View file

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

View file

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

View file

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