Skip to content

Commit

Permalink
Merge pull request #8 from x0k/workers-refactoring
Browse files Browse the repository at this point in the history
Workers refactoring
  • Loading branch information
x0k authored Jun 26, 2024
2 parents e5bebfa + 2a41ccb commit 73a2873
Show file tree
Hide file tree
Showing 71 changed files with 602 additions and 586 deletions.
1 change: 1 addition & 0 deletions apps/ppp/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default defineConfig({
external: ["sharp"],
},
},
assetsInclude: ["**/*.wasm", "**/*.zip"],
},
markdown: {
shikiConfig: {
Expand Down
8 changes: 6 additions & 2 deletions apps/ppp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
"@xterm/xterm": "^5.5.0",
"astro": "^4.10.2",
"astro-icon": "^1.1.0",
"libs": "workspace:*",
"monaco-editor": "^0.49.0",
"monaco-vim": "^0.4.1",
"tailwindcss": "^3.4.4",
"libs": "workspace:*",
"testing": "workspace:*",
"design-patterns-factory": "workspace:*"
"testing-go": "workspace:*",
"testing-javascript": "workspace:*",
"testing-php": "workspace:*",
"testing-python": "workspace:*",
"testing-typescript": "workspace:*"
},
"devDependencies": {
"@iconify-json/lucide": "^1.1.191",
Expand Down
2 changes: 1 addition & 1 deletion apps/ppp/src/adapters/monaco.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Language } from "@/lib/languages";
import { Language } from "@/shared/languages";

export const MONACO_LANGUAGE_ID: Record<Language, string> = {
[Language.PHP]: "php",
Expand Down
42 changes: 39 additions & 3 deletions apps/ppp/src/adapters/svelte-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,57 @@ import { mount } from "svelte";

import type { TestData, TestRunnerFactory } from "testing";

import {
GoWorker,
JsWorker,
PhpWorker,
PyWorker,
TsWorker,
} from "@/lib/workers";
import { Language } from "@/shared/languages";
import Editor, { type Props } from "@/components/editor/editor.svelte";
import type { Language } from "@/lib/languages";
import {
makeRemoteTestRunnerFactory,
type UniversalFactory,
} from "testing/actor";

export const LANG_WORKERS: Record<Language, new () => Worker> = {
[Language.Go]: GoWorker,
[Language.PHP]: PhpWorker,
[Language.Python]: PyWorker,
[Language.TypeScript]: TsWorker,
[Language.JavaScript]: JsWorker,
};

export function mountEditor<L extends Language, I, O>(
testsData: TestData<I, O>[],
runtimes: Record<
L,
{ initialValue: string; testRunnerFactory: TestRunnerFactory<I, O> }
{ initialValue: string; factory: UniversalFactory<I, O, any> }
>
) {
const element = document.getElementById("editor-placeholder")!;
const props = {
contentId: element.dataset.contentId!,
testsData,
runtimes,
runtimes: Object.fromEntries(
Object.keys(runtimes).map(
(lang) =>
[
lang,
{
initialValue: runtimes[lang as L].initialValue,
testRunnerFactory: makeRemoteTestRunnerFactory(
LANG_WORKERS[lang as L],
runtimes[lang as L].factory
),
},
] as const
)
) as Record<
L,
{ initialValue: string; testRunnerFactory: TestRunnerFactory<I, O> }
>,
} satisfies Props<L, I, O>;
mount(Editor, {
target: element.parentElement!,
Expand Down
151 changes: 0 additions & 151 deletions apps/ppp/src/adapters/testing-actor.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/ppp/src/components/editor/editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import {
LANGUAGE_TITLE,
Language,
} from '@/lib/languages'
} from '@/shared/languages'
import Select from '@/components/select.svelte';
import { MONACO_LANGUAGE_ID } from "@/adapters/monaco";
import { createSyncStorage } from "@/adapters/storage";
Expand Down
2 changes: 1 addition & 1 deletion apps/ppp/src/components/editor/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export const vimState = reactive(
);

export const testRunnerTimeout = reactive(
createSyncStorage(localStorage, "editor-test-runner-timeout", 8000)
createSyncStorage(localStorage, "editor-test-runner-timeout", 60000)
);
36 changes: 17 additions & 19 deletions apps/ppp/src/content/design-patterns/factory/editor.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,36 @@ const { contentId } = Astro.props;
<div id="editor-placeholder" data-content-id={contentId}></div>

<script>
import { makeRemoteTestRunnerFactory } from 'testing/actor'

import { Language } from "@/lib/languages";
import { Language } from "@/shared/languages";
import { mountEditor } from "@/adapters/svelte-editor";

import { testsData } from "design-patterns-factory";
import { code as jsCode, Worker as JsWorker } from "design-patterns-factory/js";
import { code as tsCode, Worker as TsWorker } from "design-patterns-factory/ts";
import { code as phpCode, Worker as PhpWorker } from "design-patterns-factory/php";
import { code as pyCode, Worker as PyWorker } from "design-patterns-factory/python";
import { code as goCode, Worker as GoWorker } from 'design-patterns-factory/go';
import { testsData } from "./tests-data";
import { jsCode, jsFactory } from "./js";
import { tsCode, tsFactory } from "./ts";
import { phpCode, phpFactory } from "./php";
import { pyCode, pyFactory } from "./python";
import { goCode, goFactory } from "./go";

mountEditor(testsData, {
[Language.PHP]: {
initialValue: phpCode,
testRunnerFactory: makeRemoteTestRunnerFactory(PhpWorker),
[Language.JavaScript]: {
initialValue: jsCode,
factory: jsFactory,
},
[Language.TypeScript]: {
initialValue: tsCode,
testRunnerFactory: makeRemoteTestRunnerFactory(TsWorker),
factory: tsFactory,
},
[Language.JavaScript]: {
initialValue: jsCode,
testRunnerFactory: makeRemoteTestRunnerFactory(JsWorker),
[Language.PHP]: {
initialValue: phpCode,
factory: phpFactory,
},
[Language.Python]: {
initialValue: pyCode,
testRunnerFactory: makeRemoteTestRunnerFactory(PyWorker),
factory: pyFactory,
},
[Language.Go]: {
initialValue: goCode,
testRunnerFactory: makeRemoteTestRunnerFactory(GoWorker),
}
factory: goFactory,
},
});
</script>
18 changes: 18 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/go/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Only type imports are allowed

import type { UniversalFactory } from "testing/actor";

import type { GoUniversalFactoryData } from "@/lib/workers/go";

import type { Input, Output } from "../tests-data";

export const factory: UniversalFactory<
Input,
Output,
GoUniversalFactoryData<Input, Output>
> = ({ makeTestRunnerFactory }) => {
return makeTestRunnerFactory(
({ paymentSystem, amount, base }) =>
`factory.Payment(factory.PaymentSystemType("${paymentSystem}"), ${base}, ${amount})`
);
};
2 changes: 2 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/go/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as goCode } from "./code.go?raw";
export { factory as goFactory } from "./factory";
21 changes: 21 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/js/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Only type imports are allowed
import type { UniversalFactory } from "testing/actor";

import type { UniversalFactoryData } from "@/lib/workers/js";

import type { Input, Output } from "../tests-data";
import type { PaymentSystemType } from "../reference";

interface TestingModule {
payment(type: PaymentSystemType, base: number, amount: number): number;
}

export const factory: UniversalFactory<
Input,
Output,
UniversalFactoryData<TestingModule, Input, Output>
> = ({ makeTestRunnerFactory }) => {
return makeTestRunnerFactory(async (m, input) =>
m.payment(input.paymentSystem, input.base, input.amount)
);
};
2 changes: 2 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/js/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as jsCode } from "./code.js?raw";
export { factory as jsFactory } from "./factory";
32 changes: 32 additions & 0 deletions apps/ppp/src/content/design-patterns/factory/php/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Only type imports are allowed
import type { UniversalFactory } from "testing/actor";

import type { UniversalFactoryData } from "@/lib/workers/php";

import type { Input, Output } from "../tests-data";

// Const enum import is allowed
import type { PaymentSystemType } from "../reference";

export const factory: UniversalFactory<
Input,
Output,
UniversalFactoryData<Input, Output>
> = ({ makeTestRunnerFactory }) => {
const PHP_PAYMENT_SYSTEM_TYPES: Record<PaymentSystemType, string> = {
paypal: "PaymentSystemType::PAYPAL",
webmoney: "PaymentSystemType::WEBMONEY",
"cat-bank": "PaymentSystemType::CAT_BANK",
};
return makeTestRunnerFactory(
({ paymentSystem, base, amount }: Input) =>
`strval(payment(${PHP_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}))`,
(result: string) => {
const r = parseInt(result, 10);
if (isNaN(r)) {
throw new Error(`Invalid result type: ${result}, expected number`);
}
return r;
}
);
};
Loading

0 comments on commit 73a2873

Please sign in to comment.