Skip to content

Commit

Permalink
Add return types to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Dec 13, 2023
1 parent 85e3f71 commit b583fad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
22 changes: 17 additions & 5 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ options.diffed = vnode => {
previousComponent = currentComponent = null;
};

/** @type {(vnode: import('./internal').VNode) => void} */
// TODO: Improve typing of commitQueue parameter
/** @type {(vnode: import('./internal').VNode, commitQueue: any) => void} */
options._commit = (vnode, commitQueue) => {
commitQueue.some(component => {
try {
Expand Down Expand Up @@ -158,6 +159,7 @@ function getHookState(index, type) {
/**
* @template {unknown} S
* @param {import('./index').StateUpdater<S>} [initialState]
* @returns {[S, (state: S) => void]}
*/
export function useState(initialState) {
currentHook = 1;
Expand Down Expand Up @@ -230,8 +232,7 @@ export function useReducer(reducer, initialState, init) {
function updateHookState(p, s, c) {
if (!hookState._component.__hooks) return true;

/**
* @type {(x: import('./internal').HookState) => x is import('./internal').ReducerHookState} */
/** @type {(x: import('./internal').HookState) => x is import('./internal').ReducerHookState} */
const isStateHook = x => !!x._component;
const stateHooks =
hookState._component.__hooks._list.filter(isStateHook);
Expand Down Expand Up @@ -273,6 +274,7 @@ export function useReducer(reducer, initialState, init) {
/**
* @param {import('./internal').Effect} callback
* @param {unknown[]} args
* @returns {void}
*/
export function useEffect(callback, args) {
/** @type {import('./internal').EffectHookState} */
Expand All @@ -288,6 +290,7 @@ export function useEffect(callback, args) {
/**
* @param {import('./internal').Effect} callback
* @param {unknown[]} args
* @returns {void}
*/
export function useLayoutEffect(callback, args) {
/** @type {import('./internal').EffectHookState} */
Expand All @@ -310,6 +313,7 @@ export function useRef(initialValue) {
* @param {object} ref
* @param {() => object} createHandle
* @param {unknown[]} args
* @returns {void}
*/
export function useImperativeHandle(ref, createHandle, args) {
currentHook = 6;
Expand All @@ -328,11 +332,13 @@ export function useImperativeHandle(ref, createHandle, args) {
}

/**
* @param {() => unknown} factory
* @template {unknown} T
* @param {() => T} factory
* @param {unknown[]} args
* @returns {T}
*/
export function useMemo(factory, args) {
/** @type {import('./internal').MemoHookState} */
/** @type {import('./internal').MemoHookState<T>} */
const state = getHookState(currentIndex++, 7);
if (argsChanged(state._args, args)) {
state._pendingValue = factory();
Expand All @@ -347,6 +353,7 @@ export function useMemo(factory, args) {
/**
* @param {() => void} callback
* @param {unknown[]} args
* @returns {() => void}
*/
export function useCallback(callback, args) {
currentHook = 8;
Expand Down Expand Up @@ -390,6 +397,7 @@ export function useDebugValue(value, formatter) {

/**
* @param {(error: unknown, errorInfo: import('preact').ErrorInfo) => void} cb
* @returns {[unknown, () => void]}
*/
export function useErrorBoundary(cb) {
/** @type {import('./internal').ErrorBoundaryHookState} */
Expand Down Expand Up @@ -479,6 +487,7 @@ function afterNextFrame(callback) {
/**
* Schedule afterPaintEffects flush after the browser paints
* @param {number} newQueueLength
* @returns {void}
*/
function afterPaint(newQueueLength) {
if (newQueueLength === 1 || prevRaf !== options.requestAnimationFrame) {
Expand All @@ -489,6 +498,7 @@ function afterPaint(newQueueLength) {

/**
* @param {import('./internal').HookState} hook
* @returns {void}
*/
function invokeCleanup(hook) {
// A hook cleanup can introduce a call to render which creates a new root, this will call options.vnode
Expand All @@ -506,6 +516,7 @@ function invokeCleanup(hook) {
/**
* Invoke a Hook's effect
* @param {import('./internal').EffectHookState} hook
* @returns {void}
*/
function invokeEffect(hook) {
// A hook call can introduce a call to render which creates a new root, this will call options.vnode
Expand All @@ -518,6 +529,7 @@ function invokeEffect(hook) {
/**
* @param {unknown[]} oldArgs
* @param {unknown[]} newArgs
* @returns {boolean}
*/
function argsChanged(oldArgs, newArgs) {
return (
Expand Down
8 changes: 4 additions & 4 deletions hooks/src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ export interface EffectHookState extends BaseHookState {
_cleanup?: Cleanup | void;
}

export interface MemoHookState extends BaseHookState {
_value?: unknown;
_pendingValue?: unknown;
export interface MemoHookState<T = unknown> extends BaseHookState {
_value?: T;
_pendingValue?: T;
_args?: unknown[];
_pendingArgs?: unknown[];
_factory?: () => unknown;
_factory?: () => T;
}

export interface ReducerHookState<S = unknown, A = unknown>
Expand Down

0 comments on commit b583fad

Please sign in to comment.