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 { isType } from 'tcomb'; | |
/** | |
* (...values) => (...types) => value||[values] | |
* | |
* This is a curried function to help with type checking your javascript. | |
* It is curried in two parts, the first part taking the values that you | |
* wish to type check, the second part of the curry takes the types you wish | |
* to validate them with. | |
* |
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
// This gist illustrates a very simple strategy of declaring and exposing | |
// dependencies for your modules. Allowing them to be overridden for | |
// tests (or other), and providing a mechanism to restore the original | |
// dependencies. | |
// It's a work in progress, is very simple and naive, but its a simple | |
// mechanism to achieve required results without having to depend on | |
// any complex DI frameworks. |
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
// This is a basic demonstration of using mockery alongside mocha/chai | |
// for unit testing in node. |
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
const model = { | |
todos: { | |
items: [], | |
}, | |
session: { | |
user: null | |
} | |
}; |
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
const model = { | |
todos: { | |
items: [], | |
// 👇 an action | |
add: (state, payload) => { | |
state.items.push(payload); | |
} | |
}, | |
session: { | |
user: null |
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 'easy-peasy'; | |
// Pass in your model and you get back a Redux store | |
const store = createStore(model); | |
// you can query state as normal | |
store.getState().todos.items; | |
// ['Install easy-peasy'] | |
// and dispatch actions |
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 { StoreProvider, createStore, useStore, useAction } from 'easy-peasy'; | |
// 👇 firstly, create your store by providing your model | |
const store = createStore({ | |
todos: { | |
items: ['Install easy-peasy', 'Build app', 'Profit'], | |
// 👇 define actions | |
add: (state, payload) => { | |
state.items.push(payload) // 👈 you mutate state to update (we convert | |
// to immutable updates) |
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 { StoreProvider } from 'easy-peasy'; | |
function App() { | |
return ( | |
// 👇 secondly, surround your app with the provider to expose the store to your app | |
<StoreProvider store={store}> | |
<TodoList /> | |
</StoreProvider> | |
); | |
} |
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 { useStore, useAction } from 'easy-peasy'; | |
function TodoList() { | |
// 👇 Access state | |
const todos = useStore(state => state.todos.items) | |
// 👇 Access actions | |
const add = useAction(dispatch => dispatch.todos.add) | |
return ( | |
<div> | |
{todos.map((todo, idx) => <div key={idx}>{todo.text}</div>)} |
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, effect } from 'easy-peasy'; // 👈 import the helper | |
const store = createStore({ | |
session: { | |
user: undefined, | |
// 👇 define your effectful action | |
login: effect(async (dispatch, payload, getState) => { | |
const user = await loginService(payload) | |
dispatch.session.loginSucceeded(user) | |
}), |
OlderNewer