Enforce check interval correctly
This commit is contained in:
+28
-28
@@ -69,29 +69,29 @@ async function waitForEvent(
|
||||
const elapsed = Date.now() - started;
|
||||
const remainingMs = Math.max(1, timeoutMs - elapsed);
|
||||
const queryTimeoutMs = Math.max(200, Math.min(1_000, remainingMs));
|
||||
const found = await withTimeout<boolean>(
|
||||
new Promise<boolean>((resolve) => {
|
||||
let settled = false;
|
||||
const sub = relay.subscribe([filter], {
|
||||
onevent: (evt) => {
|
||||
if (settled) return;
|
||||
if (evt.id === expectedEventId) {
|
||||
settled = true;
|
||||
sub.close();
|
||||
resolve(true);
|
||||
}
|
||||
},
|
||||
oneose: () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
sub.close();
|
||||
resolve(false);
|
||||
},
|
||||
});
|
||||
}),
|
||||
queryTimeoutMs,
|
||||
"write confirm read-after-write query",
|
||||
).catch(() => false);
|
||||
const found = await new Promise<boolean>((resolve) => {
|
||||
let settled = false;
|
||||
let sub: ReturnType<Relay["subscribe"]> | undefined;
|
||||
const finish = (found: boolean): void => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
sub?.close();
|
||||
resolve(found);
|
||||
};
|
||||
const timer = setTimeout(() => finish(false), queryTimeoutMs);
|
||||
|
||||
sub = relay.subscribe([filter], {
|
||||
onevent: (evt) => {
|
||||
if (evt.id === expectedEventId) {
|
||||
finish(true);
|
||||
}
|
||||
},
|
||||
oneose: () => {
|
||||
finish(false);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (found) return true;
|
||||
|
||||
@@ -142,20 +142,20 @@ export async function runWriteConfirm(input: WriteConfirmInput): Promise<WriteCo
|
||||
};
|
||||
}
|
||||
|
||||
const elapsedMs = performance.now() - startedAt;
|
||||
const remainingMs = Math.max(1, input.timeoutMs - Math.floor(elapsedMs));
|
||||
|
||||
const confirmed = await waitForEvent(
|
||||
relay,
|
||||
{
|
||||
ids: [event.id],
|
||||
authors: [derivedPubkey],
|
||||
kinds: [input.kind],
|
||||
"#d": [dTag],
|
||||
limit: 1,
|
||||
since: Math.max(0, createdAt - 5),
|
||||
},
|
||||
event.id,
|
||||
remainingMs,
|
||||
// Connect and publish each have their own timeout, so read-after-write
|
||||
// must receive one too. Charging all three stages to a single timeout
|
||||
// can reduce this to 1 ms after a slow, successful publish.
|
||||
input.timeoutMs,
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user