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 ListView = (store) => ({ | |
data: () => ({ | |
items: [] | |
}), | |
methods: { | |
complete(id) { | |
store.complete(id) | |
}, | |
add() { | |
const value = prompt('Name of 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
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
import { Injectable } from '@angular/core' | |
import { Http } from '@angular/http' | |
import { environment } from '../../environments/environment' | |
const sleep = (d: number) => new Promise(res => setTimeout(res, d)) | |
@Injectable() | |
export class NewVersionService { | |
pollRate = 1000 * 60 * 10 // 10 minutes | |
currentBuild = environment.build |
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
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
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
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
export const App = () => { | |
const { todoInstance } = useContext(StateContext) | |
const todos = useSubscribable(todoInstance.onUpdate, []) | |
return <div> | |
<LogButton /> | |
<div> | |
{ todos.map(todo => <div>{todo}</div>)} | |
</div> | |
</div> |