Skip to content

Commit

Permalink
updated for the latest react native
Browse files Browse the repository at this point in the history
  • Loading branch information
cauyeung-wco committed Jan 13, 2016
1 parent 2e65a71 commit dc3d089
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 287 deletions.
144 changes: 2 additions & 142 deletions dist/defiant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,152 +3,12 @@
* Search JSON structures plus smart templating with XSLT and XPath.
* http://defiantjs.com
*
* Copyright (c) 2013-2015, Hakan Bilgin <[email protected]>
* Copyright (c) 2013-2016, Hakan Bilgin <[email protected]>
* Licensed under the MIT License
*/
(function(window, undefined) {
//'use strict';

var x10 = {
init: function() {
return this;
},
work_handler: function(event) {
var args = Array.prototype.slice.call(event.data, 1),
func = event.data[0],
ret = tree[func].apply(tree, args);

// return process finish
postMessage([func, ret]);
},
setup: function(tree) {
var url = window.URL || window.webkitURL,
script = 'var tree = {'+ this.parse(tree).join(',') +'};',
blob = new Blob([script + 'self.addEventListener("message", '+ this.work_handler.toString() +', false);'],
{type: 'text/javascript'}),
worker = new Worker(url.createObjectURL(blob));

// thread pipe
worker.onmessage = function(event) {
var args = Array.prototype.slice.call(event.data, 1),
func = event.data[0];
x10.observer.emit('x10:'+ func, args);
};

return worker;
},
call_handler: function(func, worker) {
return function() {
var args = Array.prototype.slice.call(arguments, 0, -1),
callback = arguments[arguments.length-1];

// add method name
args.unshift(func);

// listen for 'done'
x10.observer.on('x10:'+ func, function(event) {
callback(event.detail[0]);
});

// start worker
worker.postMessage(args);
};
},
compile: function(hash) {
var worker = this.setup(typeof(hash) === 'function' ? {func: hash} : hash),
obj = {},
fn;
// create return object
if (typeof(hash) === 'function') {
obj.func = this.call_handler('func', worker);
return obj.func;
} else {
for (fn in hash) {
obj[fn] = this.call_handler(fn, worker);
}
return obj;
}
},
parse: function(tree, isArray) {
var hash = [],
key,
val,
v;

for (key in tree) {
v = tree[key];
// handle null
if (v === null) {
hash.push(key +':null');
continue;
}
// handle undefined
if (v === undefined) {
hash.push(key +':undefined');
continue;
}
switch (v.constructor) {
case Date: val = 'new Date('+ v.valueOf() +')'; break;
case Object: val = '{'+ this.parse(v).join(',') +'}'; break;
case Array: val = '['+ this.parse(v, true).join(',') +']'; break;
case String: val = '"'+ v.replace(/"/g, '\\"') +'"'; break;
case RegExp:
case Function: val = v.toString(); break;
default: val = v;
}
if (isArray) hash.push(val);
else hash.push(key +':'+ val);
}
return hash;
},
// simple event emitter
observer: (function() {
var stack = {};

return {
on: function(type, fn) {
if (!stack[type]) {
stack[type] = [];
}
stack[type].unshift(fn);
},
off: function(type, fn) {
if (!stack[type]) return;
var i = stack[type].indexOf(fn);
stack[type].splice(i,1);
},
emit: function(type, detail) {
if (!stack[type]) return;
var event = {
type : type,
detail : detail,
isCanceled : false,
cancelBubble : function() {
this.isCanceled = true;
}
},
len = stack[type].length;
while(len--) {
if (event.isCanceled) return;
stack[type][len](event);
}
}
};
})()
};

if (typeof module === "undefined") {
// publish x10
window.x10 = x10.init();
} else {
module.exports = x10.init();
}

})(this);


if (typeof module === "undefined") {
var module = { exports: undefined };
module = { exports: undefined };
} else {
// Node env adaptation goes here...
}
Expand Down
Loading

0 comments on commit dc3d089

Please sign in to comment.