test query progress
This commit is contained in:
parent
e9fd08d808
commit
8c44d123bd
60
packages/app/src/System/Query.test.ts
Normal file
60
packages/app/src/System/Query.test.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { Connection, RelaySettings } from "@snort/nostr";
|
||||
import { unixNow } from "Util";
|
||||
import { Query } from "./Query";
|
||||
|
||||
describe("query", () => {
|
||||
test("progress", () => {
|
||||
const q = new Query("test", {
|
||||
filters: [
|
||||
{
|
||||
kinds: [1],
|
||||
authors: ["test"],
|
||||
},
|
||||
],
|
||||
started: unixNow(),
|
||||
});
|
||||
const opt = {
|
||||
read: true,
|
||||
write: true,
|
||||
} as RelaySettings;
|
||||
const c1 = new Connection("wss://one.com", opt);
|
||||
c1.Down = false;
|
||||
const c2 = new Connection("wss://two.com", opt);
|
||||
c2.Down = false;
|
||||
const c3 = new Connection("wss://three.com", opt);
|
||||
c3.Down = false;
|
||||
|
||||
q.sendToRelay(c1);
|
||||
q.sendToRelay(c2);
|
||||
q.sendToRelay(c3);
|
||||
|
||||
expect(q.progress).toBe(0);
|
||||
q.eose(q.id, c1.Address);
|
||||
expect(q.progress).toBe(1 / 3);
|
||||
q.eose(q.id, c1.Address);
|
||||
expect(q.progress).toBe(1 / 3);
|
||||
q.eose(q.id, c2.Address);
|
||||
expect(q.progress).toBe(2 / 3);
|
||||
q.eose(q.id, c3.Address);
|
||||
expect(q.progress).toBe(1);
|
||||
|
||||
const qs = new Query("test-1", {
|
||||
filters: [
|
||||
{
|
||||
kinds: [1],
|
||||
authors: ["test-sub"],
|
||||
},
|
||||
],
|
||||
started: unixNow(),
|
||||
});
|
||||
q.subQueries.push(qs);
|
||||
qs.sendToRelay(c1);
|
||||
|
||||
expect(q.progress).toBe(0.5);
|
||||
q.eose(qs.id, c1.Address);
|
||||
expect(q.progress).toBe(1);
|
||||
qs.sendToRelay(c2);
|
||||
// 1 + 0.5 (1/2 sent sub query)
|
||||
expect(q.progress).toBe(1.5 / 2);
|
||||
});
|
||||
});
|
@ -102,6 +102,8 @@ export class Query {
|
||||
const subQ = this.subQueries.find(a => a.id === sub);
|
||||
if (subQ) {
|
||||
subQ.eose(sub, relay);
|
||||
} else {
|
||||
throw new Error("No query found");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -110,7 +112,10 @@ export class Query {
|
||||
* Get the progress to EOSE, can be used to determine when we should load more content
|
||||
*/
|
||||
get progress() {
|
||||
const thisProgress = this.#eoseRelays.size / this.#sentToRelays.reduce((acc, v) => (acc += v.Down ? 0 : 1), 0);
|
||||
let thisProgress = this.#eoseRelays.size / this.#sentToRelays.reduce((acc, v) => (acc += v.Down ? 0 : 1), 0);
|
||||
if (isNaN(thisProgress)) {
|
||||
thisProgress = 0;
|
||||
}
|
||||
if (this.subQueries.length === 0) {
|
||||
return thisProgress;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user