Skip to content

Commit 4f0dd01

Browse files
committed
(#53) Further code cleanup
- Refactored code to get rid of tick method for mouse and keyboard input - Decreased keyboard typing speed
1 parent 14068d0 commit 4f0dd01

File tree

2 files changed

+12
-53
lines changed

2 files changed

+12
-53
lines changed

lib/keyboard.class.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NativeAdapter } from "./adapter/native.adapter.class";
22
import { Key } from "./key.enum";
3+
import { sleep } from "./sleep.function";
34

45
type StringOrKey = string[] | Key[];
56

@@ -10,24 +11,20 @@ const inputIsString = (input: string[] | Key[]): input is string[] => {
1011
export class Keyboard {
1112

1213
public config = {
13-
autoDelayMs: 500,
14+
autoDelayMs: 300,
1415
};
1516

16-
private lastAction: number;
17-
1817
constructor(private nativeAdapter: NativeAdapter) {
1918
this.nativeAdapter.setKeyboardDelay(this.config.autoDelayMs);
20-
this.lastAction = Date.now();
2119
}
2220

2321
public type(...input: StringOrKey): Promise<Keyboard> {
2422
return new Promise<Keyboard>(async (resolve, reject) => {
2523
try {
2624
if (inputIsString(input)) {
2725
for (const char of input.join(" ").split("")) {
28-
await this.nextTick();
26+
await sleep(this.config.autoDelayMs);
2927
await this.nativeAdapter.type(char);
30-
this.updateTick();
3128
}
3229
} else {
3330
await this.nativeAdapter.click(...input as Key[]);
@@ -60,18 +57,4 @@ export class Keyboard {
6057
}
6158
});
6259
}
63-
64-
private updateTick() {
65-
this.lastAction = Date.now();
66-
}
67-
68-
private async nextTick(): Promise<void> {
69-
return new Promise<void>(resolve => {
70-
let current = Date.now();
71-
while (current - this.lastAction < this.config.autoDelayMs) {
72-
current = Date.now();
73-
}
74-
resolve();
75-
});
76-
}
7760
}

lib/mouse.class.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ import { NativeAdapter } from "./adapter/native.adapter.class";
22
import { Button } from "./button.enum";
33
import { MovementType } from "./movementtype.class";
44
import { Point } from "./point.class";
5+
import { sleep } from "./sleep.function";
56

67
export class Mouse {
78
public config = {
89
autoDelayMs: 100,
910
mouseSpeed: 1000,
1011
};
1112

12-
private lastAction: number;
13-
1413
constructor(private native: NativeAdapter) {
1514
this.native.setMouseDelay(0);
16-
this.lastAction = Date.now();
1715
}
1816

1917
public async setPosition(target: Point): Promise<Mouse> {
@@ -38,9 +36,8 @@ export class Mouse {
3836
for (let idx = 0; idx < path.length; ++idx) {
3937
const node = path[idx];
4038
const minTime = timeSteps[idx];
41-
await this.waitForNextTick(minTime);
39+
await sleep(minTime);
4240
await this.native.setMousePosition(node);
43-
await this.updateTick();
4441
}
4542
resolve(this);
4643
} catch (e) {
@@ -51,19 +48,17 @@ export class Mouse {
5148

5249
public async leftClick(): Promise<Mouse> {
5350
return new Promise<Mouse>(async resolve => {
54-
await this.waitForNextTick(this.config.autoDelayMs);
51+
await sleep(this.config.autoDelayMs);
5552
await this.native.leftClick();
56-
await this.updateTick();
5753
resolve(this);
5854
});
5955
}
6056

6157
public async rightClick(): Promise<Mouse> {
6258
return new Promise<Mouse>(async (resolve, reject) => {
6359
try {
64-
await this.waitForNextTick(this.config.autoDelayMs);
60+
await sleep(this.config.autoDelayMs);
6561
await this.native.rightClick();
66-
await this.updateTick();
6762
resolve(this);
6863
} catch (e) {
6964
reject(e);
@@ -74,9 +69,8 @@ export class Mouse {
7469
public async scrollDown(amount: number): Promise<Mouse> {
7570
return new Promise<Mouse>(async (resolve, reject) => {
7671
try {
77-
await this.waitForNextTick(this.config.autoDelayMs);
72+
await sleep(this.config.autoDelayMs);
7873
await this.native.scrollDown(amount);
79-
await this.updateTick();
8074
resolve(this);
8175
} catch (e) {
8276
reject(e);
@@ -87,9 +81,8 @@ export class Mouse {
8781
public async scrollUp(amount: number): Promise<Mouse> {
8882
return new Promise<Mouse>(async (resolve, reject) => {
8983
try {
90-
await this.waitForNextTick(this.config.autoDelayMs);
84+
await sleep(this.config.autoDelayMs);
9185
await this.native.scrollUp(amount);
92-
await this.updateTick();
9386
resolve(this);
9487
} catch (e) {
9588
reject(e);
@@ -100,9 +93,8 @@ export class Mouse {
10093
public async scrollLeft(amount: number): Promise<Mouse> {
10194
return new Promise<Mouse>(async (resolve, reject) => {
10295
try {
103-
await this.waitForNextTick(this.config.autoDelayMs);
96+
await sleep(this.config.autoDelayMs);
10497
await this.native.scrollLeft(amount);
105-
await this.updateTick();
10698
resolve(this);
10799
} catch (e) {
108100
reject(e);
@@ -113,9 +105,8 @@ export class Mouse {
113105
public async scrollRight(amount: number): Promise<Mouse> {
114106
return new Promise<Mouse>(async (resolve, reject) => {
115107
try {
116-
await this.waitForNextTick(this.config.autoDelayMs);
108+
await sleep(this.config.autoDelayMs);
117109
await this.native.scrollRight(amount);
118-
await this.updateTick();
119110
resolve(this);
120111
} catch (e) {
121112
reject(e);
@@ -126,29 +117,14 @@ export class Mouse {
126117
public async drag(path: Point[]): Promise<Mouse> {
127118
return new Promise<Mouse>(async (resolve, reject) => {
128119
try {
129-
await this.waitForNextTick(this.config.autoDelayMs);
120+
await sleep(this.config.autoDelayMs);
130121
await this.native.pressButton(Button.LEFT);
131122
await this.move(path);
132123
await this.native.releaseButton(Button.LEFT);
133-
await this.updateTick();
134124
resolve(this);
135125
} catch (e) {
136126
reject(e);
137127
}
138128
});
139129
}
140-
141-
private async updateTick() {
142-
this.lastAction = Date.now();
143-
}
144-
145-
private async waitForNextTick(minTime: number): Promise<void> {
146-
return new Promise<void>(resolve => {
147-
let current = Date.now();
148-
while (current - this.lastAction < minTime) {
149-
current = Date.now();
150-
}
151-
resolve();
152-
});
153-
}
154130
}

0 commit comments

Comments
 (0)