Skip to content

Instantly share code, notes, and snippets.

View ctrlplusb's full-sized avatar
📢

Sean Matheson ctrlplusb

📢
View GitHub Profile
@ctrlplusb
ctrlplusb / tCheck.js
Created January 20, 2016 21:56
Using tcomb to type check your functions without annotations.
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.
*
@ctrlplusb
ctrlplusb / 00_Simple_dependency_management_for_tests_etc
Last active February 24, 2016 11:48
NodeJS simple dependency management for modules.
// 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 is a basic demonstration of using mockery alongside mocha/chai
// for unit testing in node.
@ctrlplusb
ctrlplusb / model.js
Created November 5, 2018 17:52
Easy peasy model
const model = {
todos: {
items: [],
},
session: {
user: null
}
};
@ctrlplusb
ctrlplusb / model-with-action.js
Created November 5, 2018 17:54
Easy peasy model with action
const model = {
todos: {
items: [],
// 👇 an action
add: (state, payload) => {
state.items.push(payload);
}
},
session: {
user: null
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
@ctrlplusb
ctrlplusb / easy-peasy.js
Last active November 5, 2018 20:05
Easy Peasy Primary API
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)
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>
);
}
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>)}
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)
}),