beads-ui 0.2.0 → 0.3.1

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.
Files changed (58) hide show
  1. package/CHANGES.md +14 -0
  2. package/README.md +4 -4
  3. package/app/data/list-selectors.js +103 -0
  4. package/app/data/providers.js +7 -138
  5. package/app/data/sort.js +47 -0
  6. package/app/data/subscription-issue-store.js +161 -0
  7. package/app/data/subscription-issue-stores.js +128 -0
  8. package/app/data/subscriptions-store.js +227 -0
  9. package/app/main.js +346 -66
  10. package/app/protocol.js +23 -17
  11. package/app/protocol.md +18 -15
  12. package/app/router.js +3 -0
  13. package/app/state.js +2 -0
  14. package/app/styles.css +222 -197
  15. package/app/utils/issue-id-renderer.js +2 -1
  16. package/app/utils/issue-id.js +1 -0
  17. package/app/utils/issue-type.js +2 -0
  18. package/app/utils/issue-url.js +1 -0
  19. package/app/utils/markdown.js +13 -198
  20. package/app/utils/priority-badge.js +1 -2
  21. package/app/utils/status-badge.js +1 -1
  22. package/app/utils/status.js +2 -0
  23. package/app/utils/toast.js +1 -1
  24. package/app/utils/type-badge.js +1 -3
  25. package/app/views/board.js +172 -148
  26. package/app/views/detail.js +79 -66
  27. package/app/views/epics.js +127 -74
  28. package/app/views/issue-dialog.js +9 -15
  29. package/app/views/issue-row.js +2 -3
  30. package/app/views/list.js +105 -104
  31. package/app/views/nav.js +1 -0
  32. package/app/views/new-issue-dialog.js +30 -34
  33. package/app/ws.js +10 -10
  34. package/bin/bdui.js +1 -1
  35. package/docs/adr/001-push-only-lists.md +134 -0
  36. package/docs/adr/002-per-subscription-stores-and-full-issue-push.md +200 -0
  37. package/docs/architecture.md +34 -84
  38. package/docs/data-exchange-subscription-plan.md +198 -0
  39. package/docs/db-watching.md +2 -1
  40. package/docs/migration-v2.md +54 -0
  41. package/docs/protocol/issues-push-v2.md +179 -0
  42. package/docs/subscription-issue-store.md +112 -0
  43. package/package.json +5 -4
  44. package/server/app.js +2 -0
  45. package/server/bd.js +4 -2
  46. package/server/cli/commands.js +5 -2
  47. package/server/cli/daemon.js +19 -5
  48. package/server/cli/index.js +2 -2
  49. package/server/cli/open.js +3 -0
  50. package/server/cli/usage.js +2 -1
  51. package/server/config.js +13 -6
  52. package/server/db.js +3 -1
  53. package/server/index.js +9 -5
  54. package/server/list-adapters.js +224 -0
  55. package/server/subscriptions.js +289 -0
  56. package/server/validators.js +113 -0
  57. package/server/watcher.js +8 -8
  58. package/server/ws.js +457 -229
