Skip to content

Commit

Permalink
performance: dont update by default
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherny committed Dec 3, 2017
1 parent 0e3fdbf commit 303fe88
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export let withStore = connect(store)
```tsx
import { withStore } from './store'

let MyComponent = withStore()(({ store }) =>
// Update the component when `today` changes
let MyComponent = withStore('today')(({ store }) =>
<div>
Hello! Today is {store.get('today')}
<button onClick={() => store.set('today')(new Date)}>Update Date</button>
Expand All @@ -73,7 +74,7 @@ store

### Lensed connects

Instead of updating your React component when anything on the model changed, you can subscribe just to specific properties on your model. Let's modify our React example to only update when `today` changes:
Instead of updating your React component when anything on the model changed, you subscribe just to specific properties on your model. Let's modify our React example to only update when `today` changes:

```tsx
let MyComponent = withStore('today')(
Expand Down
10 changes: 3 additions & 7 deletions src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ export function connect<Actions extends object>(store: Store<Actions>) {

return class extends React.Component<Omit<Props, 'store'>> {
componentDidMount() {
if (listenOn.length) {
state = listenOn.map(key =>
store.on(key).subscribe(() => this.forceUpdate())
)
} else {
state = [store.all().subscribe(() => this.forceUpdate())]
}
state = listenOn.map(key =>
store.on(key).subscribe(() => this.forceUpdate())
)
}
componentWillUnmount() {
state.forEach(_ => _.dispose())
Expand Down
12 changes: 10 additions & 2 deletions test/stateful.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,27 @@ let MyComponentWithLens = connect(store)('isTrue')(
)

test('[stateful] it should render a component', t =>
withElement(MyComponent, _ =>
withElement(MyComponentWithLens, _ =>
t.regex(_.innerHTML, /True/)
)
)

test('[stateful] it should update the component', t =>
withElement(MyComponent, _ => {
withElement(MyComponentWithLens, _ => {
t.regex(_.innerHTML, /True/)
Simulate.click(_.querySelector('button')!)
t.regex(_.innerHTML, /False/)
})
)

test('[stateful] it should not update the component if it has no lens', t =>
withElement(MyComponent, _ => {
t.regex(_.innerHTML, /False/)
Simulate.click(_.querySelector('button')!)
t.regex(_.innerHTML, /False/)
})
)

// nb: test order matters because store is shared!
test('[stateful] it should support lenses', t =>
withElement(MyComponentWithLens, _ => {
Expand Down
12 changes: 10 additions & 2 deletions test/stateless.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,27 @@ let MyComponentWithLens = connect(store)('isTrue')(({ store }) =>
)

test('[stateless] it should render a component', t =>
withElement(MyComponent, _ =>
withElement(MyComponentWithLens, _ =>
t.regex(_.innerHTML, /True/)
)
)

test('[stateless] it should update the component', t =>
withElement(MyComponent, _ => {
withElement(MyComponentWithLens, _ => {
t.regex(_.innerHTML, /True/)
Simulate.click(_.querySelector('button')!)
t.regex(_.innerHTML, /False/)
})
)

test('[stateless] it should not update the component if it has no lens', t =>
withElement(MyComponent, _ => {
t.regex(_.innerHTML, /False/)
Simulate.click(_.querySelector('button')!)
t.regex(_.innerHTML, /False/)
})
)

// nb: test order matters because store is shared!
test('[stateless] it should support lenses', t =>
withElement(MyComponentWithLens, _ => {
Expand Down

0 comments on commit 303fe88

Please sign in to comment.