annotate src/Patch.ts @ 141:27fad20dc101

rm logging
author drewp@bigasterisk.com
date Mon, 08 May 2023 13:05:59 -0700
parents cf642d395be4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
1 import { Quad, Store } from "n3";
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
2 import { Stream } from "rdf-js";
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
3
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
4 export enum PatchDirection {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
5 ADD = "+",
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
6 DEL = "-",
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
7 }
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
8
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
9 export class Patch {
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
10 quads: Quad[] = [];
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
11
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
12 constructor(public direction: PatchDirection) {}
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
13
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
14 toString(): string {
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
15 return `Patch ${this.direction} ${this.quads.length}`;
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
16 }
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
17
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
18 public async streamImport(quadStream: Stream): Promise<void> {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
19 return new Promise((resolve, reject) => {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
20 quadStream.on("data", (quad) => {
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
21 this.quads.push(quad);
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
22 });
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
23 quadStream.on("error", reject);
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
24 quadStream.on("end", resolve);
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
25 });
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
26 }
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
27
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
28 public applyToStore(s: Store) {
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
29 if (this.direction == PatchDirection.ADD) {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
30 s.addQuads(this.quads);
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
31 } else {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
32 s.removeQuads(this.quads);
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
33 }
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
34 }
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
35 public isEmpty(): boolean {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
36 return this.quads.length == 0;
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
37 }
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents:
diff changeset
38 }