Skip to content

Commit 75e55bd

Browse files
committed
lint
1 parent d7521e3 commit 75e55bd

File tree

7 files changed

+187
-202
lines changed

7 files changed

+187
-202
lines changed

packages/monitor/src/__tests__/launchpad-monitor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function createTestMonitor(
6060

6161
vi.mock("../utils/debounce-results.ts", () => ({
6262
debounceResultAsync: (fn: unknown) => fn,
63-
}))
63+
}));
6464

6565
describe("LaunchpadMonitor", () => {
6666
describe("connect", () => {

packages/monitor/src/core/app-manager.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { Logger } from "@bluecadet/launchpad-utils";
22
import { type Result, ResultAsync, err, errAsync, ok, okAsync } from "neverthrow";
33
import type pm2 from "pm2";
44
import type { ResolvedAppConfig, ResolvedMonitorConfig } from "../monitor-config.js";
5+
import { debounceResultAsync } from "../utils/debounce-results.js";
56
import sortWindows from "../utils/sort-windows.js";
67
import type { ProcessManager } from "./process-manager.js";
7-
import { debounceResultAsync } from "../utils/debounce-results.js";
88

99
export class AppManager {
1010
#logger: Logger;
@@ -19,7 +19,7 @@ export class AppManager {
1919
this.applyWindowSettings = debounceResultAsync(
2020
this.applyWindowSettings.bind(this),
2121
this.#config.windowsApi.debounceDelay,
22-
)
22+
);
2323
}
2424

2525
startApp(appName: string): ResultAsync<pm2.ProcessDescription, Error> {
@@ -108,8 +108,7 @@ export class AppManager {
108108
});
109109
});
110110

111-
return ResultAsync.combine(appResults)
112-
.andThen((apps) => {
111+
return ResultAsync.combine(appResults).andThen((apps) => {
113112
return ResultAsync.fromPromise(
114113
sortWindows(apps, this.#logger),
115114
(e) => new Error("Failed to sort windows", { cause: e }),

packages/monitor/src/launchpad-monitor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ class LaunchpadMonitor {
144144
this._pluginDriver.runHookSequential("afterAppStart", { appName: name, process }),
145145
),
146146
),
147-
)
148-
.andThen(() => this._appManager.applyWindowSettings(validatedNames));
147+
).andThen(() => this._appManager.applyWindowSettings(validatedNames));
149148
});
150149
}
151150

Lines changed: 130 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,135 @@
11
import { ResultAsync, err, ok } from "neverthrow";
2-
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { debounceResultAsync } from "../debounce-results.js";
44

55
describe("debounceResultAsync", () => {
6-
beforeEach(() => {
7-
vi.useFakeTimers();
8-
});
9-
10-
afterEach(() => {
11-
vi.restoreAllMocks();
12-
});
13-
14-
it("should debounce function calls", async () => {
15-
const mockFn = vi.fn().mockImplementation((value: number) =>
16-
ResultAsync.fromPromise(
17-
Promise.resolve(value * 2),
18-
error => error as Error
19-
)
20-
);
21-
22-
const debounced = debounceResultAsync(mockFn, 1000);
23-
24-
// Multiple calls in quick succession
25-
debounced(1);
26-
debounced(2);
27-
debounced(3);
28-
29-
// Only the last one should be executed
30-
vi.advanceTimersByTime(1000);
31-
await vi.runAllTimersAsync();
32-
33-
expect(mockFn).toHaveBeenCalledTimes(1);
34-
expect(mockFn).toHaveBeenCalledWith(3);
35-
});
36-
37-
it("should return the same promise for calls during the wait time", async () => {
38-
const mockFn = vi.fn().mockImplementation((value: number) =>
39-
ResultAsync.fromPromise(
40-
Promise.resolve(value * 2),
41-
error => error as Error
42-
)
43-
);
44-
45-
const debounced = debounceResultAsync(mockFn, 1000);
46-
47-
const promise1 = debounced(1);
48-
const promise2 = debounced(2);
49-
50-
expect(promise1).toBe(promise2);
51-
});
52-
53-
it("should return the correct result", async () => {
54-
const mockFn = vi.fn().mockImplementation((value: number) =>
55-
ResultAsync.fromPromise(
56-
Promise.resolve(value * 2),
57-
error => error as Error
58-
)
59-
);
60-
61-
const debounced = debounceResultAsync(mockFn, 1000);
62-
63-
const resultPromise = debounced(5);
64-
vi.advanceTimersByTime(1000);
65-
await vi.runAllTimersAsync();
66-
67-
const result = await resultPromise;
68-
expect(result.isOk()).toBe(true);
69-
expect(result._unsafeUnwrap()).toBe(10);
70-
});
71-
72-
it("should handle errors correctly", async () => {
73-
const testError = new Error("Test error");
74-
const mockFn = vi.fn().mockImplementation(() =>
75-
ResultAsync.fromPromise(
76-
Promise.reject(testError),
77-
error => error as Error
78-
)
79-
);
80-
81-
const debounced = debounceResultAsync(mockFn, 1000);
82-
83-
const resultPromise = debounced();
84-
vi.advanceTimersByTime(1000);
85-
await vi.runAllTimersAsync();
86-
87-
const result = await resultPromise;
88-
expect(result.isErr()).toBe(true);
89-
expect(result._unsafeUnwrapErr()).toBe(testError);
90-
});
91-
92-
it("should reset after a successful call and allow new calls", async () => {
93-
const mockFn = vi.fn()
94-
.mockImplementationOnce((value: number) =>
95-
ResultAsync.fromPromise(
96-
Promise.resolve(value * 2),
97-
error => error as Error
98-
)
99-
)
100-
.mockImplementationOnce((value: number) =>
101-
ResultAsync.fromPromise(
102-
Promise.resolve(value * 3),
103-
error => error as Error
104-
)
105-
);
106-
107-
const debounced = debounceResultAsync(mockFn, 1000);
108-
109-
// First call
110-
const resultPromise1 = debounced(5);
111-
vi.advanceTimersByTime(1000);
112-
await vi.runAllTimersAsync();
113-
114-
const result1 = await resultPromise1;
115-
expect(result1._unsafeUnwrap()).toBe(10);
116-
117-
// Second call should use a new promise
118-
const resultPromise2 = debounced(5);
119-
vi.advanceTimersByTime(1000);
120-
await vi.runAllTimersAsync();
121-
122-
const result2 = await resultPromise2;
123-
expect(result2._unsafeUnwrap()).toBe(15);
124-
125-
expect(mockFn).toHaveBeenCalledTimes(2);
126-
});
127-
128-
it("should work with functions taking multiple arguments", async () => {
129-
const mockFn = vi.fn().mockImplementation((a: number, b: string, c: boolean) =>
130-
ResultAsync.fromPromise(
131-
Promise.resolve(`${a}-${b}-${c}`),
132-
error => error as Error
133-
)
134-
);
135-
136-
const debounced = debounceResultAsync(mockFn, 1000);
137-
138-
const resultPromise = debounced(1, "test", true);
139-
vi.advanceTimersByTime(1000);
140-
await vi.runAllTimersAsync();
141-
142-
const result = await resultPromise;
143-
expect(result._unsafeUnwrap()).toBe("1-test-true");
144-
});
6+
beforeEach(() => {
7+
vi.useFakeTimers();
8+
});
9+
10+
afterEach(() => {
11+
vi.restoreAllMocks();
12+
});
13+
14+
it("should debounce function calls", async () => {
15+
const mockFn = vi
16+
.fn()
17+
.mockImplementation((value: number) =>
18+
ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error),
19+
);
20+
21+
const debounced = debounceResultAsync(mockFn, 1000);
22+
23+
// Multiple calls in quick succession
24+
debounced(1);
25+
debounced(2);
26+
debounced(3);
27+
28+
// Only the last one should be executed
29+
vi.advanceTimersByTime(1000);
30+
await vi.runAllTimersAsync();
31+
32+
expect(mockFn).toHaveBeenCalledTimes(1);
33+
expect(mockFn).toHaveBeenCalledWith(3);
34+
});
35+
36+
it("should return the same promise for calls during the wait time", async () => {
37+
const mockFn = vi
38+
.fn()
39+
.mockImplementation((value: number) =>
40+
ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error),
41+
);
42+
43+
const debounced = debounceResultAsync(mockFn, 1000);
44+
45+
const promise1 = debounced(1);
46+
const promise2 = debounced(2);
47+
48+
expect(promise1).toBe(promise2);
49+
});
50+
51+
it("should return the correct result", async () => {
52+
const mockFn = vi
53+
.fn()
54+
.mockImplementation((value: number) =>
55+
ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error),
56+
);
57+
58+
const debounced = debounceResultAsync(mockFn, 1000);
59+
60+
const resultPromise = debounced(5);
61+
vi.advanceTimersByTime(1000);
62+
await vi.runAllTimersAsync();
63+
64+
const result = await resultPromise;
65+
expect(result.isOk()).toBe(true);
66+
expect(result._unsafeUnwrap()).toBe(10);
67+
});
68+
69+
it("should handle errors correctly", async () => {
70+
const testError = new Error("Test error");
71+
const mockFn = vi
72+
.fn()
73+
.mockImplementation(() =>
74+
ResultAsync.fromPromise(Promise.reject(testError), (error) => error as Error),
75+
);
76+
77+
const debounced = debounceResultAsync(mockFn, 1000);
78+
79+
const resultPromise = debounced();
80+
vi.advanceTimersByTime(1000);
81+
await vi.runAllTimersAsync();
82+
83+
const result = await resultPromise;
84+
expect(result.isErr()).toBe(true);
85+
expect(result._unsafeUnwrapErr()).toBe(testError);
86+
});
87+
88+
it("should reset after a successful call and allow new calls", async () => {
89+
const mockFn = vi
90+
.fn()
91+
.mockImplementationOnce((value: number) =>
92+
ResultAsync.fromPromise(Promise.resolve(value * 2), (error) => error as Error),
93+
)
94+
.mockImplementationOnce((value: number) =>
95+
ResultAsync.fromPromise(Promise.resolve(value * 3), (error) => error as Error),
96+
);
97+
98+
const debounced = debounceResultAsync(mockFn, 1000);
99+
100+
// First call
101+
const resultPromise1 = debounced(5);
102+
vi.advanceTimersByTime(1000);
103+
await vi.runAllTimersAsync();
104+
105+
const result1 = await resultPromise1;
106+
expect(result1._unsafeUnwrap()).toBe(10);
107+
108+
// Second call should use a new promise
109+
const resultPromise2 = debounced(5);
110+
vi.advanceTimersByTime(1000);
111+
await vi.runAllTimersAsync();
112+
113+
const result2 = await resultPromise2;
114+
expect(result2._unsafeUnwrap()).toBe(15);
115+
116+
expect(mockFn).toHaveBeenCalledTimes(2);
117+
});
118+
119+
it("should work with functions taking multiple arguments", async () => {
120+
const mockFn = vi
121+
.fn()
122+
.mockImplementation((a: number, b: string, c: boolean) =>
123+
ResultAsync.fromPromise(Promise.resolve(`${a}-${b}-${c}`), (error) => error as Error),
124+
);
125+
126+
const debounced = debounceResultAsync(mockFn, 1000);
127+
128+
const resultPromise = debounced(1, "test", true);
129+
vi.advanceTimersByTime(1000);
130+
await vi.runAllTimersAsync();
131+
132+
const result = await resultPromise;
133+
expect(result._unsafeUnwrap()).toBe("1-test-true");
134+
});
145135
});

0 commit comments

Comments
 (0)