Skip to content

Commit

Permalink
refactor: remove async/await
Browse files Browse the repository at this point in the history
In order to not require regenerator/runtime.
  • Loading branch information
gregberge committed Jun 24, 2017
1 parent b9a00aa commit 087e29e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
"author": "Greg Bergé <berge.greg@gmail.com>",
"keywords": [
"react",
"promise",
"code",
"splitting",
"split",
"ssr",
"webpack",
"async"
"code-splitting",
"react-router",
"server-side-rendering",
"dynamic-import",
"react-loadable",
"react-async-components"
],
"license": "MIT",
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion src/loadComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { LOADABLE, COMPONENT_IDS } from './constants'
import * as componentTracker from './componentTracker'

async function loadComponents() {
function loadComponents() {
if (typeof window === 'undefined') {
throw new Error(
'`loadComponents` must be called client-side: `window` is undefined',
Expand Down
11 changes: 6 additions & 5 deletions src/loadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ function loadable(
class LoadableComponent extends React.Component {
static Component = null

static async load() {
const module = await getComponent()
const Component = resolveModuleDefault(module)
LoadableComponent.Component = Component
return Component
static load() {
return getComponent().then(module => {
const Component = resolveModuleDefault(module)
LoadableComponent.Component = Component
return Component
})
}

state = {
Expand Down
47 changes: 24 additions & 23 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function getQueriesFromTree(
return queries
}

export async function getLoadableState(
export function getLoadableState(
rootElement,
rootContext = {},
fetchRoot = true,
Expand All @@ -118,29 +118,30 @@ export async function getLoadableState(
let componentIds = []

// wait on each query that we found, re-rendering the subtree when it's done
const mappedQueries = queries.map(async ({ query, element, context }) => {
const mappedQueries = queries.map(({ query, element, context }) =>
// we've just grabbed the query for element, so don't try and get it again
try {
const componentId = await query
componentIds.push(componentId)
const state = await getLoadableState(element, context, false)
componentIds = [...componentIds, ...state.componentIds]
} catch (e) {
errors.push(e)
query
.then(componentId => {
componentIds.push(componentId)
return getLoadableState(element, context, false)
})
.then(state => {
componentIds = [...componentIds, ...state.componentIds]
})
.catch(e => errors.push(e)),
)

return Promise.all(mappedQueries).then(() => {
if (errors.length > 0) {
const error = errors.length === 1
? errors[0]
: new Error(
`${errors.length} errors were thrown when importing your modules.`,
)
error.queryErrors = errors
throw error
}
})

await Promise.all(mappedQueries)

if (errors.length > 0) {
const error = errors.length === 1
? errors[0]
: new Error(
`${errors.length} errors were thrown when importing your modules.`,
)
error.queryErrors = errors
throw error
}

return new DeferredState(componentIds)
return new DeferredState(componentIds)
})
}

0 comments on commit 087e29e

Please sign in to comment.