Skip to content

Commit

Permalink
Merge pull request #28 from euforic/bug/nativeModules
Browse files Browse the repository at this point in the history
fix native module require
  • Loading branch information
Christian Sullivan committed May 2, 2013
2 parents e07fd55 + 6fb5c8d commit b114493
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
27 changes: 9 additions & 18 deletions build/liveview.js
Expand Up @@ -416,6 +416,7 @@ Module.patch = function (globalCtx, port, url) {
try{
var evt = JSON.parse(''+data);
if (evt.type === 'event' && evt.name === 'reload') {
Module._cache = [];
Module.global.reload();
}
} catch (e) { /*discard non JSON data for now*/ }
Expand Down Expand Up @@ -463,23 +464,21 @@ Module.include = function(id) {
Module.require = function(id) {
var fullPath = id;
var cached = Module.getCached(fullPath);

if (cached) {
if (!!cached) {
console.log(cached.exports);
return cached.exports;
}

if (!Module.exists(fullPath)) {
try {
if (id === 'app') { id = '_app'; }
Module._requireNative(id);
} catch (e) {
console.log('Checking for new file: ' + id);
}
return Module._requireNative(id);
} catch (e) { }
}

var freshModule = new Module(fullPath);

if (global.ENV !== 'liveview') { freshModule.cache(); }
freshModule.cache();
freshModule._compile();

while (!freshModule.loaded) {}
Expand Down Expand Up @@ -536,7 +535,7 @@ Module.prototype._getRemoteSource = function(file,timeout){
rsp = request.responseText;
} else if ((expireTime - (new Date()).getTime()) <= 0) {
rsp = true;
throw new Error('Timed Out');
throw new Error('[LiveView]', 'File Sever unavailable. Host Unreachable');
}
}

Expand Down Expand Up @@ -573,8 +572,7 @@ Module.prototype._getSource = function() {

Module._wrap = function(source) {
source = source.replace(/Ti(tanium)?.include/g, 'Module.include');
var script = (global.CATCH_ERRORS) ? Module._errWrapper[0] + source + Module._errWrapper[1] : source;
return Module._wrapper[0] + script + Module._wrapper[1];
return (global.CATCH_ERRORS) ? Module._errWrapper[0] + source + Module._errWrapper[1] : source;
};

// uncaught exception handler wrapper
Expand All @@ -584,13 +582,6 @@ Module._errWrapper = [
'} catch (err) { process.emit("uncaughtException", {module: __filename, error: err})}'
];

// commonjs anon function wrapper

Module._wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'})'
];

/**
* compile commonjs module and string to js
*
Expand All @@ -599,7 +590,7 @@ Module._wrapper = [

Module.prototype._compile = function() {
var source = Module._wrap(this._getSource());
var fn = eval(source);
var fn = Function('exports, require, module, __filename, __dirname',source);
fn(this.exports, Module.require, this, this.filename, this.__dirname);
this.loaded = true;
};
Expand Down
2 changes: 1 addition & 1 deletion build/liveview.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 9 additions & 18 deletions lib/platform/require.js
Expand Up @@ -107,6 +107,7 @@ Module.patch = function (globalCtx, port, url) {
try{
var evt = JSON.parse(''+data);
if (evt.type === 'event' && evt.name === 'reload') {
Module._cache = [];
Module.global.reload();
}
} catch (e) { /*discard non JSON data for now*/ }
Expand Down Expand Up @@ -154,23 +155,21 @@ Module.include = function(id) {
Module.require = function(id) {
var fullPath = id;
var cached = Module.getCached(fullPath);

if (cached) {
if (!!cached) {
console.log(cached.exports);
return cached.exports;
}

if (!Module.exists(fullPath)) {
try {
if (id === 'app') { id = '_app'; }
Module._requireNative(id);
} catch (e) {
console.log('Checking for new file: ' + id);
}
return Module._requireNative(id);
} catch (e) { }
}

var freshModule = new Module(fullPath);

if (global.ENV !== 'liveview') { freshModule.cache(); }
freshModule.cache();
freshModule._compile();

while (!freshModule.loaded) {}
Expand Down Expand Up @@ -227,7 +226,7 @@ Module.prototype._getRemoteSource = function(file,timeout){
rsp = request.responseText;
} else if ((expireTime - (new Date()).getTime()) <= 0) {
rsp = true;
throw new Error('Timed Out');
throw new Error('[LiveView]', 'File Sever unavailable. Host Unreachable');
}
}

Expand Down Expand Up @@ -264,8 +263,7 @@ Module.prototype._getSource = function() {

Module._wrap = function(source) {
source = source.replace(/Ti(tanium)?.include/g, 'Module.include');
var script = (global.CATCH_ERRORS) ? Module._errWrapper[0] + source + Module._errWrapper[1] : source;
return Module._wrapper[0] + script + Module._wrapper[1];
return (global.CATCH_ERRORS) ? Module._errWrapper[0] + source + Module._errWrapper[1] : source;
};

// uncaught exception handler wrapper
Expand All @@ -275,13 +273,6 @@ Module._errWrapper = [
'} catch (err) { process.emit("uncaughtException", {module: __filename, error: err})}'
];

// commonjs anon function wrapper

Module._wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'})'
];

/**
* compile commonjs module and string to js
*
Expand All @@ -290,7 +281,7 @@ Module._wrapper = [

Module.prototype._compile = function() {
var source = Module._wrap(this._getSource());
var fn = eval(source);
var fn = Function('exports, require, module, __filename, __dirname',source);
fn(this.exports, Module.require, this, this.filename, this.__dirname);
this.loaded = true;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "liveview",
"version": "0.1.10",
"version": "0.1.11",
"description": "Titanium Live Realtime App Development",
"main": "index.js",
"private": true,
Expand Down

0 comments on commit b114493

Please sign in to comment.