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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

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> 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
Loading ..................

Console