cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

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="http://sam.js.org" target="_blank">SAM pattern</a> rocket launcher example implemented with <a href="http://meiosis.js.org" target="_blank">Meiosis</a> and <a href="https://github.com/paldepind/snabbdom/" target="_blank">Snabbdom</a>.
 </div>

  <div>
    Explanations are in the <a href="https://foxdonut.gitbooks.io/meiosis-guide/" target="_blank">Meiosis Guide</a>.
  </div>
  
  <hr>

  <div id="app"></div>
  <div id="tracer" style="position: fixed; top: 0px; right: 0px;"></div>
  
  <hr>
  
  <div>
    Rocker 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>
            
          
!
            
              const COUNTER_MAX = 10;

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

const actions = propose => ({
  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 });
  }
});

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

const receive = state => (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;
};

const nextAction = state => (model, proposal, actions) => {
  if (state.counting(model)) {
    if (model.counter > 0) {
      actions.decrement(model.counter);
    }
    else if (model.counter === 0) {
      actions.launch();
    }
  }
};

var h = meiosisSnabbdom.renderer.h;

const view = {
  // State representation of the ready state
  ready: (model, actions) => {
    const onStart = function(evt) {
      evt.preventDefault();
      actions.start();
    };
    return h("div", [
      h("p", "Counter: " + model.counter),
      h("form", [
        h("input.btn.btn-primary", {attrs: {type: "submit", value: "Start"}, on: {click: onStart}})
      ])
    ]);
  },

  // State representation of the counting state
  counting: (model, actions) => {
    const onAbort = function(evt) {
      evt.preventDefault();
      actions.abort();
    };
    return h("div", [
      h("p", "Count down: " + model.counter + " " + (model.even ? "(Even)" : "(Odd)") +
        (model.closeToLaunch ? " CLOSE TO LAUNCH!" : "")),
      h("form", [
        h("input.btn.btn-danger", {attrs: {type: "submit", value: "Abort"}, on: {click: onAbort}})
      ])
    ]);
  },

  // State representation of the aborted state
  aborted: model => h("p", "Aborted at Counter: " + model.counter),

  // State representation of the launched state
  launched: () => h("p", "Launched")
};

const display = (state, view) => (model, actions) => {
  var representation = "oops... something went wrong, the system is in an invalid state";

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

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

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

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

  return representation;
};

const createComponent = meiosis.createComponent;

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

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

const renderRoot = meiosis.run({
  renderer: meiosisSnabbdom.renderer().intoId(document, "app"),
  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