Skip to content

Commit 6d27d79

Browse files
committed
Async
1 parent ef6b311 commit 6d27d79

20 files changed

+193
-106
lines changed

demo/clipboard.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
const { clipboard } = require("../dist");
55

66
(async () => {
7-
clipboard.copy("clipboard test!");
8-
console.log(clipboard.paste());
7+
await clipboard.copy("clipboard test!");
8+
console.log(await clipboard.paste());
99
})();

demo/mouse.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
const { mouse, movement, screen, Location } = require("../dist");
66

77
const square = async () => {
8-
await mouse.move(movement.right(500));
9-
await mouse.move(movement.down(500));
10-
await mouse.move(movement.left(500));
11-
await mouse.move(movement.up(500));
8+
await mouse.move(await movement.right(500));
9+
await mouse.move(await movement.down(500));
10+
await mouse.move(await movement.left(500));
11+
await mouse.move(await movement.up(500));
1212
};
1313

1414
(async () => {
@@ -18,13 +18,13 @@ const square = async () => {
1818
try {
1919
screen.config.resourceDirectory = "./assets";
2020
const whale = await screen.findOnScreen("docker.png");
21-
mouse.move(movement.straightTo(Location.centerOf(whale)));
21+
mouse.move(await movement.straightTo(Location.centerOf(whale)));
2222

2323
const gitlens = await screen.findOnScreen("gitlens.png");
24-
mouse.move(movement.straightTo(Location.centerOf(gitlens)));
25-
mouse.drag(movement.right(600));
26-
mouse.move(movement.straightTo(Location.centerOf(gitlens)));
27-
mouse.drag(movement.straightTo(Location.centerOf(whale)));
24+
mouse.move(await movement.straightTo(Location.centerOf(gitlens)));
25+
mouse.drag(await movement.right(600));
26+
mouse.move(await movement.straightTo(Location.centerOf(gitlens)));
27+
mouse.drag(await movement.straightTo(Location.centerOf(whale)));
2828
} catch (error) {
2929
console.log(error);
3030
}

lib/adapter/native.adapter.class.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ export class NativeAdapter {
5858
this.mouse.setMouseDelay(delay);
5959
}
6060

61+
/**
62+
* setKeyboardDelay configures keyboard delay between key presses
63+
*
64+
* @param {number} delay The delay
65+
* @memberof NativeAdapter
66+
*/
67+
public setKeyboardDelay(delay: number): void {
68+
this.keyboard.setKeyboardDelay(delay);
69+
}
70+
6171
/**
6272
* setMousePosition changes the current mouse cursor position to a given point
6373
*
@@ -71,10 +81,10 @@ export class NativeAdapter {
7181
/**
7282
* getMousePosition returns the current mouse position
7383
*
74-
* @returns {Point} Current cursor position
84+
* @returns {Promise<Point>} Current cursor position
7585
* @memberof NativeAdapter
7686
*/
77-
public currentMousePosition(): Point {
87+
public currentMousePosition(): Promise<Point> {
7888
return this.mouse.currentMousePosition();
7989
}
8090

@@ -83,10 +93,10 @@ export class NativeAdapter {
8393
* Please notice that on e.g. Apples Retina display the reported width
8494
* and the actual pixel size may differ
8595
*
86-
* @returns {number} The main screen's width as reported by the OS
96+
* @returns {Promise<number>} The main screen's width as reported by the OS
8797
* @memberof NativeAdapter
8898
*/
89-
public screenWidth(): number {
99+
public screenWidth(): Promise<number> {
90100
return this.screen.screenWidth();
91101
}
92102

@@ -95,10 +105,10 @@ export class NativeAdapter {
95105
* Please notice that on e.g. Apples Retina display the reported width
96106
* and the actual pixel size may differ
97107
*
98-
* @returns {number} The main screen's height as reported by the OS
108+
* @returns {Promise<number>} The main screen's height as reported by the OS
99109
* @memberof NativeAdapter
100110
*/
101-
public screenHeight(): number {
111+
public screenHeight(): Promise<number> {
102112
return this.screen.screenHeight();
103113
}
104114

@@ -107,10 +117,10 @@ export class NativeAdapter {
107117
* Please notice that on e.g. Apples Retina display the reported width
108118
* and the actual pixel size may differ
109119
*
110-
* @returns {Region} The Region object the size of your main screen
120+
* @returns {Promise<Region>} The Region object the size of your main screen
111121
* @memberof NativeAdapter
112122
*/
113-
public screenSize(): Region {
123+
public screenSize(): Promise<Region> {
114124
return this.screen.screenSize();
115125
}
116126

@@ -182,21 +192,21 @@ export class NativeAdapter {
182192
/**
183193
* pressKey presses and holds a given Key
184194
*
185-
* @param {Key} key The Key to press and hold
195+
* @param {Key[]} keys The Keys to press and hold
186196
* @memberof NativeAdapter
187197
*/
188-
public pressKey(key: Key): void {
189-
this.keyboard.pressKey(key);
198+
public pressKey(...keys: Key[]): void {
199+
this.keyboard.pressKey(...keys);
190200
}
191201

192202
/**
193203
* releaseKey releases a Key previously presses via pressKey
194204
*
195-
* @param {Key} key The Key to release
205+
* @param {Key[]} keys The Keys to release
196206
* @memberof NativeAdapter
197207
*/
198-
public releaseKey(key: Key): void {
199-
this.keyboard.releaseKey(key);
208+
public releaseKey(...keys: Key[]): void {
209+
this.keyboard.releaseKey(...keys);
200210
}
201211

202212
/**
@@ -252,10 +262,10 @@ export class NativeAdapter {
252262
/**
253263
* paste pastes the current text on the system clipboard
254264
*
255-
* @returns {string} The clipboard text
265+
* @returns {Promise<string>} The clipboard text
256266
* @memberof NativeAdapter
257267
*/
258-
public paste(): string {
268+
public paste(): Promise<string> {
259269
return this.clipboard.paste();
260270
}
261271
}

lib/clipboard.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Clipboard {
77
this.nativeAdapter.copy(text);
88
}
99

10-
public paste(): string {
10+
public paste(): Promise<string> {
1111
return this.nativeAdapter.paste();
1212
}
1313
}

lib/keyboard.class.spec.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,87 @@ beforeEach(() => {
99
});
1010

1111
describe("Keyboard", () => {
12-
it("should pass input strings down to the type call.", () => {
12+
it("should have a default delay of 20 ms", () => {
13+
// GIVEN
1314
const adapterMock = new NativeAdapter();
1415
const SUT = new Keyboard(adapterMock);
1516

17+
// WHEN
18+
19+
// THEN
20+
expect(SUT.config.autoDelayMs).toEqual(20);
21+
});
22+
23+
it("should pass input strings down to the type call.", () => {
24+
// GIVEN
25+
const adapterMock = new NativeAdapter();
26+
const SUT = new Keyboard(adapterMock);
1627
const payload = "Test input!";
1728

29+
// WHEN
1830
SUT.type(payload);
31+
32+
// THEN
1933
expect(adapterMock.type).toHaveBeenCalledTimes(1);
2034
expect(adapterMock.type).toHaveBeenCalledWith(payload);
2135
});
2236

2337
it("should pass input keys down to the click call.", () => {
38+
// GIVEN
2439
const adapterMock = new NativeAdapter();
2540
const SUT = new Keyboard(adapterMock);
26-
2741
const payload = Key.A;
2842

43+
// WHEN
2944
SUT.type(payload);
45+
46+
// THEN
3047
expect(adapterMock.click).toHaveBeenCalledTimes(1);
3148
expect(adapterMock.click).toHaveBeenCalledWith(payload);
3249
});
3350

3451
it("should pass a list of input keys down to the click call.", () => {
52+
// GIVEN
3553
const adapterMock = new NativeAdapter();
3654
const SUT = new Keyboard(adapterMock);
37-
3855
const payload = [Key.A, Key.S, Key.D, Key.F];
3956

57+
// WHEN
4058
for (const key of payload) {
4159
SUT.type(key);
4260
}
61+
62+
// THEN
4363
expect(adapterMock.click).toHaveBeenCalledTimes(payload.length);
4464
});
4565

4666
it("should pass a list of input keys down to the pressKey call.", () => {
67+
// GIVEN
4768
const adapterMock = new NativeAdapter();
4869
const SUT = new Keyboard(adapterMock);
49-
5070
const payload = [Key.A, Key.S, Key.D, Key.F];
5171

72+
// WHEN
5273
for (const key of payload) {
5374
SUT.pressKey(key);
5475
}
76+
77+
// THEN
5578
expect(adapterMock.pressKey).toHaveBeenCalledTimes(payload.length);
5679
});
5780

5881
it("should pass a list of input keys down to the releaseKey call.", () => {
82+
// GIVEN
5983
const adapterMock = new NativeAdapter();
6084
const SUT = new Keyboard(adapterMock);
61-
6285
const payload = [Key.A, Key.S, Key.D, Key.F];
6386

87+
// WHEN
6488
for (const key of payload) {
6589
SUT.releaseKey(key);
6690
}
91+
92+
// THEN
6793
expect(adapterMock.releaseKey).toHaveBeenCalledTimes(payload.length);
6894
});
6995
});

lib/keyboard.class.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import { NativeAdapter } from "./adapter/native.adapter.class";
22
import { Key } from "./key.enum";
33

44
export class Keyboard {
5+
56
private static keyIsString(input: string | Key): input is string {
67
return typeof input === "string";
78
}
8-
constructor(private nativeAdapter: NativeAdapter) {}
9+
public config = {
10+
autoDelayMs: 20,
11+
};
12+
13+
constructor(private nativeAdapter: NativeAdapter) {
14+
this.nativeAdapter.setKeyboardDelay(this.config.autoDelayMs);
15+
}
916

1017
public type(input: string | Key): Keyboard {
1118
if (Keyboard.keyIsString(input)) {
@@ -16,13 +23,13 @@ export class Keyboard {
1623
return this;
1724
}
1825

19-
public pressKey(key: Key): Keyboard {
20-
this.nativeAdapter.pressKey(key);
26+
public pressKey(...keys: Key[]): Keyboard {
27+
this.nativeAdapter.pressKey(...keys);
2128
return this;
2229
}
2330

24-
public releaseKey(key: Key): Keyboard {
25-
this.nativeAdapter.releaseKey(key);
31+
public releaseKey(...keys: Key[]): Keyboard {
32+
this.nativeAdapter.releaseKey(...keys);
2633
return this;
2734
}
2835
}

lib/mouse.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class Mouse {
1818
return this;
1919
}
2020

21-
public getPosition(): Point {
21+
public getPosition(): Promise<Point> {
2222
return this.native.currentMousePosition();
2323
}
2424

lib/movement.class.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ export class Movement {
66
constructor(private native: NativeAdapter, private lineHelper: LineHelper) {
77
}
88

9-
public straightTo(target: Point): Point[] {
10-
const origin = this.getPosition();
9+
public async straightTo(target: Point): Promise<Point[]> {
10+
const origin = await this.getPosition();
1111
return this.lineHelper.straightLine(origin, target);
1212
}
1313

14-
public down(px: number): Point[] {
15-
const pos = this.getPosition();
14+
public async down(px: number): Promise<Point[]> {
15+
const pos = await this.getPosition();
1616
return this.straightTo(new Point(pos.x, pos.y + px));
1717
}
1818

19-
public up(px: number): Point[] {
20-
const pos = this.getPosition();
19+
public async up(px: number): Promise<Point[]> {
20+
const pos = await this.getPosition();
2121
return this.straightTo(new Point(pos.x, pos.y - px));
2222
}
2323

24-
public left(px: number): Point[] {
25-
const pos = this.getPosition();
24+
public async left(px: number): Promise<Point[]> {
25+
const pos = await this.getPosition();
2626
return this.straightTo(new Point(pos.x - px, pos.y));
2727
}
2828

29-
public right(px: number): Point[] {
30-
const pos = this.getPosition();
29+
public async right(px: number): Promise<Point[]> {
30+
const pos = await this.getPosition();
3131
return this.straightTo(new Point(pos.x + px, pos.y));
3232
}
3333

34-
private getPosition(): Point {
35-
const pos = this.native.currentMousePosition();
34+
private async getPosition(): Promise<Point> {
35+
const pos = await this.native.currentMousePosition();
3636
return new Point(pos.x, pos.y);
3737
}
3838
}

lib/provider/native/clipboard-action-provider.interface.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ export interface ClipboardActionProvider {
77
/**
88
* hasText should return whether the system clipboard currently holds text or not
99
*
10-
* @returns {boolean} True if there's text on the clipboard, false otherwise
10+
* @returns {Promise<boolean>} True if there's text on the clipboard, false otherwise
1111
* @memberof ClipboardActionProvider
1212
*/
13-
hasText(): boolean;
13+
hasText(): Promise<boolean>;
1414

1515
/**
1616
* clear should allow to clear the system clipboard
1717
*
18-
* @returns {boolean}
18+
* @returns {Promise<boolean>} Successfully cleared or not
1919
* @memberof ClipboardActionProvider
2020
*/
21-
clear(): boolean;
21+
clear(): Promise<boolean>;
2222

2323
/**
2424
* copy should allow to copy text to the system's clipboard
@@ -31,8 +31,8 @@ export interface ClipboardActionProvider {
3131
/**
3232
* paste should allow to paste the current text on the system's clipboard
3333
*
34-
* @returns {string} The current clipboard text
34+
* @returns {Promise<string>} The current clipboard text
3535
* @memberof ClipboardActionProvider
3636
*/
37-
paste(): string;
37+
paste(): Promise<string>;
3838
}

0 commit comments

Comments
 (0)