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 { from } from 'rxjs'; | |
import { distinctUntilKeyChanged } from 'rxjs/operators'; | |
const todoLib = createTodoLib(); | |
const authLib = createAuthLib(); | |
from(todoLib.state$).pipe( | |
distinctUntilKeyChanged('authFailed'), | |
filter(state => state.authFailed), | |
).subscribe( |
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 todoLib = createTodoLib(); | |
const authLib = createAuthLib(); | |
authLib.state$.subscribe(state => { | |
todoLib.setUser(state.user); | |
}); | |
todoLib.state$.subscribe(state => { | |
if (state.authFailed) { | |
authLib.refresh(); |
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 { use } from '@stated-library/react'; | |
import { appState$ } from './state'; | |
const App = () => { | |
const {addTodo, allTodos, visibleTodos} = use(appState$); | |
return ( | |
<> |
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 appState$ = mapState( | |
todoLib.state$, | |
state => ({ | |
addTodo: todoLib.addTodo, | |
allTodos: state.todos, | |
isFetchingTodos: state.isFetching, |
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
// reset state module for each test | |
let state; | |
beforeEach(() => { | |
jest.resetModules(); | |
state = require('./state'); | |
}) | |
test('visibleTodos$ contains todos filtered thru visibilityFilter', () => { | |
const { todoLib, visibleTodos$ } = state; | |
todoLib.addTodo("First"); |
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 = createCounterLib(); | |
const visLib = createVisibilityLib(); | |
const visibleTodos$ = mapState( | |
[todoLib.state$, visLib.state$], | |
([todoLibState, visLibState]) => { | |
switch (visLibState.filter) { |
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 { createSelector } from 'reselect'; | |
import createBase from './StatedLibBase'; | |
export default function createTodoLib() { | |
const getCompletedTodos = createSelector( | |
state => state.todos, | |
todos => todos.filter(todo => todo.completed), | |
); |
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 createBase(initialState, deriveState){ | |
let state = deriveState ? deriveState(initialState) : initialState; | |
let state$ = new Observable(); | |
function setState(newState) { | |
state = deriveState ? deriveState(newState) : newState; | |
state$.next(state); | |
} | |
function updateState(update) { |
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 createBase from './StatedLibBase'; | |
export default function createTodoLib() { | |
const base = createBase({ todos: [], isFetching: false }); | |
const {updateState, state$} = base; | |
return { | |
get state: {return base.state}, | |
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
import createBase from './StatedLibBase'; | |
export default function createCounter() { | |
let base = createBase(0); | |
return { | |
get state() { return base.state }, | |
state$: base.state$, |
NewerOlder