Skip to content

Commit

Permalink
Merge pull request #288 from skrenek/master
Browse files Browse the repository at this point in the history
Render error handling
  • Loading branch information
chris-held committed Aug 5, 2014
2 parents 58e9b6b + f08bd06 commit e04020a
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 99 deletions.
4 changes: 4 additions & 0 deletions lib/feather-client/lib/jquery-1.7.1.min.js

Large diffs are not rendered by default.

72 changes: 43 additions & 29 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var sys = util = require("util"),
var sys = util = require("util"),
_ = require("underscore")._,
fs = require("fs"),
jsdom = require("jsdom"),
fs = require("fs"),
jsdom = require("jsdom"),
encoder = new (require('./encoder.js'))(),
EventPublisher = require("./event-publisher"),
Semaphore = require("./semaphore"),
Expand All @@ -24,9 +24,9 @@ var optionsTagFixRegex = /<(\/*)options/g;
* Parses the given file and passes a render function to the callback.
* @memberOf Parser
* @param {String} path path to the file to parse
* @param {Object} options Object containing
* @param {Object} options Object containing
* <ul class="desc"><li>request: HTTP Request</li>
* <li>callback: callback Function taking one parameter, a Function that will render the results of the parse operation</li></ul>
* <li>callback: callback Function taking one parameter, a Function that will render the results of the parse operation</li></ul>
*/
var parseFile = exports.parseFile = function(options, cb) {
cache.getItems([
Expand Down Expand Up @@ -60,8 +60,8 @@ var parseFile = exports.parseFile = function(options, cb) {
var html = data;

html = html.replace(/<resources ?\/>/, '<resources></resources>');
domPool.getResource(function(dom) {

domPool.getResource(function(dom) {
dom.document.innerHTML = html;

Widget.render({
Expand All @@ -81,21 +81,21 @@ var parseFile = exports.parseFile = function(options, cb) {

//inject scripts and resources into the dom
var body = dom.$j("body")[0];

var scriptStr = [
'feather.stateMachine.onceState("loadingComplete", function() {\n',
' feather.appOptions = ' + clientOptions + ";\n",
'feather.stateMachine.onceState("loadingComplete", function() {\n',
' feather.appOptions = ' + clientOptions + ";\n",
' feather.stateMachine.fire("ready");\n',
'});\n',
'feather.stateMachine.onceState("ready", function(){\n',
result.scripts.join("\n") + '\n',
'});\n'
].join('');

//last thing on the page should be to tell the main stateMachine loading is complete
scriptStr += "feather.stateMachine.fire('loadingComplete');\n";
scriptStr = encoder.htmlDecode(scriptStr);

dom.$j("<clientscript type='text/javascript'>{pageScript}</clientscript>").appendTo(body);

/**
Expand Down Expand Up @@ -135,12 +135,12 @@ var parseFile = exports.parseFile = function(options, cb) {
//cache resulting html
var _html = dom.document.innerHTML;
_html = _html
.replace(/\<\/?resources[^\>]*\>/g, "")
.replace(/(\<\/?)clientscript/g, "$1script")
.replace(/\<\/?resources[^\>]*\>/g, "")
.replace(/(\<\/?)clientscript/g, "$1script")
.replace("{pageScript}", scriptStr)
.replace(/\\n/g, "\n");

//final replaces: if sending custom tags to the client, namespace them (required for IE)
//final replaces: if sending custom tags to the client, namespace them (required for IE)
_html = _html.replace(widgetTagFixRegex, "<$1feather:widget");
_html = _html.replace(optionsTagFixRegex, "<$1feather:options");

Expand All @@ -155,7 +155,7 @@ var parseFile = exports.parseFile = function(options, cb) {
appOptions: appOptions
}, function(err, pagePackageResult) {
if (err) console.log(err);
//defer cleanup
//defer cleanup
process.nextTick(function() {
//unload this request's widget instances from memory (which will also clean up the DOM)
for (var i = 0, l = result.widgets.items.length, w; i < l; i++) {
Expand All @@ -173,13 +173,13 @@ var parseFile = exports.parseFile = function(options, cb) {
});
});
}
});
});
});
}
});
}
});

};

/*
Expand Down Expand Up @@ -210,9 +210,9 @@ cache.getItemWait("feather-dom", function(err, dom) {
var instanceScript = dom.$j.template(null, [
'var widget = new ${widgetName}(options);\\n'
].join(''));

cache.setItem(localId + ":optionTemplate", optionTemplate);
cache.setItem(localId + ":instanceScript", instanceScript);
cache.setItem(localId + ":instanceScript", instanceScript);
});

/**
Expand Down Expand Up @@ -241,10 +241,10 @@ var parseWidget = exports.parseWidget = function(options, cb) {
domPool.getResource(function(dom) {
var document = dom.document,
$j = dom.$j;

var body = $j('body')[0],
cls = '';

var optionsStr = "";
if (options.options) {
for (var p in options.options) {
Expand All @@ -260,7 +260,7 @@ var parseWidget = exports.parseWidget = function(options, cb) {
}
}
}

//avoid server side id collisions (be safe)
var widgetId = options.id || simpleId();
var safeWidgetId = simpleId() + "___" + widgetId; //3 underscores just to make the replaces below more accurate
Expand All @@ -287,19 +287,33 @@ var parseWidget = exports.parseWidget = function(options, cb) {
return null;
}
}, function(err, result) {
if (err) cb(err); else {
if (err) {
//defer cleanup to next tick
process.nextTick(function() {
if (result) {
//unload this request's widget instances from memory (which will also clean up the server's DOM)
for (var i = 0, l = result.widgets.items.length, w; i < l; i++) {
w = result.widgets.items[i];
w && w.dispose && w.dispose();
}
}

domPool.release(dom);
});
cb(err);
} else {
var scriptStr = encoder
.htmlDecode(result.scripts.join("\n"))
.replace(widgetIdRegex, widgetId)
.replace(/\\n/g, "");
scriptStr = "(function(options) {" + scriptStr + "})";

//convert to html
var html = $j(body).html();
html = html
.replace(widgetIdRegex, widgetId)
.replace(/\\n/g, "\n");

var classes = [];
for (var id in result.widgetClassRegistry.itemCache) {
//if there is not classDef, this is clientOnly and we want to include the id,
Expand Down Expand Up @@ -340,12 +354,12 @@ var parseWidget = exports.parseWidget = function(options, cb) {
for (var i = 0, l = result.widgets.items.length, w; i < l; i++) {
w = result.widgets.items[i];
w && w.dispose && w.dispose();
}
}

domPool.release(dom);
});
}
});
});
});
}
});
Expand Down
15 changes: 14 additions & 1 deletion lib/resource-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var ResourcePool = module.exports = function(options) {
ResourcePool.super.apply(this);
this.min = options.min;
this.max = options.max;

//allow bolt-on createResource providers via configuration
//NOTE: any supplied options.createResource function will
//override any class-level implementation
Expand Down Expand Up @@ -67,6 +67,19 @@ ResourcePool.prototype.getResource = function(cb) {
}
};

ResourcePool.prototype.getStats = function(cb) {
var me = this;
var lockCount = 0;
me.each(function(resource) {
if (resource.locked) lockCount += 1;
});
cb({
min: me.min,
max: me.max,
available: me.max - lockCount
})
};

/**
* Releases a resource back to the pool
* @param {Object} resource
Expand Down
Loading

0 comments on commit e04020a

Please sign in to comment.