Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dearlordylord committed Jan 8, 2024
1 parent d7c7eb5 commit 96a040c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 47 deletions.
5 changes: 4 additions & 1 deletion adapters/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { StatefulSimulation, StatefulSimulationOpts } from './lib/statefulSimulation';
export {
StatefulSimulation,
StatefulSimulationOpts,
} from './lib/statefulSimulation';
7 changes: 5 additions & 2 deletions adapters/src/lib/statefulSimulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export class StatefulSimulation<QueueItemType extends string = string> {
withCurrent: true,
});
}
#changeListeners = new Map<number, (next: QueueItem<QueueItemType> | null) => void>();
#changeListeners = new Map<
number,
(next: QueueItem<QueueItemType> | null) => void
>();
#nextListenerId = 1;

onChange = (
Expand Down Expand Up @@ -143,4 +146,4 @@ export class StatefulSimulation<QueueItemType extends string = string> {
current = () => {
return current(this.#state);
};
};
}
1 change: 0 additions & 1 deletion fsm/src/lib/fsm.ts
Original file line number Diff line number Diff line change
@@ -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<string>) => {
Expand Down
19 changes: 6 additions & 13 deletions react/src/lib/react.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
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);
expect(result.current.running).toBe(false);
});
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());
Expand All @@ -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);
});
Expand Down
67 changes: 41 additions & 26 deletions react/src/lib/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => <Kind extends string = string>(program: Program<Kind>) => {
const programHash = useMemo(() => hashProgram(program), [program/*assume they don't mutate*/]);
const ref = useRef<StatefulSimulation<Kind>>();
if (!ref.current) {
ref.current = new StatefulSimulation([], opts);
}
const sim = assertExists(ref.current);
// cleanup on unmount
useEffect(() => () => sim.stop(), []);
const [queueItem, setQueueItem] = useState<QueueItem<Kind> | null>(null);
// watch until unmount
useEffect(() => sim.onChange(setQueueItem), []);
// restart on program change
useEffect(() => {
export const makeUseTimer =
(opts?: StatefulSimulationOpts) =>
<Kind extends string = string>(program: Program<Kind>) => {
const programHash = useMemo(
() => hashProgram(program),
[program /*assume they don't mutate*/]
);
const ref = useRef<StatefulSimulation<Kind>>();
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<QueueItem<Kind> | 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();

Expand Down
9 changes: 5 additions & 4 deletions utils/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ export const assertExists = <T>(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);
Expand Down

0 comments on commit 96a040c

Please sign in to comment.