Created with svelte.dev/repl
Last active
October 16, 2019 07:40
-
-
Save Sawtaytoes/b089c33fe78667593a76c159da4c8dc9 to your computer and use it in GitHub Desktop.
Hello world
This file contains hidden or 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
<script> | |
import { createStore } from './fakeRedux.js' | |
let count = 0 | |
const stateModifiers = { | |
DECREMENT: () => { | |
count -= 1 | |
}, | |
INCREMENT: () => { | |
count += 1 | |
}, | |
} | |
const store = ( | |
createStore( | |
stateModifiers | |
) | |
) | |
</script> | |
<h1>Count: {count}</h1> | |
<button | |
on:click={() => { | |
store.dispatch({ type: 'DECREMENT' }) | |
}} | |
> | |
+ | |
</button> | |
<button | |
on:click={() => { | |
store.dispatch({ type: 'INCREMENT' }) | |
}} | |
> | |
- | |
</button> |
This file contains hidden or 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
export var createStore = function(stateModifiers) { | |
var localState = { | |
subscribers: [], | |
} | |
var subscribe = function(subscribers) { | |
localState | |
.subscribers = ( | |
localState | |
.subscribers | |
.concat(subscribers) | |
) | |
return { | |
unsubscribe: function() { | |
subscribers | |
.forEach(function(subscriber) { | |
const index = ( | |
localState | |
.subscribers | |
.indexOf(subscriber) | |
) | |
localState | |
.subscribers | |
.splice(index, 1) | |
}) | |
} | |
} | |
} | |
var createDispatch = function(stateModifiers) { | |
return function(action) { | |
if (!action.type) { | |
console | |
.error( | |
"No type for action", | |
action, | |
) | |
} | |
if (window.__isDebugging) { | |
console | |
.info( | |
action.type, | |
action, | |
) | |
} | |
stateModifiers[action.type] | |
&& stateModifiers[action.type](action) | |
localState | |
.subscribers | |
.forEach(function(subscriber) { | |
subscriber(action) | |
}) | |
return action | |
} | |
} | |
return { | |
dispatch: createDispatch(stateModifiers), | |
subscribe: subscribe, | |
} | |
} |
This file contains hidden or 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
{ | |
"svelte": true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment