Skip to content

Instantly share code, notes, and snippets.

@ChetHarrison
Last active August 29, 2015 14:10
Show Gist options
  • Save ChetHarrison/c610d4fe4a5e9f9280a2 to your computer and use it in GitHub Desktop.
Save ChetHarrison/c610d4fe4a5e9f9280a2 to your computer and use it in GitHub Desktop.

Revisions

  1. ChetHarrison revised this gist Nov 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.js
    Original 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
    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 });
  2. ChetHarrison revised this gist Nov 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.js
    Original 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;
    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 });
  3. ChetHarrison revised this gist Nov 20, 2014. 4 changed files with 84 additions and 7 deletions.
    26 changes: 19 additions & 7 deletions index.js
    Original 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 );
    // 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');
    globalChannel.emit('nutrientRequest', 'sugar');
    globalChannel.emit('nutrientRequest', 'calories');
    globalChannel.emit('destroy');
    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
    23 changes: 23 additions & 0 deletions nutrients.js
    Original 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;
    22 changes: 22 additions & 0 deletions state-container.js
    Original 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 );
    }
    };
    20 changes: 20 additions & 0 deletions utilities.js
    Original 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;
    }
    };
  4. ChetHarrison created this gist Nov 20, 2014.
    80 changes: 80 additions & 0 deletions index.js
    Original 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');