Skip to content
Merged
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
29 changes: 21 additions & 8 deletions src/lib/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TaskView, TaskStepView } from '../resources/tasks';
import { ExhaustiveSwitchCheck } from './types';
import { type DeepMutable, ExhaustiveSwitchCheck } from './types';

export type ReducerEvent = TaskView | null;

Expand All @@ -8,8 +8,8 @@ export type BrowserState = Readonly<{
sessionId: string;

liveUrl: string | null;

steps: ReadonlyArray<TaskStepView>;
doneOutput: string | null;
}> | null;

type BrowserAction = {
Expand All @@ -24,22 +24,25 @@ export function reducer(state: BrowserState, action: BrowserAction): [BrowserSta

if (state == null) {
const liveUrl = action.status.sessionLiveUrl ?? null;
const doneOutput = action.status.doneOutput ?? null;

const state: BrowserState = {
taskId: action.status.id,
sessionId: action.status.sessionId,
liveUrl: liveUrl,
steps: action.status.steps,
doneOutput: doneOutput,
};

return [state, action.status];
}

// UPDATE

const liveUrl = action.status.sessionLiveUrl ?? null;
const steps: TaskStepView[] = [...state.steps];
const liveUrl = action.status.sessionLiveUrl ?? state.liveUrl;
const doneOutput = action.status.doneOutput ?? state.doneOutput;

const steps: TaskStepView[] = [...state.steps];
if (action.status.steps != null) {
const newSteps = action.status.steps.slice(state.steps.length);

Expand All @@ -48,15 +51,25 @@ export function reducer(state: BrowserState, action: BrowserAction): [BrowserSta
}
}

const newState: BrowserState = { ...state, liveUrl, steps };
const newState: DeepMutable<BrowserState> = {
...state,
liveUrl,
steps,
doneOutput,
};

// CHANGES

if ((state.liveUrl == null && liveUrl != null) || state.steps.length !== steps.length) {
if (
(state.liveUrl == null && liveUrl != null) ||
state.steps.length !== steps.length ||
state.doneOutput != doneOutput
) {
const update: ReducerEvent = {
...action.status,
steps: steps,
sessionLiveUrl: liveUrl,
steps: newState.steps,
sessionLiveUrl: newState.liveUrl,
doneOutput: newState.doneOutput,
};

return [newState, update];
Expand Down