utils/ts_core/src/clock.ts
Krzysztof kuhy Rudnicki c5b3fceeae ts_core 0.1.1, web_ui 0.4.0: consume @kuhyx/ts-config
The two shared packages are the first repos on the preset (link:../ts_config
inside the monorepo; consumers outside use the git tag). Each eslint.config.js
is now the defineConfig() call plus one override with its reason: a public
API name (`nextChance`, `fuzzyMatch`) the boolean-prefix rule would rename,
which is a breaking change for the three repos importing it.

Everything else the preset flagged is fixed in code, not disabled:
key order (perfectionist), hex casing, sort compare functions in tests,
Iterator#map().toArray(), explicit returns on multiline arrows.

web_ui: react-refresh/only-export-components was a real finding --
range-slider.tsx exported `steppedValue` next to the component, which breaks
Vite HMR for the module. The keyboard contract moves to stepped-value.ts as
`steppedValue` + a new pure `keyTarget(key, values, current)`, which also
resolves unicorn/no-late-event-control: the handler now decides the target
first and calls preventDefault() only when it owns the key. Its tests move
with it (the 250-line gate caught the combined file). Minor bump for the new
export. dist/ rebuilt in both packages.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DnAYyNH1Sm4iJNidfYbrZJ
2026-09-16 21:46:41 +02:00

49 lines
1.6 KiB
TypeScript

/**
* Injectable time source.
*
* All elapsed-time reads flow through this interface instead of calling
* `performance.now()`/`Date.now()` directly (banned by lint everywhere except
* this file), so real-time mechanics can be driven by a scripted clock in
* tests rather than racing the wall clock.
*/
export interface Clock {
/** Current time in milliseconds. Not comparable across different clocks. */
now: () => number
}
/** A {@link Clock} backed by the browser's monotonic performance timer. */
export const createRealClock = (): Clock => ({ now: (): number => performance.now() })
/**
* The one real clock instance UI code should pass around.
*
* A single shared instance (rather than calling {@link createRealClock} per
* component) keeps its identity stable across renders, which matters for
* hooks that take it as an effect dependency -- a fresh object each render
* re-fires the effect every time.
*/
export const realClock: Clock = createRealClock()
/** A {@link Clock} test double whose time only moves when told to. */
export interface ManualClock extends Clock {
/** Moves the clock forward by the given number of milliseconds. */
advance: (deltaMs: number) => void
/** Jumps the clock to an absolute time in milliseconds. */
set: (timeMs: number) => void
}
/** Creates a {@link ManualClock} starting at `startMs` (default 0). */
export const createManualClock = (startMs = 0): ManualClock => {
let current = startMs
return {
advance: (deltaMs: number): void => {
current += deltaMs
},
now: (): number => current,
set: (timeMs: number): void => {
current = timeMs
},
}
}