@@ -0,0 +1,227 @@
1
+ /**
2
+ * @import { MessageType } from '../protocol.js'
3
+ */
4
+ /**
5
+ * Client-side list subscription store.
6
+ *
7
+ * Maintains per-subscription state keyed by client-provided `id`.
8
+ * Applies server `list-delta` events per subscription key and exposes simple
9
+ * selectors for rendering.
10
+ */
11
+
12
+ /**
13
+ * @typedef {{ type: string, params?: Record<string, string|number|boolean> }} SubscriptionSpec
14
+ */
15
+
16
+ /**
17
+ * Generate a stable subscription key string from a spec.
18
+ * Mirrors server `keyOf` implementation (sorted params, URLSearchParams).
19
+ *
20
+ * @param {SubscriptionSpec} spec
21
+ * @returns {string}
22
+ */
23
+ export function subKeyOf(spec) {
24
+ const type = String(spec.type || '').trim();
25
+ /** @type {Record<string, string>} */
26
+ const flat = {};
27
+ if (spec.params && typeof spec.params === 'object') {
28
+ const keys = Object.keys(spec.params).sort();
29
+ for (const k of keys) {
30
+ const v = spec.params[k];
31
+ flat[k] = String(v);
32
+ }
33
+ }
34
+ const enc = new URLSearchParams(flat).toString();
35
+ return enc.length > 0 ? `${type}?${enc}` : type;
36
+ }
37
+
38
+ /**
39
+ * Create a list subscription store.
40
+ *
41
+ * Wiring:
42
+ * - Use `subscribeList` to register a subscription and send the request.
43
+ *
44
+ * Selectors are synchronous and return derived state by client id.
45
+ *
46
+ * @param {(type: MessageType, payload?: unknown) => Promise<unknown>} send - ws send.
47
+ */
48
+ export function createSubscriptionStore(send) {
49
+ /** @type {Map<string, { key: string, itemsById: Map<string, true> }>} */
50
+ const subs_by_id = new Map();
51
+ /** @type {Map<string, Set<string>>} */
52
+ const ids_by_key = new Map();
53
+
54
+ /**
55
+ * Apply a delta to all client ids mapped to a given key.
56
+ *
57
+ * @param {string} key
58
+ * @param {{ added: string[], updated: string[], removed: string[] }} delta
59
+ */
60
+ function applyDelta(key, delta) {
61
+ const id_set = ids_by_key.get(key);
62
+ if (!id_set || id_set.size === 0) {
63
+ return;
64
+ }
65
+ const added = Array.isArray(delta.added) ? delta.added : [];
66
+ const updated = Array.isArray(delta.updated) ? delta.updated : [];
67
+ const removed = Array.isArray(delta.removed) ? delta.removed : [];
68
+
69
+ for (const client_id of Array.from(id_set)) {
70
+ const entry = subs_by_id.get(client_id);
71
+ if (!entry) {
72
+ continue;
73
+ }
74
+ const items = entry.itemsById;
75
+ for (const id of added) {
76
+ if (typeof id === 'string' && id.length > 0) {
77
+ items.set(id, true);
78
+ }
79
+ }
80
+ for (const id of updated) {
81
+ if (typeof id === 'string' && id.length > 0) {
82
+ items.set(id, true);
83
+ }
84
+ }
85
+ for (const id of removed) {
86
+ if (typeof id === 'string' && id.length > 0) {
87
+ items.delete(id);
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Subscribe to a list spec with a client-provided id.
95
+ * Returns an unsubscribe function.
96
+ * Creates an empty items store immediately; server will publish deltas.
97
+ *
98
+ * @param {string} client_id
99
+ * @param {SubscriptionSpec} spec
100
+ * @returns {Promise<() => Promise<void>>}
101
+ */
102
+ async function subscribeList(client_id, spec) {
103
+ const key = subKeyOf(spec);
104
+ // Initialize local entry immediately to capture early deltas
105
+ if (!subs_by_id.has(client_id)) {
106
+ subs_by_id.set(client_id, { key, itemsById: new Map() });
107
+ } else {
108
+ // Update key mapping if client id is reused for a different spec
109
+ const prev = subs_by_id.get(client_id);
110
+ if (prev && prev.key !== key) {
111
+ const prev_ids = ids_by_key.get(prev.key);
112
+ if (prev_ids) {
113
+ prev_ids.delete(client_id);
114
+ if (prev_ids.size === 0) {
115
+ ids_by_key.delete(prev.key);
116
+ }
117
+ }
118
+ subs_by_id.set(client_id, { key, itemsById: new Map() });
119
+ }
120
+ }
121
+ if (!ids_by_key.has(key)) {
122
+ ids_by_key.set(key, new Set());
123
+ }
124
+ const set = ids_by_key.get(key);
125
+ if (set) {
126
+ set.add(client_id);
127
+ }
128
+ try {
129
+ await send('subscribe-list', {
130
+ id: client_id,
131
+ type: spec.type,
132
+ params: spec.params
133
+ });
134
+ } catch {
135
+ // keep local mapping to allow retries; caller may unsubscribe or retry
136
+ }
137
+
138
+ return async () => {
139
+ try {
140
+ await send('unsubscribe-list', { id: client_id });
141
+ } catch {
142
+ // ignore transport errors on unsubscribe
143
+ }
144
+ // Cleanup local mappings
145
+ const entry = subs_by_id.get(client_id) || null;
146
+ if (entry) {
147
+ const s = ids_by_key.get(entry.key);
148
+ if (s) {
149
+ s.delete(client_id);
150
+ if (s.size === 0) {
151
+ ids_by_key.delete(entry.key);
152
+ }
153
+ }
154
+ }
155
+ subs_by_id.delete(client_id);
156
+ };
157
+ }
158
+
159
+ /**
160
+ * Selectors by client id.
161
+ */
162
+ const selectors = {
163
+ /**
164
+ * Get an array of item ids for a subscription.
165
+ *
166
+ * @param {string} client_id
167
+ * @returns {string[]}
168
+ */
169
+ getIds(client_id) {
170
+ const entry = subs_by_id.get(client_id);
171
+ if (!entry) {
172
+ return [];
173
+ }
174
+ return Array.from(entry.itemsById.keys());
175
+ },
176
+ /**
177
+ * Check if an id exists in a subscription.
178
+ *
179
+ * @param {string} client_id
180
+ * @param {string} id
181
+ * @returns {boolean}
182
+ */
183
+ has(client_id, id) {
184
+ const entry = subs_by_id.get(client_id);
185
+ if (!entry) {
186
+ return false;
187
+ }
188
+ return entry.itemsById.has(id);
189
+ },
190
+ /**
191
+ * Count items for a subscription.
192
+ *
193
+ * @param {string} client_id
194
+ * @returns {number}
195
+ */
196
+ count(client_id) {
197
+ const entry = subs_by_id.get(client_id);
198
+ return entry ? entry.itemsById.size : 0;
199
+ },
200
+ /**
201
+ * Return a shallow object copy `{ [id]: true }` for rendering helpers.
202
+ *
203
+ * @param {string} client_id
204
+ * @returns {Record<string, true>}
205
+ */
206
+ getItemsById(client_id) {
207
+ const entry = subs_by_id.get(client_id);
208
+ /** @type {Record<string, true>} */
209
+ const out = {};
210
+ if (!entry) {
211
+ return out;
212
+ }
213
+ for (const id of entry.itemsById.keys()) {
214
+ out[id] = true;
215
+ }
216
+ return out;
217
+ }
218
+ };
219
+
220
+ return {
221
+ subscribeList,
222
+ // test/diagnostics helpers
223
+ _applyDelta: applyDelta,
224
+ _subKeyOf: subKeyOf,
225
+ selectors
226
+ };
227
+ }