Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
version of PR1236 (hotswap support) for "next" branch
Browse files Browse the repository at this point in the history
  • Loading branch information
drewfish committed Sep 9, 2013
1 parent 3e8f18f commit 13af8eb
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 52 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Expand Up @@ -9,6 +9,10 @@ Deprecations, Removals

Features
--------
* The `lib/store.js` has a new `getAppConfig()` function. This is a better choice
for reading the static application configuration than `createStore()`.
* The resource store now exposes the server's runtime YUI instance via the
`runtimeYUI` member. Resource store addons can access it via `this.get('host').runtimeYUI`.

Bug Fixes
---------
Expand Down
91 changes: 48 additions & 43 deletions lib/mojito.js
Expand Up @@ -84,49 +84,42 @@ Creates a new instance of mojito and attach to `app`.
@method _init
@protected
@param {express.application} app optional
@param {express.application} app optional
**/
Mojito.prototype._init = function (app) {

app = app || this._app;

var my = this,
appConfig, // application.json
options, //
pack, // package.json
root, // the app root directory
store; // reference to resource store
var context = {},
appConfig, // application.json
options = {}, //
store; // reference to resource store

// debug('applying mojito.defaultConfiguration()');

if (!app.mojito) {
throw new Error('`app.mojito` was not initialized correctly.');
}

// TODO: `root` is required for the resource store, which will be replaced
// by `locator` at some point. `locator` will have access to the appRoot
// folder.
// The application root directory
root = process.cwd();

store = libstore.createStore({
root: root,
preload: 'skip', // only need appConfig and package.json
context: { }
});
appConfig = store.getAppConfig();
// TODO: use correct static context here
context.runtime = 'server';

options = {};
options.port = process.env.PORT || appConfig.appPort || 8666;
options.context = { runtime: 'server' }; // TODO: use correct static context here
options.context = context;
options.mojitoRoot = __dirname;
options.root = root;
// TODO: `root` is required for the resource store, which will be replaced
// by `locator` at some point. `locator` will have access to the appRoot
// folder.
// The application root directory
options.root = process.cwd();

// create new store, configure mojito, and optimize
appConfig = libstore.getAppConfig(options.root, context);
options.Y = this._createYUIInstance(options, appConfig);
liblogger.configureLogger(options.Y);
store = libstore.createStore(options);

my._configureAppInstance(app, store, options);
this._configureAppInstance(app, store, options, appConfig);

// set port from application.json
app.set('port', options.port);
Expand Down Expand Up @@ -167,27 +160,18 @@ Mojito.prototype._configureYUI = function (Y, store) {


/**
Adds Mojito framework components to the Express application instance.
Creates the YUI instance used by the server.
@method _configureAppInstance
@method _createYUIInstance
@protected
@param {express.application} app The Express application instance to Mojito-enable.
@param {ResourceStore} store
@param {Object} options
@param {Integer} options.port port to listen on
@param {Object} options.context static context set at startup
@param {String} options.mojitoRoot
@param {String} options.root the app root dir
**/
Mojito.prototype._configureAppInstance = function (app, store, options) {
var my = this,
Y,
appConfig,
yuiConfig,
@param {Object} options Same as the `options` parameter to `_configureAppInstance()`.
@param {Object} appConfig The static configuration for the application.
*/
Mojito.prototype._createYUIInstance = function (options, appConfig) {
var yuiConfig,
modules = [],
debugConfig;
Y;

appConfig = store.getStaticAppConfig();
yuiConfig = (appConfig.yui && appConfig.yui.config) || {};

// redefining "combine" and/or "base" in the server side have side effects
Expand Down Expand Up @@ -216,8 +200,29 @@ Mojito.prototype._configureAppInstance = function (app, store, options) {
useSync: true
});

liblogger.configureLogger(Y);
modules = my._configureYUI(Y, store);
return Y;
};


/**
Adds Mojito framework components to the Express application instance.
@method _configureAppInstance
@protected
@param {express.application} app The Express application instance to Mojito-enable.
@param {ResourceStore} store
@param {Object} options
@param {Integer} options.port port to listen on
@param {Object} options.context static context set at startup
@param {String} options.mojitoRoot
@param {String} options.root the app root dir
@param {Object} appConfig The static configuration for the application.
**/
Mojito.prototype._configureAppInstance = function (app, store, options, appConfig) {
var Y = options.Y,
modules = [];

modules = this._configureYUI(Y, store);

// attaching all modules available for this application for the server side
Y.applyConfig({ useSync: true });
Expand Down
23 changes: 23 additions & 0 deletions lib/store.js
Expand Up @@ -35,6 +35,8 @@ var Store = {};
* @param {string "skip"|"initial"|"full"} options.preload Whether to preload
* the application. Defaults to "full". If you only care about appConfig
* and package.json you can use the 'skip' option which is faster.
* @param {object} options.Y The runtime Y instance used by the mojito server.
* This will be exposed to store addons as `store.runtimeYUI`.
* @return Y.mojito.ResourceStore
*/
Store.createStore = function(options) {
Expand Down Expand Up @@ -91,6 +93,8 @@ Store.createStore = function(options) {
appConfig = store.getStaticAppConfig();
Y.applyConfig((appConfig && appConfig.yui && appConfig.yui.config) || {});

store.runtimeYUI = options.Y;

if ('initial' === options.preload) {
store.preloadInitial();
} else if ('skip' !== options.preload) {
Expand All @@ -100,6 +104,25 @@ Store.createStore = function(options) {
return store;
};


/**
* Returns the application config.
* @method getAppConfig
* @param {string} dir The directory containing the application config.
* @param {Object} context The runtime context.
* @return {Object} The application config.
*/
Store.getAppConfig = function(dir, context) {
var store;
store = this.createStore({
root: dir,
context: context,
preload: 'skip'
});
return store.getStaticAppConfig();
};


// ----------------------------------------------------------------------------
// EXPORT(S)
// ----------------------------------------------------------------------------
Expand Down
20 changes: 11 additions & 9 deletions tests/unit/lib/test-mojito.js
Expand Up @@ -673,8 +673,7 @@ YUI().use('mojito', 'mojito-test-extra', 'test', function (Y) {
'test configureAppInstance': function () {
var dispatcher,
madeY, // the Y instance made in mojito.js
appwtf = {
app = {
store: {
getAllURLDetails: function () {
return {};
Expand Down Expand Up @@ -721,16 +720,19 @@ YUI().use('mojito', 'mojito-test-extra', 'test', function (Y) {
use: function (x) {
dispatcher = x;
}
},
options = {},
appConfig = {
perf: true,
middleware: ['mojito-handler-dispatcher']
};
Mojito.Server.prototype._configureLogger = function(y) {
madeY = y;
};
Mojito.Server.prototype._configureAppInstance(appwtf);
options.Y = Mojito.Server.prototype._createYUIInstance(options, appConfig);
Mojito.Server.prototype._configureAppInstance(app, options, appConfig);
dispatcher({command: {}}, {}, function() {});
A.isObject(madeY.mojito.Dispatcher.outputHandler, 'output handler object');
A.isObject(madeY.mojito.Dispatcher.outputHandler.page, 'page object');
A.isObject(madeY.mojito.Dispatcher.outputHandler.page.staticAppConfig, 'staticAppConfig object');
A.isObject(options.Y.mojito.Dispatcher.outputHandler, 'output handler object');
A.isObject(options.Y.mojito.Dispatcher.outputHandler.page, 'page object');
A.isObject(options.Y.mojito.Dispatcher.outputHandler.page.staticAppConfig, 'staticAppConfig object');
}
*/

Expand Down

0 comments on commit 13af8eb

Please sign in to comment.