Skip to content

Instantly share code, notes, and snippets.

@selfsame
Last active May 3, 2018 18:06
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 selfsame/810a63a131ab78fb4332284f08260036 to your computer and use it in GitHub Desktop.
Save selfsame/810a63a131ab78fb4332284f08260036 to your computer and use it in GitHub Desktop.
import { createStore } from "redux"
const shallowcopy = (col) => {
if (Array.isArray(col)) {
return [...col]}
else {
return {...col}}}
const assoc = (col, k, v) => {
col = shallowcopy(col)
col[k] = v
return col}
const update = (col, k, f) => {
col = shallowcopy(col)
col[k] = f(col[k])
return col}
const action = (type, payload) => ({type:type, payload:payload})
const initialState = {
title: 'botnik',
view: 'dashboard',
loggedin: true}
const Reducer = (state = initialState, action) => {
switch (action.type) {
case 'ASSOC' : return assoc(state, action.payload.k, action.payload.v)
case 'UPDATE': return update(state, action.payload.k, action.payload.f)
default: return state}}
const store = createStore(Reducer)
const ASSOC = (k, v) => store.dispatch(action('ASSOC', {k:k, v:v}))
const UPDATE = (k, f) => store.dispatch(action('UPDATE', {k:k, f:f}))
export {store, assoc, update, ASSOC, UPDATE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment