93
|
1 // Load requested view and provide access to it
|
|
2 import { Store, Parser } from "n3";
|
|
3
|
|
4 async function fetchAndParse(url: string): Promise<Store> {
|
|
5 const store = new Store();
|
|
6 const res = await fetch(url);
|
|
7 const body = await res.text();
|
|
8 const parser = new Parser({ format: "N3" });
|
|
9 await new Promise((done, rej) => {
|
|
10 parser.parse(body, (err, quad, prefixes) => {
|
|
11 if (err) {
|
|
12 throw err;
|
|
13 }
|
|
14 if (quad) {
|
|
15 store.addQuad(quad);
|
|
16 } else {
|
|
17 done(null);
|
|
18 }
|
|
19 });
|
|
20 });
|
|
21 return store;
|
|
22 }
|
|
23
|
|
24 export class View {
|
|
25 graph: Store;
|
|
26 ready: Promise<null>;
|
|
27 constructor(public url: string | "") {
|
|
28 this.graph = new Store();
|
|
29 this.ready = new Promise((res, rej) => {
|
|
30 if (url) {
|
|
31 fetchAndParse(url).then((s2) => {
|
|
32 this.graph = s2;
|
|
33 res(null);
|
|
34 });
|
|
35 } else {
|
|
36 res(null);
|
|
37 }
|
|
38 });
|
|
39 }
|
|
40 label() {
|
|
41 return "nicelabel";
|
|
42 }
|
|
43 }
|