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

Make startable-stoppable also restartable

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-04-12 15:45:49 +03:00 committed by Janne Savolainen
parent bcf1a2aaf0
commit 9b48478c2d
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
2 changed files with 31 additions and 7 deletions

View File

@ -39,6 +39,12 @@ describe("getStartableStoppable", () => {
expect(startMock).toHaveBeenCalled(); expect(startMock).toHaveBeenCalled();
}); });
it("when started again, throws", () => {
expect(() => {
actual.start();
}).toThrow("Tried to start something that has already started.");
});
it("does not stop yet", () => { it("does not stop yet", () => {
expect(stopMock).not.toHaveBeenCalled(); expect(stopMock).not.toHaveBeenCalled();
}); });
@ -58,10 +64,24 @@ describe("getStartableStoppable", () => {
}).toThrow("Tried to stop something that has already stopped."); }).toThrow("Tried to stop something that has already stopped.");
}); });
it("when started again, throws for restart being YAGNI with logical blind spots", () => { describe("when started again", () => {
expect(() => { beforeEach(() => {
startMock.mockClear();
actual.start(); actual.start();
}).toThrow("Tried to restart something that has stopped."); });
it("starts", () => {
expect(startMock).toHaveBeenCalled();
});
it("when stopped, stops", () => {
stopMock.mockClear();
actual.stop();
expect(stopMock).toHaveBeenCalled();
});
}); });
}); });
}); });

View File

@ -12,22 +12,26 @@ export const getStartableStoppable = (
return { return {
start: () => { start: () => {
if (started) { if (started) {
throw new Error("Tried to restart something that has stopped."); throw new Error("Tried to start something that has already started.");
} }
stopped = false;
dispose = startAndGetStopCallback(); dispose = startAndGetStopCallback();
started = true; started = true;
}, },
stop: () => { stop: () => {
if (stopped) {
throw new Error("Tried to stop something that has already stopped.");
}
if (!started) { if (!started) {
throw new Error("Tried to stop something that has not started yet."); throw new Error("Tried to stop something that has not started yet.");
} }
if (stopped) { started = false;
throw new Error("Tried to stop something that has already stopped.");
}
dispose(); dispose();