Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Add createSubspaceProvider, mirroring Redux's createProvider.
Browse files Browse the repository at this point in the history
Fixes #89
  • Loading branch information
conartist6 committed Jul 10, 2018
1 parent 3dceb86 commit bd313d7
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 31 deletions.
14 changes: 13 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@
"contributions": [
"talk"
]
},
{
"login": "conartist6",
"name": "Conrad Buck",
"avatar_url": "https://avatars1.githubusercontent.com/u/540777?v=4",
"profile": "http://burningpotato.com",
"contributions": [
"code",
"doc",
"test"
]
}
]
],
"repoType": "github"
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![npm downloads](https://img.shields.io/npm/dm/redux-subspace.svg?style=flat-square)](https://www.npmjs.com/package/redux-subspace)
[![License: MIT](https://img.shields.io/npm/l/redux-subspace.svg?style=flat-square)](/LICENSE.md)

[![All Contributors](https://img.shields.io/badge/all_contributors-18-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

[![Watch on GitHub](https://img.shields.io/github/watchers/ioof-holdings/redux-subspace.svg?style=social)](https://github.com/ioof-holdings/redux-subspace/watchers)
Expand Down Expand Up @@ -84,10 +84,10 @@ Thanks goes to these wonderful people ([emojis](https://github.com/kentcdodds/al

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
| [<img src="https://avatars0.githubusercontent.com/u/23029903?v=4" width="100px;"/><br /><sub><b>Michael Peyper</b></sub>](https://github.com/mpeyper)<br />[💬](#question-mpeyper "Answering Questions") [🐛](https://github.com/ioof-holdings/redux-subspace/issues?q=author%3Ampeyper "Bug reports") [💻](https://github.com/ioof-holdings/redux-subspace/commits?author=mpeyper "Code") [📖](https://github.com/ioof-holdings/redux-subspace/commits?author=mpeyper "Documentation") [💡](#example-mpeyper "Examples") [🤔](#ideas-mpeyper "Ideas, Planning, & Feedback") [🚇](#infra-mpeyper "Infrastructure (Hosting, Build-Tools, etc)") [👀](#review-mpeyper "Reviewed Pull Requests") [📦](#platform-mpeyper "Packaging/porting to new platform") [📢](#talk-mpeyper "Talks") [⚠️](https://github.com/ioof-holdings/redux-subspace/commits?author=mpeyper "Tests") [🔧](#tool-mpeyper "Tools") | [<img src="https://avatars2.githubusercontent.com/u/6560018?v=4" width="100px;"/><br /><sub><b>Jonathan Peyper</b></sub>](https://github.com/jpeyper)<br />[💬](#question-jpeyper "Answering Questions") [💻](https://github.com/ioof-holdings/redux-subspace/commits?author=jpeyper "Code") [🤔](#ideas-jpeyper "Ideas, Planning, & Feedback") [👀](#review-jpeyper "Reviewed Pull Requests") [⚠️](https://github.com/ioof-holdings/redux-subspace/commits?author=jpeyper "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1409738?v=4" width="100px;"/><br /><sub><b>Vivian Farrell</b></sub>](https://github.com/vivian-farrell)<br />[🤔](#ideas-vivian-farrell "Ideas, Planning, & Feedback") [📦](#platform-vivian-farrell "Packaging/porting to new platform") [👀](#review-vivian-farrell "Reviewed Pull Requests") [📢](#talk-vivian-farrell "Talks") | [<img src="https://avatars2.githubusercontent.com/u/971283?v=4" width="100px;"/><br /><sub><b>Emily Rosengren</b></sub>](https://github.com/emirose)<br />[📢](#talk-emirose "Talks") | [<img src="https://avatars1.githubusercontent.com/u/121742?v=4" width="100px;"/><br /><sub><b>Morgan Larosa</b></sub>](https://github.com/chaos95)<br />[🚇](#infra-chaos95 "Infrastructure (Hosting, Build-Tools, etc)") | [<img src="https://avatars2.githubusercontent.com/u/656565?v=4" width="100px;"/><br /><sub><b>Amit Kothari</b></sub>](http://amitkothari.com)<br />[💻](https://github.com/ioof-holdings/redux-subspace/commits?author=amitkothari "Code") [💡](#example-amitkothari "Examples") | [<img src="https://avatars3.githubusercontent.com/u/1206987?v=4" width="100px;"/><br /><sub><b>Riku Rouvila</b></sub>](http://rikurouvila.fi)<br />[💻](https://github.com/ioof-holdings/redux-subspace/commits?author=rikukissa "Code") [📖](https://github.com/ioof-holdings/redux-subspace/commits?author=rikukissa "Documentation") [⚠️](https://github.com/ioof-holdings/redux-subspace/commits?author=rikukissa "Tests") |
| [<img src="https://avatars0.githubusercontent.com/u/23029903?v=4" width="100px;"/><br /><sub><b>Michael Peyper</b></sub>](https://github.com/mpeyper)<br />[💬](#question-mpeyper "Answering Questions") [🐛](/ioof-holdings/redux-subspace/issues?q=author%3Ampeyper "Bug reports") [💻](/ioof-holdings/redux-subspace/commits?author=mpeyper "Code") [📖](/ioof-holdings/redux-subspace/commits?author=mpeyper "Documentation") [💡](#example-mpeyper "Examples") [🤔](#ideas-mpeyper "Ideas, Planning, & Feedback") [🚇](#infra-mpeyper "Infrastructure (Hosting, Build-Tools, etc)") [👀](#review-mpeyper "Reviewed Pull Requests") [📦](#platform-mpeyper "Packaging/porting to new platform") [📢](#talk-mpeyper "Talks") [⚠️](/ioof-holdings/redux-subspace/commits?author=mpeyper "Tests") [🔧](#tool-mpeyper "Tools") | [<img src="https://avatars2.githubusercontent.com/u/6560018?v=4" width="100px;"/><br /><sub><b>Jonathan Peyper</b></sub>](https://github.com/jpeyper)<br />[💬](#question-jpeyper "Answering Questions") [💻](/ioof-holdings/redux-subspace/commits?author=jpeyper "Code") [🤔](#ideas-jpeyper "Ideas, Planning, & Feedback") [👀](#review-jpeyper "Reviewed Pull Requests") [⚠️](/ioof-holdings/redux-subspace/commits?author=jpeyper "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1409738?v=4" width="100px;"/><br /><sub><b>Vivian Farrell</b></sub>](https://github.com/vivian-farrell)<br />[🤔](#ideas-vivian-farrell "Ideas, Planning, & Feedback") [📦](#platform-vivian-farrell "Packaging/porting to new platform") [👀](#review-vivian-farrell "Reviewed Pull Requests") [📢](#talk-vivian-farrell "Talks") | [<img src="https://avatars2.githubusercontent.com/u/971283?v=4" width="100px;"/><br /><sub><b>Emily Rosengren</b></sub>](https://github.com/emirose)<br />[📢](#talk-emirose "Talks") | [<img src="https://avatars1.githubusercontent.com/u/121742?v=4" width="100px;"/><br /><sub><b>Morgan Larosa</b></sub>](https://github.com/chaos95)<br />[🚇](#infra-chaos95 "Infrastructure (Hosting, Build-Tools, etc)") | [<img src="https://avatars2.githubusercontent.com/u/656565?v=4" width="100px;"/><br /><sub><b>Amit Kothari</b></sub>](http://amitkothari.com)<br />[💻](/ioof-holdings/redux-subspace/commits?author=amitkothari "Code") [💡](#example-amitkothari "Examples") | [<img src="https://avatars3.githubusercontent.com/u/1206987?v=4" width="100px;"/><br /><sub><b>Riku Rouvila</b></sub>](http://rikurouvila.fi)<br />[💻](/ioof-holdings/redux-subspace/commits?author=rikukissa "Code") [📖](/ioof-holdings/redux-subspace/commits?author=rikukissa "Documentation") [⚠️](/ioof-holdings/redux-subspace/commits?author=rikukissa "Tests") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars2.githubusercontent.com/u/2007370?v=4" width="100px;"/><br /><sub><b>Michael</b></sub>](https://github.com/mradionov)<br />[💻](https://github.com/ioof-holdings/redux-subspace/commits?author=mradionov "Code") | [<img src="https://avatars0.githubusercontent.com/u/9067274?v=4" width="100px;"/><br /><sub><b>James Adams</b></sub>](https://medium.com/@jamesadams0)<br />[📖](https://github.com/ioof-holdings/redux-subspace/commits?author=James-E-Adams "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/21031458?v=4" width="100px;"/><br /><sub><b>Lee Kyles</b></sub>](https://github.com/lkyles1991)<br />[💻](https://github.com/ioof-holdings/redux-subspace/commits?author=lkyles1991 "Code") [⚠️](https://github.com/ioof-holdings/redux-subspace/commits?author=lkyles1991 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/6398211?v=4" width="100px;"/><br /><sub><b>Evert Bouw</b></sub>](https://github.com/evertbouw)<br />[💻](https://github.com/ioof-holdings/redux-subspace/commits?author=evertbouw "Code") [⚠️](https://github.com/ioof-holdings/redux-subspace/commits?author=evertbouw "Tests") | [<img src="https://avatars0.githubusercontent.com/u/4831814?v=4" width="100px;"/><br /><sub><b>Paweł Bród</b></sub>](https://github.com/Crazy-Ivan)<br />[🐛](https://github.com/ioof-holdings/redux-subspace/issues?q=author%3ACrazy-Ivan "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/2294352?v=4" width="100px;"/><br /><sub><b>majo44</b></sub>](https://github.com/majo44)<br />[🐛](https://github.com/ioof-holdings/redux-subspace/issues?q=author%3Amajo44 "Bug reports") [💻](https://github.com/ioof-holdings/redux-subspace/commits?author=majo44 "Code") [⚠️](https://github.com/ioof-holdings/redux-subspace/commits?author=majo44 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/26989071?v=4" width="100px;"/><br /><sub><b>Garth Newton</b></sub>](https://github.com/garth-newton)<br />[🐛](https://github.com/ioof-holdings/redux-subspace/issues?q=author%3Agarth-newton "Bug reports") [📖](https://github.com/ioof-holdings/redux-subspace/commits?author=garth-newton "Documentation") |
| [<img src="https://avatars2.githubusercontent.com/u/9800850?v=4" width="100px;"/><br /><sub><b>Mateusz Burzyński</b></sub>](https://github.com/Andarist)<br />[🔧](#tool-Andarist "Tools") | [<img src="https://avatars3.githubusercontent.com/u/6784822?v=4" width="100px;"/><br /><sub><b>psamusev</b></sub>](https://github.com/psamusev)<br />[🐛](https://github.com/ioof-holdings/redux-subspace/issues?q=author%3Apsamusev "Bug reports") | [<img src="https://avatars1.githubusercontent.com/u/762949?v=4" width="100px;"/><br /><sub><b>Jay Phelps</b></sub>](https://twitter.com/_jayphelps)<br />[👀](#review-jayphelps "Reviewed Pull Requests") | [<img src="https://avatars1.githubusercontent.com/u/1128784?v=4" width="100px;"/><br /><sub><b>Mark Erikson</b></sub>](http://blog.isquaredsoftware.com)<br />[📢](#talk-markerikson "Talks") |
| [<img src="https://avatars2.githubusercontent.com/u/2007370?v=4" width="100px;"/><br /><sub><b>Michael</b></sub>](https://github.com/mradionov)<br />[💻](/ioof-holdings/redux-subspace/commits?author=mradionov "Code") | [<img src="https://avatars0.githubusercontent.com/u/9067274?v=4" width="100px;"/><br /><sub><b>James Adams</b></sub>](https://medium.com/@jamesadams0)<br />[📖](/ioof-holdings/redux-subspace/commits?author=James-E-Adams "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/21031458?v=4" width="100px;"/><br /><sub><b>Lee Kyles</b></sub>](https://github.com/lkyles1991)<br />[💻](/ioof-holdings/redux-subspace/commits?author=lkyles1991 "Code") [⚠️](/ioof-holdings/redux-subspace/commits?author=lkyles1991 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/6398211?v=4" width="100px;"/><br /><sub><b>Evert Bouw</b></sub>](https://github.com/evertbouw)<br />[💻](/ioof-holdings/redux-subspace/commits?author=evertbouw "Code") [⚠️](/ioof-holdings/redux-subspace/commits?author=evertbouw "Tests") | [<img src="https://avatars0.githubusercontent.com/u/4831814?v=4" width="100px;"/><br /><sub><b>Paweł Bród</b></sub>](https://github.com/Crazy-Ivan)<br />[🐛](/ioof-holdings/redux-subspace/issues?q=author%3ACrazy-Ivan "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/2294352?v=4" width="100px;"/><br /><sub><b>majo44</b></sub>](https://github.com/majo44)<br />[🐛](/ioof-holdings/redux-subspace/issues?q=author%3Amajo44 "Bug reports") [💻](/ioof-holdings/redux-subspace/commits?author=majo44 "Code") [⚠️](/ioof-holdings/redux-subspace/commits?author=majo44 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/26989071?v=4" width="100px;"/><br /><sub><b>Garth Newton</b></sub>](https://github.com/garth-newton)<br />[🐛](/ioof-holdings/redux-subspace/issues?q=author%3Agarth-newton "Bug reports") [📖](/ioof-holdings/redux-subspace/commits?author=garth-newton "Documentation") |
| [<img src="https://avatars2.githubusercontent.com/u/9800850?v=4" width="100px;"/><br /><sub><b>Mateusz Burzyński</b></sub>](https://github.com/Andarist)<br />[🔧](#tool-Andarist "Tools") | [<img src="https://avatars3.githubusercontent.com/u/6784822?v=4" width="100px;"/><br /><sub><b>psamusev</b></sub>](https://github.com/psamusev)<br />[🐛](/ioof-holdings/redux-subspace/issues?q=author%3Apsamusev "Bug reports") | [<img src="https://avatars1.githubusercontent.com/u/762949?v=4" width="100px;"/><br /><sub><b>Jay Phelps</b></sub>](https://twitter.com/_jayphelps)<br />[👀](#review-jayphelps "Reviewed Pull Requests") | [<img src="https://avatars1.githubusercontent.com/u/1128784?v=4" width="100px;"/><br /><sub><b>Mark Erikson</b></sub>](http://blog.isquaredsoftware.com)<br />[📢](#talk-markerikson "Talks") | [<img src="https://avatars1.githubusercontent.com/u/540777?v=4" width="100px;"/><br /><sub><b>Conrad Buck</b></sub>](http://burningpotato.com)<br />[💻](/ioof-holdings/redux-subspace/commits?author=conartist6 "Code") [📖](/ioof-holdings/redux-subspace/commits?author=conartist6 "Documentation") [⚠️](/ioof-holdings/redux-subspace/commits?author=conartist6 "Tests") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification.
Expand Down
1 change: 1 addition & 0 deletions packages/react-redux-subspace/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
* [Examples](/docs/Examples.md#react-redux-subspace)
* [API Reference](/packages/react-redux-subspace/docs/api/README.md)
* [SubspaceProvider](/packages/react-redux-subspace/docs/api/SubspaceProvider.md)
* [createSubspaceProvider](/packages/react-redux-subspace/docs/api/createSubspaceProvider.md)
* [subspaced](/packages/react-redux-subspace/docs/api/subspaced.md)
1 change: 1 addition & 0 deletions packages/react-redux-subspace/docs/api/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# API Reference

* [SubspaceProvider](/packages/react-redux-subspace/docs/api/SubspaceProvider.md)
* [createSubspaceProvider](/packages/react-redux-subspace/docs/api/createSubspaceProvider.md)
* [subspaced](/packages/react-redux-subspace/docs/api/subspaced.md)
65 changes: 65 additions & 0 deletions packages/react-redux-subspace/docs/api/createSubspaceProvider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# `createSubspaceProvider([storeKey], [parentStoreKey])`

A function that returns a new [SubspaceProvider](/packages/react-redux-subspace/docs/api/SubspaceProvider.md) which provides and consumes stores on the given context keys. `SubspaceProvider` itself is simply the result of calling `createSubspaceProvider()`.

## Arguments

1. `storeKey` (_string_): The context key to provide. The default is `store`, which is the default key that Redux's `connect` will look at.
2. `parentStoreKey` (_string_): The context key to on which to look for a store. The default is `store`, which is the key provided by Redux's default Provider.

## Examples

Here is a basic example in which a subspace is provided on the 'subAppStore' key. This can be useful if you have a micro-frontend which will need to return rendering control to its parent, which will expect to have the root store defined on the `store` context key.

```javascript
import React from `react`
import { createSubspaceProvider } from 'react-redux-subspace'
import { connect } from 'react-redux'

const SubspaceProvider = createSubspaceProvider('subAppStore')

function MyComponent() {
return (
<SubspaceProvider mapState={(state) => state.subApp}>
<SubAppComponent />
</SubspaceProvider>
)
}

let SubAppComponent = props => <span>{props.foo}</span>;

function mapStateToProps(state) {
return {foo: state.foo};
}

const SubAppComponent = connect(mapStateToProps, null, null, {storeKey: 'subAppStore'})(SubAppComponent)
```

This example consumes a root Redux store which is defined on a different context key, namely `rootStore`.

```javascript
import React from `react`
import { createSubspaceProvider } from 'react-redux-subspace'
import { connect, createProvider } from 'react-redux'

const Provider = createProvider('rootStore');
const SubspaceProvider = createSubspaceProvider('subAppStore', 'rootStore');

function MyComponent() {
return (
<Provider store={reduxStore}>
<SubspaceProvider mapState={(state) => state.subApp}>
<SubAppComponent />
</SubspaceProvider>
</Provider>
)
}

let SubAppComponent = props => <span>{props.foo}</span>;

function mapStateToProps(state) {
return {foo: state.foo};
}

const SubAppComponent = connect(mapStateToProps, null, null, {storeKey: 'subAppStore'})(SubAppComponent)
```
56 changes: 33 additions & 23 deletions packages/react-redux-subspace/src/components/SubspaceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,47 @@ import React, { Children } from 'react'
import PropTypes from 'prop-types'
import { subspace } from 'redux-subspace'

class SubspaceProvider extends React.PureComponent {
export function createSubspaceProvider(paramStoreKey = 'store') {
let storeKey = typeof storeKey === 'object' ? paramStoreKey.storeKey : paramStoreKey;
let ownStoreKey = typeof storeKey === 'object' ? paramStoreKey.parentStoreKey : paramStoreKey;

getChildContext() {
const makeSubspaceDecorator = (props) => props.subspaceDecorator || subspace(props.mapState, props.namespace)
class SubspaceProvider extends React.PureComponent {

return {
store: makeSubspaceDecorator(this.props)(this.context.store)
getChildContext() {
const makeSubspaceDecorator = (props) => props.subspaceDecorator || subspace(props.mapState, props.namespace)

return {
[storeKey]: makeSubspaceDecorator(this.props)(this.context[parentStoreKey]),
[`${storeKey}Subscription`]: this.context[`${parentStoreKey}Subscription`],
}
}

render() {
return Children.only(this.props.children)
}
}

render() {
return Children.only(this.props.children)
SubspaceProvider.propTypes = {
children: PropTypes.element.isRequired,
mapState: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
]),
namespace: PropTypes.string,
subspaceDecorator: PropTypes.func,
}
}

SubspaceProvider.propTypes = {
children: PropTypes.element.isRequired,
mapState: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
]),
namespace: PropTypes.string,
subspaceDecorator: PropTypes.func,
}
SubspaceProvider.contextTypes = {
[parentStoreKey]: PropTypes.object.isRequired,
[`${parentStoreKey}Subscription`]: PropTypes.object.isRequired,
}

SubspaceProvider.contextTypes = {
store: PropTypes.object.isRequired
}
SubspaceProvider.childContextTypes = {
[storeKey]: PropTypes.object,
[`${storeKey}Subscription`]: PropTypes.object,
}

SubspaceProvider.childContextTypes = {
store: PropTypes.object
return SubspaceProvider;
}

export default SubspaceProvider
export default createSubspaceProvider()
2 changes: 1 addition & 1 deletion packages/react-redux-subspace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/

export { default as subspaced } from './components/subspaced'
export { default as SubspaceProvider } from './components/SubspaceProvider'
export { default as SubspaceProvider, createSubspaceProvider } from './components/SubspaceProvider'
Loading

0 comments on commit bd313d7

Please sign in to comment.