Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-experiment: improve run typing #211

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-bees-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lightmill/react-experiment': patch
---

Run's elements prop must now support every tasks in timeline, even when no task types have been provided or registered
11 changes: 6 additions & 5 deletions packages/react-experiment/src/run.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { ReadonlyDeep } from 'type-fest';
import { RegisteredTask, Typed, RegisteredLog } from './config.js';
import { loggerContext, noLoggerSymbol, timelineContext } from './contexts.js';
import useManagedTimeline, {
Expand All @@ -15,13 +16,13 @@ export type RunElements<T extends Typed> = {
export type Logger<Log> = (log: Log) => Promise<void>;

export type RunProps<Task extends Typed, Log> = {
elements: RunElements<Task>;
elements: Readonly<RunElements<Task>>;
confirmBeforeUnload?: boolean;
} & UseRunParameter<Task, Log>;

// This component uses explicit return type to prevent the function from
// returning undefined, which could indicate a state isn't being handled.
export function Run<T extends RegisteredTask>({
export function Run<const T extends RegisteredTask>({
elements,
confirmBeforeUnload = true,
...useRunParameter
Expand Down Expand Up @@ -74,13 +75,13 @@ type UseRunParameter<Task extends { type: string }, Log> = {
onLog?: Logger<Log>;
resumeAfter?: { type: Task['type']; number: number };
} & (
| { timeline: Timeline<Task>; loading?: boolean }
| { timeline?: Timeline<Task> | null; loading: true }
| { timeline: ReadonlyDeep<Timeline<Task>>; loading?: boolean }
| { timeline?: ReadonlyDeep<Timeline<Task>> | null; loading: true }
);
type RunState<Task, Log> = Exclude<TimelineState<Task>, { status: 'error' }> & {
onLog: ((newLog: Log) => void) | null;
};
function useRun<T extends { type: string }, L>({
function useRun<const T extends { type: string }, L>({
onCompleted,
timeline,
resumeAfter,
Expand Down