cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-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://meiosis.js.org" target="_blank">Meiosis</a>.
 </div>

  <div>
    Explanations are in the <a href="https://foxdonut.gitbooks.io/meiosis-guide/" target="_blank">Meiosis Guide</a>. You will also find the source code in the <a href="https://github.com/foxdonut/meiosis-examples/tree/master/examples/rocket-launcher" target="_blank">meiosis-examples</a> repository.
  </div>
  
  <hr>

  <div id="app"></div>
  <div id="tracer" style="position: fixed; top: 0px; right: 0px;"></div>
  
  <hr>
  
  <div>
    Rocket Launcher idea 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 actions = function(propose) {
  return {
    start: function() {
      propose({ started: true });
    },
    decrement: function(counter) {
      setTimeout(function() {
        propose({ counter: counter - 1 });
      }, 1000);
    },
    launch: function() {
      propose({ launched: true });
    },
    abort: function() {
      propose({ 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 receive = function(state) {
  return function(model, proposal) {
    if (state.counting(model)) {
      if (model.counter === 0) {
        model.launched = proposal.launched || false;
      }
      else {
        model.aborted = proposal.aborted || false;
        if (proposal.counter !== undefined) {
          model.counter = proposal.counter;
        }
      }
    }
    else if (state.ready(model)) {
      model.started = proposal.started || false;
    }

    return model;
  };
};

var nextAction = function(state) {
  return function(model, proposal, actions) {
    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\" class=\"btn btn-primary\" value=\"Start\">\n\
        </form>"
    );
  },

  // State representation of the counting state
  counting: function(model) {
    return (
        "<p>\n\
          Count down: "+model.counter+" "+(model.even ? "(Even)" : "(Odd)") + "\n" +
          (model.closeToLaunch ? " CLOSE TO LAUNCH!" : "") +
        "</p>\n\
        <form class=\"counting\">\n\
          <input type=\"submit\" class=\"btn btn-danger\" 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 display = 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 renderer = meiosisVanillaJs.renderer();

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

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

var createComponent = meiosis.createComponent;

var Main = createComponent({
  view: display(state, view),
  actions: actions,
  ready: ready,
  receive: receive(state),
  nextAction: nextAction(state)
});

var stateFn = function(model) {
  var appState = Object.assign({}, model);
  appState.even = model.counter % 2 === 0;
  appState.closeToLaunch = model.counter < 4;
  return appState;
};

var renderRoot = meiosis.run({
  renderer: renderer.intoId(document, "app"),
  initialModel: initialModel,
  state: stateFn,
  rootComponent: Main
});

meiosisTracer(createComponent, renderRoot, "#tracer");

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console