annotate web/patch.ts @ 2408:7e7874fed2e3

buttons to add panels to the layout
author drewp@bigasterisk.com
date Sat, 18 May 2024 21:02:32 -0700
parents 4556eebe5d73
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2091
9324fc8285ad Effect repairs duplicate :settings edges when it finds them
drewp@bigasterisk.com
parents: 2071
diff changeset
1 import * as async from "async";
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
2 import debug from "debug";
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
3 import * as N3 from "n3";
2091
9324fc8285ad Effect repairs duplicate :settings edges when it finds them
drewp@bigasterisk.com
parents: 2071
diff changeset
4 import { NamedNode, Parser, Quad, Writer } from "n3";
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
5 import * as Immutable from "immutable";
2260
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
6 export interface QuadPattern {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
7 subject: N3.Quad_Subject | null;
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
8 predicate: N3.Quad_Predicate | null;
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
9 object: N3.Quad_Object | null; // literals allowed? needs review. probably 'yes'.
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
10 graph: N3.Quad_Graph | null;
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
11 }
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
12
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
13 const log = debug("patch");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
14
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
15 export class Patch {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
16 // immutable
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
17 private dels: Immutable.Set<Quad>;
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
18 private adds: Immutable.Set<Quad>;
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
19 private _allPredsCache?: Immutable.Set<string>;
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
20 private _allSubjsCache?: Immutable.Set<string>;
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
21 constructor(dels: Iterable<Quad>, adds: Iterable<Quad>) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
22 this.dels = Immutable.Set(dels);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
23 this.adds = Immutable.Set(adds);
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
24 this.validate();
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
25 }
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
26
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
27 private validate() {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
28 // todo: finish porting this from coffeescript
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
29 this.adds.union(this.dels).forEach((q: Quad) => {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
30 if (!q.equals) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
31 throw new Error("doesn't look like a proper Quad");
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
32 }
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
33 if (!q.subject.id || q.graph.id == null || q.predicate.id == null) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
34 throw new Error(`corrupt patch: ${JSON.stringify(q)}`);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
35 }
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
36 if (
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
37 q.object.termType == "Literal" &&
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
38 (q.object.datatypeString == "http://www.w3.org/2001/XMLSchema#float" || q.object.datatypeString == "http://www.w3.org/2001/XMLSchema#double")
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
39 ) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
40 throw new Error(`${JSON.stringify(q)} is using non-decimal for numbers, which is going to break some comparisons`);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
41 }
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
42 });
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
43 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
44
2260
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
45 matches(pat: QuadPattern): boolean {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
46 const allQuads = this.dels.concat(this.adds);
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
47 return allQuads.some((quad) => {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
48 return (
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
49 (pat.subject === null || pat.subject.equals(quad.subject)) && //
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
50 (pat.predicate === null || pat.predicate.equals(quad.predicate)) && //
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
51 (pat.object === null || pat.object.equals(quad.object)) && //
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
52 (pat.graph === null || pat.graph.equals(quad.graph))
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
53 );
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
54 });
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
55 }
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2258
diff changeset
56
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
57 isEmpty() {
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
58 return Immutable.is(this.dels, this.adds);
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
59 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
60
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
61 applyToGraph(g: N3.Store) {
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
62 for (let quad of this.dels) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
63 g.removeQuad(quad);
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
64 }
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
65 for (let quad of this.adds) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
66 g.addQuad(quad);
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
67 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
68 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
69
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
70 update(other: Patch): Patch {
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
71 // this is approx, since it doesnt handle cancelling existing quads.
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
72 return new Patch(this.dels.union(other.dels), this.adds.union(other.adds));
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
73 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
74
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
75 summary(): string {
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
76 return "-" + this.dels.size + " +" + this.adds.size;
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
77 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
78
2268
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
79 dump(): string {
2322
cdfd2901918a logging
drewp@bigasterisk.com
parents: 2277
diff changeset
80 if (this.dels.size + this.adds.size > 20) {
cdfd2901918a logging
drewp@bigasterisk.com
parents: 2277
diff changeset
81 return this.summary();
cdfd2901918a logging
drewp@bigasterisk.com
parents: 2277
diff changeset
82 }
2268
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
83 const lines: string[] = [];
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
84 const s = (term: N3.Term): string => {
2273
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
85 if (term.termType == "Literal") return term.value;
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
86 if (term.termType == "NamedNode")
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
87 return term.value
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
88 .replace("http://light9.bigasterisk.com/effect/", "effect:")
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
89 .replace("http://light9.bigasterisk.com/", ":")
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
90 .replace("http://www.w3.org/2000/01/rdf-schema#", "rdfs:")
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
91 .replace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:");
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
92 if (term.termType == "BlankNode") return "_:" + term.value;
2268
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
93 return term.id;
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
94 };
2273
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
95 const delPrefix = "- ",
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
96 addPrefix = "\u200B+ "; // dels to sort before adds
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
97 this.dels.forEach((d) => lines.push(delPrefix + s(d.subject) + " " + s(d.predicate) + " " + s(d.object)));
4074dbec5c46 logging
drewp@bigasterisk.com
parents: 2268
diff changeset
98 this.adds.forEach((d) => lines.push(addPrefix + s(d.subject) + " " + s(d.predicate) + " " + s(d.object)));
2268
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
99 lines.sort();
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2274
diff changeset
100 return lines.join("\n") + "\n" + (this.isEmpty() ? "(empty)" : "(nonempty)");
2268
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
101 }
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2267
diff changeset
102
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
103 async toJsonPatch(): Promise<string> {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
104 return new Promise((res, rej) => {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
105 const out: SyncgraphPatchMessage = { patch: { adds: "", deletes: "" } };
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
106
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
107 const writeDels = (cb1: () => void) => {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
108 const writer = new Writer({ format: "N-Quads" });
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
109 writer.addQuads(this.dels.toArray());
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
110 writer.end(function (err: any, result: string) {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
111 out.patch.deletes = result;
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
112 cb1();
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
113 });
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
114 };
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
115
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
116 const writeAdds = (cb2: () => void) => {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
117 const writer = new Writer({ format: "N-Quads" });
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
118 writer.addQuads(this.adds.toArray());
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
119 writer.end(function (err: any, result: string) {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
120 out.patch.adds = result;
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
121 cb2();
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
122 });
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
123 };
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
124
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
125 async.parallel([writeDels, writeAdds], (err: any) => res(JSON.stringify(out)));
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
126 });
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
127 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
128
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
129 containsAnyPreds(preds: Iterable<NamedNode>): boolean {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
130 if (this._allPredsCache === undefined) {
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
131 this._allPredsCache = Immutable.Set();
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
132 this._allPredsCache.withMutations((cache) => {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
133 for (let qq of [this.adds, this.dels]) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
134 for (let q of Array.from(qq)) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
135 cache.add(q.predicate.value);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
136 }
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
137 }
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
138 });
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
139 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
140
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
141 for (let p of preds) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
142 if (this._allPredsCache.has(p.value)) {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
143 return true;
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
144 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
145 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
146 return false;
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
147 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
148
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
149 allSubjs(): Immutable.Set<string> {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
150 // returns subjs as Set of strings
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
151 if (this._allSubjsCache === undefined) {
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
152 this._allSubjsCache = Immutable.Set();
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
153 this._allSubjsCache.withMutations((cache) => {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
154 for (let qq of [this.adds, this.dels]) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
155 for (let q of Array.from(qq)) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
156 cache.add(q.subject.value);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
157 }
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
158 }
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
159 });
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
160 }
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
161
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
162 return this._allSubjsCache;
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
163 }
2267
7796bc3e46b1 cleanup
drewp@bigasterisk.com
parents: 2260
diff changeset
164
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
165 allPreds(): Immutable.Set<NamedNode> {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
166 // todo: this could cache
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
167 const ret = Immutable.Set<NamedNode>();
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
168 ret.withMutations((r) => {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
169 for (let qq of [this.adds, this.dels]) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
170 for (let q of Array.from(qq)) {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
171 if (q.predicate.termType == "Variable") throw "unsupported";
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
172 r.add(q.predicate);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
173 }
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
174 }
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2273
diff changeset
175 });
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
176 return ret;
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
177 }
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
178 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
179
2267
7796bc3e46b1 cleanup
drewp@bigasterisk.com
parents: 2260
diff changeset
180 // The schema of the json sent from graph server.
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
181 export interface SyncgraphPatchMessage {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
182 patch: { adds: string; deletes: string };
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
183 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
184
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
185 export function patchToDeleteEntireGraph(g: N3.Store) {
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
186 return new Patch(g.getQuads(null, null, null, null), []);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
187 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
188
2108
e92db17f3e7e effectSequencer can now also process some note-like values coming from the fade/ ui
drewp@bigasterisk.com
parents: 2091
diff changeset
189 export function parseJsonPatch(input: SyncgraphPatchMessage, cb: (p: Patch) => void): void {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
190 // note response cb doesn't have an error arg.
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
191 const dels: Quad[] = [];
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
192 const adds: Quad[] = [];
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
193
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
194 const parseAdds = (cb2: () => any) => {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
195 const parser = new Parser();
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
196 return parser.parse(input.patch.adds, (error: any, quad: Quad, prefixes: any) => {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
197 if (quad) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
198 return adds.push(quad);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
199 } else {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
200 return cb2();
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
201 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
202 });
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
203 };
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
204 const parseDels = (cb3: () => any) => {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
205 const parser = new Parser();
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
206 return parser.parse(input.patch.deletes, (error: any, quad: any, prefixes: any) => {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
207 if (quad) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
208 return dels.push(quad);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
209 } else {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
210 return cb3();
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
211 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
212 });
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
213 };
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
214
2091
9324fc8285ad Effect repairs duplicate :settings edges when it finds them
drewp@bigasterisk.com
parents: 2071
diff changeset
215 // todo: is it faster to run them in series? might be
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
216 async.parallel([parseAdds, parseDels], (err: any) => cb(new Patch(dels, adds)));
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents:
diff changeset
217 }