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 { Component } from '@angular/core'; | |
import * as item from './store' | |
const store = item.createStore() | |
@Component({ | |
selector: 'my-app', | |
template: ` | |
<main> | |
<button (click)="add()">Add</button> |
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
type Thing = { | |
data: string | |
printData: () => void | |
} | |
const printData = (t: Thing) => () => { | |
console.log(t.data) | |
} | |
const createThing = (): Thing => { |
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 todoInstance = CreateTodoList() | |
const App = () => { | |
const [todos, setTodos] = useState([]) | |
useEffect(() => { | |
const subscription = todoInstance.onUpdate.subscribe(setTodos) | |
return () => subscription.unsubscribe() | |
}, [todoInstnace]) | |
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 const App = () => { | |
const { todoInstance } = useContext(StateContext) | |
const todos = useSubscribable(todoInstance.onUpdate, []) | |
return <div> | |
<LogButton /> | |
<div> | |
{ todos.map(todo => <div>{todo}</div>)} | |
</div> | |
</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
const CreateTodoList = () => { | |
const todos = [] | |
const add = (todo) => { | |
todos.push(todo) | |
} | |
return { | |
todos, | |
add |
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 { useSubscribable } from 'use-subscribable' | |
const todoInstance = CreateTodoList() | |
const App = () => { | |
const todos = useSubscribable( | |
todoInstance.todos, // Observable | |
todoInstance.todos.getValue() // Default value | |
) | |
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 CreateTodoList = () => { | |
const todos = new BehaviorSubject([]) | |
const add = (todo) => { | |
// this is kind of like "setState" | |
todos.next([ | |
...todos.getValue(), | |
todo | |
]) | |
} |
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 todoInstance = CreateTodoList() | |
todoInstance | |
.todos | |
.subscribe( | |
todosArray => console.log(todosArray) | |
) | |
todoInstance.add('one') | |
todoInstance.add('two') |
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 const randomString = () => | |
Math.random().toString(36).substring(7) | |
export type callback<T = any> = (value: T) => void | |
export interface Subscription { | |
unsubscribe: () => void | |
} | |
export class ObserverWithState<T = any> { |