From f5e3df4ebf455b5e392f800b1c20568a26ebbe85 Mon Sep 17 00:00:00 2001 From: Casper Warden <216465704+casperwardensl@users.noreply.github.com> Date: Sat, 6 Oct 2018 15:29:06 +0100 Subject: [PATCH] Some code tidy and migrate simple promises to async/await --- lib/classes/commands/AgentCommands.ts | 12 ++-- lib/classes/commands/AssetCommands.ts | 4 +- .../commands/CommunicationsCommands.ts | 69 +++++++++---------- lib/classes/commands/GroupCommands.ts | 26 +++---- lib/classes/commands/NetworkCommands.ts | 5 +- lib/classes/commands/TeleportCommands.ts | 4 +- 6 files changed, 57 insertions(+), 63 deletions(-) diff --git a/lib/classes/commands/AgentCommands.ts b/lib/classes/commands/AgentCommands.ts index 9ede338..9f0afe3 100644 --- a/lib/classes/commands/AgentCommands.ts +++ b/lib/classes/commands/AgentCommands.ts @@ -5,7 +5,7 @@ import {CommandsBase} from './CommandsBase'; export class AgentCommands extends CommandsBase { - private animate(anim: UUID[], run: boolean): Promise + private async animate(anim: UUID[], run: boolean): Promise { const circuit = this.currentRegion.circuit; @@ -24,16 +24,16 @@ export class AgentCommands extends CommandsBase }); }); - return circuit.waitForAck(circuit.sendMessage(animPacket, PacketFlags.Reliable), 10000); + return await circuit.waitForAck(circuit.sendMessage(animPacket, PacketFlags.Reliable), 10000); } - startAnimations(anim: UUID[]): Promise + async startAnimations(anim: UUID[]): Promise { - return this.animate(anim, true); + return await this.animate(anim, true); } - stopAnimations(anim: UUID[]): Promise + async stopAnimations(anim: UUID[]): Promise { - return this.animate(anim, false); + return await this.animate(anim, false); } } diff --git a/lib/classes/commands/AssetCommands.ts b/lib/classes/commands/AssetCommands.ts index b74d2a2..3c2b043 100644 --- a/lib/classes/commands/AssetCommands.ts +++ b/lib/classes/commands/AssetCommands.ts @@ -6,9 +6,9 @@ import {Utils} from '../Utils'; export class AssetCommands extends CommandsBase { - downloadAsset(type: HTTPAssets, uuid: UUID) + async downloadAsset(type: HTTPAssets, uuid: UUID): Promise { - return this.currentRegion.caps.downloadAsset(uuid, type); + return await this.currentRegion.caps.downloadAsset(uuid, type); } uploadAsset(type: HTTPAssets, data: Buffer, name: string, description: string): Promise diff --git a/lib/classes/commands/CommunicationsCommands.ts b/lib/classes/commands/CommunicationsCommands.ts index 600b9c6..9d390cd 100644 --- a/lib/classes/commands/CommunicationsCommands.ts +++ b/lib/classes/commands/CommunicationsCommands.ts @@ -8,17 +8,14 @@ import {ChatFromViewerMessage} from '../messages/ChatFromViewer'; import {ChatType} from '../../enums/ChatType'; import {InstantMessageDialog} from '../../enums/InstantMessageDialog'; import Timer = NodeJS.Timer; -import {GroupChatSessionJoinEvent} from '../../events/GroupChatSessionJoinEvent'; -import {FriendRequestEvent} from '../../events/FriendRequestEvent'; import {AcceptFriendshipMessage} from '../messages/AcceptFriendship'; -import {AssetType} from '../../enums/AssetType'; import {DeclineFriendshipMessage} from '../messages/DeclineFriendship'; import {InventoryOfferedEvent} from '../../events/InventoryOfferedEvent'; -import {ChatSourceType} from '../../enums/ChatSourceType'; +import {AssetType, ChatSourceType, FriendRequestEvent, GroupChatSessionJoinEvent} from '../..'; export class CommunicationsCommands extends CommandsBase { - sendInstantMessage(to: UUID | string, message: string): Promise + async sendInstantMessage(to: UUID | string, message: string): Promise { const circuit = this.circuit; if (typeof to === 'string') @@ -49,10 +46,10 @@ export class CommunicationsCommands extends CommandsBase EstateID: 0 }; const sequenceNo = circuit.sendMessage(im, PacketFlags.Reliable); - return circuit.waitForAck(sequenceNo, 10000); + return await circuit.waitForAck(sequenceNo, 10000); } - nearbyChat(message: string, type: ChatType, channel?: number): Promise + async nearbyChat(message: string, type: ChatType, channel?: number): Promise { if (channel === undefined) { @@ -69,25 +66,25 @@ export class CommunicationsCommands extends CommandsBase Channel: channel }; const sequenceNo = this.circuit.sendMessage(cfv, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } - say(message: string, channel?: number): Promise + async say(message: string, channel?: number): Promise { - return this.nearbyChat(message, ChatType.Normal, channel); + return await this.nearbyChat(message, ChatType.Normal, channel); } - whisper(message: string, channel?: number): Promise + async whisper(message: string, channel?: number): Promise { - return this.nearbyChat(message, ChatType.Whisper, channel); + return await this.nearbyChat(message, ChatType.Whisper, channel); } - shout(message: string, channel?: number): Promise + async shout(message: string, channel?: number): Promise { - return this.nearbyChat(message, ChatType.Shout, channel); + return await this.nearbyChat(message, ChatType.Shout, channel); } - startTypingLocal(): Promise + async startTypingLocal(): Promise { const cfv = new ChatFromViewerMessage(); cfv.AgentData = { @@ -100,10 +97,10 @@ export class CommunicationsCommands extends CommandsBase Channel: 0 }; const sequenceNo = this.circuit.sendMessage(cfv, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } - stopTypingLocal(): Promise + async stopTypingLocal(): Promise { const cfv = new ChatFromViewerMessage(); cfv.AgentData = { @@ -116,10 +113,10 @@ export class CommunicationsCommands extends CommandsBase Channel: 0 }; const sequenceNo = this.circuit.sendMessage(cfv, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } - startTypingIM(to: UUID | string): Promise + async startTypingIM(to: UUID | string): Promise { if (typeof to === 'string') { @@ -150,10 +147,10 @@ export class CommunicationsCommands extends CommandsBase EstateID: 0 }; const sequenceNo = circuit.sendMessage(im, PacketFlags.Reliable); - return circuit.waitForAck(sequenceNo, 10000); + return await circuit.waitForAck(sequenceNo, 10000); } - stopTypingIM(to: UUID | string): Promise + async stopTypingIM(to: UUID | string): Promise { if (typeof to === 'string') { @@ -184,7 +181,7 @@ export class CommunicationsCommands extends CommandsBase EstateID: 0 }; const sequenceNo = circuit.sendMessage(im, PacketFlags.Reliable); - return circuit.waitForAck(sequenceNo, 10000); + return await circuit.waitForAck(sequenceNo, 10000); } typeInstantMessage(to: UUID | string, message: string, thinkingTime?: number, charactersPerSecond?: number): Promise @@ -364,7 +361,7 @@ export class CommunicationsCommands extends CommandsBase }); } - acceptFriendRequest(event: FriendRequestEvent): Promise + async acceptFriendRequest(event: FriendRequestEvent): Promise { const accept: AcceptFriendshipMessage = new AcceptFriendshipMessage(); accept.AgentData = { @@ -381,10 +378,10 @@ export class CommunicationsCommands extends CommandsBase } ); const sequenceNo = this.circuit.sendMessage(accept, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } - sendFriendRequest(to: UUID | string, message: string) + async sendFriendRequest(to: UUID | string, message: string): Promise { if (typeof to === 'string') { @@ -415,10 +412,10 @@ export class CommunicationsCommands extends CommandsBase EstateID: 0 }; const sequenceNo = this.circuit.sendMessage(im, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } - private respondToInventoryOffer(event: InventoryOfferedEvent, response: InstantMessageDialog): Promise + private async respondToInventoryOffer(event: InventoryOfferedEvent, response: InstantMessageDialog): Promise { const agentName = this.agent.firstName + ' ' + this.agent.lastName; const im: ImprovedInstantMessageMessage = new ImprovedInstantMessageMessage(); @@ -449,34 +446,34 @@ export class CommunicationsCommands extends CommandsBase EstateID: 0 }; const sequenceNo = this.circuit.sendMessage(im, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } - acceptInventoryOffer(event: InventoryOfferedEvent): Promise + async acceptInventoryOffer(event: InventoryOfferedEvent): Promise { if (event.source === ChatSourceType.Object) { - return this.respondToInventoryOffer(event, InstantMessageDialog.TaskInventoryAccepted); + return await this.respondToInventoryOffer(event, InstantMessageDialog.TaskInventoryAccepted); } else { - return this.respondToInventoryOffer(event, InstantMessageDialog.InventoryAccepted); + return await this.respondToInventoryOffer(event, InstantMessageDialog.InventoryAccepted); } } - rejectInventoryOffer(event: InventoryOfferedEvent): Promise + async rejectInventoryOffer(event: InventoryOfferedEvent): Promise { if (event.source === ChatSourceType.Object) { - return this.respondToInventoryOffer(event, InstantMessageDialog.TaskInventoryDeclined); + return await this.respondToInventoryOffer(event, InstantMessageDialog.TaskInventoryDeclined); } else { - return this.respondToInventoryOffer(event, InstantMessageDialog.InventoryDeclined); + return await this.respondToInventoryOffer(event, InstantMessageDialog.InventoryDeclined); } } - rejectFriendRequest(event: FriendRequestEvent): Promise + async rejectFriendRequest(event: FriendRequestEvent): Promise { const reject: DeclineFriendshipMessage = new DeclineFriendshipMessage(); reject.AgentData = { @@ -487,7 +484,7 @@ export class CommunicationsCommands extends CommandsBase TransactionID: event.requestID }; const sequenceNo = this.circuit.sendMessage(reject, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } sendGroupMessage(groupID: UUID | string, message: string): Promise diff --git a/lib/classes/commands/GroupCommands.ts b/lib/classes/commands/GroupCommands.ts index b6a4c6a..ada2e63 100644 --- a/lib/classes/commands/GroupCommands.ts +++ b/lib/classes/commands/GroupCommands.ts @@ -6,23 +6,19 @@ import {PacketFlags} from '../../enums/PacketFlags'; import {ImprovedInstantMessageMessage} from '../messages/ImprovedInstantMessage'; import {Vector3} from '../Vector3'; import {InviteGroupRequestMessage} from '../messages/InviteGroupRequest'; -import {GroupInviteEvent} from '../../events/GroupInviteEvent'; import {GroupRole} from '../GroupRole'; import {GroupRoleDataRequestMessage} from '../messages/GroupRoleDataRequest'; import {Message} from '../../enums/Message'; import {Packet} from '../Packet'; import {GroupRoleDataReplyMessage} from '../messages/GroupRoleDataReply'; import {GroupMember} from '../GroupMember'; -import {GroupMembersRequestMessage} from '../messages/GroupMembersRequest'; -import {GroupMembersReplyMessage} from '../messages/GroupMembersReply'; import {FilterResponse} from '../../enums/FilterResponse'; -import * as Long from 'long'; -import {GroupChatSessionJoinEvent} from '../../events/GroupChatSessionJoinEvent'; import * as LLSD from '@caspertech/llsd'; +import {GroupInviteEvent} from '../..'; export class GroupCommands extends CommandsBase { - sendGroupNotice(groupID: UUID | string, subject: string, message: string) + async sendGroupNotice(groupID: UUID | string, subject: string, message: string): Promise { if (typeof groupID === 'string') { @@ -53,10 +49,10 @@ export class GroupCommands extends CommandsBase EstateID: 0 }; const sequenceNo = circuit.sendMessage(im, PacketFlags.Reliable); - return circuit.waitForAck(sequenceNo, 10000); + return await circuit.waitForAck(sequenceNo, 10000); } - sendGroupInviteBulk(groupID: UUID | string, sendTo: { + async sendGroupInviteBulk(groupID: UUID | string, sendTo: { avatarID: UUID | string, roleID: UUID | string | undefined }[]): Promise @@ -95,7 +91,7 @@ export class GroupCommands extends CommandsBase }); const sequenceNo = this.circuit.sendMessage(igr, PacketFlags.Reliable); - return this.circuit.waitForAck(sequenceNo, 10000); + return await this.circuit.waitForAck(sequenceNo, 10000); } getSessionAgentCount(sessionID: UUID | string): number @@ -107,16 +103,16 @@ export class GroupCommands extends CommandsBase return this.agent.getSessionAgentCount(sessionID); } - sendGroupInvite(groupID: UUID | string, to: UUID | string, role: UUID | string | undefined): Promise + async sendGroupInvite(groupID: UUID | string, to: UUID | string, role: UUID | string | undefined): Promise { const sendTo = [{ avatarID: to, roleID: role }]; - return this.sendGroupInviteBulk(groupID, sendTo); + return await this.sendGroupInviteBulk(groupID, sendTo); } - acceptGroupInvite(event: GroupInviteEvent): Promise + async acceptGroupInvite(event: GroupInviteEvent): Promise { const circuit = this.circuit; const agentName = this.agent.firstName + ' ' + this.agent.lastName; @@ -143,10 +139,10 @@ export class GroupCommands extends CommandsBase EstateID: 0 }; const sequenceNo = circuit.sendMessage(im, PacketFlags.Reliable); - return circuit.waitForAck(sequenceNo, 10000); + return await circuit.waitForAck(sequenceNo, 10000); } - rejectGroupInvite(event: GroupInviteEvent): Promise + async rejectGroupInvite(event: GroupInviteEvent): Promise { const circuit = this.circuit; const agentName = this.agent.firstName + ' ' + this.agent.lastName; @@ -173,7 +169,7 @@ export class GroupCommands extends CommandsBase EstateID: 0 }; const sequenceNo = circuit.sendMessage(im, PacketFlags.Reliable); - return circuit.waitForAck(sequenceNo, 10000); + return await circuit.waitForAck(sequenceNo, 10000); } getMemberList(groupID: UUID | string): Promise diff --git a/lib/classes/commands/NetworkCommands.ts b/lib/classes/commands/NetworkCommands.ts index 3812b38..49db1b4 100644 --- a/lib/classes/commands/NetworkCommands.ts +++ b/lib/classes/commands/NetworkCommands.ts @@ -6,7 +6,7 @@ export class NetworkCommands extends CommandsBase { private throttleGenCounter = 0; - setBandwidth(total: number) + async setBandwidth(total: number): Promise { const agentThrottle: AgentThrottleMessage = new AgentThrottleMessage(); agentThrottle.AgentData = { @@ -45,6 +45,7 @@ export class NetworkCommands extends CommandsBase GenCounter: this.throttleGenCounter++, Throttles: throttleData }; - this.circuit.sendMessage(agentThrottle, PacketFlags.Reliable); + const sequenceNo = this.circuit.sendMessage(agentThrottle, PacketFlags.Reliable); + return await this.circuit.waitForAck(sequenceNo, 10000); } } diff --git a/lib/classes/commands/TeleportCommands.ts b/lib/classes/commands/TeleportCommands.ts index 1fedfec..a2a101c 100644 --- a/lib/classes/commands/TeleportCommands.ts +++ b/lib/classes/commands/TeleportCommands.ts @@ -102,7 +102,7 @@ export class TeleportCommands extends CommandsBase }); } - teleportToHandle(handle: Long, position: Vector3, lookAt: Vector3) + teleportToHandle(handle: Long, position: Vector3, lookAt: Vector3): Promise { return new Promise((resolve, reject) => { @@ -127,7 +127,7 @@ export class TeleportCommands extends CommandsBase }); } - teleportTo(regionName: string, position: Vector3, lookAt: Vector3) + teleportTo(regionName: string, position: Vector3, lookAt: Vector3): Promise { return new Promise((resolve, reject) => {