From 96a040cf573003150b0bca3bd7f628e489e2674a Mon Sep 17 00:00:00 2001 From: Igor Loskutov Date: Mon, 8 Jan 2024 23:11:20 +0700 Subject: [PATCH] lint --- adapters/src/index.ts | 5 +- adapters/src/lib/statefulSimulation.ts | 7 ++- fsm/src/lib/fsm.ts | 1 - react/src/lib/react.spec.tsx | 19 +++----- react/src/lib/react.tsx | 67 ++++++++++++++++---------- utils/src/lib/utils.ts | 9 ++-- 6 files changed, 61 insertions(+), 47 deletions(-) diff --git a/adapters/src/index.ts b/adapters/src/index.ts index b0559dc..edf9348 100644 --- a/adapters/src/index.ts +++ b/adapters/src/index.ts @@ -1 +1,4 @@ -export { StatefulSimulation, StatefulSimulationOpts } from './lib/statefulSimulation'; +export { + StatefulSimulation, + StatefulSimulationOpts, +} from './lib/statefulSimulation'; diff --git a/adapters/src/lib/statefulSimulation.ts b/adapters/src/lib/statefulSimulation.ts index a18ae07..54a6190 100644 --- a/adapters/src/lib/statefulSimulation.ts +++ b/adapters/src/lib/statefulSimulation.ts @@ -74,7 +74,10 @@ export class StatefulSimulation { withCurrent: true, }); } - #changeListeners = new Map | null) => void>(); + #changeListeners = new Map< + number, + (next: QueueItem | null) => void + >(); #nextListenerId = 1; onChange = ( @@ -143,4 +146,4 @@ export class StatefulSimulation { current = () => { return current(this.#state); }; -}; +} diff --git a/fsm/src/lib/fsm.ts b/fsm/src/lib/fsm.ts index f41ea04..93a3e18 100644 --- a/fsm/src/lib/fsm.ts +++ b/fsm/src/lib/fsm.ts @@ -1,5 +1,4 @@ import { assertRNEA, assertTrue, isRNEA, last, lastNEA } from '@jikan0/utils'; -import { K } from 'vitest/dist/reporters-5f784f42'; let duration0QueueItemErrorShown = false; const printDuration0QueueItemError = (queueItem: QueueItem) => { diff --git a/react/src/lib/react.spec.tsx b/react/src/lib/react.spec.tsx index b126b47..ffcbc91 100644 --- a/react/src/lib/react.spec.tsx +++ b/react/src/lib/react.spec.tsx @@ -1,15 +1,13 @@ -jest.useFakeTimers(); - import { act, renderHook } from '@testing-library/react'; import { useTimer } from './react'; +jest.useFakeTimers(); + describe('useTimer', () => { it('has predictable initial state', () => { const step1 = { kind: 'a', duration: 1 }; - const program = [ - step1, - ]; + const program = [step1]; const { result } = renderHook(() => useTimer(program)); // TODO questionable; we may want null here expect(result.current.current).toMatchObject(step1); @@ -17,9 +15,7 @@ describe('useTimer', () => { }); it('has predictable final state', () => { const step1 = { kind: 'a', duration: 1 }; - const program = [ - step1, - ]; + const program = [step1]; const { result } = renderHook(() => useTimer(program)); result.current.start(); act(() => jest.runOnlyPendingTimers()); @@ -29,13 +25,10 @@ describe('useTimer', () => { it('has predictable intermediate state', () => { const step1 = { kind: 'a', duration: 1000 }; const step2 = { kind: 'b', duration: 1000 }; - const program = [ - step1, - step2, - ]; + const program = [step1, step2]; const { result } = renderHook(() => useTimer(program)); result.current.start(); - act(() => jest.advanceTimersByTime(1000)/*runs step1*/); + act(() => jest.advanceTimersByTime(1000) /*runs step1*/); expect(result.current.current).toMatchObject(step2); expect(result.current.running).toBe(true); }); diff --git a/react/src/lib/react.tsx b/react/src/lib/react.tsx index a855963..d67a746 100644 --- a/react/src/lib/react.tsx +++ b/react/src/lib/react.tsx @@ -6,35 +6,50 @@ import { useEffect, useMemo, useRef, useState } from 'react'; const hashProgram = (program: Program) => { const allKinds = [...new Set(program.map(({ kind }) => kind))]; const kindsHash = stringHashCode(allKinds.join(':')); - const kindIndices = Object.fromEntries(allKinds.map((kind, i) => [kind, i] as const)); - return program.reduce((acc, { duration, kind }) => acc +/*not sure if "+" is good here, but probably good enough*/ ((duration + 1) * allKinds.length) + kindIndices[kind], kindsHash); + const kindIndices = Object.fromEntries( + allKinds.map((kind, i) => [kind, i] as const) + ); + return program.reduce( + (acc, { duration, kind }) => + acc + + /*not sure if "+" is good here, but probably good enough*/ (duration + + 1) * + allKinds.length + + kindIndices[kind], + kindsHash + ); }; -export const makeUseTimer = (opts?: StatefulSimulationOpts) => (program: Program) => { - const programHash = useMemo(() => hashProgram(program), [program/*assume they don't mutate*/]); - const ref = useRef>(); - if (!ref.current) { - ref.current = new StatefulSimulation([], opts); - } - const sim = assertExists(ref.current); - // cleanup on unmount - useEffect(() => () => sim.stop(), []); - const [queueItem, setQueueItem] = useState | null>(null); - // watch until unmount - useEffect(() => sim.onChange(setQueueItem), []); - // restart on program change - useEffect(() => { +export const makeUseTimer = + (opts?: StatefulSimulationOpts) => + (program: Program) => { + const programHash = useMemo( + () => hashProgram(program), + [program /*assume they don't mutate*/] + ); + const ref = useRef>(); + if (!ref.current) { + ref.current = new StatefulSimulation([], opts); + } const sim = assertExists(ref.current); - sim.reset(); - sim.push(program); - }, [programHash]); - const start = useMemo(() => sim.start/*to keep it bound*/, []); - return { - current: queueItem, - running: sim.isRunning(), - start - } -} + // cleanup on unmount + useEffect(() => () => sim.stop(), []); + const [queueItem, setQueueItem] = useState | null>(null); + // watch until unmount + useEffect(() => sim.onChange(setQueueItem), []); + // restart on program change + useEffect(() => { + const sim = assertExists(ref.current); + sim.reset(); + sim.push(program); + }, [programHash]); + const start = useMemo(() => sim.start /*to keep it bound*/, []); + return { + current: queueItem, + running: sim.isRunning(), + start, + }; + }; const useTimerDefault = makeUseTimer(); diff --git a/utils/src/lib/utils.ts b/utils/src/lib/utils.ts index bd246e8..6fe20a5 100644 --- a/utils/src/lib/utils.ts +++ b/utils/src/lib/utils.ts @@ -22,15 +22,16 @@ export const assertExists = (x: T | null | undefined): T => { }; const cyrb53 = (str: string, seed = 0) => { - let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed; - for(let i = 0, ch; i < str.length; i++) { + let h1 = 0xdeadbeef ^ seed, + h2 = 0x41c6ce57 ^ seed; + for (let i = 0, ch; i < str.length; i++) { ch = str.charCodeAt(i); h1 = Math.imul(h1 ^ ch, 2654435761); h2 = Math.imul(h2 ^ ch, 1597334677); } - h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507); + h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507); h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909); - h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507); + h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507); h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909); return 4294967296 * (2097151 & h2) + (h1 >>> 0);