Last active
May 3, 2018 18:06
-
-
Save selfsame/810a63a131ab78fb4332284f08260036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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