@zyro-sec/session-ui 0.0.0-stage → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +25 -0
- package/bunfig.toml +2 -0
- package/component-tests/basic-tool.fixture.tsx +46 -0
- package/component-tests/basic-tool.spec.ts +63 -0
- package/component-tests/file-media.fixture.tsx +39 -0
- package/component-tests/file-media.spec.ts +13 -0
- package/component-tests/file-tool.spec.ts +62 -0
- package/component-tests/file-viewer.fixture.tsx +109 -0
- package/component-tests/file-viewer.spec.ts +134 -0
- package/component-tests/file.spec.ts +33 -0
- package/component-tests/line-comment-v2.spec.ts +15 -0
- package/component-tests/markdown-code-wrap.spec.ts +94 -0
- package/component-tests/markdown-readiness.spec.ts +95 -0
- package/component-tests/markdown.fixture.tsx +104 -0
- package/component-tests/markdown.spec.ts +1564 -0
- package/component-tests/mermaid-preview.spec.ts +43 -0
- package/component-tests/patch-group.spec.ts +79 -0
- package/component-tests/read-group.spec.ts +72 -0
- package/component-tests/read-image.fixture.tsx +66 -0
- package/component-tests/read-image.spec.ts +154 -0
- package/component-tests/session-compaction.spec.ts +254 -0
- package/component-tests/session-lifecycle.spec.ts +190 -0
- package/component-tests/session-message-projection.spec.ts +639 -0
- package/component-tests/session-notice-projection.spec.ts +20 -0
- package/component-tests/session-review-comments.spec.ts +179 -0
- package/component-tests/session-shell.spec.ts +93 -0
- package/component-tests/session-timeline-mobile.spec.ts +201 -0
- package/component-tests/session-timeline-notices.spec.ts +38 -0
- package/component-tests/session-timeline-reasoning.spec.ts +107 -0
- package/component-tests/session-timeline.spec.ts +157 -0
- package/component-tests/session-tool-projection.spec.ts +323 -0
- package/component-tests/text-lifecycle.fixture.tsx +26 -0
- package/component-tests/text-lifecycle.spec.ts +42 -0
- package/component-tests/timeline-subagent-rows.spec.ts +33 -0
- package/component-tests/tool-disclosure.spec.ts +48 -0
- package/component-tests/tool-group-sticky.spec.ts +37 -0
- package/component-tests/tool-group.spec.ts +290 -0
- package/component-tests/tool-status-title.fixture.tsx +22 -0
- package/component-tests/tool-status-title.spec.ts +30 -0
- package/component-tests/user-message-quotes.spec.ts +111 -0
- package/package.json +91 -4
- package/playwright.components.config.ts +4 -0
- package/playwright.local.config.ts +2 -0
- package/src/actions.ts +54 -0
- package/src/components/apply-patch-file.ts +122 -0
- package/src/components/basic-tool.css +487 -0
- package/src/components/basic-tool.tsx +258 -0
- package/src/components/copy-machine.ts +78 -0
- package/src/components/copy.ts +85 -0
- package/src/components/diagram-preview.tsx +28 -0
- package/src/components/disclosure-machine.ts +188 -0
- package/src/components/disclosure.ts +129 -0
- package/src/components/dock-prompt.stories.tsx +73 -0
- package/src/components/dock-prompt.tsx +29 -0
- package/src/components/file-comment-selection.tsx +146 -0
- package/src/components/file-media-machine.ts +165 -0
- package/src/components/file-media.stories.tsx +112 -0
- package/src/components/file-media.tsx +323 -0
- package/src/components/file-search.tsx +73 -0
- package/src/components/file.css +59 -0
- package/src/components/file.tsx +1300 -0
- package/src/components/image-preview.tsx +35 -0
- package/src/components/line-comment-annotations.tsx +580 -0
- package/src/components/line-comment-machine.ts +262 -0
- package/src/components/line-comment-styles.ts +293 -0
- package/src/components/line-comment.stories.tsx +152 -0
- package/src/components/line-comment.tsx +448 -0
- package/src/components/link-segments.ts +20 -0
- package/src/components/linked-text.tsx +21 -0
- package/src/components/markdown-cache-machine.ts +257 -0
- package/src/components/markdown-cache.tsx +556 -0
- package/src/components/markdown-clipboard.ts +101 -0
- package/src/components/markdown-code-machine.ts +200 -0
- package/src/components/markdown-code-state.ts +22 -0
- package/src/components/markdown-fragment-links.ts +74 -0
- package/src/components/markdown-github.svg +3 -0
- package/src/components/markdown-globe.svg +5 -0
- package/src/components/markdown-image-machine.ts +170 -0
- package/src/components/markdown-image.ts +156 -0
- package/src/components/markdown-inline-code-kind.ts +39 -0
- package/src/components/markdown-mermaid.ts +18 -0
- package/src/components/markdown-phase-machine.ts +102 -0
- package/src/components/markdown-projection.ts +13 -0
- package/src/components/markdown-session-links.ts +183 -0
- package/src/components/markdown-solid.test.tsx +92 -0
- package/src/components/markdown-solid.tsx +113 -0
- package/src/components/markdown-stream.ts +95 -0
- package/src/components/markdown-worker-machine.ts +516 -0
- package/src/components/markdown-worker-protocol.ts +73 -0
- package/src/components/markdown-worker.ts +250 -0
- package/src/components/markdown.css +672 -0
- package/src/components/markdown.stories.tsx +199 -0
- package/src/components/markdown.tsx +1612 -0
- package/src/components/markdown.worker.ts +188 -0
- package/src/components/message-file.ts +102 -0
- package/src/components/message-nav.css +134 -0
- package/src/components/message-nav.tsx +159 -0
- package/src/components/message-part.css +2109 -0
- package/src/components/message-part.tsx +3 -0
- package/src/components/question-prompt-experimental.css +576 -0
- package/src/components/question-prompt.css +527 -0
- package/src/components/quote-highlight.css +59 -0
- package/src/components/review-focus-driver.ts +142 -0
- package/src/components/review-focus-machine.ts +95 -0
- package/src/components/session-compaction.css +270 -0
- package/src/components/session-diff.ts +358 -0
- package/src/components/session-error.tsx +41 -0
- package/src/components/session-retry.tsx +92 -0
- package/src/components/session-review-machine.ts +241 -0
- package/src/components/session-review.css +264 -0
- package/src/components/session-review.stories.tsx +232 -0
- package/src/components/session-review.tsx +733 -0
- package/src/components/session-turn.css +282 -0
- package/src/components/shell-submessage.css +24 -0
- package/src/components/timeline-separator.tsx +69 -0
- package/src/components/tool-count-label.css +57 -0
- package/src/components/tool-count-label.tsx +76 -0
- package/src/components/tool-count-summary.css +102 -0
- package/src/components/tool-count-summary.stories.tsx +99 -0
- package/src/components/tool-count-summary.tsx +44 -0
- package/src/components/tool-error-card.css +178 -0
- package/src/components/tool-error-card.stories.tsx +113 -0
- package/src/components/tool-error-card.tsx +202 -0
- package/src/components/tool-status-title-machine.ts +128 -0
- package/src/components/tool-status-title.css +89 -0
- package/src/components/tool-status-title.tsx +163 -0
- package/src/context/data.tsx +107 -0
- package/src/context/index.ts +1 -0
- package/src/context/markdown.tsx +129 -0
- package/src/context/sources.tsx +34 -0
- package/src/document.ts +8 -0
- package/src/file-presentation.ts +34 -0
- package/src/message/action-machine.ts +72 -0
- package/src/message/agent-label.ts +3 -0
- package/src/message/attachment-card.css +119 -0
- package/src/message/attachment-card.tsx +40 -0
- package/src/message/attachment-media-machine.ts +213 -0
- package/src/message/comment-card.tsx +155 -0
- package/src/message/compaction-entrance-machine.ts +93 -0
- package/src/message/current-message.tsx +157 -0
- package/src/message/current-tool-state.ts +161 -0
- package/src/message/message-content.tsx +1391 -0
- package/src/message/paced-text-machine.ts +73 -0
- package/src/message/prompt-message.stories.tsx +149 -0
- package/src/message/relative-time.ts +45 -0
- package/src/message/user-message-quote-parts.ts +16 -0
- package/src/message/user-message-quotes.stories.tsx +797 -0
- package/src/message/user-message-quotes.tsx +135 -0
- package/src/pierre/comment-hover-machine.ts +53 -0
- package/src/pierre/comment-hover.ts +131 -0
- package/src/pierre/comment-selection-machine.ts +257 -0
- package/src/pierre/commented-lines.ts +106 -0
- package/src/pierre/diff-selection.ts +91 -0
- package/src/pierre/file-find-machine.ts +323 -0
- package/src/pierre/file-find.ts +537 -0
- package/src/pierre/file-runtime.ts +42 -0
- package/src/pierre/file-selection.ts +140 -0
- package/src/pierre/index.ts +344 -0
- package/src/pierre/media.ts +140 -0
- package/src/pierre/render-machine.ts +381 -0
- package/src/pierre/selection-bridge.ts +82 -0
- package/src/pierre/selection-machine.ts +198 -0
- package/src/pierre/virtualizer.ts +114 -0
- package/src/pierre/worker.ts +46 -0
- package/src/sources/experiment.ts +59 -0
- package/src/sources/site-icon.tsx +53 -0
- package/src/sources/sources-chip.tsx +57 -0
- package/src/sources/sources-expand-machine.ts +101 -0
- package/src/sources/sources-panel.tsx +249 -0
- package/src/sources/sources-title.tsx +21 -0
- package/src/sources/sources.css +266 -0
- package/src/sources/sources.ts +141 -0
- package/src/storybook/current-session-fixtures.ts +1492 -0
- package/src/storybook/current-session-scenarios.ts +76 -0
- package/src/storybook/current-session-story.tsx +154 -0
- package/src/storybook/index.ts +43 -0
- package/src/styles/index.css +19 -0
- package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
- package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
- package/src/timeline/code-blocks.stories.tsx +105 -0
- package/src/timeline/commentary.stories.tsx +70 -0
- package/src/timeline/commentary.ts +38 -0
- package/src/timeline/detail.ts +111 -0
- package/src/timeline/error-card.stories.tsx +134 -0
- package/src/timeline/error-spacing.stories.tsx +71 -0
- package/src/timeline/facts.ts +493 -0
- package/src/timeline/file-changes.stories.tsx +436 -0
- package/src/timeline/group-label-fixtures.ts +97 -0
- package/src/timeline/mermaid.stories.tsx +131 -0
- package/src/timeline/place.ts +280 -0
- package/src/timeline/placement-fixtures.ts +365 -0
- package/src/timeline/placement-reactivity.ts +133 -0
- package/src/timeline/projection.ts +1261 -0
- package/src/timeline/research-and-agents.stories.tsx +461 -0
- package/src/timeline/session-timeline-row.tsx +1675 -0
- package/src/timeline/session-timeline.tsx +68 -0
- package/src/timeline/steer-arrival-machine.ts +134 -0
- package/src/timeline/terminal-work.stories.tsx +464 -0
- package/src/timeline/timeline-row.stories.tsx +1069 -0
- package/src/timeline/timeline-row.ts +238 -0
- package/src/timeline/turn-phase-machine.ts +112 -0
- package/src/timeline/user-message-fold.stories.tsx +194 -0
- package/src/timeline/user-runs.ts +87 -0
- package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
- package/src/timeline-next/compaction.tsx +236 -0
- package/src/timeline-next/errors.ts +101 -0
- package/src/timeline-next/event-row.tsx +168 -0
- package/src/timeline-next/events.ts +103 -0
- package/src/timeline-next/index.ts +10 -0
- package/src/timeline-next/nested-rail.ts +125 -0
- package/src/timeline-next/projection.ts +1028 -0
- package/src/timeline-next/row.tsx +1728 -0
- package/src/timeline-next/rules.ts +89 -0
- package/src/timeline-next/separator.tsx +73 -0
- package/src/timeline-next/spacing.ts +127 -0
- package/src/timeline-next/status-line.tsx +41 -0
- package/src/timeline-next/status.ts +33 -0
- package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
- package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
- package/src/timeline-next/stories/compare.stories.tsx +486 -0
- package/src/timeline-next/stories/playground.stories.tsx +297 -0
- package/src/timeline-next/stories/session-replay.ts +1156 -0
- package/src/timeline-next/stories/session-timeline.tsx +50 -0
- package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
- package/src/timeline-next/timeline-next.css +649 -0
- package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
- package/src/timeline-next/tools/activity-label.ts +166 -0
- package/src/timeline-next/tools/assistant-content.tsx +84 -0
- package/src/timeline-next/tools/context-group.tsx +619 -0
- package/src/timeline-next/tools/error-card.tsx +147 -0
- package/src/timeline-next/tools/explore.tsx +119 -0
- package/src/timeline-next/tools/file-group.tsx +128 -0
- package/src/timeline-next/tools/patch.tsx +174 -0
- package/src/timeline-next/tools/shell.tsx +186 -0
- package/src/timeline-next/tools/thought.tsx +87 -0
- package/src/timeline-next/tools/tool-display.tsx +119 -0
- package/src/tools/ansi.ts +8 -0
- package/src/tools/context-group-label.ts +123 -0
- package/src/tools/file-runs.ts +35 -0
- package/src/tools/project-path.ts +18 -0
- package/src/tools/read-group.stories.tsx +159 -0
- package/src/tools/run-machine.ts +185 -0
- package/src/tools/shell-output-machine.ts +233 -0
- package/src/tools/shell-output.ts +91 -0
- package/src/tools/subagent-row-variants.stories.tsx +129 -0
- package/src/tools/tool-group.stories.tsx +345 -0
- package/src/tools/tool-renderer.tsx +2876 -0
- package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
- package/src/v2/components/session-file-panel-v2.tsx +149 -0
- package/src/v2/components/session-progress-indicator-v2.css +5 -0
- package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
- package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
- package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
- package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
- package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
- package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
- package/src/v2/components/session-review-v2.css +653 -0
- package/src/v2/components/session-review-v2.tsx +422 -0
- package/src/v2/components/sidebar-presence-machine.ts +69 -0
- package/tsconfig.json +20 -0
- package/README.md +0 -3
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import type { Block, Projection } from "./markdown-stream"
|
|
2
|
+
import {
|
|
3
|
+
applyMarkdownWorkerResponse,
|
|
4
|
+
applyProjectionBlocks,
|
|
5
|
+
shouldReleaseMarkdownWorkerState,
|
|
6
|
+
type MarkdownWorkerLaneTarget,
|
|
7
|
+
type MarkdownWorkerRequest,
|
|
8
|
+
type MarkdownWorkerResponse,
|
|
9
|
+
type MarkdownWorkerState,
|
|
10
|
+
} from "./markdown-worker-protocol"
|
|
11
|
+
|
|
12
|
+
/** Highlight streams kept before the least recently updated one without a request in flight is evicted. */
|
|
13
|
+
export const HIGHLIGHT_STREAM_LIMIT = 200
|
|
14
|
+
|
|
15
|
+
/** Cooldown after a worker's first failure; each consecutive failure doubles it, up to the maximum. */
|
|
16
|
+
export const WORKER_RETRY_DELAY_MS = 2_000
|
|
17
|
+
|
|
18
|
+
export const WORKER_RETRY_MAX_DELAY_MS = 60_000
|
|
19
|
+
|
|
20
|
+
/** The worker fails when the oldest request in flight makes no progress across one whole window. */
|
|
21
|
+
export const WORKER_HANG_TIMEOUT_MS = 15_000
|
|
22
|
+
|
|
23
|
+
/** The window until a worker's first reply, which also covers loading its bundle and first grammar over a slow link. */
|
|
24
|
+
export const WORKER_BOOT_TIMEOUT_MS = 60_000
|
|
25
|
+
|
|
26
|
+
/** A request held for one worker attempt. The same object is posted as the worker message. */
|
|
27
|
+
export type WorkerRequest = Exclude<MarkdownWorkerRequest, { type: "dispose" }>
|
|
28
|
+
|
|
29
|
+
/** One key's coalescing lane: `active` is posted to the worker, `queued` is the newest snapshot waiting behind it. */
|
|
30
|
+
export type WorkerLane = { readonly active: number; readonly queued?: number }
|
|
31
|
+
|
|
32
|
+
export type WorkerFailure =
|
|
33
|
+
/** The worker could not start, or is cooling down after a failure. */
|
|
34
|
+
{ readonly kind: "unavailable"; readonly message: string } | { readonly kind: "error"; readonly message: string }
|
|
35
|
+
|
|
36
|
+
export type WorkerRejectReason = { readonly kind: "disposed" } | { readonly kind: "superseded" } | WorkerFailure
|
|
37
|
+
|
|
38
|
+
type Counters = {
|
|
39
|
+
/** Request ids issued across every attempt; the next request takes `issued + 1`. */
|
|
40
|
+
readonly issued: number
|
|
41
|
+
/** Workers spawned so far; the live or last worker is attempt `attempts`. */
|
|
42
|
+
readonly attempts: number
|
|
43
|
+
/** Failures since the last worker reply, which set the retry backoff. */
|
|
44
|
+
readonly failures: number
|
|
45
|
+
/** The highest stream generation dropped so far, so a recreated stream never repeats a rendered generation. */
|
|
46
|
+
readonly generationFloor: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type MarkdownWorkerReady = Counters & {
|
|
50
|
+
readonly status: "ready"
|
|
51
|
+
/** The oldest request in flight when the watchdog was armed; undefined while it is disarmed. */
|
|
52
|
+
readonly watched: number | undefined
|
|
53
|
+
/** Whether this worker has replied yet; until then the watchdog allows for its cold start. */
|
|
54
|
+
readonly replied: boolean
|
|
55
|
+
/** Every unsettled request by id, in issue order. */
|
|
56
|
+
readonly requests: ReadonlyMap<number, WorkerRequest>
|
|
57
|
+
readonly projectLanes: ReadonlyMap<string, WorkerLane>
|
|
58
|
+
/** The blocks of the last projection the worker sent per key, which its next response builds on. */
|
|
59
|
+
readonly projections: ReadonlyMap<string, Block[]>
|
|
60
|
+
readonly highlightLanes: ReadonlyMap<string, WorkerLane>
|
|
61
|
+
/** Accumulated tokens per streamed code block, least recently updated first. */
|
|
62
|
+
readonly highlightStreams: ReadonlyMap<string, MarkdownWorkerState>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* `idle` has no worker and spawns one on the next request. `ready` owns worker `attempts`, every request in flight, and
|
|
67
|
+
* the per-key caches built on its replies. `failed` terminated that worker and rejects requests until its cooldown ends.
|
|
68
|
+
*/
|
|
69
|
+
export type MarkdownWorkerMachineState =
|
|
70
|
+
| (Counters & { readonly status: "idle" })
|
|
71
|
+
| MarkdownWorkerReady
|
|
72
|
+
| (Counters & { readonly status: "failed"; readonly message: string })
|
|
73
|
+
|
|
74
|
+
export type MarkdownWorkerEvent =
|
|
75
|
+
| { readonly type: "parse.requested"; readonly text: string }
|
|
76
|
+
| { readonly type: "parse.aborted"; readonly id: number }
|
|
77
|
+
| { readonly type: "project.requested"; readonly key: string; readonly text: string; readonly live: boolean }
|
|
78
|
+
| { readonly type: "project.disposed"; readonly key: string }
|
|
79
|
+
| {
|
|
80
|
+
readonly type: "highlight.requested"
|
|
81
|
+
readonly key: string
|
|
82
|
+
readonly text: string
|
|
83
|
+
readonly language: string
|
|
84
|
+
readonly complete: boolean
|
|
85
|
+
}
|
|
86
|
+
| { readonly type: "highlight.disposed"; readonly key: string }
|
|
87
|
+
| { readonly type: "worker.message"; readonly attempt: number; readonly response: MarkdownWorkerResponse }
|
|
88
|
+
| { readonly type: "worker.spawn-failed"; readonly attempt: number; readonly message: string }
|
|
89
|
+
| { readonly type: "worker.crashed"; readonly attempt: number; readonly message: string }
|
|
90
|
+
| { readonly type: "watchdog.fired"; readonly attempt: number }
|
|
91
|
+
| { readonly type: "retry.ready"; readonly attempt: number }
|
|
92
|
+
|
|
93
|
+
export type MarkdownWorkerEffect =
|
|
94
|
+
| { readonly type: "spawn-worker"; readonly attempt: number }
|
|
95
|
+
| { readonly type: "close-worker"; readonly attempt: number }
|
|
96
|
+
| { readonly type: "post"; readonly attempt: number; readonly message: MarkdownWorkerRequest }
|
|
97
|
+
| { readonly type: "arm-watchdog"; readonly attempt: number; readonly delay: number }
|
|
98
|
+
| { readonly type: "schedule-retry"; readonly attempt: number; readonly delay: number }
|
|
99
|
+
| { readonly type: "resolve-parse"; readonly id: number; readonly html: Extract<MarkdownWorkerResponse, { type: "parse" }>["html"] }
|
|
100
|
+
| { readonly type: "resolve-project"; readonly id: number; readonly projection: Projection }
|
|
101
|
+
| { readonly type: "resolve-highlight"; readonly id: number; readonly value: MarkdownWorkerState }
|
|
102
|
+
| { readonly type: "reject"; readonly ids: readonly number[]; readonly reason: WorkerRejectReason }
|
|
103
|
+
|
|
104
|
+
export type MarkdownWorkerTransition = {
|
|
105
|
+
readonly state: MarkdownWorkerMachineState
|
|
106
|
+
readonly effects: readonly MarkdownWorkerEffect[]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const INITIAL_MARKDOWN_WORKER_STATE: MarkdownWorkerMachineState = Object.freeze({
|
|
110
|
+
status: "idle",
|
|
111
|
+
issued: 0,
|
|
112
|
+
attempts: 0,
|
|
113
|
+
failures: 0,
|
|
114
|
+
generationFloor: 0,
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
export const EMPTY_WORKER_EFFECTS: readonly MarkdownWorkerEffect[] = Object.freeze([])
|
|
118
|
+
|
|
119
|
+
const DISPOSED: WorkerRejectReason = Object.freeze({ kind: "disposed" })
|
|
120
|
+
|
|
121
|
+
const SUPERSEDED: WorkerRejectReason = Object.freeze({ kind: "superseded" })
|
|
122
|
+
|
|
123
|
+
const TIMED_OUT: WorkerFailure = Object.freeze({ kind: "error", message: "Markdown worker timed out" })
|
|
124
|
+
|
|
125
|
+
const OUT_OF_SYNC: WorkerFailure = Object.freeze({ kind: "error", message: "Markdown projection out of sync" })
|
|
126
|
+
|
|
127
|
+
type Requested = Extract<MarkdownWorkerEvent, { type: "parse.requested" | "project.requested" | "highlight.requested" }>
|
|
128
|
+
|
|
129
|
+
type KeyedRequest = Extract<WorkerRequest, { key: string }>
|
|
130
|
+
|
|
131
|
+
type LaneField = "projectLanes" | "highlightLanes"
|
|
132
|
+
|
|
133
|
+
export function transitionMarkdownWorker(
|
|
134
|
+
state: MarkdownWorkerMachineState,
|
|
135
|
+
event: MarkdownWorkerEvent,
|
|
136
|
+
): MarkdownWorkerTransition {
|
|
137
|
+
if (event.type === "parse.requested" || event.type === "project.requested" || event.type === "highlight.requested")
|
|
138
|
+
return request(state, event)
|
|
139
|
+
|
|
140
|
+
if (event.type === "retry.ready") {
|
|
141
|
+
if (state.status !== "failed" || event.attempt !== state.attempts) return { state, effects: EMPTY_WORKER_EFFECTS }
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
state: {
|
|
145
|
+
status: "idle",
|
|
146
|
+
issued: state.issued,
|
|
147
|
+
attempts: state.attempts,
|
|
148
|
+
failures: state.failures,
|
|
149
|
+
generationFloor: state.generationFloor,
|
|
150
|
+
},
|
|
151
|
+
effects: EMPTY_WORKER_EFFECTS,
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Every other event belongs to a live worker. A failed one settled everything once and ignores the rest.
|
|
156
|
+
if (state.status !== "ready") return { state, effects: EMPTY_WORKER_EFFECTS }
|
|
157
|
+
|
|
158
|
+
if (event.type === "parse.aborted") {
|
|
159
|
+
if (state.requests.get(event.id)?.type !== "parse") return { state, effects: EMPTY_WORKER_EFFECTS }
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
state: { ...state, requests: without(state.requests, event.id) },
|
|
163
|
+
effects: [{ type: "reject", ids: [event.id], reason: DISPOSED }],
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (event.type === "project.disposed") return dispose(state, "projectLanes", event.key)
|
|
168
|
+
|
|
169
|
+
if (event.type === "highlight.disposed") return dispose(state, "highlightLanes", event.key)
|
|
170
|
+
|
|
171
|
+
if (event.attempt !== state.attempts) return { state, effects: EMPTY_WORKER_EFFECTS }
|
|
172
|
+
|
|
173
|
+
if (event.type === "worker.message")
|
|
174
|
+
return message(state.replied ? state : { ...state, replied: true }, event.response)
|
|
175
|
+
|
|
176
|
+
if (event.type === "worker.spawn-failed") return fail(state, { kind: "unavailable", message: event.message })
|
|
177
|
+
|
|
178
|
+
if (event.type === "worker.crashed") return fail(state, { kind: "error", message: event.message })
|
|
179
|
+
|
|
180
|
+
if (state.requests.size === 0) return { state: { ...state, watched: undefined }, effects: EMPTY_WORKER_EFFECTS }
|
|
181
|
+
const oldest = state.requests.keys().next().value
|
|
182
|
+
|
|
183
|
+
if (oldest === state.watched) return fail(state, TIMED_OUT)
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
state: { ...state, watched: oldest },
|
|
187
|
+
effects: [{ type: "arm-watchdog", attempt: state.attempts, delay: watchdogDelay(state) }],
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function request(state: MarkdownWorkerMachineState, event: Requested): MarkdownWorkerTransition {
|
|
192
|
+
const id = state.issued + 1
|
|
193
|
+
|
|
194
|
+
if (state.status === "failed")
|
|
195
|
+
return {
|
|
196
|
+
state: { ...state, issued: id },
|
|
197
|
+
effects: [{ type: "reject", ids: [id], reason: { kind: "unavailable", message: state.message } }],
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const message = requestMessage(event, id)
|
|
201
|
+
|
|
202
|
+
if (state.status === "ready") return admit(state, message, EMPTY_WORKER_EFFECTS)
|
|
203
|
+
const attempt = state.attempts + 1
|
|
204
|
+
|
|
205
|
+
return admit(
|
|
206
|
+
{
|
|
207
|
+
status: "ready",
|
|
208
|
+
issued: state.issued,
|
|
209
|
+
attempts: attempt,
|
|
210
|
+
failures: state.failures,
|
|
211
|
+
generationFloor: state.generationFloor,
|
|
212
|
+
watched: undefined,
|
|
213
|
+
replied: false,
|
|
214
|
+
requests: new Map(),
|
|
215
|
+
projectLanes: new Map(),
|
|
216
|
+
projections: new Map(),
|
|
217
|
+
highlightLanes: new Map(),
|
|
218
|
+
highlightStreams: new Map(),
|
|
219
|
+
},
|
|
220
|
+
message,
|
|
221
|
+
[{ type: "spawn-worker", attempt }],
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function requestMessage(event: Requested, id: number): WorkerRequest {
|
|
226
|
+
if (event.type === "parse.requested") return { type: "parse", id, text: event.text }
|
|
227
|
+
|
|
228
|
+
if (event.type === "project.requested")
|
|
229
|
+
return { type: "project", id, key: event.key, text: event.text, live: event.live }
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
type: "highlight",
|
|
233
|
+
id,
|
|
234
|
+
key: event.key,
|
|
235
|
+
text: event.text,
|
|
236
|
+
language: event.language,
|
|
237
|
+
complete: event.complete,
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Claims the request id, then posts the request or holds it as its key's newest snapshot behind the one in flight.
|
|
242
|
+
function admit(
|
|
243
|
+
state: MarkdownWorkerReady,
|
|
244
|
+
message: WorkerRequest,
|
|
245
|
+
spawn: readonly MarkdownWorkerEffect[],
|
|
246
|
+
): MarkdownWorkerTransition {
|
|
247
|
+
const requests = new Map(state.requests).set(message.id, message)
|
|
248
|
+
const lane = message.type === "parse" ? undefined : state[laneField(message)].get(message.key)
|
|
249
|
+
|
|
250
|
+
if (lane && message.type !== "parse") {
|
|
251
|
+
if (lane.queued !== undefined) requests.delete(lane.queued)
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
state: withLane(
|
|
255
|
+
{ ...state, issued: message.id, requests },
|
|
256
|
+
laneField(message),
|
|
257
|
+
new Map(state[laneField(message)]).set(message.key, { active: lane.active, queued: message.id }),
|
|
258
|
+
),
|
|
259
|
+
effects:
|
|
260
|
+
lane.queued === undefined ? EMPTY_WORKER_EFFECTS : [{ type: "reject", ids: [lane.queued], reason: SUPERSEDED }],
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// A lane's active request is older than its queued one, so the oldest request in flight has always been posted.
|
|
265
|
+
const arm = state.watched === undefined
|
|
266
|
+
const post: MarkdownWorkerEffect = { type: "post", attempt: state.attempts, message }
|
|
267
|
+
|
|
268
|
+
const effects = arm
|
|
269
|
+
? [...spawn, post, { type: "arm-watchdog" as const, attempt: state.attempts, delay: watchdogDelay(state) }]
|
|
270
|
+
: [...spawn, post]
|
|
271
|
+
|
|
272
|
+
const next = { ...state, issued: message.id, watched: arm ? message.id : state.watched, requests }
|
|
273
|
+
|
|
274
|
+
if (message.type === "parse") return { state: next, effects }
|
|
275
|
+
const field = laneField(message)
|
|
276
|
+
|
|
277
|
+
return { state: withLane(next, field, new Map(state[field]).set(message.key, { active: message.id })), effects }
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function dispose(state: MarkdownWorkerReady, field: LaneField, key: string): MarkdownWorkerTransition {
|
|
281
|
+
const lane = state[field].get(key)
|
|
282
|
+
const stream = field === "highlightLanes" ? state.highlightStreams.get(key) : undefined
|
|
283
|
+
const projection = field === "projectLanes" ? state.projections.get(key) : undefined
|
|
284
|
+
|
|
285
|
+
if (!lane && !stream && !projection) return { state, effects: EMPTY_WORKER_EFFECTS }
|
|
286
|
+
const ids = !lane ? [] : lane.queued === undefined ? [lane.active] : [lane.active, lane.queued]
|
|
287
|
+
|
|
288
|
+
const post = disposeEffect(state, field === "projectLanes" ? "project" : "highlight", key)
|
|
289
|
+
|
|
290
|
+
const next = withLane(
|
|
291
|
+
{
|
|
292
|
+
...state,
|
|
293
|
+
requests: ids.reduce((requests, id) => without(requests, id), state.requests),
|
|
294
|
+
projections: projection ? without(state.projections, key) : state.projections,
|
|
295
|
+
highlightStreams: stream ? without(state.highlightStreams, key) : state.highlightStreams,
|
|
296
|
+
generationFloor: Math.max(state.generationFloor, stream?.generation ?? 0),
|
|
297
|
+
},
|
|
298
|
+
field,
|
|
299
|
+
without(state[field], key),
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
return { state: next, effects: ids.length > 0 ? [{ type: "reject", ids, reason: DISPOSED }, post] : [post] }
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function message(state: MarkdownWorkerReady, response: MarkdownWorkerResponse): MarkdownWorkerTransition {
|
|
306
|
+
const entry = state.requests.get(response.id)
|
|
307
|
+
|
|
308
|
+
// The main thread already settled this request: disposed, aborted or superseded.
|
|
309
|
+
if (!entry) return { state, effects: EMPTY_WORKER_EFFECTS }
|
|
310
|
+
|
|
311
|
+
if (response.type === "error") return refused(state, entry, response)
|
|
312
|
+
|
|
313
|
+
if (response.type === "parse" && entry.type === "parse")
|
|
314
|
+
return {
|
|
315
|
+
state: { ...state, failures: 0, requests: without(state.requests, entry.id) },
|
|
316
|
+
effects: [{ type: "resolve-parse", id: entry.id, html: response.html }],
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (response.type === "project" && entry.type === "project") return projected(state, entry, response)
|
|
320
|
+
|
|
321
|
+
if (response.type === "highlight" && entry.type === "highlight") return highlighted(state, entry, response)
|
|
322
|
+
|
|
323
|
+
return { state, effects: EMPTY_WORKER_EFFECTS }
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function projected(
|
|
327
|
+
state: MarkdownWorkerReady,
|
|
328
|
+
entry: Extract<WorkerRequest, { type: "project" }>,
|
|
329
|
+
response: Extract<MarkdownWorkerResponse, { type: "project" }>,
|
|
330
|
+
): MarkdownWorkerTransition {
|
|
331
|
+
const next = advance(state, entry)
|
|
332
|
+
const previous = state.projections.get(entry.key)
|
|
333
|
+
|
|
334
|
+
// The worker diffed against blocks this thread never applied: reset both sides instead of dropping the blocks between.
|
|
335
|
+
if (response.from > (previous?.length ?? 0))
|
|
336
|
+
return {
|
|
337
|
+
state: { ...next.state, projections: without(state.projections, entry.key) },
|
|
338
|
+
effects: [
|
|
339
|
+
{ type: "reject", ids: [entry.id], reason: OUT_OF_SYNC },
|
|
340
|
+
disposeEffect(state, "project", entry.key),
|
|
341
|
+
...next.effects,
|
|
342
|
+
],
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const blocks = applyProjectionBlocks(previous, response.from, response.blocks)
|
|
346
|
+
|
|
347
|
+
return {
|
|
348
|
+
state: { ...next.state, projections: new Map(state.projections).set(entry.key, blocks) },
|
|
349
|
+
effects: [{ type: "resolve-project", id: entry.id, projection: { text: entry.text, blocks } }, ...next.effects],
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function highlighted(
|
|
354
|
+
state: MarkdownWorkerReady,
|
|
355
|
+
entry: Extract<WorkerRequest, { type: "highlight" }>,
|
|
356
|
+
response: Extract<MarkdownWorkerResponse, { type: "highlight" }>,
|
|
357
|
+
): MarkdownWorkerTransition {
|
|
358
|
+
const latest = state.highlightLanes.get(entry.key)?.queued ?? entry.id
|
|
359
|
+
const next = advance(state, entry)
|
|
360
|
+
const previous = state.highlightStreams.get(entry.key)
|
|
361
|
+
|
|
362
|
+
const value = applyMarkdownWorkerResponse(
|
|
363
|
+
previous ??
|
|
364
|
+
(state.generationFloor > 0
|
|
365
|
+
? { id: 0, generation: state.generationFloor, language: "", stable: [], unstable: [] }
|
|
366
|
+
: undefined),
|
|
367
|
+
response,
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
const resolve: MarkdownWorkerEffect = { type: "resolve-highlight", id: entry.id, value }
|
|
371
|
+
|
|
372
|
+
if (shouldReleaseMarkdownWorkerState(entry.complete ?? false, latest, entry.id))
|
|
373
|
+
return {
|
|
374
|
+
state: previous ? { ...next.state, highlightStreams: without(state.highlightStreams, entry.key) } : next.state,
|
|
375
|
+
effects: [resolve, ...next.effects],
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const streams = new Map(state.highlightStreams)
|
|
379
|
+
streams.delete(entry.key)
|
|
380
|
+
streams.set(entry.key, value)
|
|
381
|
+
const lanes = next.state.highlightLanes
|
|
382
|
+
// Only a stream with nothing in flight is evicted, so a visible block never loses a pending highlight.
|
|
383
|
+
const evicted = streams.size > HIGHLIGHT_STREAM_LIMIT ? [...streams.keys()].find((key) => !lanes.has(key)) : undefined
|
|
384
|
+
const dropped = evicted === undefined ? undefined : streams.get(evicted)
|
|
385
|
+
|
|
386
|
+
if (evicted === undefined || !dropped)
|
|
387
|
+
return { state: { ...next.state, highlightStreams: streams }, effects: [resolve, ...next.effects] }
|
|
388
|
+
streams.delete(evicted)
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
state: {
|
|
392
|
+
...next.state,
|
|
393
|
+
highlightStreams: streams,
|
|
394
|
+
generationFloor: Math.max(state.generationFloor, dropped.generation),
|
|
395
|
+
},
|
|
396
|
+
effects: [resolve, disposeEffect(state, "highlight", evicted), ...next.effects],
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// The worker refused a request. An error also forgets the key's cached base on both sides, so the next request starts
|
|
401
|
+
// again from nothing instead of building on a reply this thread never applied.
|
|
402
|
+
function refused(
|
|
403
|
+
state: MarkdownWorkerReady,
|
|
404
|
+
entry: WorkerRequest,
|
|
405
|
+
response: Extract<MarkdownWorkerResponse, { type: "error" }>,
|
|
406
|
+
): MarkdownWorkerTransition {
|
|
407
|
+
const reject: MarkdownWorkerEffect = {
|
|
408
|
+
type: "reject",
|
|
409
|
+
ids: [entry.id],
|
|
410
|
+
reason: { kind: "error", message: response.message },
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (entry.type === "parse")
|
|
414
|
+
return { state: { ...state, failures: 0, requests: without(state.requests, entry.id) }, effects: [reject] }
|
|
415
|
+
const next = advance(state, entry)
|
|
416
|
+
const stream = entry.type === "highlight" ? state.highlightStreams.get(entry.key) : undefined
|
|
417
|
+
|
|
418
|
+
return {
|
|
419
|
+
state: {
|
|
420
|
+
...next.state,
|
|
421
|
+
projections: entry.type === "project" ? without(state.projections, entry.key) : state.projections,
|
|
422
|
+
highlightStreams:
|
|
423
|
+
entry.type === "highlight" ? without(state.highlightStreams, entry.key) : state.highlightStreams,
|
|
424
|
+
generationFloor: Math.max(state.generationFloor, stream?.generation ?? 0),
|
|
425
|
+
},
|
|
426
|
+
effects: [reject, disposeEffect(state, entry.type, entry.key), ...next.effects],
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function disposeEffect(
|
|
431
|
+
state: MarkdownWorkerReady,
|
|
432
|
+
target: MarkdownWorkerLaneTarget,
|
|
433
|
+
key: string,
|
|
434
|
+
): MarkdownWorkerEffect {
|
|
435
|
+
return { type: "post", attempt: state.attempts, message: { type: "dispose", target, key } }
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Settles a key's active request and posts its queued snapshot, if any.
|
|
439
|
+
function advance(state: MarkdownWorkerReady, entry: KeyedRequest) {
|
|
440
|
+
const field = laneField(entry)
|
|
441
|
+
const lane = state[field].get(entry.key)
|
|
442
|
+
const queued = lane?.active === entry.id ? lane.queued : undefined
|
|
443
|
+
const promoted = queued === undefined ? undefined : state.requests.get(queued)
|
|
444
|
+
|
|
445
|
+
const lanes =
|
|
446
|
+
lane?.active !== entry.id
|
|
447
|
+
? state[field]
|
|
448
|
+
: queued === undefined
|
|
449
|
+
? without(state[field], entry.key)
|
|
450
|
+
: new Map(state[field]).set(entry.key, { active: queued })
|
|
451
|
+
|
|
452
|
+
const effects: readonly MarkdownWorkerEffect[] = promoted
|
|
453
|
+
? [{ type: "post", attempt: state.attempts, message: promoted }]
|
|
454
|
+
: EMPTY_WORKER_EFFECTS
|
|
455
|
+
|
|
456
|
+
return {
|
|
457
|
+
state: withLane({ ...state, failures: 0, requests: without(state.requests, entry.id) }, field, lanes),
|
|
458
|
+
effects,
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function fail(state: MarkdownWorkerReady, reason: WorkerFailure): MarkdownWorkerTransition {
|
|
463
|
+
const failures = state.failures + 1
|
|
464
|
+
const ids = [...state.requests.keys()]
|
|
465
|
+
|
|
466
|
+
const retry: MarkdownWorkerEffect = {
|
|
467
|
+
type: "schedule-retry",
|
|
468
|
+
attempt: state.attempts,
|
|
469
|
+
delay: Math.min(WORKER_RETRY_DELAY_MS * 2 ** (failures - 1), WORKER_RETRY_MAX_DELAY_MS),
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const close: MarkdownWorkerEffect = { type: "close-worker", attempt: state.attempts }
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
state: {
|
|
476
|
+
status: "failed",
|
|
477
|
+
issued: state.issued,
|
|
478
|
+
attempts: state.attempts,
|
|
479
|
+
failures,
|
|
480
|
+
generationFloor: [...state.highlightStreams.values()].reduce(
|
|
481
|
+
(floor, stream) => Math.max(floor, stream.generation),
|
|
482
|
+
state.generationFloor,
|
|
483
|
+
),
|
|
484
|
+
message: reason.message,
|
|
485
|
+
},
|
|
486
|
+
effects: ids.length > 0 ? [close, { type: "reject", ids, reason }, retry] : [close, retry],
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function watchdogDelay(state: MarkdownWorkerReady) {
|
|
491
|
+
return state.replied ? WORKER_HANG_TIMEOUT_MS : WORKER_BOOT_TIMEOUT_MS
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function laneField(request: KeyedRequest): LaneField {
|
|
495
|
+
return request.type === "project" ? "projectLanes" : "highlightLanes"
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function withLane(
|
|
499
|
+
state: MarkdownWorkerReady,
|
|
500
|
+
field: LaneField,
|
|
501
|
+
lanes: ReadonlyMap<string, WorkerLane>,
|
|
502
|
+
): MarkdownWorkerReady {
|
|
503
|
+
if (state[field] === lanes) return state
|
|
504
|
+
|
|
505
|
+
if (field === "projectLanes") return { ...state, projectLanes: lanes }
|
|
506
|
+
|
|
507
|
+
return { ...state, highlightLanes: lanes }
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function without<K, V>(map: ReadonlyMap<K, V>, key: K): ReadonlyMap<K, V> {
|
|
511
|
+
if (!map.has(key)) return map
|
|
512
|
+
const next = new Map(map)
|
|
513
|
+
next.delete(key)
|
|
514
|
+
|
|
515
|
+
return next
|
|
516
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Block } from "./markdown-stream"
|
|
2
|
+
|
|
3
|
+
export type MarkdownToken = [content: string, style: string]
|
|
4
|
+
|
|
5
|
+
/** Only the worker can supply fragments. The prose never authorizes styles on a subtree. */
|
|
6
|
+
export type MarkdownHTML = string | { html: string; fragments: Map<string, string> }
|
|
7
|
+
|
|
8
|
+
/** Projections and highlight streams keep separate per-key caches, so a disposal names the one it clears. */
|
|
9
|
+
export type MarkdownWorkerLaneTarget = "project" | "highlight"
|
|
10
|
+
|
|
11
|
+
export type MarkdownWorkerRequest =
|
|
12
|
+
| { type: "parse"; id: number; text: string }
|
|
13
|
+
| { type: "project"; id: number; key: string; text: string; live: boolean }
|
|
14
|
+
| { type: "highlight"; id: number; key: string; text: string; language: string; complete?: boolean }
|
|
15
|
+
| { type: "dispose"; target: MarkdownWorkerLaneTarget; key: string }
|
|
16
|
+
|
|
17
|
+
export type MarkdownWorkerResponse =
|
|
18
|
+
| { type: "parse"; id: number; html: MarkdownHTML }
|
|
19
|
+
| { type: "project"; id: number; key: string; from: number; blocks: Block[] }
|
|
20
|
+
| {
|
|
21
|
+
type: "highlight"
|
|
22
|
+
id: number
|
|
23
|
+
key: string
|
|
24
|
+
language: string
|
|
25
|
+
reset: boolean
|
|
26
|
+
stable: MarkdownToken[]
|
|
27
|
+
unstable: MarkdownToken[]
|
|
28
|
+
}
|
|
29
|
+
| { type: "error"; id: number; key?: string; message: string }
|
|
30
|
+
|
|
31
|
+
export type MarkdownWorkerState = {
|
|
32
|
+
id: number
|
|
33
|
+
generation: number
|
|
34
|
+
language: string
|
|
35
|
+
stable: MarkdownToken[]
|
|
36
|
+
unstable: MarkdownToken[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function shouldReleaseMarkdownWorkerState(complete: boolean, latestID: number | undefined, responseID: number) {
|
|
40
|
+
return complete && latestID === responseID
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A streamed answer keeps its settled blocks, so each projection response carries only the blocks from the first one
|
|
44
|
+
// that changed since the previous response for its key. Resending them all copied the whole answer on every delta.
|
|
45
|
+
export function diffProjectionBlocks(previous: Block[] | undefined, next: Block[]) {
|
|
46
|
+
const same = previous ? next.findIndex((block, index) => block !== previous[index]) : 0
|
|
47
|
+
const from = same === -1 ? next.length : same
|
|
48
|
+
|
|
49
|
+
return { from, blocks: next.slice(from) }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function applyProjectionBlocks(previous: Block[] | undefined, from: number, blocks: Block[]) {
|
|
53
|
+
return [...(previous ?? []).slice(0, from), ...blocks]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function markdownBlockKey(owner: string, cacheKey: string | undefined, index: number, mode: string) {
|
|
57
|
+
return `${owner}:${cacheKey ? `${cacheKey}:${index}:${mode}` : `block:${index}`}`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function applyMarkdownWorkerResponse(
|
|
61
|
+
state: MarkdownWorkerState | undefined,
|
|
62
|
+
response: Extract<MarkdownWorkerResponse, { type: "highlight" }>,
|
|
63
|
+
) {
|
|
64
|
+
if (state && response.id <= state.id) return state
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
id: response.id,
|
|
68
|
+
generation: (state?.generation ?? 0) + (response.reset ? 1 : 0),
|
|
69
|
+
language: response.language,
|
|
70
|
+
stable: response.reset ? response.stable : [...(state?.stable ?? []), ...response.stable],
|
|
71
|
+
unstable: response.unstable,
|
|
72
|
+
}
|
|
73
|
+
}
|