Skip to content

Commit

Permalink
refactor(rstream-query): simplify addQuery()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 23, 2018
1 parent a4aa4cb commit 16fabb8
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions packages/rstream-query/src/graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { equiv } from "@thi.ng/api/equiv";
// import { SortedMap } from "@thi.ng/associative/sorted-map";
import { intersection } from "@thi.ng/associative/intersection";
import { Stream, trace, Subscription, sync } from "@thi.ng/rstream";
import { Transducer, Reducer } from "@thi.ng/transducers/api";
Expand Down Expand Up @@ -60,15 +59,9 @@ export class FactGraph {
}

addQuery(id: string, [s, p, o]: Pattern) {
const qs: Subscription<any, Set<number>> = s != null ?
this.streamS.transform(indexSel(s), "s") :
this.streamAll.subscribe(null, "s");
const qp: Subscription<any, Set<number>> = p != null ?
this.streamP.transform(indexSel(p), "p") :
this.streamAll.subscribe(null, "p");
const qo: Subscription<any, Set<number>> = o != null ?
this.streamO.transform(indexSel(o), "o") :
this.streamAll.subscribe(null, "o");
const qs: Subscription<any, Set<number>> = this.getIndexSelection(this.streamS, s, "s");
const qp: Subscription<any, Set<number>> = this.getIndexSelection(this.streamP, p, "p");
const qo: Subscription<any, Set<number>> = this.getIndexSelection(this.streamO, o, "o");
const results = sync<Set<number>, Set<Fact>>({
id,
src: [qs, qp, qo],
Expand Down Expand Up @@ -111,6 +104,12 @@ export class FactGraph {
}
return -1;
}

protected getIndexSelection(stream: Stream<Edit>, key: any, id: string): Subscription<any, Set<number>> {
return key != null ?
stream.transform(indexSel(key), id) :
this.streamAll.subscribe(null, id);
}
}

export const indexSel = (key: any): Transducer<Edit, Set<number>> =>
Expand Down

0 comments on commit 16fabb8

Please sign in to comment.