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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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>
            
          
!
            
              div {
  font-size: 14pt;
}
            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console