public
Created

Browser APIs and their weird dissimilitudes

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// 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?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.