Skip to content

Commit

Permalink
fix($promiseAll): resolve promise.all array from babel-plugin into si…
Browse files Browse the repository at this point in the history
…ngle import
  • Loading branch information
faceyspacey committed Jul 6, 2017
1 parent 10c8a17 commit 6f34f3a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 33 deletions.
4 changes: 2 additions & 2 deletions __test-helpers__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const createBablePluginComponent = (
chunkName: () => name,
path: () => name,
resolve: () => name,
load: () => Promise.all([asyncImport()]),
load: () => Promise.all([asyncImport(), 'css']).then(prom => prom[0]),
id: name,
file: `${name}.js`
}
Expand All @@ -73,7 +73,7 @@ export const createDynamicBablePluginComponent = (
chunkName: () => page,
path: () => createPath(page),
resolve: () => createPath(page),
load: () => Promise.all([asyncImport(page)]),
load: () => Promise.all([asyncImport(page), 'css']).then(prom => prom[0]),
id: page,
file: `${page}.js`
})
Expand Down
6 changes: 4 additions & 2 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ exports[`async lifecycle error 2`] = `

exports[`async lifecycle error 3`] = `
<div>
Error!
Error:
test error
</div>
`;

Expand Down Expand Up @@ -137,7 +138,8 @@ exports[`async lifecycle timeout error 1`] = `

exports[`async lifecycle timeout error 2`] = `
<div>
Error!
Error:
timeout exceeded
</div>
`;

Expand Down
1 change: 1 addition & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('async lifecycle', () => {
expect(component.toJSON()).toMatchSnapshot() // initial

await waitFor(20)

expect(component.toJSON()).toMatchSnapshot() // error
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-universal-component",
"version": "0.0.0-development",
"version": "2.0.1",
"description": "A higher order component for loading components with promises",
"main": "dist/index.js",
"author": "James FaceySpacey Gillmore <[email protected]> (http://www.faceyspacey.com)",
Expand Down
5 changes: 1 addition & 4 deletions src/flowTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ export type Config = {
file: string
}

export type Load = (
Object,
AsyncFuncTools
) => Promise<[ImportModule]> | Promise<ImportModule>
export type Load = (Object, AsyncFuncTools) => Promise<ImportModule>

// function that returns config (babel-plugin-universal-import)
// $FlowIssue
Expand Down
34 changes: 11 additions & 23 deletions src/requireUniversalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,31 @@ export default function requireUniversalModule<Props: Props>(
if (cachedPromise) return cachedPromise

const prom = new Promise((res, rej) => {
const timer =
timeout &&
setTimeout(() => {
rej(new Error('timeout exceeded'))
}, timeout)
const reject = error => {
error = error || new Error('timeout exceeded')
clearTimeout(timer)
if (onError) onError(error)
rej(error)
}

// const timer = timeout && setTimeout(reject, timeout)
const timer = timeout && setTimeout(reject, timeout)

const resolve = mod => {
clearTimeout(timer)

const exp = resolveExport(mod, key, onLoad, chunkName, props, modCache)
if (exp) return res(exp)

rej(new Error('export not found'))
}

const reject = error => {
clearTimeout(timer)
rej(error)
reject(new Error('export not found'))
}

const request = load(props, { resolve, reject })

// if load doesn't return a promise, it must call resolveImport
// itself. Most common is the promise implementation below.
if (!request || typeof request.then !== 'function') return

request
.then(mod => {
// babel-plugin-universal-import transforms promise to: Promise.all([import(), importcss()])
if (Array.isArray(mod)) mod = mod[0]
return resolve(mod)
})
.catch(error => {
clearTimeout(timer)
if (onError) onError(error)
reject(error)
})
request.then(resolve).catch(reject)
})

cacheProm(prom, chunkName, props, promCache)
Expand Down
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const babelInterop = (mod: ?Mod) =>
mod && typeof mod === 'object' && mod.__esModule ? mod.default : mod

export const DefaultLoading = () => <div>Loading...</div>
export const DefaultError = () => <div>Error!</div>
export const DefaultError = ({ error }: { error: Object }) =>
<div>Error: {error && error.message}</div>

export const tryRequire = (id: Id): ?any => {
try {
Expand Down

0 comments on commit 6f34f3a

Please sign in to comment.