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
function createCounter() { | |
let counter = 0; | |
return { | |
get counter() { return counter }, | |
increment() { counter = counter + 1}, | |
decrement() { counter = counter - 1}, | |
} | |
} |
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
function createTodoLib() { | |
let todos = []; | |
let isFetching = false; | |
return { | |
get todos() {return todos}, | |
get isFetching() {return isFetching}, | |
addTodo(title) { | |
todos = todos.concat({title}); |
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
export default function createCounter() { | |
let state = 0; | |
return { | |
get state() { return state }, | |
increment() { state = state + 1}, | |
decrement() { state = state - 1}, | |
} | |
} |
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
export default function createTodoLib() { | |
let state = { | |
todos: [], | |
isFetching: false, | |
}; | |
return { | |
get state() {return state}, | |
addTodo(title) { |
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
export default function createCounter() { | |
let state = 0; | |
let state$ = new Observable(); | |
function setState(newState) { | |
state = newState; | |
state$.next(state); | |
} | |
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
export default function createTodoLib() { | |
let state = { | |
todos: [], | |
isFetching: false, | |
}; | |
let state$ = new Observable(); | |
function setState(newState) { | |
state = newState; |
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 { 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, |
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 { 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); |
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 { 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> |
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 { 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, |
OlderNewer