rdfjs-base / serializer-rdfjs

RDFJS JavaScript code serializer that implements the RDFJS Sink interface
MIT License
1 stars 1 forks source link

Use Environment #13

Open tpluscode opened 1 year ago

tpluscode commented 1 year ago

Now that RDF/JS Environment types have been published, would you agree to changing the interface of the generated code to actually take an environment instance parameter?

+import { Environment } from '@rdfjs/environment/Environment.js'
+import { DataFactory } from '@rdfjs/types'
+
-export default ({ factory }: { factory: import('rdf-js').DataFactory }) => {
+export default (factory: Environment<DataFactory>) => {
  return [
    factory.quad(
      factory.blankNode('foo'),
      factory.namedNode('http://example.com/bar'),
      factory.literal('baz')
    )
  ]
}

Typed like this in TS it would require that the factory is any instance of an environment which includes a DataFactory. And incidentally, that would continue to work with rdf-ext v1 using the interface we generated in version 0.0.1 of the serializer

tpluscode commented 1 year ago

As a general question, is this how you envision the use of RDF/JS Environment? Any code which requires an environment might take is as parameter.

The types add the ability for consumer to require certain factories to be included. For example, Environment<ScoreFactory | DatasetFactory> would check that the environment has been constructed at least using these two factories. rdf-ext would satisfy that type but a custom environment would be fine, and types enforce its interface