Permalink
Browse files

Merge pull request #456 from drewfish/gh438-rs-adapter

fix #438 more lightweight resource store adapter
  • Loading branch information...
2 parents 7558065 + af6fa37 commit 82ab5af974067d010f4eea2e723a8780d4cbd7d9 @drewfish drewfish committed Sep 5, 2012
@@ -727,7 +727,7 @@ YUI.add('mojito-client', function(Y, NAME) {
doRender: function(mp, data, view, cb) {
if (!mp._views || !mp._assetsRoot) {
- this.resourceStore.getType('client', mp.type, mp.context,
+ this.resourceStore.expandInstanceForEnv('client', { type: mp.type }, mp.context,
function(err, typeInfo) {
if (err) {
cb(new Error(
@@ -48,66 +48,37 @@ YUI.add('mojito-resource-store-adapter', function(Y, NAME) {
},
- expandInstanceForEnv: function(env, instance, context, callback) {
- var base = {},
- source = {},
- my = this;
-
- if (!instance.instanceId) {
- instance.instanceId = Y.guid();
- //DEBUGGING: instance.instanceId += '-instance-common-' +
- // [instance.base||'', instance.type||''].join('-');
- }
- // DEPRECATED, but kept in case a user is using.
- instance.guid = instance.instanceId;
-
- // What are being asked to expand?
- if (instance.base) {
- source.name = instance.base;
- source.func = this.getSpec;
- } else if (instance.type) {
- source.name = instance.type;
- source.func = this.getType;
- } else {
- // We don't have any inputs so fail
- throw new Error('There was no info in the "instance" object');
- }
-
- // This contains the app "definition" and app config
- my.getApp(env, context, function(app) {
-
- // Here we get either the a spec or a type
- source.func(env, source.name, context, function(err, data) {
- if (err) {
- callback(err, {});
- return;
- }
-
- // Merge the inputs from right to left (right most values
- // win)
- base = my.merge(app, data, instance);
-
- // Ensure the "instance" has been properly resolved. If
- // there are no specs in the application.json file, there is
- // an error below because the instance is invalid. We should
- // check here for a valid instance object and throw an error
- // if it is not. This happens because someone could create a
- // routes.json file with routes that don't route to mojit
- // instances, and the URI router creates invalid commands,
- // which are passed into the dispatch.
- if (!my.validate(base)) {
- callback({
- message: 'Instance was not valid.',
- stack: Y.JSON.stringify(base, null, 4)
- }, {});
- return;
- }
-
- // Add the final "base" to the cache
- my.cache(env, instance, context, base);
-
- callback(null, base);
- }, my);
+ expandInstanceForEnv: function(env, inInstance, context, callback) {
+ var me = this;
+
+ return this.store.expandInstanceForEnv(env, inInstance, context, function(err, outInstance) {
+ if (err) {
+ callback(err, {});
+ return;
+ }
+
+ // Ensure the "instance" has been properly resolved. If
+ // there are no specs in the application.json file, there is
+ // an error below because the instance is invalid. We should
+ // check here for a valid instance object and throw an error
+ // if it is not. This happens because someone could create a
+ // routes.json file with routes that don't route to mojit
+ // instances, and the URI router creates invalid commands,
+ // which are passed into the dispatch.
+ if (!me.validate(outInstance)) {
+ callback(new Error('Instance was not valid.'));
+ return;
+ }
+
+ if (!outInstance.instanceId) {
+ outInstance.instanceId = Y.guid();
+ //DEBUGGING: outInstance.instanceId += '-data-common-' +
+ // [outInstance.base||'', outInstance.type||''].join('-');
+ }
+ // DEPRECATED, but kept in case a user is using.
+ outInstance.guid = outInstance.instanceId;
+
+ callback(null, outInstance);
});
},
@@ -129,40 +100,7 @@ YUI.add('mojito-resource-store-adapter', function(Y, NAME) {
},
- getSpec: function(env, id, context, callback, scope) {
-
- if (!scope) {
- scope = this;
- }
-
- scope.store.getSpec(env, id, context, callback);
- },
-
-
- getType: function(env, type, context, callback, scope) {
-
- if (!scope) {
- scope = this;
- }
-
- scope.store.getType(env, type, context, callback);
- },
-
-
- merge: function() {
- var obj = {},
- i;
-
- for (i = 0; i < arguments.length; i += 1) {
- obj = Y.mojito.util.mergeRecursive(obj, arguments[i]);
- }
-
- return obj;
- },
-
-
validate: function(base) {
-
if (!base.type || !base.yui) {
return false;
}
@@ -204,6 +142,5 @@ YUI.add('mojito-resource-store-adapter', function(Y, NAME) {
};
}, '0.1.0', {requires: [
- 'mojito-util',
'json-stringify'
]});
@@ -132,12 +132,62 @@ YUI.add('mojito-client-store', function(Y, NAME) {
ClientStore.prototype = {
- /*
- * TODO: REVIEW RE [Issue 76].
+
+ /**
+ * Expands the instance into all details necessary to dispatch the mojit.
+ * @method expandInstanceForEnv
+ * @param {string} env The runtime environment (either `client` or `server`).
+ * @param {object} instance The instance to expand.
+ * @param {object} ctx The runtime context.
+ * @param {function} cb Callback used to return the results (or error).
+ * @param {null|Error} cb.err Error encountered while attempting to service the request.
+ * @param {object|undefined} cb.instance The expanded instance, or undefined on error.
*/
- getSpec: function(env, id, context, callback) {
+ expandInstanceForEnv: function(env, inInstance, context, callback) {
+ var source = {};
+
+ // What are being asked to expand?
+ if (inInstance.base) {
+ source.name = inInstance.base;
+ source.func = this._getSpec;
+ } else if (inInstance.type) {
+ source.name = inInstance.type;
+ source.func = this._getType;
+ } else {
+ // We don't have any inputs so fail
+ callback(new Error('There was no "base" or "type" in the "instance" object'), undefined);
+ return;
+ }
+
+ // Here we get either a spec or a type.
+ source.func(env, source.name, context, function(err, remoteInstance) {
+ var outInstance;
+
+ if (err) {
+ callback(err, undefined);
+ return;
+ }
+
+ outInstance = Y.mojito.util.mergeRecursive(remoteInstance, inInstance);
+ callback(null, outInstance);
+ });
+ },
- var parts = id.split(':'),
+
+ /**
+ * Obtains the full, expanded spec.
+ * @private
+ * @method _getSpec
+ * @param {string} env The runtime environment (either `client` or `server`).
+ * @param {object} specID The ID of the spec to obtain.
+ * @param {object} context The context.
+ * @param {function} callback Callback used to return the results (or error).
+ * @param {null|Error} callback.err Error encountered while attempting to service the request.
+ * @param {object} callback.spec The spec.
+ */
+ _getSpec: function(env, specID, context, callback) {
+
+ var parts = specID.split(':'),
typeName = parts[0],
specName = parts[1] || 'default',
ns = typeName.replace(/\./g, '_'),
@@ -150,7 +200,7 @@ YUI.add('mojito-client-store', function(Y, NAME) {
url = this.staticPrefix + '/' + typeName + '/specs/' + specName +
'.json';
- url = this.buildUrl(url, context);
+ url = this._buildUrl(url, context);
// use the compiled version if there was one built
if (isCompiled(ns, specName)) {
@@ -163,46 +213,61 @@ YUI.add('mojito-client-store', function(Y, NAME) {
},
- /*
- * TODO: REVIEW RE [Issue 77]
+ /**
+ * Obtains full details for a mojit.
+ * @private
+ * @method _getType
+ * @param {string} env The runtime environment (either `client` or `server`).
+ * @param {object} type The mojit type to obtain.
+ * @param {object} context The runtime context.
+ * @param {function} callback Callback used to return the results (or error).
+ * @param {null|Error} callback.err Error encountered while attempting to service the request.
+ * @param {object} callback.mojit The mojit details.
*/
- getType: function(env, type, context, callback) {
+ _getType: function(env, type, context, callback) {
// This should really have the tunnelPrefix. However, that
// complicates offline apps (from `mojito build html5app`).
// The mojito-handler-tunnel will be able to handle this URL
// just fine.
var url = this.staticPrefix + '/' + type + '/definition.json';
- url = this.buildUrl(url, context);
+ url = this._buildUrl(url, context);
retrieveFile(url, callback);
},
- /*
- * TODO: REVIEW RE [Issue 78]
+ /**
+ * Returns a contextualized application configuration.
+ * @method getAppConfig
+ * @param {object} ctx The runtime context.
+ * @return {object} The application configuration contextualized by the "ctx" argument.
*/
getAppConfig: function(context) {
return this.appConfig;
},
- /*
- * TODO: REVIEW RE [Issue 78]
+ /**
+ * Returns the routes configured in the application.
+ * @method getRoutes
+ * @param {object} ctx The runtime context.
+ * @return {object} The routes.
*/
getRoutes: function() {
return this.routes;
},
+
/**
* Checks the given URL and adds a context query string.
- * @param url {String} the relative url
- * @param context {Object} the runtime context
- * @return {String}
+ * @private
+ * @param url {String} The relative url.
+ * @param context {Object} The runtime context.
+ * @return {String} The URL.
*/
- buildUrl: function (url, context) {
-
+ _buildUrl: function (url, context) {
if ('/' !== url.charAt(0)) {
url = '/' + url;
}
@@ -92,7 +92,7 @@ TunnelServer.prototype = {
_handleSpec: function(req, res, next, type, basename) {
var name,
- instance = {},
+ inInstance = {},
that = this;
name = basename.split('.').slice(0, -1).join('.') || null;
@@ -102,22 +102,22 @@ TunnelServer.prototype = {
return;
}
- instance.base = type;
+ inInstance.base = type;
if (name !== 'default') {
- instance.base += ':' + name;
+ inInstance.base += ':' + name;
}
- this._store.getSpec('client', instance.base, req.context,
- function(err, data) {
+ this._store.expandInstanceForEnv('client', inInstance, req.context,
+ function(err, outInstance) {
if (err) {
that._sendError(res, 'Error opening: ' + req.url + '\n' +
err,
500
);
return;
}
- that._sendData(res, data);
+ that._sendData(res, outInstance);
});
},
@@ -133,7 +133,7 @@ TunnelServer.prototype = {
instance.type = type;
- this._store.getType('client', instance.type, req.context,
+ this._store.expandInstanceForEnv('client', instance, req.context,
function(err, data) {
if (err) {
that._sendError(res, 'Error opening: ' + req.url + '\n' +
Oops, something went wrong.

0 comments on commit 82ab5af

Please sign in to comment.