|
1 | | -import type { TaskView, TaskStepView } from '../resources/tasks'; |
2 | | -import { type DeepMutable, ExhaustiveSwitchCheck } from './types'; |
3 | | - |
4 | | -export type ReducerEvent = TaskView | null; |
5 | | - |
6 | | -export type BrowserState = Readonly<{ |
7 | | - taskId: string; |
8 | | - sessionId: string; |
9 | | - |
10 | | - liveUrl: string | null; |
11 | | - steps: ReadonlyArray<TaskStepView>; |
12 | | - doneOutput: string | null; |
13 | | -}> | null; |
14 | | - |
15 | | -type BrowserAction = { |
16 | | - kind: 'status'; |
17 | | - status: TaskView; |
18 | | -}; |
19 | | - |
20 | | -export function reducer(state: BrowserState, action: BrowserAction): [BrowserState, ReducerEvent] { |
21 | | - switch (action.kind) { |
22 | | - case 'status': { |
23 | | - // INIT |
24 | | - |
25 | | - if (state == null) { |
26 | | - const liveUrl = action.status.session.liveUrl ?? null; |
27 | | - const doneOutput = action.status.doneOutput ?? null; |
28 | | - |
29 | | - const state: BrowserState = { |
30 | | - taskId: action.status.id, |
31 | | - sessionId: action.status.sessionId, |
32 | | - liveUrl: liveUrl, |
33 | | - steps: action.status.steps, |
34 | | - doneOutput: doneOutput, |
35 | | - }; |
36 | | - |
37 | | - return [state, action.status]; |
38 | | - } |
39 | | - |
40 | | - // UPDATE |
41 | | - |
42 | | - const liveUrl = action.status.session.liveUrl ?? state.liveUrl; |
43 | | - const doneOutput = action.status.doneOutput ?? state.doneOutput; |
44 | | - |
45 | | - const steps: TaskStepView[] = [...state.steps]; |
46 | | - if (action.status.steps != null) { |
47 | | - const newSteps = action.status.steps.slice(state.steps.length); |
48 | | - |
49 | | - for (const step of newSteps) { |
50 | | - steps.push(step); |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - const newState: DeepMutable<BrowserState> = { |
55 | | - ...state, |
56 | | - liveUrl, |
57 | | - steps, |
58 | | - doneOutput, |
59 | | - }; |
60 | | - |
61 | | - // CHANGES |
62 | | - |
63 | | - if ( |
64 | | - (state.liveUrl == null && liveUrl != null) || |
65 | | - state.steps.length !== steps.length || |
66 | | - state.doneOutput != doneOutput |
67 | | - ) { |
68 | | - const update: ReducerEvent = { |
69 | | - ...action.status, |
70 | | - steps: newState.steps, |
71 | | - session: { |
72 | | - ...action.status.session, |
73 | | - liveUrl: newState.liveUrl, |
74 | | - }, |
75 | | - doneOutput: newState.doneOutput, |
76 | | - }; |
77 | | - |
78 | | - return [newState, update]; |
79 | | - } |
80 | | - |
81 | | - return [newState, null]; |
82 | | - } |
83 | | - default: |
84 | | - throw new ExhaustiveSwitchCheck(action.kind); |
85 | | - } |
| 1 | +import { createHash } from 'crypto'; |
| 2 | +import stringify from 'fast-json-stable-stringify'; |
| 3 | + |
| 4 | +import type { TaskView } from '../resources'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Hashes the task view to detect changes. |
| 8 | + * Uses fast-json-stable-stringify for deterministic JSON, then SHA-256. |
| 9 | + */ |
| 10 | +export function getTaskViewHash(view: TaskView): string { |
| 11 | + const dump = stringify(view); |
| 12 | + const hash = createHash('sha256').update(dump).digest('hex'); |
| 13 | + return hash; |
86 | 14 | } |
0 commit comments