blake-regalia / graphy.js

A collection of RDF libraries for JavaScript
https://graphy.link/
ISC License
163 stars 4 forks source link

Docs: missing section: "Construction – how to create an instance of a DatasetTree" #20

Closed happybeing closed 4 years ago

happybeing commented 4 years ago

The destination is missing for https://graphy.link/memory.dataset.fast#construction

P.S. @blake-regalia do you have any example apps using graphy?

blake-regalia commented 4 years ago

Thanks -- I've updated the documentation and added an example, but concede this still needs more. Hopefully this helps for now?

I have several local projects using graphy at the moment but unfortunately they are under wraps pending publication(s).

blake-regalia commented 4 years ago

P.S. If you have any specific use cases, I can surely help by adding some examples to the documentation. Otherwise, I am always available to respond in GH issues or on gitter 👍

happybeing commented 4 years ago

Thanks @blake-regalia. I managed to make a DatasetTree by first looking around in the source and I was add a couple of quads to it! I then iterated over these with for.. tree as quad.

I wasn't able to use the Svelte {#each} construct for iteration though, so I wonder if that relies on support for some other kind of iterator, or it might only work on arrays. I'll check this out with the Svelte community.

A new question, looking at your doc link it says:

returns a new Duplex<Quad, Quad> (accepts Quad objects on its writable side, pushes Quad objects on its readable side)

Is there a discrepancy there, maybe just terminology, between DatasetTree which I see in the debugger and Duplex<Quad, Quad>? I am not very familiar with a lot of this so maybe misunderstand something basic! Thanks for your continuing offer to help.

happybeing commented 4 years ago

Re Svelte {#each}, any chance you can add a length property to support this:

You can use each blocks to iterate over any array or array-like value — that is, any object with a length property.

Ref: https://svelte.dev/docs#each

This would make it possible to exploit one of the nice features of Svelte - compiling to reactive HTML/JS without using the DOM API.

Just a thought 😄 I guess I could create a wrapper object.

blake-regalia commented 4 years ago

As for the Svelte {#each}, the practice for iterators is to use: {#each [...iterable]} -- which to me is an anti-pattern. I left a comment on the closed issue in the Svelte repo about supporting iterators but alas.

So for Dataset (which is iterable), you could do:

{#each [...dataset] as quad}

The Duplex signature is in reference to the node.js docs, which simply means that it extends both readable and writable interfaces. When webpack bundles this up, it uses some polyfills for the browser -- i think they are both whatwg compatible at this point.

happybeing commented 4 years ago

@blake-regalia Another documentation example request. I'm trying to pass a stream to contnt.ttl.read using fetch() but it is being rejected with error:

TypeError: unexpected input type: [object ReadableStream] at ./node_modules/@graphy/content.ttl.read/main.js.module.exports

Code:

let streamRdf;
  fetch('http://www.w3.org/2000/01/rdf-schema')
    .then(res => res.body)
    .then(body => {
      body.pipeTo(readTtl({
          data(y_quad) {
              console.log(JSON.stringify(y_quad));
              $rdfDataset.add(y_quad);
              console.log('rdfDataset size: ', $rdfDataset.size);
              $rdfDataset = $rdfDataset;
          },

          eof(h_prefixes) {
              console.log('done!');
              console.log('rdfDataset size: ', $rdfDataset.size);
          },
      }))
    })
    .catch(err => console.error(err));
blake-regalia commented 4 years ago

@theWebalyst this looks like a bug in graphy to me -- I believe the code here should work as expected. I will clone your svelte repo and investigate further.

blake-regalia commented 4 years ago

It turns out graphy is not compatible with WHATWG stream pipe methods at the moment and so pipeTo and pipeThrough will not work with the readers, writers, or dataset -- my apologies. I will put this on the issue tracker.

In the meantime, you can revert to the legacy read/write style when using fetch:

fetch('http://www.w3.org/2000/01/rdf-schema')
    .then(res => res.body)
    .then((body) => {
        let body_reader = body.getReader();

        let ttl_reader = readTtl({
            data(y_quad) {
                console.log(JSON.stringify(y_quad));
                // triples.push(y_quad);
                // triples = triples;
            },

            eof(h_prefixes) {
                console.log('done!');
            },
        });

        function next() {
            body_reader.read().then(read_chunk);
        }

        function read_chunk({value, done}) {
            if(done) {
                ttl_reader.end();
                return;
            }

            ttl_reader.write(value);
            next();
        }

        next();
    });

P.S. @theWebalyst thanks for putting the svelte repo together, I've already learned something about stores after cloning it!