Skip to content

Instantly share code, notes, and snippets.

View bradfordlemley's full-sized avatar

Bradford Lemley bradfordlemley

View GitHub Profile
@bradfordlemley
bradfordlemley / Counter.js
Last active May 24, 2019 17:27
Vanilla counter library
function createCounter() {
let counter = 0;
return {
get counter() { return counter },
increment() { counter = counter + 1},
decrement() { counter = counter - 1},
}
}
@bradfordlemley
bradfordlemley / TodoLib.js
Last active May 29, 2019 20:57
Vanilla todo library
function createTodoLib() {
let todos = [];
let isFetching = false;
return {
get todos() {return todos},
get isFetching() {return isFetching},
addTodo(title) {
todos = todos.concat({title});
@bradfordlemley
bradfordlemley / Counter.js
Last active May 24, 2019 17:26
Vanilla counter with standardized state
export default function createCounter() {
let state = 0;
return {
get state() { return state },
increment() { state = state + 1},
decrement() { state = state - 1},
}
}
@bradfordlemley
bradfordlemley / TodoLib.js
Last active May 29, 2019 20:56
Vanilla todo library with standardized state
export default function createTodoLib() {
let state = {
todos: [],
isFetching: false,
};
return {
get state() {return state},
addTodo(title) {
@bradfordlemley
bradfordlemley / Counter.js
Last active May 24, 2019 17:26
Counter with observable state
export default function createCounter() {
let state = 0;
let state$ = new Observable();
function setState(newState) {
state = newState;
state$.next(state);
}
@bradfordlemley
bradfordlemley / TodoLib.js
Last active May 29, 2019 20:55
Todo library with observable state
export default function createTodoLib() {
let state = {
todos: [],
isFetching: false,
};
let state$ = new Observable();
function setState(newState) {
state = newState;
@bradfordlemley
bradfordlemley / state.js
Last active May 29, 2019 23:16
State composition with mapState
import { mapState } from '@stated-library/core';
const todoLib = createTodoLib();
const counterLib = createCounter();
const appState$ = mapState(
[todoLib.state$, counterLib.state$],
([todoLibState, counterLibState]) => ({
todos: todoLibState.todos,
counter: counterLibState,
@bradfordlemley
bradfordlemley / App.jsx
Last active May 23, 2019 00:29
React component using observable state via connect binding
import { appState$ } from './state';
const App = ({todos, counter, total}) =>
<>
<div>counter: {counter}</div>
<div>todos: {todos.map(todo => <div>{todo.title}</div>)}</div>
<div>total: {total}</div>
</>
export default connect(appState$)(App);
@bradfordlemley
bradfordlemley / App.jsx
Last active May 23, 2019 00:31
React component invoking functionality object methods
import { appState$, todoLib } from './state';
const App = ({todos, counter, total}) =>
<>
<button onClick={() => todoLib.addTodo("New todo")}>
Add Todo
</button>
<div>counter: {counter}</div>
<div>todos: {todos.map(todo => <div>{todo.title}</div>)}</div>
<div>total: {total}</div>
@bradfordlemley
bradfordlemley / state.js
Last active May 29, 2019 22:19
Adding methods to composed state
import { mapState } from '@stated-library/core';
const todoLib = createTodoLib();
const counterLib = createCounter();
const appState$ = mapState(
[todoLib.state$, counterLib.state$],
([todoLibState, counterLibState]) => ({
todos: todoLibState.todos,
counter: counterLibState,