cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container">

  <div>
    A <a href="https://sam.js.org" target="_blank">SAM pattern</a> rocket launcher example implemented with <a href="https://redux.js.org" target="_blank">Redux</a>.
 </div>

  <hr>

  <div id="app"></div>
  <div id="tracer" style="position: fixed; top: 0px; right: 0px;"></div>
  
  <hr>
  
  <div>
    Credit: <a href="http://www.ebpml.org/about" target="_blank">Jean-Jacques Dubray</a>
  </div>
  <div>
    Reference: <a href="https://bitbucket.org/snippets/jdubray/9dgKp/sam-sample" target="_blank">https://bitbucket.org/snippets/jdubray/9dgKp/sam-sample</a>
  </div>
  
</div>
            
          
!
            
              var COUNTER_MAX = 10;

var initialModel = {
  counter: COUNTER_MAX,
  started: false,
  launched: false,
  aborted: false
};

var createActions = function(present) {
  return {
    start: function() {
      present({ started: true });
    },
    decrement: function(counter) {
      setTimeout(function() {
        present({ counter: counter - 1 });
      }, 1000);
    },
    launch: function() {
      present({ launched: true });
    },
    abort: function() {
      present({ aborted: true });
    }
  };
};

var state = {
  ready: function(model) {
    return ((model.counter === COUNTER_MAX) && !model.started && !model.launched && !model.aborted);
  },
  counting: function(model) {
    return ((model.counter <= COUNTER_MAX) && (model.counter >= 0) && model.started && !model.launched && !model.aborted);
  },
  launched: function(model) {
    return ((model.counter == 0) && model.started && model.launched && !model.aborted);
  },
  aborted: function(model) {
    return (
      (model.counter <= COUNTER_MAX) && (model.counter >= 0)
      && model.started && !model.launched && model.aborted
    );
  }
};

var modelPresent = function(state) {
  return function(model, typeAndData) {
    var data = typeAndData.data;
    if (!data) {
      return model;
    }
    if (state.counting(model)) {
      if (model.counter === 0) {
        model.launched = data.launched || false;
      }
      else {
        model.aborted = data.aborted || false;
        if (data.counter !== undefined) {
          model.counter = data.counter;
        }
      }
    }
    else if (state.ready(model)) {
      model.started = data.started || false;
    }
    return model;
  };
};

var createNap = function(state, actions) {
  return function(model) {
    if (state.counting(model)) {
      if (model.counter > 0) {
        actions.decrement(model.counter);
      }
      else if (model.counter === 0) {
        actions.launch();
      }
    }
  };
};

var view = {
  // State representation of the ready state
  ready: function(model) {
    return (
      "<p>Counter:"+model.counter+"</p>\n\
        <form class=\"start\">\n\
          <input type=\"submit\" value=\"Start\">\n\
        </form>"
    );
  },

  // State representation of the counting state
  counting: function(model) {
    return (
      "<p>Count down:"+model.counter+"</p>\n\
        <form class=\"counting\">\n\
          <input type=\"submit\" value=\"Abort\">\n\
        </form>"
    );
  },

  // State representation of the aborted state
  aborted: function(model) {
    return (
      "<p>Aborted at Counter:"+model.counter+"</p>\n"
    );
  },

  // State representation of the launched state
  launched: function(_model) {
    return (
      "<p>Launched</p>"
    );
  }
};

var createDisplay = function(state, view) {
  return function(model) {
    var representation = "oops... something went wrong, the system is in an invalid state";

    if (state.ready(model)) {
      representation = view.ready(model);
    }

    if (state.counting(model)) {
      representation = view.counting(model);
    }

    if (state.launched(model)) {
      representation = view.launched(model);
    }

    if (state.aborted(model)) {
      representation = view.aborted(model) ;
    }

    return representation;
  };
};                                                                                          

var $root = $(document.getElementById("app"));

var ready = function(actions) {
  $root.on("submit", "form.start", function(evt) {
    evt.preventDefault();
    actions.start();
  });

  $root.on("submit", "form.counting", function(evt) {
    evt.preventDefault();
    actions.abort();
  });
};

var store = Redux.createStore(modelPresent(state), initialModel);

var actions = createActions(function(data) {
  store.dispatch({type: "PRESENT", data: data});
});
var nap = createNap(state, actions);

var display = createDisplay(state, view);

var render = function() {
  var model = store.getState();
  $root.html(display(model));
  nap(model);
};

store.subscribe(render);

render();
ready(actions);

            
          
!
999px
Loading ..................

Console