Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subsequent synchronous atom updates are not properly rendered #61

Merged
merged 5 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/focal/src/react/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ class FakeComponent<P> {
public props: LiftWrapperProps<P>
) {}

setState(newState: LiftWrapperState) {
setState(state: (LiftWrapperState | ((state: LiftWrapperState) => LiftWrapperState))) {
const newState = typeof state === 'function' ? state(this.state) : state

if ('subscription' in newState)
this.state.subscription = newState.subscription
if ('renderCache' in newState)
Expand Down Expand Up @@ -390,8 +392,9 @@ class RenderOne<P> implements Subscription {
const { component, props } = liftedComponent.props
const renderCache = render(component, props, [value])

if (!structEq(liftedComponent.state.renderCache, renderCache))
liftedComponent.setState({ renderCache })
liftedComponent.setState(state =>
!structEq(state.renderCache, renderCache) ? { renderCache } : {}
)
}

private _handleCompleted() {
Expand Down Expand Up @@ -486,8 +489,9 @@ class RenderMany<P> implements Subscription {
const { component, props } = liftedComponent.props
const renderCache = render(component, props, this._values)

if (!structEq(liftedComponent.state.renderCache, renderCache))
liftedComponent.setState({ renderCache })
liftedComponent.setState(state =>
!structEq(state.renderCache, renderCache) ? { renderCache } : {}
)
}

private _handleCompleted(idx: number) {
Expand Down
3 changes: 2 additions & 1 deletion packages/test/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ declare var require: (path: string) => any

const tests: { name: string, test: TestComponent<any> }[] = [
'lifted-bug',
'render-bug'
'render-bug',
'setstate-bug'
].map(name => {
return { name, test: require(`./${name}/index.tsx`).default as TestComponent<any> }
})
Expand Down
74 changes: 74 additions & 0 deletions packages/test/src/setstate-bug/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as React from 'react'
import { Atom, F } from '@grammarly/focal'
import { Observable, Subject } from 'rxjs'

class Test extends React.Component<{ trigger: Observable<void> }> {
list = Atom.create(['a', 'b', 'c'])

componentWillMount() {
this.props.trigger.subscribe(list => {
this.list.set(['a', 'b'])
this.list.set(['a', 'b', 'c'])
})
}

render() {
return (
<>
<h4>Focal</h4>
<F.ul>
{this.list.view(list => {
return list.map(l => <li key={l}>{l}</li>)
})}
</F.ul>
</>
)
}
}

class Test2 extends React.Component<{ trigger: Observable<void> }, { list: string[] }> {
constructor(props: any) {
super(props)
this.state = {
list: ['a', 'b', 'c']
}
}

componentDidMount() {
this.props.trigger.subscribe(() => {
this.setState({ list: ['a', 'b'] })
this.setState({ list: ['a', 'b', 'c'] })
})
}

render() {
const { list } = this.state
return (
<>
<h4>React</h4>
<ul>
{list.map(l => <li key={l}>{l}</li>)}
</ul>
</>
)
}
}

const trigger = new Subject<void>()

function onClick() {
trigger.next()
}

const App = () => (
<div>
<input type='button' onClick={onClick} value='Click me' />
<Test trigger={trigger} />
<Test2 trigger={trigger} />
</div>
)

export default {
Component: App,
defaultState: 0
}