Skip to content

Commit

Permalink
feat(onError): add callback (works for async requests--sync requests …
Browse files Browse the repository at this point in the history
…just return nothing)
  • Loading branch information
faceyspacey committed Jun 30, 2017
1 parent cf27d28 commit d894fc2
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 235 deletions.
25 changes: 20 additions & 5 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import req, {
flushChunkNames
} from '../src'


describe('requireSync: tries to require module synchronously on both the server and client', () => {
it('babel', () => {
const modulePath = createPath('es6')
Expand Down Expand Up @@ -174,8 +173,23 @@ describe('requireAsync: requires module asynchronously on the client, returning
expect(error.message).toEqual('ah')
}
})
})

it('rejected promise calls onError', async () => {
const error = new Error('ah')
const onError = jest.fn()
const opts = { onError }
const { requireAsync } = req(Promise.reject(error), opts)

try {
await requireAsync()
}
catch (error) {
expect(error.message).toEqual('ah')
}

expect(onError).toBeCalledWith(error)
})
})

describe('addModule: add moduleId and chunkName for SSR flushing', () => {
it('babel', () => {
Expand Down Expand Up @@ -213,7 +227,10 @@ describe('addModule: add moduleId and chunkName for SSR flushing', () => {
flushModuleIds() // insure sets are empty:
flushChunkNames()

let universal = req(undefined, { resolve: () => moduleEs6, chunkName: 'es6' })
let universal = req(undefined, {
resolve: () => moduleEs6,
chunkName: 'es6'
})
universal.addModule()

universal = req(undefined, { resolve: () => moduleEs5, chunkName: 'es5' })
Expand All @@ -230,7 +247,6 @@ describe('addModule: add moduleId and chunkName for SSR flushing', () => {
})
})


describe('other options', () => {
it('key (string): resolve export to value of key', () => {
const modulePath = createPath('es6')
Expand Down Expand Up @@ -331,7 +347,6 @@ describe('other options', () => {
})
})


describe('unit tests', () => {
test('tryRequire: requires module using key export finder + calls onLoad with module', () => {
const moduleEs6 = createPath('es6')
Expand Down
19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ declare var __webpack_require__: Function
declare var __webpack_modules__: Object

export type ResolveImport = (error: ?any, module: ?any) => void
export type AsyncFunc = ((ResolveImport, ...args: Array<any>) => Promise<*>)
export type AsyncFunc = (ResolveImport, ...args: Array<any>) => Promise<*>
export type AsyncImport = Promise<*> | AsyncFunc
export type Id = string
export type Key = string | null | ((module: ?Object) => any)
export type OnLoad = (module: Object) => void
export type OnError = (error: Object) => void
export type PathResolve = Id | (() => Id)

export type Options = {
resolve?: PathResolve, // only optional when async-only
chunkName?: string,
path?: PathResolve,
key?: Key,
timeout?: number,
onError?: OnError,
onLoad?: OnLoad,
initialRequire?: boolean,
alwaysUpdate?: boolean
Expand Down Expand Up @@ -85,11 +86,12 @@ export default (asyncImport: ?AsyncImport, options: Options = {}): Tools => {
key,
timeout = 15000,
onLoad,
onError,
initialRequire = true,
alwaysUpdate = false
} = options

const modulePath = typeof path === 'function' ? path() : (path || '')
const modulePath = typeof path === 'function' ? path() : path || ''

let mod
let weakId
Expand Down Expand Up @@ -156,12 +158,11 @@ export default (asyncImport: ?AsyncImport, options: Options = {}): Tools => {
return
}

request
.then(m => resolveImport(null, m))
.catch(error => {
clearTimeout(timer)
reject(error)
})
request.then(m => resolveImport(null, m)).catch(error => {
clearTimeout(timer)
if (onError) onError(error)
reject(error)
})
})
}

Expand Down
Loading

0 comments on commit d894fc2

Please sign in to comment.