Skip to content
This repository was archived by the owner on Aug 14, 2018. It is now read-only.

Commit 920fd8d

Browse files
committed
Bug fixes and improved resolving
1 parent 0264e85 commit 920fd8d

File tree

3 files changed

+21
-39
lines changed

3 files changed

+21
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-esc-resolver",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Easy to use Client and Server Resolver",
55
"homepage": "https://github.com/TriPSs/react-esc-resolver",
66
"bugs": {

src/Resolver.js

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -145,33 +145,17 @@ export default class Resolver extends React.Component {
145145
}
146146

147147
computeState(thisProps, state) {
148-
const { resolve, props } = thisProps
149-
let nextState = state
148+
const { resolve } = thisProps
149+
let nextState = state
150150

151-
Object.keys(resolve).forEach(name => {
151+
Object.keys(resolve).forEach((name) => {
152152
const cached = this.cached(name)
153153

154154
if (!state.resolved.hasOwnProperty(name) && !state.pending.hasOwnProperty(name) && !cached) {
155-
const factory = resolve[name]
156-
157-
nextState = {
158-
...state,
159-
pending: {
160-
...state.pending,
161-
[name]: factory(props),
162-
}
163-
164-
}
155+
nextState.pending[name] = resolve[name]
165156

166157
} else if (cached) {
167-
nextState = {
168-
...state,
169-
resolved: {
170-
...state.resolved,
171-
[name]: cached
172-
}
173-
174-
}
158+
nextState.resolved[name] = true
175159
}
176160
})
177161

@@ -226,23 +210,21 @@ export default class Resolver extends React.Component {
226210
}
227211

228212
resolve(state) {
229-
const pending = Object.keys(state.pending).map(name => {
230-
const promise = state.pending[name]
213+
const { props } = this.props
231214

232-
return { name, promise }
233-
})
215+
const pending = Object.keys(state.pending).map((name) => {
216+
const func = state.pending[name]
234217

235-
const promises = pending.map(({ promise }) => promise)
218+
return { name, func }
219+
})
236220

237-
let resolving = Promise.all(promises).then((values) => {
238-
return values.reduce((resolved, value, i) => {
239-
const { name } = pending[i]
221+
const promises = pending.map(({ func }) => func(props))
240222

241-
resolved[name] = value
223+
let resolving = Promise.all(promises).then((values) => values.reduce((resolved, value, i) => {
224+
resolved[pending[i].name] = true
242225

243-
return resolved
244-
}, {})
245-
})
226+
return resolved
227+
}, {}))
246228

247229
// Resolve listeners get the current resolved
248230
resolving = this.onResolve(resolving)
@@ -272,8 +254,6 @@ export default class Resolver extends React.Component {
272254

273255
// Prevent rendering until pending values are resolved
274256
if (this.isPending(nextState)) {
275-
this.resolve(nextState)
276-
277257
return false
278258
}
279259

@@ -288,4 +268,5 @@ export default class Resolver extends React.Component {
288268

289269
this.setState(nextState)
290270
}
271+
291272
}

src/resolve.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ const capitalize = (word) => {
77

88
export default (prop, promise, cache = true) => {
99

10-
const asyncProps = (arguments.length === 1) ? prop : { [prop]: promise }
11-
const asyncNames = Object.keys(asyncProps).map(capitalize).join("")
10+
const asyncProps = (typeof prop === 'object') ? prop : { [prop]: promise }
11+
const asyncNames = Object.keys(asyncProps).map(capitalize).join('')
12+
const cacheEnabled = (typeof prop === 'object' && typeof promise === 'boolean') ? promise : cache
1213

1314
return Component => class extends React.Component {
1415

1516
static displayName = `${asyncNames}Resolver`
1617

1718
render() {
1819
return (
19-
<Resolver props={this.props} resolve={asyncProps} cache={cache}>
20+
<Resolver props={this.props} resolve={asyncProps} cache={cacheEnabled}>
2021
{(resolved) => <Component {...resolved} />}
2122
</Resolver>
2223
)

0 commit comments

Comments
 (0)