Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

fix #438 more lightweight resource store adapter
  • Loading branch information...
commit 82ab5af974067d010f4eea2e723a8780d4cbd7d9 2 parents 7558065 + af6fa37
@drewfish drewfish authored
View
2  lib/app/autoload/mojito-client.client.js
@@ -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(
View
125 lib/app/autoload/resource-store-adapter.common.js
@@ -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'
]});
View
101 lib/app/autoload/store.client.js
@@ -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,10 +213,18 @@ 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`).
@@ -174,35 +232,42 @@ YUI.add('mojito-client-store', function(Y, NAME) {
// 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;
}
View
14 lib/app/middleware/mojito-handler-tunnel.js
@@ -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,14 +102,14 @@ 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,
@@ -117,7 +117,7 @@ TunnelServer.prototype = {
);
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' +
View
52 lib/store.server.js
@@ -461,54 +461,6 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
/**
- * Returns, via callback, the fully expanded mojit instance specification.
- *
- * @async
- * @method getSpec
- * @param {string} env the runtime environment (either `client` or `server`)
- * @param {string} id the ID of the spec to return
- * @param {object} ctx the runtime context for the spec
- * @param {function(err,spec)} callback callback used to return the results (or error)
- */
- getSpec: function(env, id, ctx, callback) {
- this.expandInstanceForEnv(env, {base: id}, ctx, function(err, obj) {
- if (err) {
- callback(err);
- return;
- }
- if (env === 'client' && obj) {
- delete obj.assets;
- }
- callback(null, obj);
- });
- },
-
-
- /**
- * Returns, via callback, the details of the mojit type.
- *
- * @async
- * @method getType
- * @param {string} env the runtime environment (either `client` or `server`)
- * @param {string} type the mojit type
- * @param {object} ctx the runtime context for the type
- * @param {function(err,spec)} callback callback used to return the results (or error)
- */
- getType: function(env, type, ctx, callback) {
- this.expandInstanceForEnv(env, {type: type}, ctx, function(err, obj) {
- if (err) {
- callback(err);
- return;
- }
- if (env === 'client' && obj) {
- delete obj.assets;
- }
- callback(null, obj);
- });
- },
-
-
- /**
* This just calls `expandInstanceForEnv()` with `env` set to `server`.
*
* @async
@@ -535,7 +487,6 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
var cacheKey = Y.JSON.stringify(instance) + Y.JSON.stringify(ctx),
cacheValue = this._expandInstanceCache[env][cacheKey],
spec,
- typeDetails,
config;
this.validateContext(ctx);
@@ -549,7 +500,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
ctx.runtime = env;
try {
- spec = this._expandSpec(env, ctx, instance);
+ spec = this._expandSpec(env, ctx, Y.clone(instance, true));
} catch (err) {
return cb(err);
}
@@ -567,6 +518,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
} catch (err2) {
return cb(err2);
}
+ spec = this.mergeRecursive(spec, instance);
if (spec.defaults && spec.defaults.config) {
config = Y.clone(spec.defaults.config, true);
this.mergeRecursive(config, spec.config);
View
17 lib/tests/autoload/app/autoload/resource-store-adapter-tests.common.js
@@ -164,23 +164,6 @@ YUI.add('mojito-resource-store-adapter-tests', function(Y, NAME) {
store.expandInstance(instance, {device:'iphone'}, function(err, instance){
A.areSame('index.iphone.hb.html', instance.views.index['content-path'].split('/').pop());
});
- },
-
- 'test getSpec() from specs dir': function(){
- var store = Y.mojito.ResourceStoreAdapter.init('server', resourceStore, dummyLog);
- store.getSpec('server', 'TestMojit2', {}, function(err, instance){
- A.isTrue(instance.type === 'TestMojit2');
- A.isTrue(instance.config.testKey1 === 'testVal1');
- });
- },
-
- 'test getType()': function(){
- var store = Y.mojito.ResourceStoreAdapter.init('server', resourceStore, dummyLog);
- store.getType('server', 'test_mojit_1', {}, function(err, instance){
- A.isTrue(instance.type === 'test_mojit_1');
- A.isTrue(instance.config.testKey4 === 'testVal4');
- A.isTrue(instance.config.testKey6.testKey7 === 'testVal7');
- });
}
}));
View
37 lib/tests/autoload/middleware/handler-tunnel-tests.server.js
@@ -20,23 +20,14 @@ YUI.add('mojito-middleware-handler-tunnel-tests', function(Y, NAME) {
setUp: function() {
var store = {
getAppConfig: function() { return { obj: 'appConfig' }; },
- getSpec: function(env, id, ctx, cb) {
- cb(null, {
- env: env,
- id: id,
- ctx: ctx
- });
- },
- getType: function(env, type, ctx, cb) {
- cb(null, {
- env: env,
- type: type,
- ctx: ctx
- });
- },
expandInstance: function(instance, context, callback) {
expandedContext = context;
callback(null, instance);
+ },
+ expandInstanceForEnv: function(env, instance, context, callback) {
+ instance.env = env;
+ instance.ctx = context;
+ callback(null, instance);
}
},
globalLogger = null;
@@ -136,8 +127,8 @@ YUI.add('mojito-middleware-handler-tunnel-tests', function(Y, NAME) {
},
end: function(data) {
var expected = {
- "env": "client",
- "id": "MojitA:orange"
+ "base": "MojitA:orange",
+ "env": "client"
};
endCalls++;
Assert.areEqual(Y.JSON.stringify(expected,null,4), data, 'should have gotten spec');
@@ -168,8 +159,8 @@ YUI.add('mojito-middleware-handler-tunnel-tests', function(Y, NAME) {
},
end: function(data) {
var expected = {
- "env": "client",
- "id": "MojitA:orange"
+ "base": "MojitA:orange",
+ "env": "client"
};
endCalls++;
Assert.areEqual(Y.JSON.stringify(expected,null,4), data, 'should have gotten spec');
@@ -200,8 +191,8 @@ YUI.add('mojito-middleware-handler-tunnel-tests', function(Y, NAME) {
},
end: function(data) {
var expected = {
- "env": "client",
- "type": "MojitA"
+ "type": "MojitA",
+ "env": "client"
};
endCalls++;
Assert.areEqual(Y.JSON.stringify(expected,null,4), data, 'should have gotten spec');
@@ -232,8 +223,8 @@ YUI.add('mojito-middleware-handler-tunnel-tests', function(Y, NAME) {
},
end: function(data) {
var expected = {
- "env": "client",
- "type": "MojitA"
+ "type": "MojitA",
+ "env": "client"
};
endCalls++;
Assert.areEqual(Y.JSON.stringify(expected,null,4), data, 'should have gotten spec');
@@ -254,4 +245,4 @@ YUI.add('mojito-middleware-handler-tunnel-tests', function(Y, NAME) {
YUITest.TestRunner.add(suite);
-}, '0.0.1');
+}, '0.0.1', {requires: ['json-stringify']});
View
18 lib/tests/autoload/store.server-tests.js
@@ -278,24 +278,6 @@ YUI.add('mojito-store-server-tests', function(Y, NAME) {
cmp(post, pre);
},
- 'call getSpec()': function() {
- store.getSpec('server', 'test1', {}, function(err, instance) {
- A.areSame('test_mojit_1', instance.type);
- A.areSame('test1', instance.id);
- // ... and all the type-specific parts...
- A.areSame('/static/test_mojit_1/assets', instance.assetsRoot);
- });
- },
-
- 'call getType()': function() {
- store.getType('server', 'test_mojit_1', {}, function(err, instance) {
- A.areSame('test_mojit_1', instance.type);
- A.isUndefined(instance.id);
- // ... and all the type-specific parts...
- A.areSame('/static/test_mojit_1/assets', instance.assetsRoot);
- });
- },
-
'instance with base pointing to non-existant spec': function() {
var spec = { base: 'nonexistant' };
store.expandInstance(spec, {}, function(err, instance) {
View
73 tests/unit/lib/app/autoload/test-resource-store-adapter.common.js
@@ -32,43 +32,22 @@ YUI().use('mojito-resource-store-adapter', 'test', function(Y) {
testKey3: 'testVal3'
};
},
- getSpec: function (env, id, context, callback) {
- var specs = {
- 'test1': {
- type: 'test_mojit_1',
- id: 'test1',
- config: {
- testKey4: 'testVal4',
- testKey5: 'testVal5',
- testKey6: {
- testKey7: 'testVal7'
- }
- },
- action: 'index',
- instanceId: 'yui_3_5_1_2_8_1345575332231_57351',
- yui: {}
- }
- };
- callback(null, specs[id]);
- },
- getType: function (env, type, context, callback) {
- var specs = {
- 'test_mojit_1': {
- type: 'test_mojit_1',
- id: 'test1',
- config: {
- testKey4: 'testVal4',
- testKey5: 'testVal5',
- testKey6: {
- testKey7: 'testVal7'
- }
- },
- action: 'index',
- instanceId: 'yui_3_5_1_2_8_1345575332231_57351',
- yui: {}
- }
+ expandInstanceForEnv: function (env, instance, context, callback) {
+ var spec = {
+ type: 'test_mojit_1',
+ id: 'test1',
+ config: {
+ testKey4: 'testVal4',
+ testKey5: 'testVal5',
+ testKey6: {
+ testKey7: 'testVal7'
+ }
+ },
+ action: 'index',
+ instanceId: 'yui_3_5_1_2_8_1345575332231_57351',
+ yui: {}
};
- callback(null, specs[type]);
+ callback(null, spec);
},
serializeClientStore: function (ctx) {
return {};
@@ -111,28 +90,6 @@ YUI().use('mojito-resource-store-adapter', 'test', function(Y) {
A.isTrue(instance.config.testKey4 === 'testVal4', 'testKey4 was wrong');
A.isTrue(instance.config.testKey6.testKey7 === 'testVal7', 'testKey6 was wrong');
});
- },
-
- 'server mojit config value via type & overrride': function() {
- var store = Y.mojito.ResourceStoreAdapter.init('server', resourceStore, dummyLog);
- var instance = {
- type:'test_mojit_1',
- config:{testKey4: 'other'}
- };
- store.expandInstance(instance, {}, function(err, instance){
- A.isTrue(instance.type === 'test_mojit_1');
- A.isTrue(instance.config.testKey4 === 'other');
- A.isTrue(instance.config.testKey5 === 'testVal5');
- });
- },
-
- 'test getType()': function(){
- var store = Y.mojito.ResourceStoreAdapter.init('server', resourceStore, dummyLog);
- store.getType('server', 'test_mojit_1', {}, function(err, instance){
- A.isTrue(instance.type === 'test_mojit_1');
- A.isTrue(instance.config.testKey4 === 'testVal4');
- A.isTrue(instance.config.testKey6.testKey7 === 'testVal7');
- });
}
}));
View
18 tests/unit/lib/test-store.server.js
@@ -287,24 +287,6 @@ YUI().use(
cmp(post, pre);
},
- 'call getSpec()': function() {
- store.getSpec('server', 'test1', {}, function(err, instance) {
- A.areSame('test_mojit_1', instance.type);
- A.areSame('test1', instance.id);
- // ... and all the type-specific parts...
- A.areSame('/static/test_mojit_1/assets', instance.assetsRoot);
- });
- },
-
- 'call getType()': function() {
- store.getType('server', 'test_mojit_1', {}, function(err, instance) {
- A.areSame('test_mojit_1', instance.type);
- A.isUndefined(instance.id);
- // ... and all the type-specific parts...
- A.areSame('/static/test_mojit_1/assets', instance.assetsRoot);
- });
- },
-
'instance with base pointing to non-existant spec': function() {
var spec = { base: 'nonexistant' };
store.expandInstance(spec, {}, function(err, instance) {
Please sign in to comment.
Something went wrong with that request. Please try again.