Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created August 21, 2011 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandopazo/1160119 to your computer and use it in GitHub Desktop.
Save juandopazo/1160119 to your computer and use it in GitHub Desktop.
Browser APIs and their weird dissimilitudes
// WEB WORKERS
var worker = new Worker('task.js');
worker.onmessage = function (e) {
};
// where's addEventListener? A Worker shoud be an EventEmitter
worker.addEventListener('message', function (e) {
});
// And you know, on() as an alias of addEventListener makes a lot of sense too
// WEBGL
// In WebGL, you need to insert a shader in a script tag. Ok, makes sense, it even uses a type different to "text/javascript".
var vSource = document.getElementById("vertex").firstChild.nodeValue;
 
// Then you need to create a shader object by calling createShader
var vShader = gl.createShader(gl.VERTEX_SHADER);
// Now wait, I have an instance and I need to set the source afterwards?
// Worse, it's an instance but I have to set the source from a method of the context?
gl.shaderSource(vShader, vSource);
// How it should be:
var vShader = gl.createShader(gl.VERTEX_SHADER, vSource);
// Of course, a shader without a source? Maybe I'm missing something and it makes sense, but it still can be an optional parameter
// At any rate, if I was going to set the source of a shader, I'd expect it to do it from the shader itself:
shader.source = vSource; //do'h
// Next we need to compile the shader.
gl.compileShader(vs);
// Wait, something we learned is that compiling is expensive and by the way this is written, it is a synchronous call
// It should be async! For many reasons, like avoiding using try...catch, but also to avoid locking the UI thread compiling shaders
// Then again, it's the shader that's compiling. Have it say that!
shader.compile(function (err) {
if (!err) {
// NodeJS de facto standard. Work here
}
});
 
var program = gl.createProgram();
gl.attachShader(program, vShader);
gl.linkProgram(program);
// Again, why aren't we doing actual OOP?
var program = gl.createProgram();
program.attachShader(vShader);
gl.linkProgram(program);
// I don't expect method chaining from a browser API, but I do expect it to be similar to other APIs
// mostly the DOM, preferrably the good parts of the DOM
// The DOM still has remnants of these APIs when it asks you to call the parent to insert a node:
sibling.parentNode.insertBefore(node, sibling);
// I mean, why do I need the parent?
node.insertBefore(sibling);
// That makes sense, right?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment