Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
19 changes: 14 additions & 5 deletions modules/AsyncProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ function filterAndFlattenComponents(components) {
return flattened
}

function loadAsyncProps(components, params, cb) {
function defaultResolver(Component, params, cb) {
Component.loadProps(params, cb)
}

function loadAsyncProps(components, params, cb, resolver) {
// flatten the multi-component routes
let componentsArray = []
let propsArray = []
let needToLoadCounter = components.length

resolver = resolver || defaultResolver

const maybeFinish = () => {
if (needToLoadCounter === 0)
cb(null, { propsArray, componentsArray })
Expand All @@ -46,7 +52,7 @@ function loadAsyncProps(components, params, cb) {
}

components.forEach((Component, index) => {
Component.loadProps(params, (error, props) => {
resolver(Component, params, (error, props) => {
needToLoadCounter--
propsArray[index] = props
componentsArray[index] = Component
Expand Down Expand Up @@ -112,7 +118,7 @@ function createElement(Component, props) {
return <Component {...props}/>
}

export function loadPropsOnServer({ components, params }, cb) {
export function loadPropsOnServer({ components, params }, cb, resolver) {
Copy link
Owner

Choose a reason for hiding this comment

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

lets put the resolver in the object with components and params. Gives nice symmetry between component props and static function "props".

loadAsyncProps(
filterAndFlattenComponents(components),
params,
Expand All @@ -125,7 +131,8 @@ export function loadPropsOnServer({ components, params }, cb) {
const scriptString = `<script>__ASYNC_PROPS__ = ${json}</script>`
cb(null, propsAndComponents, scriptString)
}
}
},
resolver
)
}

Expand Down Expand Up @@ -189,6 +196,7 @@ class AsyncProps extends React.Component {
location: object.isRequired,
onError: func.isRequired,
renderLoading: func.isRequired,
resolver: func,

// server rendering
propsArray: array,
Expand Down Expand Up @@ -303,7 +311,8 @@ class AsyncProps extends React.Component {
prevProps: null
})
}
})
}),
this.props.resolver
)
}

Expand Down
45 changes: 45 additions & 0 deletions modules/__tests__/AsyncProps-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,33 @@ describe('AsyncProps', () => {
/>
), div, next)
})

it('allows to override resolver function', (done) => {
const appSpy = spyOn(App, 'loadProps').andCallThrough()

const next = execNext([
() => {},
() => {
expect(appSpy.calls.length).toEqual(1)
expect(appSpy.calls[0].arguments[2]).toEqual('check')
},
done
])

function resolver(Component, params, cb) {
Component.loadProps(params, cb, 'check')
Copy link
Owner

Choose a reason for hiding this comment

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

this would pass even w/o your changes, right? Maybe switch the name from loadProps to resolveProps or something.

Copy link
Author

Choose a reason for hiding this comment

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

It's impossible. there a lot of places where we are checking that loadProps method exists
We need to use this method now...

Copy link
Author

Choose a reason for hiding this comment

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

#42 (diff)

this will work only with resolver feature

}

App.setAssertions(next)

render((
<Router
history={createHistory('/')}
routes={routes}
render={(props) => <AsyncProps {...props} resolver={resolver} />}
/>
), div, next)
})
})

describe('server rendering', () => {
Expand All @@ -420,6 +447,24 @@ describe('AsyncProps', () => {
})
})

it('allows to override resolver function', (done) => {
const appSpy = spyOn(App, 'loadProps').andCallThrough()
function resolver(Component, params, cb) {
Component.loadProps(params, cb, 'check')
}
const loadPropsRoutes = {
path: '/',
component: App
}
match({ routes: loadPropsRoutes, location: '/' }, (err, redirect, renderProps) => {
loadPropsOnServer(renderProps, () => {
expect(appSpy.calls.length).toEqual(1)
expect(appSpy.calls[0].arguments[2]).toEqual('check1')
done()
}, resolver)
})
})

it('renders synchronously with props from hydration', () => {
const html = renderToString(
<Router
Expand Down