mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-03 22:33:07 +00:00
fix: add abort signal support for graceful store query cancellation
This commit is contained in:
parent
519b335529
commit
ce706bada3
@ -72,6 +72,11 @@ export class StoreCore {
|
||||
|
||||
let currentCursor = queryOpts.paginationCursor;
|
||||
while (true) {
|
||||
if (queryOpts.abortSignal?.aborted) {
|
||||
log.info("Store query aborted by signal");
|
||||
break;
|
||||
}
|
||||
|
||||
const storeQueryRequest = StoreQueryRequest.create({
|
||||
...queryOpts,
|
||||
paginationCursor: currentCursor
|
||||
@ -93,13 +98,22 @@ export class StoreCore {
|
||||
break;
|
||||
}
|
||||
|
||||
const res = await pipe(
|
||||
[storeQueryRequest.encode()],
|
||||
lp.encode,
|
||||
stream,
|
||||
lp.decode,
|
||||
async (source) => await all(source)
|
||||
);
|
||||
let res;
|
||||
try {
|
||||
res = await pipe(
|
||||
[storeQueryRequest.encode()],
|
||||
lp.encode,
|
||||
stream,
|
||||
lp.decode,
|
||||
async (source) => await all(source)
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.name === "AbortError") {
|
||||
log.info(`Store query aborted for peer ${peerId.toString()}`);
|
||||
break;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
const bytes = new Uint8ArrayList();
|
||||
res.forEach((chunk) => {
|
||||
@ -126,6 +140,11 @@ export class StoreCore {
|
||||
`${storeQueryResponse.messages.length} messages retrieved from store`
|
||||
);
|
||||
|
||||
if (queryOpts.abortSignal?.aborted) {
|
||||
log.info("Store query aborted by signal before processing messages");
|
||||
break;
|
||||
}
|
||||
|
||||
const decodedMessages = storeQueryResponse.messages.map((protoMsg) => {
|
||||
if (!protoMsg.message) {
|
||||
return Promise.resolve(undefined);
|
||||
|
||||
@ -88,6 +88,12 @@ export type QueryRequestParams = {
|
||||
* Only use if you know what you are doing.
|
||||
*/
|
||||
peerId?: PeerId;
|
||||
|
||||
/**
|
||||
* An optional AbortSignal to cancel the query.
|
||||
* When the signal is aborted, the query will stop processing and return early.
|
||||
*/
|
||||
abortSignal?: AbortSignal;
|
||||
};
|
||||
|
||||
export type IStore = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user