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:
parent
76942aed42
commit
5c766367c5
7
packages/utility-features/startable-stoppable/index.ts
Normal file
7
packages/utility-features/startable-stoppable/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export type {
|
||||
StartableStoppable,
|
||||
Starter,
|
||||
Stopper,
|
||||
} from "./src/get-startable-stoppable";
|
||||
|
||||
export { getStartableStoppable } from "./src/get-startable-stoppable";
|
||||
@ -0,0 +1,2 @@
|
||||
module.exports =
|
||||
require("@k8slens/jest").monorepoPackageConfig(__dirname).configForReact;
|
||||
29
packages/utility-features/startable-stoppable/package.json
Normal file
29
packages/utility-features/startable-stoppable/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -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";
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "@k8slens/typescript/config/base.json"
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
module.exports = require("@k8slens/webpack").configForNode;
|
||||
Loading…
Reference in New Issue
Block a user