Skip to content

Instantly share code, notes, and snippets.

@rstormsf
Forked from ChetHarrison/index.js
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rstormsf/e02f2e203b6cd01b5791 to your computer and use it in GitHub Desktop.
Save rstormsf/e02f2e203b6cd01b5791 to your computer and use it in GitHub Desktop.
'use strict';
var EventEmitter = require('events').EventEmitter,
globalChannel = new EventEmitter(),
Rx = require('rx'),
nutrients = require('./food-objects/nutrients'),
nutrients,
defaultHandlers = {
onError: function (err) {
console.log('Error: ' + err);
},
onComplete: function () {
console.log('Completed');
}
},
globalChannelDestroyEvent = Rx.Observable.fromEvent( globalChannel, 'destroy').take(1),
// declaration
//-----------------------------------------
frenchFryNutrition = Object.create( nutrients ); // using delegation pattern
// lazy instantiation
//-----------------------------------------
frenchFryNutrition.initNutrition({
state: {
salt: 100,
sugar: 95,
calories: 50
},
init: function() {
console.log('init was called on this stateContainer');
}
});
// events
//-----------------------------------------
// create observables
Rx.Observable.fromEvent( globalChannel, 'nutrientRequest',
function( eventArgs ) {
return frenchFryNutrition.getState()[ eventArgs[0] ];
}).
takeUntil(globalChannelDestroyEvent).
subscribe(
function( response ) { globalChannel.emit( 'nutrientResponse', response ); },
defaultHandlers.onError,
defaultHandlers.onComplete
);
Rx.Observable.fromEvent( globalChannel, 'nutrientResponse' ).
takeUntil(globalChannelDestroyEvent).
subscribe(
function( response ) { console.log( response ); },
defaultHandlers.onError,
defaultHandlers.onComplete
);
//-----------------------------------------
// main
//-----------------------------------------
console.log(frenchFryNutrition.getNutrients()); // { salt: 100, sugar: 95, calories: 50 }
// test that we got a value not reference
nutrients = frenchFryNutrition.getNutrients();
nutrients.salt = 25; // this does not work because getNutrients() returns a shallow copy not a reference
console.log(frenchFryNutrition.getNutrients()); // { salt: 100, sugar: 95, calories: 50 }
// test modification of ingredient
frenchFryNutrition.setNutrients({ salt: 5 });
console.log(frenchFryNutrition.getNutrients()); // { salt: 5, sugar: 95, calories: 50 }
// test events
globalChannel.emit('nutrientRequest', 'salt'); // 5
globalChannel.emit('nutrientRequest', 'sugar'); // 95
globalChannel.emit('nutrientRequest', 'calories'); // 50
globalChannel.emit('destroy'); // Completed\nCompleted
// output
//-----------------------------------------
// $ node index.js
// init was called on this stateContainer
// { salt: 100, sugar: 95, calories: 50 }
// { salt: 100, sugar: 95, calories: 50 }
// { salt: 5, sugar: 95, calories: 50 }
// 5
// 95
// 50
// Completed
// Completed
'use strict';
var nutrient = Object.create( require('../base-objects/state-container') );
nutrient.initNutrition = function( options ) {
this.initStateContainer( options );
};
nutrient.getNutrients = function() {
return this.getState();
};
nutrient.setNutrients = function( nutrients ) {
// only change the ones in the arg
var keys = Object.keys( nutrients ),
state = this.state;
keys.forEach(function(key) {
state[key] = nutrients[key];
});
};
module.exports = nutrient;
'use strict';
var util = require('./utilities');
module.exports = {
initStateContainer: function( options ) {
// can send events
options = options || {};
this.setState( options.state );
this.observables = options.observables || [];
this.init = options.init || util.noop;
this.init();
},
setState: function( state ) {
this.state = state || {};
},
getState: function() {
return util.shallowCopy( this.state );
}
};
'use strict';
module.exports = {
noArg: function fatal(name) {
throw new Error('Missing required parameter: ' + name);
},
noop: function(){},
shallowCopy: function( obj ) {
var keys = Object.keys( obj ),
copy = {};
keys.forEach(function(key) {
copy[key] = obj[key];
});
return copy;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment