extensible packages

69 views
Skip to first unread message

Henri Tuhola

unread,
Nov 6, 2012, 3:39:06 AM11/6/12
to nod...@googlegroups.com
I teamed up with author of node-openvg, and we need to make a shared API for videocontext handling. I'd like to keep it open as possible, so people could extend it with their own renderers later.

surface.getContext("webgl", options) - this should grab API that's currently given by node-video.
surface.getContext("2d", options) - this should grab API that's provided by node-openvg-canvas

How to package it such that it's simple and easy to handle? Is it possible without changing the API?

Henri Tuhola

unread,
Nov 6, 2012, 3:44:20 AM11/6/12
to nod...@googlegroups.com
To make it clear, it's that extensibility property I'm looking for, without immediate need to insert anything into the library that takes care of implementing getContext -method.

Bradley Meck

unread,
Nov 6, 2012, 8:01:14 AM11/6/12
to nod...@googlegroups.com
This sounds like a shared state lookup. Document and specify an interface for the renderers ("webgl" and "2d" in this case). Once you have done that, in getContext

Surface.prototype.getContext = function (name, options) {
  var renderer = this._renderers[name] // _ prefix is for implementation detail
  if (!renderer) {
    throw ...
  }
  renderer.getContext(this, options) // or similar api
}

And then developers could extend the surface with a different api, it could require direct access to _renderers or some helper function like surface._addRenderer(name, interface).

Henri Tuhola

unread,
Nov 6, 2012, 11:22:37 AM11/6/12
to nod...@googlegroups.com
Sounds good except to the fact that it's better to not preload all those renderers. I don't know which ones will be used by the process and which not. Somehow I still feel this presents challenges to npm. I'll write some doodles today to study it out.

Henri Tuhola

unread,
Nov 6, 2012, 1:46:17 PM11/6/12
to nod...@googlegroups.com
var renderers = require("./renderers.json");

Surface.prototype.getContext = function(name, options) {
    var renderer = renderers[name];
    if (!renderer) {
        throw "no renderer found";
    }
    var api = require(renderer);
    return vc.createContext(this, api, options);
};

I could let a build script generate the renderers.json, and mark all those libraries dependencies.

greelgorke

unread,
Nov 6, 2012, 4:44:53 PM11/6/12
to nod...@googlegroups.com
instead of relying on a config file, i would implement a register function for renderers, so the user can decide, install and register the renderer she want to use. i.E:
 
Surface.prototype.registerContext = function(name, packageIdent /*could be name or relativ path*/) {
    renderers[name] = packageIdent;
};
Reply all
Reply to author
Forward
0 new messages