Skip to content

Commit

Permalink
refactor: reduce eslint-warnings & type changes (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaellis authored Jan 15, 2021
1 parent cdc351a commit 68460e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/helpers/act.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { Act } from '../types/react'

import { suppressErrorOutput } from './console'

import { isPromise } from './promises'

function createActWrapper(baseAct: Act) {
const act: Act = async (callback: () => any) => {
const act: Act = async (callback: () => unknown) => {
const restoreOutput = suppressErrorOutput()
try {
let awaitRequired = false
const actResult = (baseAct(() => {
const actResult = baseAct(() => {
const callbackResult = callback()
awaitRequired = callbackResult !== undefined && !!callbackResult.then
return callbackResult
}) as any) as PromiseLike<undefined>

awaitRequired = isPromise(callbackResult)
return callbackResult as Promise<void>
})
return awaitRequired ? await actResult : undefined
} finally {
restoreOutput()
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ async function callAfter(callback: () => void, ms: number) {
callback()
}

export { resolveAfter, callAfter }
function isPromise<T>(value: unknown): boolean {
return value !== undefined && typeof (value as PromiseLike<T>).then === 'function'
}

export { resolveAfter, callAfter, isPromise }

0 comments on commit 68460e4

Please sign in to comment.