Permalink
Browse files

major refactoring. less naive implementation. console.dir better. no …

…trace yet
  • Loading branch information...
1 parent d8229c2 commit 42bf468f756890197e27bd005389994bfc00ef1f @jeromeetienne committed Sep 9, 2011
Showing with 84 additions and 13 deletions.
  1. +29 −2 console4Worker-page.js
  2. +47 −10 console4Worker-worker.js
  3. +4 −1 examples/index.html
  4. +4 −0 examples/worker.js
View
@@ -5,9 +5,11 @@ this.console4Worker = {}
* Filter message events for console4Worker
*
* @param {MessageEvent} event the event from the 'message' event
+ * @param {Boolean} exec true if the event must be executed, false otherwise. default to false
+ *
* @return false if the message is for console4Worker, false otherwise
*/
-console4Worker.filterEvent = function(event){
+console4Worker.filterEvent = function(event, exec){
// sanity check - check the event
console.assert(event);
if( 'data' in event === false ) return false;
@@ -18,7 +20,32 @@ console4Worker.filterEvent = function(event){
var data = event.data.data;
if( type !== '_console4Worker' ) return false;
// ok now the event is for console4Worker
- console[data.type].apply(console, data.data);
+ if( exec ) console[data.type].apply(console, data.data);
// return true to notify the event has been filtered
return true;
}
+
+/**
+ * The callback to handle event
+ * @param {MessageEvent} event the event from the 'message' event
+*/
+console4Worker._callback = function(event){
+ //console.log("consoleWorker.bind():", event.data, event)
+ if( console4Worker.filterEvent(event, true) ) return;
+}
+
+/**
+ * make it bind a given worker
+ * @param {Worker} Worker the webworker to bind from now on
+*/
+console4Worker.bind = function(worker){
+ worker.addEventListener('message', this._callback, false);
+}
+
+/**
+ * make it unbind a given worker
+ * @param {Worker} Worker the webworker to no more bind
+*/
+console4Worker.unbind = function(worker){
+ worker.removeEventListener('message', this._callback, false);
+}
View
@@ -2,6 +2,44 @@
(function(){
this.console = {};
+
+ /**
+ * Convert Arguments into an Array passable thru Worker barrier
+ */
+ var _convertArgs = function(args){
+ return Array.prototype.slice.call(args).map(function(arg){
+ if( typeof arg === "number" ) return arg;
+ if( typeof arg === "boolean" ) return arg;
+ if( typeof arg === "object" ){
+ try {
+ return JSON.parse(JSON.stringify(arg));
+ }catch(e){
+ return (function(){
+ var str = "";
+ for(var key in arg){
+ if( !arg.hasOwnProperty(key) ) continue;
+ str += key + " : " + arg[key] + "\n";
+ }
+ return str;
+ })();
+ }
+ }
+ return String(arg);
+ });
+ }
+
+ var sendCommand = function(method, args){
+ self.postMessage({
+ // mark this message as coming from console4Worker
+ type : "_console4Worker",
+ data : {
+ // give it the function
+ type : method,
+ data : _convertArgs(args)
+ }
+ });
+ }
+
// define all the methodes from console.*
var methods = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
@@ -10,16 +48,15 @@
(function(method){
// define this method in particular
console[method] = function(){
- self.postMessage({
- // mark this message as coming from console4Worker
- type : "_console4Worker",
- data : {
- // give it the function
- type : method,
- data : Array.prototype.slice.call(arguments)
- }
- });
+ return sendCommand(method, Array.prototype.slice.call(arguments));
};
- })(methods[i]);
+ })(methods[i]);
}
+
+
+ console.assert = function(condition, message){
+ if( condition ) return;
+ sendCommand('assert', [false, message])
+ }
+
}.bind(this))();
View
@@ -7,7 +7,10 @@
<script language="javascript">
// init the worker
var worker = new Worker("worker.js");
- // example of message not from console4Worker
+ // bind the console4Worker to get console.* from worker
+ console4Worker.bind(worker);
+
+ // example of message handler
worker.addEventListener('message', function(event){
// filter this event if it is from console4Worker
View
@@ -2,5 +2,9 @@ importScripts('../console4Worker-worker.js');
console.log("Server says hi")
+console.assert('something true', "wow");
+
+console.assert('', "wow");
+
self.postMessage("foobar not from console.*");

0 comments on commit 42bf468

Please sign in to comment.