-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks.ts
53 lines (50 loc) · 1.24 KB
/
hooks.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { current, trigger, VHook, VNode } from './'
import type { SafeWeakMap } from './types'
const triggerHookCache = new WeakMap() as SafeWeakMap<VNode | Partial<VHook>, () => void>
/**
* Returns a callback that will trigger
* a rerender on the current component.
*
* ```tsx
* let clicked = 0
* const Foo = () => <>
* {clicked++}
* <button onclick={useHook()}>click me</button>
* </>
* ```
*
* @returns The hook callback
*/
export const useHook = () => {
const hook = current.hook!
if (triggerHookCache.has(hook)) return triggerHookCache.get(hook)
const fn = () => trigger(hook as VHook)
triggerHookCache.set(hook, fn)
return fn
}
/**
* Wraps a function along with a hook
* so when called will also trigger that hook.
*
* ```tsx
* let clicked = 0
* const Foo = () => {
* const inc = useCallback(() => clicked++)
* return <>
* {clicked}
* <button onclick={inc}>click me</button>
* </>
* }
* ```
*
* @param fn Any function to wrap with the hook
* @returns The callback function
*/
export const useCallback = (fn: (...args: unknown[]) => void) => {
const hook = useHook()
return function (this: unknown, ...args: unknown[]) {
const result = fn.apply(this, args)
hook()
return result
}
}