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 id="representation">
	<blockquote>Problem is (follow me closely here, the science is pretty complicated), if I cut a hole in the Hab, the air won't stay inside anymore.</blockquote><br>
― Andy Weir, The Martian
</div>

            
          
!
            
              const {makeDOMDriver, div, form, input, p} = CycleDOM;
const {run} = Cycle;
const {identity, merge} = R;

const STOPPED = Symbol();
const STARTED = Symbol();
const ABORTED = Symbol();
const LAUNCHED = Symbol();

function intent(DOMSource) {
  return {
    start$: DOMSource.select(".start").events("submit")
      .map(evt => {
        evt.preventDefault();
      }),

    abort$: DOMSource.select(".abort").events("submit")
      .map(evt => {
        evt.preventDefault();
      })
  };
}

function model(intents, initialState) {
  const started$ = intents.start$.map(() => ({status: STARTED}));
  const aborted$ = intents.abort$.map(() => ({status: ABORTED}));
  
  const counter$ = Rx.Observable.timer(0, 1000)
    .map(() => ({counterChange: -1}));

  return Rx.Observable.of(initialState)
    .merge(started$)
    .merge(aborted$)
    .merge(counter$)
    .scan((state, update) => {
      const nextState = merge(state, update);
      if (state.status === STARTED && update.counterChange) {
        nextState.counter += update.counterChange;
      }
      if (nextState.counter === 0) {
        nextState.status = LAUNCHED;
      }
      return nextState;
    });
}

const showIf = visible => visible ? "" : ".hidden";

function view(state$) {
  return state$.map(state => div([
    div(showIf(state.status === STOPPED), [
      p(`Counter:${state.counter}`),
      form(".start", [
        input({type: "submit", value: "Start"})
      ])
    ]),
    div(showIf(state.status === STARTED), [
      p(`Count down:${state.counter}`),
      form(".abort", [
        input({type: "submit", value: "Abort"})
      ])
    ]),
    p(showIf(state.status === ABORTED), `Aborted at Counter:${state.counter}`),
    p(showIf(state.status === LAUNCHED), "Launched")
  ]));
}

const Launcher = sources => ({ DOM: view(model(intent(sources.DOM), sources.initialState)) });

const main = function(sources) {
  const initialState = {
    counter: 10,
    status: STOPPED
  };

  return Launcher({ DOM: sources.DOM, initialState });
}

const drivers = {
  DOM: makeDOMDriver("#representation")
};

run(main, drivers);
            
          
!
999px
Loading ..................

Console