Skip to content

Commit

Permalink
Use type alias Action<string> for (value: string) => void (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
netalondon authored Jul 31, 2024
1 parent 9f29bfe commit c5c42cf
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 18 deletions.
3 changes: 2 additions & 1 deletion components/src/inline_edit.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { width } from "@davidsouther/jiffies/lib/esm/dom/css/sizing.js";
import { useCallback, useState } from "react";
import { useStateInitializer } from "./react.js";
import { Action } from "@nand2tetris/simulator/types.js";

const Mode = { VIEW: 0, EDIT: 1 };

export const InlineEdit = (props: {
mode?: keyof typeof Mode;
value: string;
highlight: boolean;
onChange: (value: string) => void;
onChange: Action<string>;
onFocus?: () => void;
}) => {
const [mode, setMode] = useState(props.mode ?? Mode.VIEW);
Expand Down
3 changes: 2 additions & 1 deletion components/src/stores/asm.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Dispatch, MutableRefObject, useContext, useMemo, useRef } from "react";
import { RunSpeed } from "src/runbar.js";
import { useImmerReducer } from "../react.js";
import { BaseContext } from "./base.context.js";
import { Action } from "@nand2tetris/simulator/types.js";

export interface TranslatorSymbol {
name: string;
Expand Down Expand Up @@ -197,7 +198,7 @@ export type AsmStoreDispatch = Dispatch<{

export function makeAsmStore(
fs: FileSystem,
setStatus: (status: string) => void,
setStatus: Action<string>,
dispatch: MutableRefObject<AsmStoreDispatch>,
) {
const translator = new Translator();
Expand Down
3 changes: 2 additions & 1 deletion components/src/stores/base.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
createAndStoreLocalAdapterInIndexedDB,
removeLocalAdapterFromIndexedDB,
} from "./base/indexDb.js";
import { Action } from "@nand2tetris/simulator/types.js";

export interface BaseContext {
fs: FileSystem;
Expand All @@ -27,7 +28,7 @@ export interface BaseContext {
closeFs: () => void;
upgraded?: string;
status: string;
setStatus: (status: string) => void;
setStatus: Action<string>;
storage: Record<string, string>;
}

Expand Down
3 changes: 2 additions & 1 deletion components/src/stores/chip.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { assert } from "@davidsouther/jiffies/lib/esm/assert.js";
import { RunSpeed } from "src/runbar.js";
import { compare } from "../compare.js";
import { BaseContext } from "./base.context.js";
import { Action } from "@nand2tetris/simulator/types.js";

export const NO_SCREEN = "noScreen";

Expand Down Expand Up @@ -184,7 +185,7 @@ export type ChipStoreDispatch = Dispatch<{

export function makeChipStore(
fs: FileSystem,
setStatus: (status: string) => void,
setStatus: Action<string>,
storage: Record<string, string>,
dispatch: MutableRefObject<ChipStoreDispatch>,
) {
Expand Down
3 changes: 2 additions & 1 deletion components/src/stores/compiler.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CompilationError } from "@nand2tetris/simulator/languages/base.js";
import { Dispatch, MutableRefObject, useContext, useMemo, useRef } from "react";
import { useImmerReducer } from "../react.js";
import { BaseContext } from "./base.context.js";
import { Action } from "@nand2tetris/simulator/types.js";

export interface CompiledFile {
vm?: string;
Expand Down Expand Up @@ -31,7 +32,7 @@ function classTemplate(name: string) {
}

export function makeCompilerStore(
setStatus: (status: string) => void,
setStatus: Action<string>,
dispatch: MutableRefObject<CompilerStoreDispatch>,
) {
let fs: FileSystem | undefined;
Expand Down
3 changes: 2 additions & 1 deletion components/src/stores/cpu.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { loadTestFiles } from "../file_utils.js";
import { useImmerReducer } from "../react.js";
import { BaseContext } from "./base.context.js";
import { ImmMemory } from "./imm_memory.js";
import { Action } from "@nand2tetris/simulator/types.js";

function makeTst() {
return `repeat {
Expand Down Expand Up @@ -93,7 +94,7 @@ export type CpuStoreDispatch = Dispatch<{

export function makeCpuStore(
fs: FileSystem,
setStatus: (status: string) => void,
setStatus: Action<string>,
storage: Record<string, string>,
dispatch: MutableRefObject<CpuStoreDispatch>,
) {
Expand Down
5 changes: 3 additions & 2 deletions components/src/stores/vm.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { TST } from "@nand2tetris/simulator/languages/tst.js";
import { VM, VmInstruction } from "@nand2tetris/simulator/languages/vm.js";
import { VMTest, VmFile } from "@nand2tetris/simulator/test/vmtst.js";
import { Action } from "@nand2tetris/simulator/types.js";
import { Vm, VmFrame } from "@nand2tetris/simulator/vm/vm.js";
import { Dispatch, MutableRefObject, useContext, useMemo, useRef } from "react";
import { ScreenScales } from "../chips/screen.js";
Expand Down Expand Up @@ -88,7 +89,7 @@ export type VmStoreDispatch = Dispatch<{
function reduceVMTest(
vmTest: VMTest,
dispatch: MutableRefObject<VmStoreDispatch>,
setStatus: (status: string) => void,
setStatus: Action<string>,
showHighlight: boolean,
): VmSim {
const RAM = new ImmMemory(vmTest.vm.RAM, dispatch);
Expand Down Expand Up @@ -121,7 +122,7 @@ function reduceVMTest(

export function makeVmStore(
fs: FileSystem,
setStatus: (status: string) => void,
setStatus: Action<string>,
storage: Record<string, string>,
dispatch: MutableRefObject<VmStoreDispatch>,
) {
Expand Down
3 changes: 2 additions & 1 deletion simulator/src/test/chiptst.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bus, Chip, HIGH, Low, LOW } from "../chip/chip.js";
import { Clock } from "../chip/clock.js";
import { Tst } from "../languages/tst.js";
import { Action } from "../types.js";
import { fill } from "./builder.js";
import { TestInstruction } from "./instruction.js";
import { Test } from "./tst.js";
Expand All @@ -13,7 +14,7 @@ export class ChipTest extends Test<ChipTestInstruction> {

private clock = Clock.get();

static from(tst: Tst, setStatus?: (status: string) => void): ChipTest {
static from(tst: Tst, setStatus?: Action<string>): ChipTest {
const test = new ChipTest(setStatus);
return fill(test, tst);
}
Expand Down
5 changes: 3 additions & 2 deletions simulator/src/test/cputst.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CPU } from "../cpu/cpu.js";
import { ROM } from "../cpu/memory.js";
import { Tst } from "../languages/tst.js";
import { Action } from "../types.js";
import { fill } from "./builder.js";
import { TestInstruction } from "./instruction.js";
import { Test } from "./tst.js";
Expand All @@ -9,12 +10,12 @@ export class CPUTest extends Test<CPUTestInstruction> {
readonly cpu: CPU;
private ticks = 0;

static from(tst: Tst, rom?: ROM, doEcho?: (status: string) => void): CPUTest {
static from(tst: Tst, rom?: ROM, doEcho?: Action<string>): CPUTest {
const test = new CPUTest(rom, doEcho);
return fill(test, tst);
}

constructor(rom: ROM = new ROM(), doEcho?: (status: string) => void) {
constructor(rom: ROM = new ROM(), doEcho?: Action<string>) {
super(doEcho);
this.cpu = new CPU({ ROM: rom });
this.reset();
Expand Down
5 changes: 3 additions & 2 deletions simulator/src/test/tst.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertExists } from "@davidsouther/jiffies/lib/esm/assert.js";
import { FileSystem } from "@davidsouther/jiffies/lib/esm/fs.js";
import { Output } from "../output.js";
import { Action } from "../types.js";
import {
OutputParams,
TestBreakInstruction,
Expand All @@ -15,9 +16,9 @@ export abstract class Test<IS extends TestInstruction = TestInstruction> {
protected _outputList: Output[] = [];
protected _log = "";
fs: FileSystem = new FileSystem();
protected doEcho?: (status: string) => void;
protected doEcho?: Action<string>;

constructor(doEcho?: (status: string) => void) {
constructor(doEcho?: Action<string>) {
this.doEcho = doEcho;
}

Expand Down
3 changes: 2 additions & 1 deletion simulator/src/test/vmtst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Vm } from "../vm/vm.js";
import { fill } from "./builder.js";
import { TestInstruction } from "./instruction.js";
import { Test } from "./tst.js";
import { Action } from "../types.js";

export interface VmFile {
name: string;
Expand All @@ -22,7 +23,7 @@ export class VMTest extends Test<VMTestInstruction> {
tst: Tst,
path?: string,
loadAction?: (files: VmFile[]) => void,
doEcho?: (status: string) => void,
doEcho?: Action<string>,
): VMTest {
const test = new VMTest(doEcho);
test.dir = path?.split("/").slice(0, -1).join("/");
Expand Down
1 change: 1 addition & 0 deletions simulator/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Action<T> = (value: T) => void;
3 changes: 2 additions & 1 deletion web/src/pico/inline_edit.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { width } from "@davidsouther/jiffies/lib/esm/dom/css/sizing";
import { useCallback, useState } from "react";
import { useStateInitializer } from "@nand2tetris/components/react.js";
import { Action } from "@nand2tetris/simulator/types";

const Mode = { VIEW: 0, EDIT: 1 };

export const InlineEdit = (props: {
mode?: keyof typeof Mode;
value: string;
onChange: (value: string) => void;
onChange: Action<string>;
}) => {
const [mode, setMode] = useState(props.mode ?? Mode.VIEW);
const [value, setValue] = useStateInitializer(props.value);
Expand Down
3 changes: 2 additions & 1 deletion web/src/shell/Monaco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as monacoT from "monaco-editor/esm/vs/editor/editor.api";
import { useCallback, useContext, useEffect, useRef, useState } from "react";
import { AppContext } from "../App.context";
import { Decoration, HighlightType } from "./editor";
import { Action } from "@nand2tetris/simulator/types";

const isRangeVisible = (
editor: monacoT.editor.IStandaloneCodeEditor | undefined,
Expand Down Expand Up @@ -79,7 +80,7 @@ export const Monaco = ({
lineNumberTransform,
}: {
value: string;
onChange: (value: string) => void;
onChange: Action<string>;
onCursorPositionChange?: (index: number) => void;
language: string;
error?: CompilationError;
Expand Down
5 changes: 3 additions & 2 deletions web/src/shell/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Span,
} from "@nand2tetris/simulator/languages/base.js";

import { Action } from "@nand2tetris/simulator/types";
import "./editor.scss";

const Monaco = lazy(() => import("./Monaco"));
Expand All @@ -34,7 +35,7 @@ const Textarea = ({
disabled = false,
}: {
value: string;
onChange: (value: string) => void;
onChange: Action<string>;
language: string;
disabled?: boolean;
}) => {
Expand Down Expand Up @@ -82,7 +83,7 @@ export const Editor = ({
disabled?: boolean;
value: string;
error?: CompilationError;
onChange: (source: string) => void;
onChange: Action<string>;
onCursorPositionChange?: (index: number) => void;
grammar?: Grammar;
language: string;
Expand Down

0 comments on commit c5c42cf

Please sign in to comment.