Skip to content

Commit

Permalink
Add support for null instead of mapState function to not subscribe to…
Browse files Browse the repository at this point in the history
… the store (#29)

* Allow user to pass null instead of mapState to not subscribe to the store

* Update withStore documentation

* Stream an empty object instead of the state when receive null as mapState

* Test if object has only dispatch properties when receive null as mapState
  • Loading branch information
leandrooriente authored Feb 19, 2018
1 parent 609b727 commit 762535e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/frint-props/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ Works with `frint-store` or Redux store set in FrintJS App as a provider.

### Arguments

1. `mapState` (`Function`): Maps state to props
1. `mapState` (`Function` OR `null`): Maps state to props
1. `mapDispatch` (`Object`): Action creators keyed by names
1. `options` (`Object`) [optional]: Object with additional configuration
1. `options.providerName` (`String`): Defaults to `store`
Expand All @@ -294,7 +294,32 @@ Works with `frint-store` or Redux store set in FrintJS App as a provider.
```js
const app = new App(); // assuming it has a `store` provider

const props$ = withState('counter', 'setCounter', 0)(app);
const mapState = state => ({
foo: state.foo,
bar: state.bar
});

const mapDispatch = {
handleClick: () => ({
type: 'HANDLE_CLICK'
})
};

const props$ = withStore(mapState, mapDispatch)(app);
```

You can also pass `null` in place of `mapState` parameter if you don't want to subscribe to store updates.

```js
const app = new App(); // assuming it has a `store` provider

const mapDispatch = {
handleClick: () => ({
type: 'HANDLE_CLICK'
})
};

const props$ = withStore(null, mapDispatch)(app);
```

<!-- -->
Expand Down
6 changes: 6 additions & 0 deletions packages/frint-props/src/withStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { switchMap } from 'rxjs/operators/switchMap';
import { scan } from 'rxjs/operators/scan';

function makeStateProps$(store, mapper) {
// when no mapper is given, we can just stream a plain object once,
// which will be merged with dispatchable action creators later
if (!mapper) {
return of({});
}

const state$ = from(store);

return state$.pipe(
Expand Down
14 changes: 14 additions & 0 deletions packages/frint-props/src/withStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ describe('withStore', function () {
expect(t.props.counter).toBe(10);
});

test('accept null as a mapper parameter', function () {
const app = createAndInstantiateTestApp();
const mapDispatch = {
increment: incrementCounter,
};

const t = new Tester(compose(
withStore(null, mapDispatch),
)(app));

expect(t.props).toHaveProperty('increment');
expect(Object.keys(t.props)).toHaveLength(1);
});

test('can dispatch actions, and update value', function () {
const app = createAndInstantiateTestApp();

Expand Down

0 comments on commit 762535e

Please sign in to comment.