view 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
line wrap: on
line source

// Load requested view and provide access to it
import { Store, Parser } from "n3";

async function fetchAndParse(url: string): Promise<Store> {
  const store = new Store();
  const res = await fetch(url);
  const body = await res.text();
  const parser = new Parser({ format: "N3" });
  await new Promise((done, rej) => {
    parser.parse(body, (err, quad, prefixes) => {
      if (err) {
        throw err;
      }
      if (quad) {
        store.addQuad(quad);
      } else {
        done(null);
      }
    });
  });
  return store;
}

export class View {
  graph: Store;
  ready: Promise<null>;
  constructor(public url: string | "") {
    this.graph = new Store();
    this.ready = new Promise((res, rej) => {
      if (url) {
        fetchAndParse(url).then((s2) => {
          this.graph = s2;
          res(null);
        });
      } else {
        res(null);
      }
    });
  }
  label() {
      return "nicelabel";
  }
}