1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Prevent restarting a startableStoppable when already being started

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-05-05 11:20:25 +03:00
parent 43598914ee
commit 34e504b65b
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
2 changed files with 26 additions and 0 deletions

View File

@ -57,6 +57,24 @@ describe("getStartableStoppable", () => {
expect(actual.started).toBe(false); expect(actual.started).toBe(false);
}); });
describe("when started again before the start has finished", () => {
let error: Error;
beforeEach(() => {
startMock.mockClear();
actual.start().catch((e) => { error = e; });
});
it("does not start starting again", () => {
expect(startMock).not.toHaveBeenCalled();
});
it("throws", () => {
expect(error.message).toBe("Tried to start \"some-id\", but it is already being started.");
});
});
describe("when starting finishes", () => { describe("when starting finishes", () => {
beforeEach(async () => { beforeEach(async () => {
await startMock.resolve(stopMock); await startMock.resolve(stopMock);

View File

@ -13,6 +13,7 @@ export const getStartableStoppable = (
let stop: Stopper; let stop: Stopper;
let stopped = false; let stopped = false;
let started = false; let started = false;
let starting = false;
let startingPromise: Promise<Stopper> | Stopper; let startingPromise: Promise<Stopper> | Stopper;
return { return {
@ -21,6 +22,12 @@ export const getStartableStoppable = (
}, },
start: async () => { start: async () => {
if (starting) {
throw new Error(`Tried to start "${id}", but it is already being started.`);
}
starting = true;
if (started) { if (started) {
throw new Error(`Tried to start "${id}", but it has already started.`); throw new Error(`Tried to start "${id}", but it has already started.`);
} }
@ -30,6 +37,7 @@ export const getStartableStoppable = (
stopped = false; stopped = false;
started = true; started = true;
starting = false;
}, },
stop: async () => { stop: async () => {