-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
29 lines (25 loc) · 844 Bytes
/
index.ts
File metadata and controls
29 lines (25 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { type DependencyList, useEffect, useInsertionEffect, useLayoutEffect } from 'react'
type EffectHook = typeof useEffect
const upgradeHook = (useHook: EffectHook) =>
(fn: () => Generator<Promise<unknown>, void>, deps?: DependencyList) => {
useHook(() => {
const i = fn()
void (async () => {
let resolved
while (true) {
const { value, done } = i.next(resolved)
if (done) {
break
}
resolved = await value
}
})()
return () => { i.return() }
}, deps)
}
export function * wait<T> (promise: Promise<T>): Generator<Promise<T>, T> {
return (yield promise) as T
}
export const useEff = upgradeHook(useEffect)
export const useLayoutEff = upgradeHook(useLayoutEffect)
export const useInsertionEff = upgradeHook(useInsertionEffect)