Skip to content

Instantly share code, notes, and snippets.

@myobie
Created August 2, 2018 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/ef42a76bb9eb44d102145b447e5c581b to your computer and use it in GitHub Desktop.
Save myobie/ef42a76bb9eb44d102145b447e5c581b to your computer and use it in GitHub Desktop.
This is an exploration into the "dream code" I wish I could write to make a quick single page app
// Since es6 exports are statically validated, we have no need to use stringy
// programming to identify which action to run in response to which event
import { render } from 'some-lib'
export function increment ({ state, emit }) {
state.counter += 1
emit(render)
}
export function decrement ({ state, emit }) {
if (state.counter > 0) {
state.counter -= 1
emit(render)
}
}
import { createApp } from 'some-lib'
import { store } from './store'
import { view } from './view'
const el = createApp(store, view)
document.body.appendChild(el)
import { createStore } from 'some-lib'
import * as actions from './actions'
export function store () {
const state = inititalState()
return createStore(state, actions) // can pass in a whitelist of actions as the second argument as shown, but is optional
}
function initialState () {
// You can choose to return an immutable object here
return {
counter: 0
}
}
import html from 'nanohtml'
import css from 'emotion'
import { increment, decrement } from './actions'
// We emit the reference to the action, we do not execute the action directly
// The store will handle running the actions in order, dealing with side effects, etc
export function view (state, emit) {
return html`
<div class=${css`
background-color: pink;
width: 200px;
margin: 0 auto;
`}>
<h1>Count is ${state.counter}</h1>
<button onclick=${emit(increment)}>Increment</button>
<button onclick=${emit(decrement)}>Decrement</button>
</div>
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment