@deepseek-ai/dsh-experimental-client-ui-forge 0.2.2-rc.1 → 0.2.2-rc.2
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/README.i18n.yaml +8 -8
- package/README.md +41 -14
- package/README.zh.md +40 -13
- package/lib/client.js +13 -11800
- package/lib/client.js.map +1 -0
- package/lib/index.js +1340 -115
- package/lib/typert.host.js +1715 -212
- package/lib/typert.remote-client.d.ts +42 -5
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +1523 -244
- package/lib/types/client/ActionToast.d.ts.map +1 -0
- package/lib/types/client/ActionToast.js.map +1 -0
- package/lib/types/client/BaseMenu.d.ts +30 -0
- package/lib/types/client/BaseMenu.d.ts.map +1 -0
- package/lib/types/client/BaseMenu.js.map +1 -0
- package/lib/types/client/ChangeRequestCard.d.ts +9 -5
- package/lib/types/client/ChangeRequestCard.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestCard.js.map +1 -0
- package/lib/types/client/ChangeRequestOverview.d.ts +124 -0
- package/lib/types/client/ChangeRequestOverview.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestOverview.js.map +1 -0
- package/lib/types/client/ChangedFiles.d.ts +2 -0
- package/lib/types/client/ChangedFiles.d.ts.map +1 -0
- package/lib/types/client/ChangedFiles.js.map +1 -0
- package/lib/types/client/ChangesTab.d.ts +30 -5
- package/lib/types/client/ChangesTab.d.ts.map +1 -0
- package/lib/types/client/ChangesTab.js.map +1 -0
- package/lib/types/client/ChangesTabTitle.d.ts.map +1 -0
- package/lib/types/client/ChangesTabTitle.js.map +1 -0
- package/lib/types/client/CommitMenu.d.ts +41 -0
- package/lib/types/client/CommitMenu.d.ts.map +1 -0
- package/lib/types/client/CommitMenu.js.map +1 -0
- package/lib/types/client/DiffFile.d.ts +47 -15
- package/lib/types/client/DiffFile.d.ts.map +1 -0
- package/lib/types/client/DiffFile.js.map +1 -0
- package/lib/types/client/FileLabel.d.ts.map +1 -0
- package/lib/types/client/FileLabel.js.map +1 -0
- package/lib/types/client/FileTreePane.d.ts +4 -2
- package/lib/types/client/FileTreePane.d.ts.map +1 -0
- package/lib/types/client/FileTreePane.js.map +1 -0
- package/lib/types/client/ForgeTab.d.ts +9 -1
- package/lib/types/client/ForgeTab.d.ts.map +1 -0
- package/lib/types/client/ForgeTab.js.map +1 -0
- package/lib/types/client/ForgeTabTitle.d.ts.map +1 -0
- package/lib/types/client/ForgeTabTitle.js.map +1 -0
- package/lib/types/client/HeaderButton.d.ts.map +1 -0
- package/lib/types/client/HeaderButton.js.map +1 -0
- package/lib/types/client/LocalChangesCard.d.ts.map +1 -0
- package/lib/types/client/LocalChangesCard.js.map +1 -0
- package/lib/types/client/RefreshGlyph.d.ts.map +1 -0
- package/lib/types/client/RefreshGlyph.js.map +1 -0
- package/lib/types/client/ReviewComposer.d.ts +20 -6
- package/lib/types/client/ReviewComposer.d.ts.map +1 -0
- package/lib/types/client/ReviewComposer.js.map +1 -0
- package/lib/types/client/ReviewControls.d.ts +79 -0
- package/lib/types/client/ReviewControls.d.ts.map +1 -0
- package/lib/types/client/ReviewControls.js.map +1 -0
- package/lib/types/client/ReviewThread.d.ts +4 -4
- package/lib/types/client/ReviewThread.d.ts.map +1 -0
- package/lib/types/client/ReviewThread.js.map +1 -0
- package/lib/types/client/ReviewThreads.d.ts +108 -0
- package/lib/types/client/ReviewThreads.d.ts.map +1 -0
- package/lib/types/client/ReviewThreads.js.map +1 -0
- package/lib/types/client/SessionHoverForge.d.ts.map +1 -0
- package/lib/types/client/SessionHoverForge.js.map +1 -0
- package/lib/types/client/SessionRowForge.d.ts +1 -1
- package/lib/types/client/SessionRowForge.d.ts.map +1 -0
- package/lib/types/client/SessionRowForge.js.map +1 -0
- package/lib/types/client/SubmitReview.d.ts +12 -4
- package/lib/types/client/SubmitReview.d.ts.map +1 -0
- package/lib/types/client/SubmitReview.js.map +1 -0
- package/lib/types/client/ThreadList.d.ts +43 -0
- package/lib/types/client/ThreadList.d.ts.map +1 -0
- package/lib/types/client/ThreadList.js.map +1 -0
- package/lib/types/client/ViewOptionsMenu.d.ts +31 -0
- package/lib/types/client/ViewOptionsMenu.d.ts.map +1 -0
- package/lib/types/client/ViewOptionsMenu.js.map +1 -0
- package/lib/types/client/WorktreeProgress.d.ts.map +1 -0
- package/lib/types/client/WorktreeProgress.js.map +1 -0
- package/lib/types/client/WorktreeSettings.d.ts +12 -0
- package/lib/types/client/WorktreeSettings.d.ts.map +1 -0
- package/lib/types/client/WorktreeSettings.js.map +1 -0
- package/lib/types/client/WorktreeToggle.d.ts.map +1 -0
- package/lib/types/client/WorktreeToggle.js.map +1 -0
- package/lib/types/client/changes-address.d.ts.map +1 -0
- package/lib/types/client/changes-address.js.map +1 -0
- package/lib/types/client/changes-store.d.ts +20 -5
- package/lib/types/client/changes-store.d.ts.map +1 -0
- package/lib/types/client/changes-store.js.map +1 -0
- package/lib/types/client/checkout.d.ts.map +1 -0
- package/lib/types/client/checkout.js.map +1 -0
- package/lib/types/client/code-selection.d.ts.map +1 -0
- package/lib/types/client/code-selection.js.map +1 -0
- package/lib/types/client/diff-rows.d.ts.map +1 -0
- package/lib/types/client/diff-rows.js.map +1 -0
- package/lib/types/client/file-tree.d.ts.map +1 -0
- package/lib/types/client/file-tree.js.map +1 -0
- package/lib/types/client/format.d.ts.map +1 -0
- package/lib/types/client/format.js.map +1 -0
- package/lib/types/client/glyphs.d.ts +4 -2
- package/lib/types/client/glyphs.d.ts.map +1 -0
- package/lib/types/client/glyphs.js.map +1 -0
- package/lib/types/client/host-viewed.d.ts +84 -0
- package/lib/types/client/host-viewed.d.ts.map +1 -0
- package/lib/types/client/host-viewed.js.map +1 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/index.js.map +1 -0
- package/lib/types/client/locales.d.ts +175 -3
- package/lib/types/client/locales.d.ts.map +1 -0
- package/lib/types/client/locales.js.map +1 -0
- package/lib/types/client/menu-search.d.ts +10 -0
- package/lib/types/client/menu-search.d.ts.map +1 -0
- package/lib/types/client/menu-search.js.map +1 -0
- package/lib/types/client/mount.d.ts +4 -0
- package/lib/types/client/mount.d.ts.map +1 -0
- package/lib/types/client/mount.js.map +1 -0
- package/lib/types/client/review-queue.d.ts +5 -8
- package/lib/types/client/review-queue.d.ts.map +1 -0
- package/lib/types/client/review-queue.js.map +1 -0
- package/lib/types/client/review-threads.d.ts +108 -0
- package/lib/types/client/review-threads.d.ts.map +1 -0
- package/lib/types/client/review-threads.js.map +1 -0
- package/lib/types/client/review.d.ts +31 -4
- package/lib/types/client/review.d.ts.map +1 -0
- package/lib/types/client/review.js.map +1 -0
- package/lib/types/client/section-reach.d.ts +26 -0
- package/lib/types/client/section-reach.d.ts.map +1 -0
- package/lib/types/client/section-reach.js.map +1 -0
- package/lib/types/client/session-list.d.ts.map +1 -0
- package/lib/types/client/session-list.js.map +1 -0
- package/lib/types/client/source.d.ts.map +1 -0
- package/lib/types/client/source.js.map +1 -0
- package/lib/types/client/status.d.ts.map +1 -0
- package/lib/types/client/status.js.map +1 -0
- package/lib/types/client/thread-state.d.ts +68 -0
- package/lib/types/client/thread-state.d.ts.map +1 -0
- package/lib/types/client/thread-state.js.map +1 -0
- package/lib/types/client/toolbar-fold.d.ts +19 -0
- package/lib/types/client/toolbar-fold.d.ts.map +1 -0
- package/lib/types/client/toolbar-fold.js.map +1 -0
- package/lib/types/client/viewed-files.d.ts +70 -0
- package/lib/types/client/viewed-files.d.ts.map +1 -0
- package/lib/types/client/viewed-files.js.map +1 -0
- package/lib/types/client/worktree-settings-form.d.ts +32 -0
- package/lib/types/client/worktree-settings-form.d.ts.map +1 -0
- package/lib/types/client/worktree-settings-form.js.map +1 -0
- package/lib/types/commit-range.d.ts +17 -0
- package/lib/types/commit-range.d.ts.map +1 -0
- package/lib/types/commit-range.js.map +1 -0
- package/lib/types/diff.d.ts +10 -2
- package/lib/types/diff.d.ts.map +1 -0
- package/lib/types/diff.js.map +1 -0
- package/lib/types/git.d.ts.map +1 -0
- package/lib/types/git.js.map +1 -0
- package/lib/types/index.d.ts +194 -14
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/layers.d.ts +66 -0
- package/lib/types/layers.d.ts.map +1 -0
- package/lib/types/layers.js.map +1 -0
- package/lib/types/local-diff.d.ts +54 -6
- package/lib/types/local-diff.d.ts.map +1 -0
- package/lib/types/local-diff.js.map +1 -0
- package/lib/types/panel.d.ts.map +1 -0
- package/lib/types/panel.js.map +1 -0
- package/lib/types/parse.d.ts +16 -0
- package/lib/types/parse.d.ts.map +1 -0
- package/lib/types/parse.js.map +1 -0
- package/lib/types/patch-cache.d.ts +12 -7
- package/lib/types/patch-cache.d.ts.map +1 -0
- package/lib/types/patch-cache.js.map +1 -0
- package/lib/types/review-cache.d.ts +54 -0
- package/lib/types/review-cache.d.ts.map +1 -0
- package/lib/types/review-cache.js.map +1 -0
- package/lib/types/states.d.ts.map +1 -0
- package/lib/types/states.js.map +1 -0
- package/lib/types/types.d.ts +113 -11
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/wildmatch.d.ts +14 -0
- package/lib/types/wildmatch.d.ts.map +1 -0
- package/lib/types/wildmatch.js.map +1 -0
- package/lib/types/worktree.d.ts +8 -6
- package/lib/types/worktree.d.ts.map +1 -0
- package/lib/types/worktree.js.map +1 -0
- package/package.json +56 -46
- package/src/client/ActionToast.tsx +38 -0
- package/src/client/BaseMenu.module.css +153 -0
- package/src/client/BaseMenu.tsx +180 -0
- package/src/client/ChangeRequestCard.tsx +439 -0
- package/src/client/ChangeRequestOverview.module.css +196 -0
- package/src/client/ChangeRequestOverview.tsx +620 -0
- package/src/client/ChangedFiles.module.css +26 -0
- package/src/client/ChangedFiles.tsx +144 -0
- package/src/client/ChangesTab.module.css +984 -0
- package/src/client/ChangesTab.tsx +1288 -0
- package/src/client/ChangesTabTitle.tsx +28 -0
- package/src/client/CommitMenu.module.css +139 -0
- package/src/client/CommitMenu.tsx +186 -0
- package/src/client/DiffFile.tsx +621 -0
- package/src/client/FileLabel.module.css +53 -0
- package/src/client/FileLabel.tsx +66 -0
- package/src/client/FileTreePane.module.css +10 -0
- package/src/client/FileTreePane.tsx +79 -0
- package/src/client/ForgeTab.module.css +627 -0
- package/src/client/ForgeTab.tsx +263 -0
- package/src/client/ForgeTabTitle.tsx +21 -0
- package/src/client/HeaderButton.module.css +29 -0
- package/src/client/HeaderButton.tsx +68 -0
- package/src/client/LocalChangesCard.tsx +83 -0
- package/src/client/RefreshGlyph.module.css +22 -0
- package/src/client/RefreshGlyph.tsx +35 -0
- package/src/client/ReviewComposer.module.css +37 -0
- package/src/client/ReviewComposer.tsx +128 -0
- package/src/client/ReviewControls.module.css +162 -0
- package/src/client/ReviewControls.tsx +191 -0
- package/src/client/ReviewThread.tsx +96 -0
- package/src/client/ReviewThreads.module.css +326 -0
- package/src/client/ReviewThreads.tsx +443 -0
- package/src/client/SessionHoverForge.tsx +72 -0
- package/src/client/SessionList.module.css +85 -0
- package/src/client/SessionRowForge.tsx +86 -0
- package/src/client/SubmitReview.module.css +26 -0
- package/src/client/SubmitReview.tsx +95 -0
- package/src/client/ThreadList.module.css +218 -0
- package/src/client/ThreadList.tsx +165 -0
- package/src/client/ViewOptionsMenu.tsx +79 -0
- package/src/client/WorktreeProgress.module.css +226 -0
- package/src/client/WorktreeProgress.tsx +280 -0
- package/src/client/WorktreeSettings.module.css +29 -0
- package/src/client/WorktreeSettings.tsx +42 -0
- package/src/client/WorktreeToggle.module.css +132 -0
- package/src/client/WorktreeToggle.tsx +165 -0
- package/src/client/changes-address.ts +110 -0
- package/src/client/changes-store.ts +155 -0
- package/src/client/checkout.ts +75 -0
- package/src/client/code-selection.ts +53 -0
- package/src/client/diff-rows.ts +199 -0
- package/src/client/file-tree.ts +86 -0
- package/src/client/format.ts +37 -0
- package/src/client/glyphs.tsx +151 -0
- package/src/client/host-viewed.ts +195 -0
- package/src/client/index.ts +31 -0
- package/src/client/locales.ts +3446 -0
- package/src/client/menu-search.ts +16 -0
- package/src/client/mount.ts +439 -0
- package/src/client/review-queue.ts +256 -0
- package/src/client/review-threads.ts +204 -0
- package/src/client/review.ts +120 -0
- package/src/client/section-reach.ts +61 -0
- package/src/client/session-list.ts +79 -0
- package/src/client/source.ts +274 -0
- package/src/client/status.ts +171 -0
- package/src/client/thread-state.ts +127 -0
- package/src/client/toolbar-fold.ts +70 -0
- package/src/client/viewed-files.ts +178 -0
- package/src/client/worktree-settings-form.ts +43 -0
- package/src/commit-range.ts +27 -0
- package/src/css-modules.d.ts +5 -0
- package/src/diff.ts +162 -0
- package/src/git.ts +293 -0
- package/src/index.ts +1162 -0
- package/src/layers.ts +163 -0
- package/src/local-diff.ts +438 -0
- package/src/panel.ts +422 -0
- package/src/parse.ts +137 -0
- package/src/patch-cache.ts +73 -0
- package/src/review-cache.ts +98 -0
- package/src/states.ts +350 -0
- package/src/types.ts +504 -0
- package/src/wildmatch.ts +121 -0
- package/src/worktree.ts +576 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Review threads of change requests kept in memory, so the changes view reads them from the code host again only
|
|
3
|
+
* after the change request's head or update time changes, the kept threads reach their age limit, or a review write drops them.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Buffer } from 'node:buffer'
|
|
7
|
+
import type { ChangeRequestKey, ForgeReviewData } from '@deepseek-ai/dsh-experimental-forge'
|
|
8
|
+
|
|
9
|
+
interface Entry {
|
|
10
|
+
/** Head commit and update time the threads were read at. */
|
|
11
|
+
readonly version: string
|
|
12
|
+
readonly review: ForgeReviewData
|
|
13
|
+
/** Epoch milliseconds of the read. */
|
|
14
|
+
readonly readAt: number
|
|
15
|
+
/** UTF-8 size of the threads as JSON. */
|
|
16
|
+
readonly bytes: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* One read of review threads per change request, bounded by age and by the total UTF-8 size of the kept reads as
|
|
21
|
+
* JSON; the least recently read change request leaves first. A read that started before a drop of any change request
|
|
22
|
+
* is not kept, so threads read before a write never replace the drop.
|
|
23
|
+
*/
|
|
24
|
+
export class ReviewCache {
|
|
25
|
+
private readonly entries = new Map<ChangeRequestKey, Entry>()
|
|
26
|
+
private bytes = 0
|
|
27
|
+
/** Number of drops so far. */
|
|
28
|
+
private drops = 0
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param maxBytes - most UTF-8 bytes kept together; a larger read is not kept.
|
|
32
|
+
* @param maxAgeMs - age in milliseconds at which a kept read is no longer returned.
|
|
33
|
+
*/
|
|
34
|
+
constructor(private readonly maxBytes: number, private readonly maxAgeMs: number) {}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read the threads kept for a change request at a version while they are younger than the age limit, marking them
|
|
38
|
+
* the most recently read.
|
|
39
|
+
* @param key - the change request.
|
|
40
|
+
* @param version - head commit and update time the reader needs.
|
|
41
|
+
* @param now - current epoch milliseconds.
|
|
42
|
+
* @returns the threads, or undefined when none are kept at that version or they reached the age limit.
|
|
43
|
+
*/
|
|
44
|
+
get(key: ChangeRequestKey, version: string, now: number): ForgeReviewData | undefined {
|
|
45
|
+
const entry = this.entries.get(key)
|
|
46
|
+
if (entry?.version !== version || now - entry.readAt >= this.maxAgeMs) return undefined
|
|
47
|
+
this.entries.delete(key)
|
|
48
|
+
this.entries.set(key, entry)
|
|
49
|
+
return entry.review
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Name the cache's state when a read starts, for {@link ReviewCache.set}.
|
|
54
|
+
* @returns the token of the read.
|
|
55
|
+
*/
|
|
56
|
+
token(): number {
|
|
57
|
+
return this.drops
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Keep a read in place of the change request's earlier one, then drop the least recently read change requests
|
|
62
|
+
* until the rest fit within `maxBytes`. A read whose token a drop outdated is not kept.
|
|
63
|
+
* @param key - the change request.
|
|
64
|
+
* @param version - head commit and update time the threads were read at.
|
|
65
|
+
* @param review - the threads.
|
|
66
|
+
* @param readAt - epoch milliseconds when the read started.
|
|
67
|
+
* @param token - what {@link ReviewCache.token} returned when the read started.
|
|
68
|
+
*/
|
|
69
|
+
set(key: ChangeRequestKey, version: string, review: ForgeReviewData, readAt: number, token: number): void {
|
|
70
|
+
if (token !== this.drops) return
|
|
71
|
+
this.forget(key)
|
|
72
|
+
const bytes = Buffer.byteLength(JSON.stringify(review))
|
|
73
|
+
if (bytes > this.maxBytes) return
|
|
74
|
+
this.entries.set(key, { version, review, readAt, bytes })
|
|
75
|
+
this.bytes += bytes
|
|
76
|
+
for (const [oldest, entry] of this.entries) {
|
|
77
|
+
if (this.bytes <= this.maxBytes) break
|
|
78
|
+
this.entries.delete(oldest)
|
|
79
|
+
this.bytes -= entry.bytes
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Forget the threads kept for a change request, and every read in flight, after a write changed them.
|
|
85
|
+
* @param key - the change request.
|
|
86
|
+
*/
|
|
87
|
+
drop(key: ChangeRequestKey): void {
|
|
88
|
+
this.drops += 1
|
|
89
|
+
this.forget(key)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private forget(key: ChangeRequestKey): void {
|
|
93
|
+
const previous = this.entries.get(key)
|
|
94
|
+
if (previous === undefined) return
|
|
95
|
+
this.entries.delete(key)
|
|
96
|
+
this.bytes -= previous.bytes
|
|
97
|
+
}
|
|
98
|
+
}
|
package/src/states.ts
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The last known state of every change request that a listed Session binds,
|
|
3
|
+
* for the Session list. One run starts with the first stream and stops with
|
|
4
|
+
* the last: it reads the stored states, lists the Sessions with their
|
|
5
|
+
* bindings, follows binding changes of loaded Sessions, and polls each bound
|
|
6
|
+
* change request, once however many Sessions bind it. The forge service
|
|
7
|
+
* owns polling cadence and terminal-state refresh. Every state change is stored, so a restart shows it before the
|
|
8
|
+
* first poll.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { createHash } from 'node:crypto'
|
|
12
|
+
import { z } from 'zod'
|
|
13
|
+
import { defineDomain } from '@deepseek-ai/dsh-storage-domain'
|
|
14
|
+
import type { ChangeRequestListener, ChangeRequestRef, ChangeRequestSnapshot } from '@deepseek-ai/dsh-experimental-forge/types'
|
|
15
|
+
import type { SessionId } from '@deepseek-ai/dsh-session/types'
|
|
16
|
+
import type { ForgeChangeRequestListState, ForgeChangeRequestStatesFrame } from './types.ts'
|
|
17
|
+
|
|
18
|
+
const stateSchema: z.ZodType<ForgeChangeRequestListState> = z.object({
|
|
19
|
+
url: z.string().min(1),
|
|
20
|
+
number: z.number().int().positive(),
|
|
21
|
+
title: z.string(),
|
|
22
|
+
state: z.enum(['open', 'merged', 'closed']),
|
|
23
|
+
draft: z.boolean(),
|
|
24
|
+
checksRollup: z.enum(['none', 'pending', 'success', 'failure']),
|
|
25
|
+
reviewDecision: z.enum(['none', 'review-required', 'approved', 'changes-requested']),
|
|
26
|
+
mergeability: z.enum(['mergeable', 'conflicting', 'unknown']),
|
|
27
|
+
}).strict()
|
|
28
|
+
|
|
29
|
+
/** Stored states keyed by {@link storedKey}; derived data, so a record that fails its schema is set aside. */
|
|
30
|
+
export const changeRequestStatesDomain = defineDomain({
|
|
31
|
+
name: 'forge_panel_change_requests',
|
|
32
|
+
version: 1,
|
|
33
|
+
layout: 'per-record',
|
|
34
|
+
invalidRecords: 'backup-and-skip',
|
|
35
|
+
tables: { states: { valueSchema: stateSchema } },
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The storage key of a change request's state: a path-safe digest of its URL.
|
|
40
|
+
* @param url - the change request's web URL.
|
|
41
|
+
* @returns the key.
|
|
42
|
+
*/
|
|
43
|
+
export function storedKey(url: string): string {
|
|
44
|
+
return createHash('sha256').update(url).digest('hex')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The stored states the run reads and writes. */
|
|
48
|
+
export interface StoredStates {
|
|
49
|
+
/** The state stored under a key. */
|
|
50
|
+
get(key: string): ForgeChangeRequestListState | undefined
|
|
51
|
+
/** Every stored state. */
|
|
52
|
+
entries(): IterableIterator<[string, ForgeChangeRequestListState]>
|
|
53
|
+
/** Store a state durably. */
|
|
54
|
+
put(key: string, value: ForgeChangeRequestListState): Promise<void>
|
|
55
|
+
/** Remove a stored state durably. */
|
|
56
|
+
delete(key: string): Promise<boolean>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** One run's storage handle; closing releases its domain for the next run. */
|
|
60
|
+
export interface OpenedStates {
|
|
61
|
+
readonly table: StoredStates
|
|
62
|
+
/** Close the domain after the run's accepted writes settle. */
|
|
63
|
+
close(): Promise<void>
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** One listed Session and the URLs of its visible bindings, undefined when they are not known without loading it. */
|
|
67
|
+
export interface ListedSession {
|
|
68
|
+
readonly id: SessionId
|
|
69
|
+
readonly urls: readonly string[] | undefined
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** What a run reads, follows, and polls. */
|
|
73
|
+
export interface ChangeRequestStateSources {
|
|
74
|
+
/** List every Session with its visible bindings. */
|
|
75
|
+
readonly sessions: (signal: AbortSignal) => Promise<readonly ListedSession[]>
|
|
76
|
+
/** Follow the visible bindings of loaded Sessions; returns the disposer. */
|
|
77
|
+
readonly onBindings: (listener: (id: SessionId, urls: readonly string[]) => void) => () => void
|
|
78
|
+
/** The change request a URL names, or undefined when no provider recognizes it. */
|
|
79
|
+
readonly refOf: (url: string) => ChangeRequestRef | undefined
|
|
80
|
+
/** Poll a change request until the returned disposer runs. */
|
|
81
|
+
readonly watch: (ref: ChangeRequestRef, listener: ChangeRequestListener) => () => void
|
|
82
|
+
/** Open the stored states; the run owns and closes the returned handle. */
|
|
83
|
+
readonly open: () => Promise<OpenedStates>
|
|
84
|
+
/** Report a failed durable write; the state stays in memory. */
|
|
85
|
+
readonly report: (error: unknown) => void
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function listStateOf(snapshot: ChangeRequestSnapshot, url: string): ForgeChangeRequestListState {
|
|
89
|
+
return {
|
|
90
|
+
url,
|
|
91
|
+
number: snapshot.ref.number,
|
|
92
|
+
title: snapshot.title,
|
|
93
|
+
state: snapshot.state,
|
|
94
|
+
draft: snapshot.draft,
|
|
95
|
+
checksRollup: snapshot.checksRollup,
|
|
96
|
+
reviewDecision: snapshot.reviewDecision,
|
|
97
|
+
mergeability: snapshot.mergeability,
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function sameState(left: ForgeChangeRequestListState | undefined, right: ForgeChangeRequestListState): boolean {
|
|
102
|
+
return left !== undefined && left.title === right.title && left.state === right.state && left.draft === right.draft
|
|
103
|
+
&& left.checksRollup === right.checksRollup && left.reviewDecision === right.reviewDecision
|
|
104
|
+
&& left.mergeability === right.mergeability && left.number === right.number
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** One stream's pending changes, coalesced by URL until the reader takes them. */
|
|
108
|
+
class Reader {
|
|
109
|
+
private pending = new Map<string, ForgeChangeRequestListState>()
|
|
110
|
+
private wake: (() => void) | undefined
|
|
111
|
+
private ended = false
|
|
112
|
+
private readonly completion = Promise.withResolvers<undefined>()
|
|
113
|
+
readonly closed = this.completion.promise
|
|
114
|
+
private readonly abort = (): void => { this.end() }
|
|
115
|
+
|
|
116
|
+
constructor(readonly allowed: (id: SessionId) => boolean, private readonly signal: AbortSignal) {
|
|
117
|
+
signal.addEventListener('abort', this.abort, { once: true })
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
push(state: ForgeChangeRequestListState): void {
|
|
121
|
+
if (this.ended) return
|
|
122
|
+
this.pending.set(state.url, state)
|
|
123
|
+
this.wake?.()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
end(): void {
|
|
127
|
+
this.ended = true
|
|
128
|
+
this.pending.clear()
|
|
129
|
+
this.completion.resolve(undefined)
|
|
130
|
+
this.wake?.()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
dispose(): void {
|
|
134
|
+
this.signal.removeEventListener('abort', this.abort)
|
|
135
|
+
this.end()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async *frames(): AsyncIterable<ForgeChangeRequestStatesFrame> {
|
|
139
|
+
while (!this.ended) {
|
|
140
|
+
if (this.pending.size === 0) {
|
|
141
|
+
await new Promise<void>((resolve) => { this.wake = resolve })
|
|
142
|
+
this.wake = undefined
|
|
143
|
+
continue
|
|
144
|
+
}
|
|
145
|
+
const states = [...this.pending.values()]
|
|
146
|
+
this.pending = new Map()
|
|
147
|
+
yield { type: 'changed', states }
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The state of one run: stored and polled states, who binds what, and the polls. */
|
|
153
|
+
class Run {
|
|
154
|
+
private readonly states = new Map<string, ForgeChangeRequestListState>()
|
|
155
|
+
private readonly sessionsOf = new Map<string, Set<SessionId>>()
|
|
156
|
+
private readonly urlsOf = new Map<SessionId, ReadonlySet<string>>()
|
|
157
|
+
private readonly polls = new Map<string, () => void>()
|
|
158
|
+
private readonly stopBindings: () => void
|
|
159
|
+
private readonly startupBindings = new Map<SessionId, readonly string[]>()
|
|
160
|
+
private starting = true
|
|
161
|
+
private readonly writes = new Set<Promise<void>>()
|
|
162
|
+
private stopped = false
|
|
163
|
+
|
|
164
|
+
constructor(
|
|
165
|
+
private readonly sources: ChangeRequestStateSources,
|
|
166
|
+
private readonly readers: ReadonlySet<Reader>,
|
|
167
|
+
private readonly storage: OpenedStates,
|
|
168
|
+
) {
|
|
169
|
+
for (const [, state] of storage.table.entries()) this.states.set(state.url, state)
|
|
170
|
+
// Subscribe before listing; updates received during the read supersede its snapshot.
|
|
171
|
+
this.stopBindings = sources.onBindings((id, urls) => {
|
|
172
|
+
if (this.starting) this.startupBindings.set(id, urls)
|
|
173
|
+
else this.bind(id, urls)
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async start(signal: AbortSignal): Promise<void> {
|
|
178
|
+
const listed = await this.sources.sessions(signal)
|
|
179
|
+
signal.throwIfAborted()
|
|
180
|
+
const updates = this.startupBindings
|
|
181
|
+
for (const session of listed) {
|
|
182
|
+
const urls = updates.get(session.id) ?? session.urls
|
|
183
|
+
if (urls !== undefined) this.bind(session.id, urls)
|
|
184
|
+
}
|
|
185
|
+
this.starting = false
|
|
186
|
+
for (const [id, urls] of updates) this.bind(id, urls)
|
|
187
|
+
updates.clear()
|
|
188
|
+
// An uncached Session may still bind a stored request; absence is not established yet.
|
|
189
|
+
if (listed.some(session => session.urls === undefined)) return
|
|
190
|
+
for (const [url] of this.states) {
|
|
191
|
+
if (this.sessionsOf.has(url)) continue
|
|
192
|
+
this.states.delete(url)
|
|
193
|
+
this.write(() => this.storage.table.delete(storedKey(url)))
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The known states of every change request that some Session the reader may read binds. */
|
|
198
|
+
visible(allowed: (id: SessionId) => boolean): ForgeChangeRequestListState[] {
|
|
199
|
+
const states: ForgeChangeRequestListState[] = []
|
|
200
|
+
for (const [url, ids] of this.sessionsOf) {
|
|
201
|
+
const state = this.states.get(url)
|
|
202
|
+
if (state !== undefined && [...ids].some(allowed)) states.push(state)
|
|
203
|
+
}
|
|
204
|
+
return states
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async stop(): Promise<void> {
|
|
208
|
+
this.stopped = true
|
|
209
|
+
this.stopBindings()
|
|
210
|
+
for (const stop of this.polls.values()) stop()
|
|
211
|
+
this.polls.clear()
|
|
212
|
+
try {
|
|
213
|
+
await Promise.all(this.writes)
|
|
214
|
+
} finally {
|
|
215
|
+
await this.storage.close()
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private bind(id: SessionId, urls: readonly string[]): void {
|
|
220
|
+
const next = new Set(urls)
|
|
221
|
+
const previous = this.urlsOf.get(id) ?? new Set<string>()
|
|
222
|
+
this.urlsOf.set(id, next)
|
|
223
|
+
for (const url of previous) {
|
|
224
|
+
if (next.has(url)) continue
|
|
225
|
+
const ids = this.sessionsOf.get(url)
|
|
226
|
+
ids?.delete(id)
|
|
227
|
+
if (ids !== undefined && ids.size === 0) {
|
|
228
|
+
this.sessionsOf.delete(url)
|
|
229
|
+
this.polls.get(url)?.()
|
|
230
|
+
this.polls.delete(url)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
for (const url of next) {
|
|
234
|
+
if (previous.has(url)) continue
|
|
235
|
+
const ids = this.sessionsOf.get(url) ?? new Set<SessionId>()
|
|
236
|
+
ids.add(id)
|
|
237
|
+
this.sessionsOf.set(url, ids)
|
|
238
|
+
const state = this.states.get(url)
|
|
239
|
+
if (state !== undefined) for (const reader of this.readers) if (reader.allowed(id)) reader.push(state)
|
|
240
|
+
this.poll(url, ids)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Poll a bound change request; `ids` is the live set of Sessions binding it, which outlives the poll. */
|
|
245
|
+
private poll(url: string, ids: ReadonlySet<SessionId>): void {
|
|
246
|
+
if (this.polls.has(url)) return
|
|
247
|
+
const ref = this.sources.refOf(url)
|
|
248
|
+
if (ref === undefined) return
|
|
249
|
+
this.polls.set(url, this.sources.watch(ref, (update) => {
|
|
250
|
+
if ('failure' in update) return
|
|
251
|
+
this.settle(url, ids, listStateOf(update.snapshot, url))
|
|
252
|
+
}))
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private settle(url: string, ids: ReadonlySet<SessionId>, state: ForgeChangeRequestListState): void {
|
|
256
|
+
if (this.stopped || sameState(this.states.get(url), state)) return
|
|
257
|
+
this.states.set(url, state)
|
|
258
|
+
this.write(() => this.storage.table.put(storedKey(url), state))
|
|
259
|
+
for (const reader of this.readers) if ([...ids].some(reader.allowed)) reader.push(state)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private write(operation: () => Promise<unknown>): void {
|
|
263
|
+
const task = operation().then(() => undefined, (error: unknown) => { this.sources.report(error) })
|
|
264
|
+
this.writes.add(task)
|
|
265
|
+
void task.then(() => { this.writes.delete(task) })
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Streams the states of the change requests that listed Sessions bind. */
|
|
271
|
+
export class ChangeRequestStates {
|
|
272
|
+
private readonly readers = new Set<Reader>()
|
|
273
|
+
private stopping: Promise<void> = Promise.resolve()
|
|
274
|
+
private disposed = false
|
|
275
|
+
private run: { readonly started: Promise<Run | undefined>; readonly aborter: AbortController } | undefined
|
|
276
|
+
|
|
277
|
+
/** @param sources - what runs read, follow, and poll. */
|
|
278
|
+
constructor(private readonly sources: ChangeRequestStateSources) {}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Stream the known states the reader may see first, then each state that
|
|
282
|
+
* changes or becomes visible to it. The stream ends when `signal` aborts or
|
|
283
|
+
* the hub is disposed.
|
|
284
|
+
* @param allowed - whether the reader may read a Session.
|
|
285
|
+
* @param signal - ends the stream.
|
|
286
|
+
* @returns frames in publication order.
|
|
287
|
+
*/
|
|
288
|
+
async *stream(allowed: (id: SessionId) => boolean, signal: AbortSignal): AsyncIterable<ForgeChangeRequestStatesFrame> {
|
|
289
|
+
signal.throwIfAborted()
|
|
290
|
+
if (this.disposed) return
|
|
291
|
+
const reader = new Reader(allowed, signal)
|
|
292
|
+
this.readers.add(reader)
|
|
293
|
+
try {
|
|
294
|
+
const run = await Promise.race([this.ensureRun(), reader.closed.then(() => undefined)])
|
|
295
|
+
// A run that the hub stopped while it started leaves nothing to read.
|
|
296
|
+
if (run === undefined) return
|
|
297
|
+
yield { type: 'baseline', states: run.visible(allowed) }
|
|
298
|
+
yield* reader.frames()
|
|
299
|
+
} finally {
|
|
300
|
+
reader.dispose()
|
|
301
|
+
this.readers.delete(reader)
|
|
302
|
+
if (this.readers.size === 0) await this.stopRun()
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* End every stream, stop polling, and wait for initialization, accepted writes, and storage closure.
|
|
308
|
+
* @returns settles after all owned work stops; subsequent streams end without starting work.
|
|
309
|
+
*/
|
|
310
|
+
async dispose(): Promise<void> {
|
|
311
|
+
this.disposed = true
|
|
312
|
+
for (const reader of this.readers) reader.end()
|
|
313
|
+
await this.stopRun()
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private ensureRun(): Promise<Run | undefined> {
|
|
317
|
+
if (this.run !== undefined) return this.run.started
|
|
318
|
+
const aborter = new AbortController()
|
|
319
|
+
const preceding = this.stopping
|
|
320
|
+
const started = (async () => {
|
|
321
|
+
await preceding
|
|
322
|
+
aborter.signal.throwIfAborted()
|
|
323
|
+
const storage = await this.sources.open()
|
|
324
|
+
let run: Run | undefined
|
|
325
|
+
try {
|
|
326
|
+
aborter.signal.throwIfAborted()
|
|
327
|
+
run = new Run(this.sources, this.readers, storage)
|
|
328
|
+
await run.start(aborter.signal)
|
|
329
|
+
return run
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (run === undefined) await storage.close()
|
|
332
|
+
else await run.stop()
|
|
333
|
+
throw error
|
|
334
|
+
}
|
|
335
|
+
})()
|
|
336
|
+
this.run = { started, aborter }
|
|
337
|
+
return started
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private stopRun(): Promise<void> {
|
|
341
|
+
const current = this.run
|
|
342
|
+
if (current === undefined) return this.stopping
|
|
343
|
+
this.run = undefined
|
|
344
|
+
current.aborter.abort()
|
|
345
|
+
this.stopping = current.started.then(run => run?.stop(), (error: unknown) => {
|
|
346
|
+
void error // Initialization failure is reported to the stream that awaited it.
|
|
347
|
+
})
|
|
348
|
+
return this.stopping
|
|
349
|
+
}
|
|
350
|
+
}
|