-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.ts
151 lines (136 loc) · 4.15 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { MonkeyCallback } from '../util';
import { NotConnectedError } from '../util';
import { Socket } from 'net';
import { Reply, ErrReply } from './reply';
import Api from './api';
import { BaseCommand, Command, ParsableCommand } from './command';
import { CommandQueue } from './commandqueue';
import { Parser } from './parser';
export class Monkey extends Api {
public queue: BaseCommand<unknown>[] = [];
private parser: Parser = new Parser();
private stream_: Socket | null = null;
private timeout: NodeJS.Timeout | undefined = undefined;
public get stream(): Socket {
if (!this.stream_) {
throw new NotConnectedError();
}
return this.stream_;
}
private sendInternal(
commands: string[] | string,
cmdConstruct: (cmd: string) => BaseCommand<unknown>
): this {
[commands].flat().forEach((command) => {
this.queue.push(cmdConstruct(command));
this.stream.write(command + '\n');
});
this.timeout = setTimeout(() => {
this.consume(new ErrReply('Command failed'));
}, 500);
return this;
}
public sendAndParse<T>(
commands: string | string[],
cb: MonkeyCallback<T>,
parser: (data: string | null) => T
): this {
return this.sendInternal(
commands,
(cmd) =>
new ParsableCommand(cmd, cb, parser) as BaseCommand<unknown>
);
}
/**
* Writes commands to monkey stream.
* @example
* monkey.send('key event 24', (err, value, command) => {});
*/
public send(
commands: string[] | string,
cb: MonkeyCallback<unknown>
): this {
return this.sendInternal(
commands,
(cmd) => new Command(cmd, cb) as BaseCommand<unknown>
);
}
protected hook(): void {
this.stream.on('data', (data) => {
clearTimeout(this.timeout);
return this.parser.parse(data);
});
this.stream.on('error', (err) => {
clearTimeout(this.timeout);
return this.emit('error', err);
});
this.stream.on('end', () => {
clearTimeout(this.timeout);
return this.emit('end');
});
this.stream.on('finish', () => {
clearTimeout(this.timeout);
return this.emit('finish');
});
this.parser.on('reply', (reply) => {
return this.consume(reply);
});
this.parser.on('error', (err) => {
return this.emit('error', err);
});
}
public on(event: 'error', listener: (err: Error) => void): this;
public on(event: 'end' | 'finish' | 'close', listener: () => void): this;
public on(
event: string | symbol,
listener: (...args: [Error]) => void
): this {
return super.on(event, listener);
}
private consume(reply: Reply): void {
const command = this.queue.shift();
if (!command) {
this.emit(
'error',
new Error('Command queue depleted, but replies still coming in')
);
return;
}
if (reply.isError()) {
return command.callback?.(reply.toError(), null, command.command);
}
if (command.isParsable()) {
return command.callback?.(
null,
command.parser(reply.value),
command.command
);
}
command.callback?.(null, reply.value, command.command);
}
public connect(param: Socket): this {
this.stream_ = param;
this.hook();
return this;
}
public end(cb?: () => void): this {
clearTimeout(this.timeout);
this.stream.end(cb);
return this;
}
/**
* Allows executing commands in a queue.
* @example
* monkey
* .commandQueue()
* .touchDown(100, 0)
* .sleep(5)
* .touchUp(100, 0)
* .execute((err, values) => {
* monkey.end();
* });
*/
public commandQueue(): CommandQueue {
return new CommandQueue(this);
}
}