Skip to content

Commit bf4d053

Browse files
committed
(#47) Refactored mouse class
1 parent 877d5d6 commit bf4d053

File tree

1 file changed

+76
-44
lines changed

1 file changed

+76
-44
lines changed

lib/mouse.class.ts

+76-44
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ export class Mouse {
1717
}
1818

1919
public async setPosition(target: Point): Promise<Mouse> {
20-
return new Promise<Mouse>(async resolve => {
21-
await this.native.setMousePosition(target);
22-
resolve(this);
20+
return new Promise<Mouse>(async (resolve, reject) => {
21+
try {
22+
await this.native.setMousePosition(target);
23+
resolve(this);
24+
} catch (e) {
25+
reject(e);
26+
}
2327
});
2428
}
2529

@@ -28,16 +32,20 @@ export class Mouse {
2832
}
2933

3034
public async move(path: Point[], movementType = MovementType.linear): Promise<Mouse> {
31-
return new Promise<Mouse>(async resolve => {
32-
const timeSteps = movementType(path.length, this.config.mouseSpeed);
33-
for (let idx = 0; idx < path.length; ++idx) {
34-
const node = path[idx];
35-
const minTime = timeSteps[idx];
36-
await this.waitForNextTick(minTime);
37-
await this.native.setMousePosition(node);
38-
await this.updateTick();
35+
return new Promise<Mouse>(async (resolve, reject) => {
36+
try {
37+
const timeSteps = movementType(path.length, this.config.mouseSpeed);
38+
for (let idx = 0; idx < path.length; ++idx) {
39+
const node = path[idx];
40+
const minTime = timeSteps[idx];
41+
await this.waitForNextTick(minTime);
42+
await this.native.setMousePosition(node);
43+
await this.updateTick();
44+
}
45+
resolve(this);
46+
} catch (e) {
47+
reject(e);
3948
}
40-
resolve(this);
4149
});
4250
}
4351

@@ -51,58 +59,82 @@ export class Mouse {
5159
}
5260

5361
public async rightClick(): Promise<Mouse> {
54-
return new Promise<Mouse>(async resolve => {
55-
await this.waitForNextTick(this.config.autoDelayMs);
56-
await this.native.rightClick();
57-
await this.updateTick();
58-
resolve(this);
62+
return new Promise<Mouse>(async (resolve, reject) => {
63+
try {
64+
await this.waitForNextTick(this.config.autoDelayMs);
65+
await this.native.rightClick();
66+
await this.updateTick();
67+
resolve(this);
68+
} catch (e) {
69+
reject(e);
70+
}
5971
});
6072
}
6173

6274
public async scrollDown(amount: number): Promise<Mouse> {
63-
return new Promise<Mouse>(async resolve => {
64-
await this.waitForNextTick(this.config.autoDelayMs);
65-
await this.native.scrollDown(amount);
66-
await this.updateTick();
67-
resolve(this);
75+
return new Promise<Mouse>(async (resolve, reject) => {
76+
try {
77+
await this.waitForNextTick(this.config.autoDelayMs);
78+
await this.native.scrollDown(amount);
79+
await this.updateTick();
80+
resolve(this);
81+
} catch (e) {
82+
reject(e);
83+
}
6884
});
6985
}
7086

7187
public async scrollUp(amount: number): Promise<Mouse> {
72-
return new Promise<Mouse>(async resolve => {
73-
await this.waitForNextTick(this.config.autoDelayMs);
74-
await this.native.scrollUp(amount);
75-
await this.updateTick();
76-
resolve(this);
88+
return new Promise<Mouse>(async (resolve, reject) => {
89+
try {
90+
await this.waitForNextTick(this.config.autoDelayMs);
91+
await this.native.scrollUp(amount);
92+
await this.updateTick();
93+
resolve(this);
94+
} catch (e) {
95+
reject(e);
96+
}
7797
});
7898
}
7999

80100
public async scrollLeft(amount: number): Promise<Mouse> {
81-
return new Promise<Mouse>(async resolve => {
82-
await this.waitForNextTick(this.config.autoDelayMs);
83-
await this.native.scrollLeft(amount);
84-
await this.updateTick();
85-
resolve(this);
101+
return new Promise<Mouse>(async (resolve, reject) => {
102+
try {
103+
await this.waitForNextTick(this.config.autoDelayMs);
104+
await this.native.scrollLeft(amount);
105+
await this.updateTick();
106+
resolve(this);
107+
} catch (e) {
108+
reject(e);
109+
}
86110
});
87111
}
88112

89113
public async scrollRight(amount: number): Promise<Mouse> {
90-
return new Promise<Mouse>(async resolve => {
91-
await this.waitForNextTick(this.config.autoDelayMs);
92-
await this.native.scrollRight(amount);
93-
await this.updateTick();
94-
resolve(this);
114+
return new Promise<Mouse>(async (resolve, reject) => {
115+
try {
116+
await this.waitForNextTick(this.config.autoDelayMs);
117+
await this.native.scrollRight(amount);
118+
await this.updateTick();
119+
resolve(this);
120+
} catch (e) {
121+
reject(e);
122+
}
95123
});
96124
}
97125

98126
public async drag(path: Point[]): Promise<Mouse> {
99-
return new Promise<Mouse>(async resolve => {
100-
await this.waitForNextTick(this.config.autoDelayMs);
101-
await this.native.pressButton(Button.LEFT);
102-
await this.move(path);
103-
await this.native.releaseButton(Button.LEFT);
104-
await this.updateTick();
105-
resolve(this);
127+
return new Promise<Mouse>(async (resolve, reject) => {
128+
try {
129+
await this.waitForNextTick(this.config.autoDelayMs);
130+
await this.native.pressButton(Button.LEFT);
131+
await this.move(path);
132+
await this.native.releaseButton(Button.LEFT);
133+
await this.updateTick();
134+
resolve(this);
135+
} catch (e) {
136+
reject(e);
137+
}
106138
});
107139
}
108140

0 commit comments

Comments
 (0)