feat: replaceable query filters

This commit is contained in:
2024-07-17 16:44:35 +01:00
parent 24a72529c2
commit 44a014b8c6
5 changed files with 37 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@snort/system-react", "name": "@snort/system-react",
"version": "1.4.0", "version": "1.4.1",
"description": "React hooks for @snort/system", "description": "React hooks for @snort/system",
"main": "dist/index.js", "main": "dist/index.js",
"module": "src/index.ts", "module": "src/index.ts",
@ -17,7 +17,7 @@
], ],
"dependencies": { "dependencies": {
"@snort/shared": "^1.0.16", "@snort/shared": "^1.0.16",
"@snort/system": "^1.4.0", "@snort/system": "^1.4.1",
"react": "^18.2.0" "react": "^18.2.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@snort/system", "name": "@snort/system",
"version": "1.4.0", "version": "1.4.1",
"description": "Snort nostr system package", "description": "Snort nostr system package",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -24,15 +24,17 @@ export class QueryTrace extends EventEmitter<QueryTraceEvents> {
eose?: number; eose?: number;
close?: number; close?: number;
#wasForceClosed = false; #wasForceClosed = false;
filters: Array<ReqFilter>;
constructor( constructor(
readonly relay: string, readonly relay: string,
readonly filters: Array<ReqFilter>, filters: Array<ReqFilter>,
readonly connId: string, readonly connId: string,
) { ) {
super(); super();
this.id = uuid(); this.id = uuid();
this.start = unixNowMs(); this.start = unixNowMs();
this.filters = filters;
} }
sentToRelay() { sentToRelay() {
@ -153,6 +155,11 @@ export class Query extends EventEmitter<QueryEvents> {
*/ */
#groupTimeout?: ReturnType<typeof setTimeout>; #groupTimeout?: ReturnType<typeof setTimeout>;
/**
* If the query should only every replace a previous trace on the same connection
*/
#replaceable: boolean = false;
#log = debug("Query"); #log = debug("Query");
constructor(req: RequestBuilder) { constructor(req: RequestBuilder) {
@ -162,6 +169,7 @@ export class Query extends EventEmitter<QueryEvents> {
this.#leaveOpen = req.options?.leaveOpen ?? false; this.#leaveOpen = req.options?.leaveOpen ?? false;
this.#timeout = req.options?.timeout ?? 5_000; this.#timeout = req.options?.timeout ?? 5_000;
this.#groupingDelay = req.options?.groupingDelay ?? 100; this.#groupingDelay = req.options?.groupingDelay ?? 100;
this.#replaceable = req.options?.replaceable ?? false;
this.#checkTraces(); this.#checkTraces();
this.requests.push(...req.buildRaw()); this.requests.push(...req.buildRaw());
@ -315,6 +323,9 @@ export class Query extends EventEmitter<QueryEvents> {
this.#log("Starting emit of %s", this.id); this.#log("Starting emit of %s", this.id);
let rawFilters = [...this.requests]; let rawFilters = [...this.requests];
this.requests = []; this.requests = [];
if (this.#replaceable) {
rawFilters.push(...this.filters);
}
this.emit("request", this.id, rawFilters); this.emit("request", this.id, rawFilters);
} }
@ -363,11 +374,10 @@ export class Query extends EventEmitter<QueryEvents> {
return true; return true;
} }
#sendQueryInternal(c: ConnectionType, q: BuiltRawReqFilter) { #setupNewTrace(c: ConnectionType, q: BuiltRawReqFilter) {
let filters = q.filters; const qt = new QueryTrace(c.address, q.filters, c.id);
const qt = new QueryTrace(c.address, filters, c.id);
qt.on("close", x => c.closeRequest(x)); qt.on("close", x => c.closeRequest(x));
qt.on("eose", (id, connId, forced) => { qt.on("eose", (id, _connId, forced) => {
this.emit("trace", { this.emit("trace", {
id, id,
conn: c, conn: c,
@ -400,6 +410,16 @@ export class Query extends EventEmitter<QueryEvents> {
c.off("closed", eoseHandler); c.off("closed", eoseHandler);
}); });
this.#tracing.push(qt); this.#tracing.push(qt);
return qt;
}
#sendQueryInternal(c: ConnectionType, q: BuiltRawReqFilter) {
const qt = this.#replaceable
? this.#tracing.find(a => a.connId === c.id) ?? this.#setupNewTrace(c, q)
: this.#setupNewTrace(c, q);
//always replace filters array
qt.filters = [...q.filters];
if (q.syncFrom !== undefined) { if (q.syncFrom !== undefined) {
c.request(["SYNC", qt.id, q.syncFrom, ...qt.filters], () => qt.sentToRelay()); c.request(["SYNC", qt.id, q.syncFrom, ...qt.filters], () => qt.sentToRelay());

View File

@ -36,6 +36,13 @@ export interface RequestBuilderOptions {
* How many milli-seconds to wait to allow grouping * How many milli-seconds to wait to allow grouping
*/ */
groupingDelay?: number; groupingDelay?: number;
/**
* Replace the query every time a change in the query is detected
*
* eg. Live stream chat reactions
*/
replaceable?: boolean;
} }
/** /**

View File

@ -1,6 +1,6 @@
{ {
"name": "@snort/wallet", "name": "@snort/wallet",
"version": "0.1.6", "version": "0.1.7",
"description": "Snort wallet system package", "description": "Snort wallet system package",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
@ -23,7 +23,7 @@
"@lightninglabs/lnc-web": "^0.3.1-alpha", "@lightninglabs/lnc-web": "^0.3.1-alpha",
"@scure/base": "^1.1.6", "@scure/base": "^1.1.6",
"@snort/shared": "^1.0.16", "@snort/shared": "^1.0.16",
"@snort/system": "^1.3.8", "@snort/system": "^1.4.1",
"debug": "^4.3.4", "debug": "^4.3.4",
"eventemitter3": "^5.0.1" "eventemitter3": "^5.0.1"
}, },