From bc2ed98b146e4957c0ae547226c65bd4cce9a198 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Sun, 30 Oct 2022 22:24:07 +0300 Subject: [PATCH] refactor: use ??= for memoization --- .../client/src/types/auth/terms-of-service.ts | 10 +-- .../client/src/types/bots/callback-query.ts | 18 ++--- .../client/src/types/bots/game-high-score.ts | 12 ++-- .../client/src/types/bots/inline-query.ts | 18 ++--- packages/client/src/types/media/document.ts | 36 ++++------ packages/client/src/types/media/game.ts | 6 +- packages/client/src/types/media/invoice.ts | 48 +++++--------- packages/client/src/types/media/sticker.ts | 2 +- packages/client/src/types/media/video.ts | 14 ++-- packages/client/src/types/messages/message.ts | 10 ++- .../client/src/types/messages/reactions.ts | 11 +--- packages/client/src/types/misc/sticker-set.ts | 12 ++-- packages/client/src/types/peers/chat-event.ts | 18 ++--- .../src/types/peers/chat-invite-link.ts | 12 ++-- .../client/src/types/peers/chat-location.ts | 12 ++-- .../client/src/types/peers/chat-member.ts | 8 +-- packages/client/src/types/peers/chat-photo.ts | 65 +++++++------------ .../client/src/types/peers/chat-preview.ts | 18 ++--- packages/client/src/types/peers/chat.ts | 47 +++++--------- packages/client/src/types/peers/user.ts | 14 ++-- .../types/updates/bot-chat-join-request.ts | 33 ++++------ .../client/src/types/updates/bot-stopped.ts | 12 ++-- .../src/types/updates/chat-join-request.ts | 10 +-- .../src/types/updates/chat-member-update.ts | 61 +++++++---------- .../src/types/updates/chosen-inline-result.ts | 18 ++--- .../client/src/types/updates/poll-vote.ts | 12 ++-- .../src/types/updates/pre-checkout-query.ts | 9 ++- 27 files changed, 188 insertions(+), 358 deletions(-) diff --git a/packages/client/src/types/auth/terms-of-service.ts b/packages/client/src/types/auth/terms-of-service.ts index dcc1e1e7..ab89f76a 100644 --- a/packages/client/src/types/auth/terms-of-service.ts +++ b/packages/client/src/types/auth/terms-of-service.ts @@ -36,13 +36,9 @@ export class TermsOfService { * Terms of Service entities text */ get entities(): ReadonlyArray { - if (!this._entities) { - this._entities = this.tos.entities - .map((it) => MessageEntity._parse(it)) - .filter((it) => it !== null) as MessageEntity[] - } - - return this._entities + return (this._entities ??= this.tos.entities + .map((it) => MessageEntity._parse(it)) + .filter((it) => it !== null) as MessageEntity[]) } } diff --git a/packages/client/src/types/bots/callback-query.ts b/packages/client/src/types/bots/callback-query.ts index 5b3e1396..c8adcdbe 100644 --- a/packages/client/src/types/bots/callback-query.ts +++ b/packages/client/src/types/bots/callback-query.ts @@ -33,14 +33,10 @@ export class CallbackQuery { * User who has pressed the button */ get user(): User { - if (!this._user) { - this._user = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } /** @@ -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')) } /** diff --git a/packages/client/src/types/bots/game-high-score.ts b/packages/client/src/types/bots/game-high-score.ts index 8550bd4c..6c6c86de 100644 --- a/packages/client/src/types/bots/game-high-score.ts +++ b/packages/client/src/types/bots/game-high-score.ts @@ -19,14 +19,10 @@ export class GameHighScore { * User who has scored this score */ get user(): User { - if (!this._user) { - this._user = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } /** diff --git a/packages/client/src/types/bots/inline-query.ts b/packages/client/src/types/bots/inline-query.ts index 87f65dab..7a799c93 100644 --- a/packages/client/src/types/bots/inline-query.ts +++ b/packages/client/src/types/bots/inline-query.ts @@ -33,14 +33,10 @@ export class InlineQuery { * User who sent this query */ get user(): User { - if (!this._user) { - this._user = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } /** @@ -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)) } /** diff --git a/packages/client/src/types/media/document.ts b/packages/client/src/types/media/document.ts index bab7dcaa..37fd8e77 100644 --- a/packages/client/src/types/media/document.ts +++ b/packages/client/src/types/media/document.ts @@ -131,20 +131,16 @@ export class RawDocument extends FileLocation { * representing this document. */ get fileId(): string { - if (!this._fileId) { - this._fileId = toFileId({ - type: this._fileIdType(), - dcId: this.raw.dcId, - fileReference: this.raw.fileReference, - location: { - _: 'common', - id: this.raw.id, - accessHash: this.raw.accessHash, - }, - }) - } - - return this._fileId + return (this._fileId ??= toFileId({ + type: this._fileIdType(), + dcId: this.raw.dcId, + fileReference: this.raw.fileReference, + location: { + _: 'common', + id: this.raw.id, + accessHash: this.raw.accessHash, + }, + })) } 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, { - _: 'common', - id: this.raw.id, - }) - } - - return this._uniqueFileId + return (this._uniqueFileId ??= toUniqueFileId(td.FileType.Document, { + _: 'common', + id: this.raw.id, + })) } } diff --git a/packages/client/src/types/media/game.ts b/packages/client/src/types/media/game.ts index 73cf58b4..f6424d5c 100644 --- a/packages/client/src/types/media/game.ts +++ b/packages/client/src/types/media/game.ts @@ -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 diff --git a/packages/client/src/types/media/invoice.ts b/packages/client/src/types/media/invoice.ts index f7b19d2b..ba8ddfd6 100644 --- a/packages/client/src/types/media/invoice.ts +++ b/packages/client/src/types/media/invoice.ts @@ -41,15 +41,11 @@ export class InvoiceExtendedMediaPreview { return null } - if (!this._thumbnail) { - this._thumbnail = new Thumbnail( - this.client, - this.raw, - this.raw.thumb - ) - } - - return this._thumbnail + return (this._thumbnail ??= new Thumbnail( + this.client, + this.raw, + this.raw.thumb + )) } /** @@ -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( - this.client, - this.raw.extendedMedia - ) - } - - return this._extendedMediaPreview + return (this._extendedMediaPreview ??= new InvoiceExtendedMediaPreview( + this.client, + this.raw.extendedMedia + )) } 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( - this.client, - null, - this.raw.extendedMedia.media - ) - } - - return this._extendedMedia + return (this._extendedMedia ??= _messageMediaFromTl( + this.client, + null, + this.raw.extendedMedia.media + )) } /** diff --git a/packages/client/src/types/media/sticker.ts b/packages/client/src/types/media/sticker.ts index e59957c9..ce15396f 100644 --- a/packages/client/src/types/media/sticker.ts +++ b/packages/client/src/types/media/sticker.ts @@ -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, diff --git a/packages/client/src/types/media/video.ts b/packages/client/src/types/media/video.ts index a5d858cb..4c3aac9d 100644 --- a/packages/client/src/types/media/video.ts +++ b/packages/client/src/types/media/video.ts @@ -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 = - this.attr._ === 'documentAttributeImageSize' || - this.raw.attributes.some( - (it) => it._ === 'documentAttributeAnimated' - ) - } - - return this._isAnimation + return (this._isAnimation ??= + this.attr._ === 'documentAttributeImageSize' || + this.raw.attributes.some( + (it) => it._ === 'documentAttributeAnimated' + )) } /** diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index 7db370f6..de13f6bb 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -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 + ) } } diff --git a/packages/client/src/types/messages/reactions.ts b/packages/client/src/types/messages/reactions.ts index 9b824798..e05c5916 100644 --- a/packages/client/src/types/messages/reactions.ts +++ b/packages/client/src/types/messages/reactions.ts @@ -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) + )) } /** diff --git a/packages/client/src/types/misc/sticker-set.ts b/packages/client/src/types/misc/sticker-set.ts index 105cdd85..f5f1fdcf 100644 --- a/packages/client/src/types/misc/sticker-set.ts +++ b/packages/client/src/types/misc/sticker-set.ts @@ -205,14 +205,10 @@ export class StickerSet { * (i.e. first sticker should be used as thumbnail) */ get thumbnails(): ReadonlyArray { - if (!this._thumbnails) { - this._thumbnails = - this.brief.thumbs?.map( - (sz) => new Thumbnail(this.client, this.brief, sz) - ) ?? [] - } - - return this._thumbnails + return (this._thumbnails ??= + this.brief.thumbs?.map( + (sz) => new Thumbnail(this.client, this.brief, sz) + ) ?? []) } /** diff --git a/packages/client/src/types/peers/chat-event.ts b/packages/client/src/types/peers/chat-event.ts index 230f6297..bd510a7c 100644 --- a/packages/client/src/types/peers/chat-event.ts +++ b/packages/client/src/types/peers/chat-event.ts @@ -538,23 +538,15 @@ export class ChatEvent { * Actor of the event */ get actor(): User { - if (!this._actor) { - this._actor = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._actor + return (this._actor ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } 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)) } } diff --git a/packages/client/src/types/peers/chat-invite-link.ts b/packages/client/src/types/peers/chat-invite-link.ts index 4c5c16f5..269389a6 100644 --- a/packages/client/src/types/peers/chat-invite-link.ts +++ b/packages/client/src/types/peers/chat-invite-link.ts @@ -83,14 +83,10 @@ export class ChatInviteLink { get creator(): User | null { if (!this._peers) return null - if (!this._creator) { - this._creator = new User( - this.client, - this._peers.user(this.raw.adminId) - ) - } - - return this._creator + return (this._creator ??= new User( + this.client, + this._peers.user(this.raw.adminId) + )) } /** diff --git a/packages/client/src/types/peers/chat-location.ts b/packages/client/src/types/peers/chat-location.ts index 1820606a..f4258fcd 100644 --- a/packages/client/src/types/peers/chat-location.ts +++ b/packages/client/src/types/peers/chat-location.ts @@ -18,14 +18,10 @@ export class ChatLocation { * Location of the chat */ get location(): Location { - if (!this._location) { - this._location = new Location( - this.client, - this.raw.geoPoint as tl.RawGeoPoint - ) - } - - return this._location + return (this._location ??= new Location( + this.client, + this.raw.geoPoint as tl.RawGeoPoint + )) } /** diff --git a/packages/client/src/types/peers/chat-member.ts b/packages/client/src/types/peers/chat-member.ts index 6aaf7395..9d6a1349 100644 --- a/packages/client/src/types/peers/chat-member.ts +++ b/packages/client/src/types/peers/chat-member.ts @@ -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 + )) } /** diff --git a/packages/client/src/types/peers/chat-photo.ts b/packages/client/src/types/peers/chat-photo.ts index a71006f9..942a6869 100644 --- a/packages/client/src/types/peers/chat-photo.ts +++ b/packages/client/src/types/peers/chat-photo.ts @@ -91,21 +91,17 @@ 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( - tdFileId.FileType.ProfilePhoto, - { - _: 'photo', - id: this.obj.photoId, - source: { - _: 'dialogPhoto', - big: this.big, - } as any, - } - ) - } - - return this._uniqueFileId + return (this._uniqueFileId ??= toUniqueFileId( + tdFileId.FileType.ProfilePhoto, + { + _: 'photo', + id: this.obj.photoId, + source: { + _: 'dialogPhoto', + big: this.big, + } as any, + } + )) } } @@ -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( - this.client, - this.peer, - this.obj, - false - ) - } - - return this._smallFile + return (this._smallFile ??= new ChatPhotoSize( + this.client, + this.peer, + this.obj, + false + )) } private _bigFile?: ChatPhotoSize + /** Chat photo file location in big resolution (640x640) */ get big(): ChatPhotoSize { - if (!this._bigFile) { - this._bigFile = new ChatPhotoSize( - this.client, - this.peer, - this.obj, - true - ) - } - - return this._bigFile + return (this._bigFile ??= new ChatPhotoSize( + this.client, + this.peer, + this.obj, + true + )) } 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)) } } diff --git a/packages/client/src/types/peers/chat-preview.ts b/packages/client/src/types/peers/chat-preview.ts index dbfd8d45..77fa59c9 100644 --- a/packages/client/src/types/peers/chat-preview.ts +++ b/packages/client/src/types/peers/chat-preview.ts @@ -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 { - 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)) + : []) } /** diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index 748a70e7..7f63d1e1 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -281,15 +281,11 @@ export class Chat { ) return null - if (!this._photo) { - this._photo = new ChatPhoto( - this.client, - this.inputPeer, - this.peer.photo - ) - } - - return this._photo + return (this._photo ??= new ChatPhoto( + this.client, + this.inputPeer, + this.peer.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( - this.peer.defaultBannedRights - ) - } - - return this._permissions + return (this._permissions ??= new ChatPermissions( + this.peer.defaultBannedRights + )) } /** @@ -439,14 +429,10 @@ export class Chat { ) return null - if (!this._location) { - this._location = new ChatLocation( - this.client, - this.fullPeer.location - ) - } - - return this._location + return (this._location ??= new ChatLocation( + this.client, + this.fullPeer.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 */ diff --git a/packages/client/src/types/peers/user.ts b/packages/client/src/types/peers/user.ts index ba48d419..14e5ec4f 100644 --- a/packages/client/src/types/peers/user.ts +++ b/packages/client/src/types/peers/user.ts @@ -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( - this.client, - this.inputPeer, - this.raw.photo - ) - } - - return this._photo + return (this._photo ??= new ChatPhoto( + this.client, + this.inputPeer, + this.raw.photo + )) } /** diff --git a/packages/client/src/types/updates/bot-chat-join-request.ts b/packages/client/src/types/updates/bot-chat-join-request.ts index 17e3733a..4e6abba0 100644 --- a/packages/client/src/types/updates/bot-chat-join-request.ts +++ b/packages/client/src/types/updates/bot-chat-join-request.ts @@ -31,14 +31,10 @@ export class BotChatJoinRequestUpdate { * Object containing the chat information. */ get chat(): Chat { - if (!this._chat) { - this._chat = new Chat( - this.client, - this._peers.chat(getBarePeerId(this.raw.peer)) - ) - } - - return this._chat + return (this._chat ??= new Chat( + this.client, + this._peers.chat(getBarePeerId(this.raw.peer)) + )) } /** @@ -53,14 +49,10 @@ export class BotChatJoinRequestUpdate { * Object containing the user information. */ get user(): User { - if (!this._user) { - this._user = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } /** @@ -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 + )) } /** diff --git a/packages/client/src/types/updates/bot-stopped.ts b/packages/client/src/types/updates/bot-stopped.ts index 24751623..3ada9b4d 100644 --- a/packages/client/src/types/updates/bot-stopped.ts +++ b/packages/client/src/types/updates/bot-stopped.ts @@ -30,14 +30,10 @@ export class BotStoppedUpdate { * User who stopped or restarted the bot */ get user(): User { - if (!this._user) { - this._user = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } /** diff --git a/packages/client/src/types/updates/chat-join-request.ts b/packages/client/src/types/updates/chat-join-request.ts index 73de1d13..02d0ed02 100644 --- a/packages/client/src/types/updates/chat-join-request.ts +++ b/packages/client/src/types/updates/chat-join-request.ts @@ -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( - (id) => new User(this.client, this._peers.user(id)) - ) - } - - return this._recentRequesters + return (this._recentRequesters ??= this.raw.recentRequesters.map( + (id) => new User(this.client, this._peers.user(id)) + )) } /** diff --git a/packages/client/src/types/updates/chat-member-update.ts b/packages/client/src/types/updates/chat-member-update.ts index ed315b2e..dc2c8b6d 100644 --- a/packages/client/src/types/updates/chat-member-update.ts +++ b/packages/client/src/types/updates/chat-member-update.ts @@ -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( - this.client, - this._peers.user(this.raw.actorId) - ) - } - - return this._actor + return (this._actor ??= new User( + this.client, + this._peers.user(this.raw.actorId) + )) } 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( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } 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( - this.client, - this.raw.prevParticipant, - this._peers - ) - } - - return this._oldMember + return (this._oldMember ??= new ChatMember( + this.client, + this.raw.prevParticipant, + this._peers + )) } 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( - this.client, - this.raw.newParticipant, - this._peers - ) - } - - return this._newMember + return (this._newMember ??= new ChatMember( + this.client, + this.raw.newParticipant, + this._peers + )) } 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 + )) } } diff --git a/packages/client/src/types/updates/chosen-inline-result.ts b/packages/client/src/types/updates/chosen-inline-result.ts index b5968627..550ed5a5 100644 --- a/packages/client/src/types/updates/chosen-inline-result.ts +++ b/packages/client/src/types/updates/chosen-inline-result.ts @@ -31,14 +31,10 @@ export class ChosenInlineResult { * User who has chosen the query */ get user(): User { - if (!this._user) { - this._user = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } /** @@ -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)) } /** diff --git a/packages/client/src/types/updates/poll-vote.ts b/packages/client/src/types/updates/poll-vote.ts index 45c5697b..42e2e920 100644 --- a/packages/client/src/types/updates/poll-vote.ts +++ b/packages/client/src/types/updates/poll-vote.ts @@ -28,14 +28,10 @@ export class PollVoteUpdate { * User who has voted */ get user(): User { - if (!this._user) { - this._user = new User( - this.client, - this._peers.user(this.raw.userId) - ) - } - - return this._user + return (this._user ??= new User( + this.client, + this._peers.user(this.raw.userId) + )) } /** diff --git a/packages/client/src/types/updates/pre-checkout-query.ts b/packages/client/src/types/updates/pre-checkout-query.ts index e087256f..8b81bac2 100644 --- a/packages/client/src/types/updates/pre-checkout-query.ts +++ b/packages/client/src/types/updates/pre-checkout-query.ts @@ -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) + )) } /**