const React = require('react')
const ReactDOM = require('react-dom')
const { interval, from } = require('rxjs')
const redux = require('redux')
const msleep = require('async-msleep')
const { Subscribe, subscribable, devtool } = require('../src')
//
// city-state
//
@subscribable
class Counter {
constructor () {
this.count = 0
}
increment () {
this.count += 1
}
decrement () {
this.count -= 1
}
}
const counter = new Counter()
try {
devtool(counter, { name: 'Counter' })
} catch (_) {}
function CounterView () {
return (
{() => {
return (
Counter
state: {counter.count}
)
}}
)
}
//
// city-state async
//
@subscribable
class CounterAsync {
constructor () {
this.count = 0
this.isComputing = false
}
async increment () {
this.isComputing = true
await msleep(1000)
this.count += 1
this.isComputing = false
}
async decrement () {
this.isComputing = true
await msleep(1000)
this.count -= 1
this.isComputing = false
}
}
const counterAsync = new CounterAsync()
try {
devtool(counterAsync, { name: 'CounterAsync' })
} catch (_) {}
function CounterAsyncView () {
return (
{(counterAsync) => {
return (
Counter Async
state: {counterAsync.isComputing ? '...' : counterAsync.count}
)
}}
)
}
//
// redux
//
function counterReducer (state = { count: 0 }, action) {
switch (action.type) {
case 'INCREMENT':
return {
count: state.count + 1
}
case 'DECREMENT':
return {
count: state.count - 1
}
default:
return state
}
}
const counterStore = redux.createStore(counterReducer)
function CounterReduxView () {
return (
{(counterState = {}) => {
return (
Counter with redux
state: {counterState.count}
)
}}
)
}
//
// Native Observable
//
let ObservableCounterView = null
// RxJS is throwing an error on Safari
try {
const counter2 = new Counter()
const observableCounter = from(counter2)
ObservableCounterView = () => {
return (
{(counterState = {}) => {
return (
Counter Observable
state: {counterState.count}
)
}}
)
}
} catch (error) {
ObservableCounterView = () => null
}
//
// rxjs
//
class TimerView extends React.Component {
constructor () {
super()
this.interval1 = interval(2000)
this.interval2 = interval(1000)
try {
devtool(this.interval1, { name: 'interval1' })
devtool(this.interval2, { name: 'interval2' })
} catch (_) {}
}
// RxJS is throwing an error on Safari
componentDidCatch () {
return null
}
render () {
return (
{(time1, time2) => {
return (
rxjs
time1: {time1 || 0}
time2: {time2 || 0}
)
}}
)
}
}
function Main () {
return (
)
}
const $container = document.createElement('div')
document.body.appendChild($container)
ReactDOM.render(, $container)