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

Extract startable-stoppable to NPM package

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2023-03-13 08:27:57 +02:00
parent 76942aed42
commit 5c766367c5
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
7 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,7 @@
export type {
StartableStoppable,
Starter,
Stopper,
} from "./src/get-startable-stoppable";
export { getStartableStoppable } from "./src/get-startable-stoppable";

View File

@ -0,0 +1,2 @@
module.exports =
require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;

View File

@ -0,0 +1,29 @@
{
"name": "@k8slens/startable-stoppable",
"private": false,
"version": "6.5.0-alpha.0",
"description": "TBD",
"type": "commonjs",
"files": [
"build"
],
"repository": {
"type": "git",
"url": "git+https://github.com/lensapp/lens.git"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": {
"name": "OpenLens Authors",
"email": "info@k8slens.dev"
},
"license": "MIT",
"homepage": "https://github.com/lensapp/lens",
"scripts": {
"build": "webpack",
"dev": "webpack --mode=development --watch",
"test": "jest --coverage --runInBand",
"lint": "lens-lint",
"lint:fix": "lens-lint --fix"
}
}

View File

@ -0,0 +1,66 @@
import type { StartableStoppable } from "./get-startable-stoppable";
import { getStartableStoppable } from "./get-startable-stoppable";
describe("getStartableStoppable", () => {
let stopMock: jest.MockedFunction<() => void>;
let startMock: jest.MockedFunction<() => () => void>;
let actual: StartableStoppable;
beforeEach(() => {
stopMock = jest.fn();
startMock = jest.fn().mockImplementation(() => stopMock);
actual = getStartableStoppable("some-id", startMock);
});
it("does not start yet", () => {
expect(startMock).not.toHaveBeenCalled();
});
it("does not stop yet", () => {
expect(stopMock).not.toHaveBeenCalled();
});
it("when stopping before ever starting, throws", () => {
expect(() => actual.stop()).toThrow(
'Tried to stop "some-id", but it is already stopped.'
);
});
it("is not started", () => {
expect(actual.started).toBe(false);
});
describe("when started", () => {
beforeEach(() => {
actual.start();
});
it("calls start function", () => {
expect(startMock).toHaveBeenCalled();
});
it("is started", () => {
expect(actual.started).toBe(true);
});
it("when started again, throws", () => {
expect(() => actual.start()).toThrow(
'Tried to start "some-id", but it is already started.'
);
});
describe("when stopped", () => {
beforeEach(() => {
actual.stop();
});
it("calls stop function", () => {
expect(stopMock).toBeCalled();
});
it("is stopped", () => {
expect(actual.started).toBe(false);
});
});
});
});

View File

@ -0,0 +1,43 @@
export type Stopper = () => void;
export type Starter = () => Stopper;
export interface StartableStoppable {
readonly started: boolean;
start: () => void;
stop: () => void;
}
type StartableStoppableState = "stopped" | "started" | "starting";
export function getStartableStoppable(
id: string,
startAndGetStopper: Starter
): StartableStoppable {
let stop: Stopper;
let state: StartableStoppableState = "stopped";
return {
get started() {
return state === "started";
},
start: () => {
if (state !== "stopped") {
throw new Error(`Tried to start "${id}", but it is already ${state}.`);
}
state = "starting";
stop = startAndGetStopper();
state = "started";
},
stop: () => {
if (state !== "started") {
throw new Error(`Tried to stop "${id}", but it is already ${state}.`);
}
stop();
state = "stopped";
},
};
}

View File

@ -0,0 +1,3 @@
{
"extends": "@k8slens/typescript/config/base.json"
}

View File

@ -0,0 +1 @@
module.exports = require("@k8slens/webpack").configForNode;