From 65fdbba4f5be7dfb1fb279a7140252a8b727be57 Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Mon, 26 Sep 2022 15:09:22 +0300 Subject: [PATCH] Remove local implementation of Runnable, being replaced by a shared NPM-package Signed-off-by: Iku-turso --- src/common/runnable/run-many-for.test.ts | 267 ------------------ src/common/runnable/run-many-for.ts | 51 ---- src/common/runnable/run-many-sync-for.test.ts | 197 ------------- src/common/runnable/run-many-sync-for.ts | 50 ---- .../throw-with-incorrect-hierarchy-for.ts | 16 -- 5 files changed, 581 deletions(-) delete mode 100644 src/common/runnable/run-many-for.test.ts delete mode 100644 src/common/runnable/run-many-for.ts delete mode 100644 src/common/runnable/run-many-sync-for.test.ts delete mode 100644 src/common/runnable/run-many-sync-for.ts delete mode 100644 src/common/runnable/throw-with-incorrect-hierarchy-for.ts diff --git a/src/common/runnable/run-many-for.test.ts b/src/common/runnable/run-many-for.test.ts deleted file mode 100644 index 35db17952a..0000000000 --- a/src/common/runnable/run-many-for.test.ts +++ /dev/null @@ -1,267 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import type { AsyncFnMock } from "@async-fn/jest"; -import asyncFn from "@async-fn/jest"; -import { createContainer, getInjectable, getInjectionToken } from "@ogre-tools/injectable"; -import type { Runnable } from "./run-many-for"; -import { runManyFor } from "./run-many-for"; -import { getPromiseStatus } from "../test-utils/get-promise-status"; - -describe("runManyFor", () => { - describe("given no hierarchy, when running many", () => { - let runMock: AsyncFnMock<(...args: unknown[]) => Promise>; - let actualPromise: Promise; - - beforeEach(() => { - const rootDi = createContainer("irrelevant"); - - runMock = asyncFn(); - - const someInjectionTokenForRunnables = getInjectionToken({ - id: "some-injection-token", - }); - - const someInjectable = getInjectable({ - id: "some-injectable", - instantiate: () => ({ run: () => runMock("some-call") }), - injectionToken: someInjectionTokenForRunnables, - }); - - const someOtherInjectable = getInjectable({ - id: "some-other-injectable", - instantiate: () => ({ run: () => runMock("some-other-call") }), - injectionToken: someInjectionTokenForRunnables, - }); - - rootDi.register(someInjectable, someOtherInjectable); - - const runMany = runManyFor(rootDi)(someInjectionTokenForRunnables); - - actualPromise = runMany() as Promise; - }); - - it("runs all runnables at the same time", () => { - expect(runMock.mock.calls).toEqual([ - ["some-call"], - ["some-other-call"], - ]); - }); - - it("does not resolve yet", async () => { - const promiseStatus = await getPromiseStatus(actualPromise); - - expect(promiseStatus.fulfilled).toBe(false); - }); - - it("when all runnables resolve, resolves", async () => { - await Promise.all([runMock.resolve(), runMock.resolve()]); - - expect(await actualPromise).toBe(undefined); - }); - }); - - describe("given hierarchy that is three levels deep, when running many", () => { - let runMock: AsyncFnMock<(...args: unknown[]) => Promise>; - let actualPromise: Promise; - - beforeEach(() => { - const di = createContainer("irrelevant"); - - runMock = asyncFn(); - - const someInjectionTokenForRunnables = getInjectionToken({ - id: "some-injection-token", - }); - - const someInjectable1 = getInjectable({ - id: "some-injectable-1", - - instantiate: (di) => ({ - run: () => runMock("third-level-run"), - runAfter: di.inject(someInjectable2), - }), - - injectionToken: someInjectionTokenForRunnables, - }); - - const someInjectable2 = getInjectable({ - id: "some-injectable-2", - - instantiate: (di) => ({ - run: () => runMock("second-level-run"), - runAfter: di.inject(someInjectable3), - }), - - injectionToken: someInjectionTokenForRunnables, - }); - - const someInjectable3 = getInjectable({ - id: "some-injectable-3", - instantiate: () => ({ run: () => runMock("first-level-run") }), - injectionToken: someInjectionTokenForRunnables, - }); - - di.register(someInjectable1, someInjectable2, someInjectable3); - - const runMany = runManyFor(di)(someInjectionTokenForRunnables); - - actualPromise = runMany() as Promise; - }); - - it("runs first level runnables", () => { - expect(runMock.mock.calls).toEqual([["first-level-run"]]); - }); - - it("does not resolve yet", async () => { - const promiseStatus = await getPromiseStatus(actualPromise); - - expect(promiseStatus.fulfilled).toBe(false); - }); - - describe("when first level runnables resolve", () => { - beforeEach(async () => { - runMock.mockClear(); - - await runMock.resolveSpecific(["first-level-run"]); - }); - - it("runs second level runnables", async () => { - expect(runMock.mock.calls).toEqual([["second-level-run"]]); - }); - - it("does not resolve yet", async () => { - const promiseStatus = await getPromiseStatus(actualPromise); - - expect(promiseStatus.fulfilled).toBe(false); - }); - - describe("when second level runnables resolve", () => { - beforeEach(async () => { - runMock.mockClear(); - - await runMock.resolveSpecific(["second-level-run"]); - }); - - it("runs final third level runnables", async () => { - expect(runMock.mock.calls).toEqual([["third-level-run"]]); - }); - - it("does not resolve yet", async () => { - const promiseStatus = await getPromiseStatus(actualPromise); - - expect(promiseStatus.fulfilled).toBe(false); - }); - - describe("when final third level runnables resolve", () => { - beforeEach(async () => { - await runMock.resolveSpecific(["third-level-run"]); - }); - - it("resolves", async () => { - const promiseStatus = await getPromiseStatus(actualPromise); - - expect(promiseStatus.fulfilled).toBe(true); - }); - }); - }); - }); - }); - - it("given invalid hierarchy, when running runnables, throws", () => { - const rootDi = createContainer("irrelevant"); - - const runMock = asyncFn<(...args: unknown[]) => void>(); - - const someInjectionToken = getInjectionToken({ - id: "some-injection-token", - }); - - const someOtherInjectionToken = getInjectionToken({ - id: "some-other-injection-token", - }); - - const someInjectable = getInjectable({ - id: "some-runnable-1", - - instantiate: (di) => ({ - run: () => runMock("some-runnable-1"), - runAfter: di.inject(someOtherInjectable), - }), - - injectionToken: someInjectionToken, - }); - - const someOtherInjectable = getInjectable({ - id: "some-runnable-2", - - instantiate: () => ({ - run: () => runMock("some-runnable-2"), - }), - - injectionToken: someOtherInjectionToken, - }); - - rootDi.register(someInjectable, someOtherInjectable); - - const runMany = runManyFor(rootDi)( - someInjectionToken, - ); - - return expect(() => runMany()).rejects.toThrow( - "Tried to run runnable after other runnable which does not same injection token.", - ); - }); - - describe("when running many with parameter", () => { - let runMock: AsyncFnMock<(...args: unknown[]) => Promise>; - - beforeEach(() => { - const rootDi = createContainer("irrelevant"); - - runMock = asyncFn(); - - const someInjectionTokenForRunnablesWithParameter = getInjectionToken< - Runnable - >({ - id: "some-injection-token", - }); - - const someInjectable = getInjectable({ - id: "some-runnable-1", - - instantiate: () => ({ - run: (parameter) => runMock("run-of-some-runnable-1", parameter), - }), - - injectionToken: someInjectionTokenForRunnablesWithParameter, - }); - - const someOtherInjectable = getInjectable({ - id: "some-runnable-2", - - instantiate: () => ({ - run: (parameter) => runMock("run-of-some-runnable-2", parameter), - }), - - injectionToken: someInjectionTokenForRunnablesWithParameter, - }); - - rootDi.register(someInjectable, someOtherInjectable); - - const runMany = runManyFor(rootDi)( - someInjectionTokenForRunnablesWithParameter, - ); - - runMany("some-parameter"); - }); - - it("runs all runnables using the parameter", () => { - expect(runMock.mock.calls).toEqual([ - ["run-of-some-runnable-1", "some-parameter"], - ["run-of-some-runnable-2", "some-parameter"], - ]); - }); - }); -}); diff --git a/src/common/runnable/run-many-for.ts b/src/common/runnable/run-many-for.ts deleted file mode 100644 index f2c1a4ae56..0000000000 --- a/src/common/runnable/run-many-for.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { pipeline } from "@ogre-tools/fp"; -import type { - DiContainerForInjection, - InjectionToken, -} from "@ogre-tools/injectable"; -import { filter, forEach, map, tap } from "lodash/fp"; -import { throwWithIncorrectHierarchyFor } from "./throw-with-incorrect-hierarchy-for"; - -export interface Runnable { - run: Run; - runAfter?: this; -} - -type Run = (parameter: Param) => Promise | void; - -export type RunMany = ( - injectionToken: InjectionToken, void> -) => Run; - -export function runManyFor(di: DiContainerForInjection): RunMany { - return (injectionToken) => async (parameter) => { - const allRunnables = di.injectMany(injectionToken); - - const throwWithIncorrectHierarchy = throwWithIncorrectHierarchyFor(allRunnables); - - const recursedRun = async ( - runAfterRunnable: Runnable | undefined = undefined, - ) => - await pipeline( - allRunnables, - - tap(runnables => forEach(throwWithIncorrectHierarchy, runnables)), - - filter((runnable) => runnable.runAfter === runAfterRunnable), - - map(async (runnable) => { - await runnable.run(parameter); - - await recursedRun(runnable); - }), - - (promises) => Promise.all(promises), - ); - - await recursedRun(); - }; -} diff --git a/src/common/runnable/run-many-sync-for.test.ts b/src/common/runnable/run-many-sync-for.test.ts deleted file mode 100644 index fe47516725..0000000000 --- a/src/common/runnable/run-many-sync-for.test.ts +++ /dev/null @@ -1,197 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { createContainer, getInjectable, getInjectionToken } from "@ogre-tools/injectable"; -import type { RunnableSync } from "./run-many-sync-for"; -import { runManySyncFor } from "./run-many-sync-for"; - -describe("runManySyncFor", () => { - describe("given hierarchy, when running many", () => { - let runMock: jest.Mock; - - beforeEach(() => { - const rootDi = createContainer("irrelevant"); - - runMock = jest.fn(); - - const someInjectionTokenForRunnables = getInjectionToken({ - id: "some-injection-token", - }); - - const someInjectable = getInjectable({ - id: "some-injectable", - instantiate: () => ({ run: () => runMock("some-call") }), - injectionToken: someInjectionTokenForRunnables, - }); - - const someOtherInjectable = getInjectable({ - id: "some-other-injectable", - instantiate: () => ({ run: () => runMock("some-other-call") }), - injectionToken: someInjectionTokenForRunnables, - }); - - rootDi.register(someInjectable, someOtherInjectable); - - const runMany = runManySyncFor(rootDi)(someInjectionTokenForRunnables); - - runMany(); - }); - - it("runs all runnables at the same time", () => { - expect(runMock.mock.calls).toEqual([ - ["some-call"], - ["some-other-call"], - ]); - }); - }); - - describe("given hierarchy that is three levels deep, when running many", () => { - let runMock: jest.Mock<(arg: string) => void>; - - beforeEach(() => { - const di = createContainer("irrelevant"); - - runMock = jest.fn(); - - const someInjectionTokenForRunnables = getInjectionToken({ - id: "some-injection-token", - }); - - const someInjectable1 = getInjectable({ - id: "some-injectable-1", - - instantiate: (di) => ({ - run: () => runMock("third-level-run"), - runAfter: di.inject(someInjectable2), - }), - - injectionToken: someInjectionTokenForRunnables, - }); - - const someInjectable2 = getInjectable({ - id: "some-injectable-2", - - instantiate: (di) => ({ - run: () => runMock("second-level-run"), - runAfter: di.inject(someInjectable3), - }), - - injectionToken: someInjectionTokenForRunnables, - }); - - const someInjectable3 = getInjectable({ - id: "some-injectable-3", - instantiate: () => ({ run: () => runMock("first-level-run") }), - injectionToken: someInjectionTokenForRunnables, - }); - - di.register(someInjectable1, someInjectable2, someInjectable3); - - const runMany = runManySyncFor(di)(someInjectionTokenForRunnables); - - runMany(); - }); - - it("runs runnables in order", () => { - expect(runMock.mock.calls).toEqual([["first-level-run"], ["second-level-run"], ["third-level-run"]]); - }); - }); - - it("given invalid hierarchy, when running runnables, throws", () => { - const rootDi = createContainer("irrelevant"); - - const runMock = jest.fn(); - - const someInjectionToken = getInjectionToken({ - id: "some-injection-token", - }); - - const someOtherInjectionToken = getInjectionToken({ - id: "some-other-injection-token", - }); - - const someInjectable = getInjectable({ - id: "some-runnable-1", - - instantiate: (di) => ({ - run: () => runMock("some-runnable-1"), - runAfter: di.inject(someOtherInjectable), - }), - - injectionToken: someInjectionToken, - }); - - const someOtherInjectable = getInjectable({ - id: "some-runnable-2", - - instantiate: () => ({ - run: () => runMock("some-runnable-2"), - }), - - injectionToken: someOtherInjectionToken, - }); - - rootDi.register(someInjectable, someOtherInjectable); - - const runMany = runManySyncFor(rootDi)( - someInjectionToken, - ); - - return expect(() => runMany()).rejects.toThrow( - "Tried to run runnable after other runnable which does not same injection token.", - ); - }); - - describe("when running many with parameter", () => { - let runMock: jest.Mock<(arg: string, arg2: string) => void>; - - beforeEach(() => { - const rootDi = createContainer("irrelevant"); - - runMock = jest.fn(); - - const someInjectionTokenForRunnablesWithParameter = getInjectionToken< - RunnableSync - >({ - id: "some-injection-token", - }); - - const someInjectable = getInjectable({ - id: "some-runnable-1", - - instantiate: () => ({ - run: (parameter) => runMock("run-of-some-runnable-1", parameter), - }), - - injectionToken: someInjectionTokenForRunnablesWithParameter, - }); - - const someOtherInjectable = getInjectable({ - id: "some-runnable-2", - - instantiate: () => ({ - run: (parameter) => runMock("run-of-some-runnable-2", parameter), - }), - - injectionToken: someInjectionTokenForRunnablesWithParameter, - }); - - rootDi.register(someInjectable, someOtherInjectable); - - const runMany = runManySyncFor(rootDi)( - someInjectionTokenForRunnablesWithParameter, - ); - - runMany("some-parameter"); - }); - - it("runs all runnables using the parameter", () => { - expect(runMock.mock.calls).toEqual([ - ["run-of-some-runnable-1", "some-parameter"], - ["run-of-some-runnable-2", "some-parameter"], - ]); - }); - }); -}); - diff --git a/src/common/runnable/run-many-sync-for.ts b/src/common/runnable/run-many-sync-for.ts deleted file mode 100644 index cfe93fa4b3..0000000000 --- a/src/common/runnable/run-many-sync-for.ts +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { pipeline } from "@ogre-tools/fp"; -import type { - DiContainerForInjection, - InjectionToken, -} from "@ogre-tools/injectable"; -import { filter, forEach, map, tap } from "lodash/fp"; -import type { Runnable } from "./run-many-for"; -import { throwWithIncorrectHierarchyFor } from "./throw-with-incorrect-hierarchy-for"; - -export interface RunnableSync { - run: RunSync; - runAfter?: this; -} - -type RunSync = (parameter: Param) => void; - -export type RunManySync = ( - injectionToken: InjectionToken, void> -) => RunSync; - -export function runManySyncFor(di: DiContainerForInjection): RunManySync { - return (injectionToken) => async (parameter) => { - const allRunnables = di.injectMany(injectionToken); - - const throwWithIncorrectHierarchy = throwWithIncorrectHierarchyFor(allRunnables); - - const recursedRun = ( - runAfterRunnable: RunnableSync | undefined = undefined, - ) => - pipeline( - allRunnables, - - tap(runnables => forEach(throwWithIncorrectHierarchy, runnables)), - - filter((runnable) => runnable.runAfter === runAfterRunnable), - - map((runnable) => { - runnable.run(parameter); - - recursedRun(runnable); - }), - ); - - recursedRun(); - }; -} diff --git a/src/common/runnable/throw-with-incorrect-hierarchy-for.ts b/src/common/runnable/throw-with-incorrect-hierarchy-for.ts deleted file mode 100644 index 03073c4044..0000000000 --- a/src/common/runnable/throw-with-incorrect-hierarchy-for.ts +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import type { Runnable } from "./run-many-for"; -import type { RunnableSync } from "./run-many-sync-for"; - -export const throwWithIncorrectHierarchyFor = - (allRunnables: Runnable[] | RunnableSync[]) => - (runnable: Runnable | RunnableSync) => { - if (runnable.runAfter && !allRunnables.includes(runnable.runAfter)) { - throw new Error( - "Tried to run runnable after other runnable which does not same injection token.", - ); - } - };