Skip to content

Commit bb5b91f

Browse files
committed
data readiness
1 parent ae45529 commit bb5b91f

File tree

7 files changed

+280
-168
lines changed

7 files changed

+280
-168
lines changed

special-pages/pages/history/app/global-state/DataProvider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const DataState = createContext({
2323
*
2424
* @param {Object} props
2525
* @param {import('../history.service.js').HistoryService} props.service - An instance of the history service to manage state updates.
26-
* @param {import('../history.service.js').ServiceData} props.initial - The initial state data for the history service.
26+
* @param {import('../history.service.js').InitialServiceData} props.initial - The initial state data for the history service.
2727
* @param {import('preact').ComponentChildren} props.children
2828
*/
2929
export function DataProvider({ service, initial, children }) {

special-pages/pages/history/app/global-state/HistoryServiceProvider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function useRangeChange(service) {
135135
const dispatch = useHistoryServiceDispatch();
136136
useSignalEffect(() => {
137137
function handler(/** @type {CustomEvent<{start: number, end: number}>} */ event) {
138-
if (!service.query.data) throw new Error('unreachable');
138+
if (!service.query) throw new Error('unreachable');
139139
const { end } = event.detail;
140140
dispatch({ kind: 'request-more', end });
141141
}

special-pages/pages/history/app/global-state/SelectionProvider.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export function useRowInteractions() {
235235
break;
236236
}
237237
case 'delete': {
238+
if (selected.value.size === 0) break;
238239
historyDispatch({ kind: 'delete-entries-by-index', value: [...selected.value] });
239240
break;
240241
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @typedef {import('../types/history.js').Range} Range
3+
* @typedef {{ranges: Range[]}} RangeData
4+
* @typedef {{kind: 'none'} | { kind: 'domain-search'; value: string }} MenuContinuation
5+
*/
6+
7+
export class HistoryRangeService {
8+
data = new EventTarget();
9+
10+
/**
11+
* @type {RangeData|null}
12+
*/
13+
ranges = null;
14+
15+
/**
16+
* @param {import("../src/index.js").HistoryPage} history
17+
*/
18+
constructor(history) {
19+
this.history = history;
20+
}
21+
22+
/**
23+
* @param {RangeData} d
24+
*/
25+
accept(d) {
26+
this.ranges = d;
27+
this.data.dispatchEvent(new Event('data'));
28+
}
29+
30+
/**
31+
* @returns {Promise<RangeData>}
32+
*/
33+
async getInitial() {
34+
const rangesPromise = await this.history.messaging.request('getRanges');
35+
this.accept(rangesPromise);
36+
return rangesPromise;
37+
}
38+
39+
/**
40+
* @param {(data: RangeData) => void} cb
41+
*/
42+
onResults(cb) {
43+
const controller = new AbortController();
44+
this.data.addEventListener(
45+
'data',
46+
() => {
47+
if (this.ranges === null) throw new Error('unreachable');
48+
cb(this.ranges);
49+
},
50+
{ signal: controller.signal },
51+
);
52+
return () => controller.abort();
53+
}
54+
55+
/**
56+
* @param {(d: RangeData) => RangeData} updater
57+
*/
58+
update(updater) {
59+
if (this.ranges === null) throw new Error('unreachable');
60+
this.accept(updater(this.ranges));
61+
}
62+
63+
/**
64+
* @param {Range} range
65+
*/
66+
async deleteRange(range) {
67+
console.log('📤 [deleteRange]: ', JSON.stringify({ range }));
68+
const resp = await this.history.messaging.request('deleteRange', { range });
69+
if (resp.action === 'delete') {
70+
if (range === 'all') {
71+
this.update((_old) => {
72+
return {
73+
ranges: ['all'],
74+
};
75+
});
76+
} else {
77+
this.update((old) => {
78+
return {
79+
...old,
80+
ranges: old.ranges.filter((x) => x !== range),
81+
};
82+
});
83+
}
84+
}
85+
return resp;
86+
}
87+
}

0 commit comments

Comments
 (0)