More refactoring stuff
- Add a waitForEventQueue promise, to get rid of the 5 second fudge from testBot.js - Async'ify testBot.js - Complete plumbing for Hint's ScriptDialog support (see changes to ScriptDialogEvent.ts and Comms.ts - Fix: The EventQueue was pausing for 5 seconds every 30 seconds
This commit is contained in:
49
lib/Bot.ts
49
lib/Bot.ts
@@ -28,6 +28,8 @@ import {Subscription} from 'rxjs/Subscription';
|
||||
import {BotOptionFlags} from './enums/BotOptionFlags';
|
||||
import {FilterResponse} from './enums/FilterResponse';
|
||||
import {LogoutReplyMessage} from './classes/messages/LogoutReply';
|
||||
import {EventQueueStateChangeEvent} from './events/EventQueueStateChangeEvent';
|
||||
import {UUID} from './classes/UUID';
|
||||
|
||||
export class Bot
|
||||
{
|
||||
@@ -39,8 +41,11 @@ export class Bot
|
||||
private lastSuccessfulPing = 0;
|
||||
private circuitSubscription: Subscription | null = null;
|
||||
private options: BotOptionFlags;
|
||||
private eventQueueRunning = false;
|
||||
public clientEvents: ClientEvents;
|
||||
public clientCommands: ClientCommands;
|
||||
private eventQueueWaits: any = {};
|
||||
|
||||
|
||||
|
||||
constructor(login: LoginParameters, options: BotOptionFlags)
|
||||
@@ -48,6 +53,21 @@ export class Bot
|
||||
this.clientEvents = new ClientEvents();
|
||||
this.loginParams = login;
|
||||
this.options = options;
|
||||
|
||||
this.clientEvents.onEventQueueStateChange.subscribe((evt: EventQueueStateChangeEvent) =>
|
||||
{
|
||||
this.eventQueueRunning = evt.active;
|
||||
for (const waitID of Object.keys(this.eventQueueWaits))
|
||||
{
|
||||
try
|
||||
{
|
||||
clearTimeout(this.eventQueueWaits[waitID].timer);
|
||||
this.eventQueueWaits[waitID].resolve();
|
||||
delete this.eventQueueWaits[waitID];
|
||||
}
|
||||
catch (ignore){}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async login()
|
||||
@@ -73,6 +93,35 @@ export class Bot
|
||||
await this.connectToSim();
|
||||
}
|
||||
|
||||
waitForEventQueue(timeout: number = 1000): Promise<void>
|
||||
{
|
||||
return new Promise((resolve, reject) =>
|
||||
{
|
||||
if (this.eventQueueRunning)
|
||||
{
|
||||
resolve();
|
||||
}
|
||||
else
|
||||
{
|
||||
const waitID = UUID.random().toString();
|
||||
const newWait: {
|
||||
'resolve': any,
|
||||
'timer'?: Timer
|
||||
} = {
|
||||
'resolve': resolve
|
||||
};
|
||||
|
||||
newWait.timer = setTimeout(() =>
|
||||
{
|
||||
delete this.eventQueueWaits[waitID];
|
||||
reject(new Error('Timeout'));
|
||||
}, timeout);
|
||||
|
||||
this.eventQueueWaits[waitID] = newWait;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private closeCircuit()
|
||||
{
|
||||
this.agent.shutdown();
|
||||
|
||||
Reference in New Issue
Block a user