Skip to content

Instantly share code, notes, and snippets.

View bradfordlemley's full-sized avatar

Bradford Lemley bradfordlemley

View GitHub Profile
@bradfordlemley
bradfordlemley / StatedLibBase.js
Last active May 29, 2019 20:53
Stated Library base implementation
export default function createBase(initialState){
let state = initialState;
let state$ = new Observable();
function setState(newState) {
state = newState;
state$.next(state);
}
function updateState(update) {
@bradfordlemley
bradfordlemley / TodoLib.test.js
Last active May 29, 2019 21:06
Todo library test
test("Adds todos", () => {
const todoLib = createTodoLib();
todoLib.addTodo("First todo");
expect(todoLib.state.todos).toHaveLength(1);
expect(todoLib.state.activeTodos).toHaveLength(1);
expect(todoLib.state.completedTodos).toHaveLength(0);
todoLib.addTodo("Second Todo");
expect(todoLib.state.todos).toHaveLength(2);
@bradfordlemley
bradfordlemley / App.jsx
Last active May 29, 2019 23:14
React component with methods in composed state
import { connect } from '@stated-library/react';
import { appState$ } from './state';
const App = ({addTodo, allTodos, visibleTodos}) =>
<>
<button onClick={() => addTodo("New todo")}>
Add Todo
</button>
<div>All todos: {allTodos.map(todo => <div>{todo.title}</div>)}</div>
<div>Visible todos: {visibleTodos.map(todo => <div>{todo.title}</div>)}</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,
@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 / 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 / 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 / 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 / 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:56
Vanilla todo library with standardized state
export default function createTodoLib() {
let state = {
todos: [],
isFetching: false,
};
return {
get state() {return state},
addTodo(title) {