Last active
August 29, 2015 14:10
-
-
Save ChetHarrison/c610d4fe4a5e9f9280a2 to your computer and use it in GitHub Desktop.
Revisions
-
ChetHarrison revised this gist
Nov 20, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -66,7 +66,7 @@ subscribe( 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 }); -
ChetHarrison revised this gist
Nov 20, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -66,7 +66,7 @@ subscribe( 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 value not a referance console.log(frenchFryNutrition.getNutrients()); // { salt: 100, sugar: 95, calories: 50 } // test modification of ingredient frenchFryNutrition.setNutrients({ salt: 5 }); -
ChetHarrison revised this gist
Nov 20, 2014 . 4 changed files with 84 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,9 +18,9 @@ var EventEmitter = require('events').EventEmitter, globalChannelDestroyEvent = Rx.Observable.fromEvent( globalChannel, 'destroy').take(1), // declaration //----------------------------------------- frenchFryNutrition = Object.create( nutrients ); // using delegation pattern // lazy instantiation //----------------------------------------- @@ -73,8 +73,20 @@ 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ '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; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ '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 ); } }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ '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; } }; -
ChetHarrison created this gist
Nov 20, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,80 @@ '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 ); // 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; 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'); globalChannel.emit('nutrientRequest', 'sugar'); globalChannel.emit('nutrientRequest', 'calories'); globalChannel.emit('destroy');