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

Passthrough provided meta in identity requests #41

Merged
merged 1 commit into from
Jan 19, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Instead, it *returns* a new, connected component class, for you to use.
- `andThen(value, meta): { prop: request, ... }` *(Function)*: returns an object of request mappings to fetch after fulfillment of this request but does not replace this request. Takes the `value` and `meta` of this request as arguments.
- `andCatch(reason, meta): { prop: request, ... }` *(Function)*: returns an object of request mappings to fetch after rejection of this request but does not replace this request. Takes the `value` and `meta` of this request as arguments.
- `value` *(Any)*: Data to passthrough directly to `PromiseState` as an alternative to providing a URL. This is an advanced option used for static data and data transformations.

- `meta` *(Any)*: Metadata to passthrough directly to `PromiseState`. This attribute is only recognized if `value` is also provided.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #42


Requests specified as functions are not fetched immediately when props are received, but rather bound to the props and injected into the component to be called at a later time in response to user actions. Functions should be pure and return the same format as `mapPropsToRequestsToProps` itself. If a function maps a request to the same name as an existing prop, the prop will be overwritten. This is commonly used for taking some action that updates an existing `PromiseState`. Consider setting `refreshing: true` in such it situation.

* [`options`] *(Object)* If specified, further customizes the behavior of the connector.
Expand Down
2 changes: 1 addition & 1 deletion src/components/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default function connect(mapPropsToRequestsToProps, options = {}) {
const onRejection = this.createPromiseStateOnRejection(prop, mapping, startedAt)

if (mapping.value) {
const meta = {}
const meta = mapping.meta || {}
this.setAtomicState(prop, startedAt, mapping, initPS(meta))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and made this set the provided meta on the pending PromiseState; however, this is different behavior from the value, which is not set until the PromiseState is fulfilled. I'm not super happy about this, but I think it is the most expected behavior. We can think about setting value up front, but then it's different from regular URL-based requests. Feedback welcome.

cc: @jsullivan @hburrows

return Promise.resolve(mapping.value).then(onFulfillment(meta), onRejection(meta))
} else {
Expand Down
6 changes: 3 additions & 3 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('React', () => {
})

it('should passthrough value of identity requests', (done) => {
@connect(() => ({ testFetch: { value: 'foo' } }))
@connect(() => ({ testFetch: { value: 'foo', meta: 'voodoo' } }))
class Container extends Component {
render() {
return <Passthrough {...this.props} />
Expand All @@ -197,7 +197,7 @@ describe('React', () => {

const stub = TestUtils.findRenderedComponentWithType(container, Passthrough)
expect(stub.props.testFetch).toIncludeKeyValues({
fulfilled: false, pending: true, refreshing: false, reason: null, rejected: false, settled: false, value: null
fulfilled: false, pending: true, refreshing: false, reason: null, rejected: false, settled: false, value: null, meta: 'voodoo'
})

setImmediate(() => {
Expand All @@ -206,7 +206,7 @@ describe('React', () => {

const stub = TestUtils.findRenderedComponentWithType(container, Passthrough)
expect(stub.props.testFetch).toIncludeKeyValues({
fulfilled: true, pending: false, refreshing: false, reason: null, rejected: false, settled: true, value: 'foo'
fulfilled: true, pending: false, refreshing: false, reason: null, rejected: false, settled: true, value: 'foo', meta: 'voodoo'
})
done()
})
Expand Down