Skip to content

Commit

Permalink
enhanced: console example
Browse files Browse the repository at this point in the history
  • Loading branch information
valango committed Jul 21, 2015
1 parent 0bbdbe2 commit 344adcb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
32 changes: 22 additions & 10 deletions examples/node/console.js
Expand Up @@ -9,28 +9,34 @@

'use strict';

// Event bus is the only connection between functional modules.
// Event bus is the only connection between functional modules.
var bus = require('eventist')();
var emit = bus.emit;
var logging = true;
var pendingInput = null;

// Override the original method for logging purposes.
bus.emit = function () {
logging && emit.apply(bus, ['ui', 'log', Array.prototype.join.call(arguments, '::')]);
logging &&
emit.apply(bus, ['ui', 'log', Array.prototype.join.call(arguments, '::')]);

return emit.apply(bus, arguments);
};

/*
Application logic:
- maintaining dynamic configuration and connections between modules;
- implementing some functions of existential significance.
Application logic:
- maintaining dynamic configuration and connections between modules;
- implementing some functions of existential significance.
*/
bus
.on('module.connected', function () {
.on('module.connected', function (name) {
if (name === 'decoder') {
bus.send('user', pendingInput);
}
return true;
})
.once('module.quit', function (name) {
// Make a dramatic farewell before finishing the seppuku.
// Make a dramatic farewell during committing seppuku.
bus.send('ui', 'say',
['Ewwww... you have killed my', name, '!!! :,('].join(' '));
var left = 9
Expand All @@ -47,6 +53,14 @@ bus
};
die();
})
// Here we simulate the lazy loading scenario.
.once('user', function (input) {
bus.send('ui', 'say', 'Did you say "' + (pendingInput = input) + '"?');
bus.send('ui', 'say', 'Just a second - the interpreter is being loaded...');
setTimeout(function () {
require('./decoder')(bus);
}, 1000);
})
// Some application-level events are handled here.
.on('app', function (cmd, a) {
switch (cmd) {
Expand All @@ -71,9 +85,7 @@ bus
return true;
});

// Here we load functional modules

// Load functional modules
require('./ui-simple')(bus);
require('./decoder')(bus);
// And now we are good to go...
bus.send('ui', 'prompt');
12 changes: 9 additions & 3 deletions examples/node/decoder/index.js
Expand Up @@ -17,7 +17,7 @@ var decode;
*
* @param {string} command
*/
var lastTry = function(command){
var lastTry = function (command) {

var res = !retry && bus.emit('app', 'bad-user', command);

Expand All @@ -26,7 +26,7 @@ var lastTry = function(command){
} else if (tolerance) {
bus.send('ui', 'say',
['I do not understand! Type "help", if you mind...',
tolerance, 'try left.'].join(' '));
tolerance, 'try is left.'].join(' '));
--tolerance;
bus.send('ui', 'prompt');
} else { // Module suicide - letting the whole world to know.
Expand All @@ -36,9 +36,12 @@ var lastTry = function(command){
}
};

/* jshint maxcomplexity:10 */
// Handle the 'user' events and return true to indicate it's done.
/* jshint maxcomplexity:12 */
decode = function (command) {

command = command.trim();

switch (command) {
case 'help':
bus.send('ui', 'say',
Expand Down Expand Up @@ -71,6 +74,9 @@ decode = function (command) {
bus.send('app', 'close');
break;

case '':
break;

default:
return lastTry(command);
}
Expand Down
13 changes: 12 additions & 1 deletion examples/node/ui-simple/index.js
Expand Up @@ -3,14 +3,24 @@
* and performs rudimentary output functions.
*
* @author Villem Alango <villem.alango@gmail.com>
*/
*/

'use strict';

var B = '\u001B[34m'; // blue
var G = '\u001B[32m'; // green
var M = '\u001B[35m'; // magenta
var R = '\u001B[39m'; // reset
var helloText = [
'Hey, I am a simple terminal-style interpreter demo.',
'You type something in and I shall do something... not dangerous.',
'Meaning of the colors:',
G + ' - your input;',
B + ' - internal events log;',
M + ' - my responses.',
R + 'Good luck!',
''];

var name = 'ui-simple'
, bus
, prompt = 'OHAI'
Expand Down Expand Up @@ -64,6 +74,7 @@ var init = function (busInstance) {

clear();
setPrompt('OHAI');
console.log(helloText.join('\n'));

// Actual input feed event listener.
rl.on('line', function (line) {
Expand Down

0 comments on commit 344adcb

Please sign in to comment.