annotate src/view_loader.ts @ 93:955cde1550c3

start the View work: parse view document
author drewp@bigasterisk.com
date Wed, 12 Jan 2022 20:26:57 -0800
parents
children a5f53d397526
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
1 // Load requested view and provide access to it
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
2 import { Store, Parser } from "n3";
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
3
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
4 async function fetchAndParse(url: string): Promise<Store> {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
5 const store = new Store();
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
6 const res = await fetch(url);
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
7 const body = await res.text();
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
8 const parser = new Parser({ format: "N3" });
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
9 await new Promise((done, rej) => {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
10 parser.parse(body, (err, quad, prefixes) => {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
11 if (err) {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
12 throw err;
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
13 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
14 if (quad) {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
15 store.addQuad(quad);
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
16 } else {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
17 done(null);
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
18 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
19 });
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
20 });
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
21 return store;
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
22 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
23
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
24 export class View {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
25 graph: Store;
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
26 ready: Promise<null>;
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
27 constructor(public url: string | "") {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
28 this.graph = new Store();
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
29 this.ready = new Promise((res, rej) => {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
30 if (url) {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
31 fetchAndParse(url).then((s2) => {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
32 this.graph = s2;
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
33 res(null);
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
34 });
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
35 } else {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
36 res(null);
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
37 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
38 });
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
39 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
40 label() {
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
41 return "nicelabel";
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
42 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
43 }