Skip to content

feat: Thenable utility #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
FiberRoot,
MemoizedState,
ReactRenderer,
Thenable,
} from './types.js';

// https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactWorkTags.js
Expand Down Expand Up @@ -261,6 +262,54 @@ export const traverseProps = (
return false;
};

export type ThenableData =
| ({ status: 'fulfilled'; value: unknown } & Thenable<unknown>)
| ({ status: 'rejected'; reason: unknown } & Thenable<unknown>)
| ({ status: 'pending' } & Thenable<unknown>);

interface ThenableState {
_debugThenableState: ThenableData[] | { thenables: ThenableData[] };
}

function isThenableState(dependencies: object): dependencies is ThenableState {
return '_debugThenableState' in dependencies;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this available in the react production build?

Copy link
Contributor Author

@lxsmnsyc lxsmnsyc Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's dev-only, had to check first if it's assigned.

}

export const getThenables = (fiber: Fiber): ThenableData[] => {
if (fiber.dependencies && isThenableState(fiber.dependencies)) {
// https://github.com/facebook/react/blob/b3a95caf61bc716fb618997e6e9f3a0c8c9c8374/packages/react-reconciler/src/ReactFiberThenable.js#L80
if (Array.isArray(fiber.dependencies._debugThenableState)) {
return fiber.dependencies._debugThenableState;
}
return fiber.dependencies._debugThenableState.thenables;
}
return [];
};

/**
* Traverses up or down a {@link Fiber}'s Thenables, return `true` to stop and select the current and previous props value.
*/
export const traverseThenables = (
fiber: Fiber,
selector: (
prev?: ThenableData,
next?: ThenableData,
// biome-ignore lint/suspicious/noConfusingVoidType: may or may not exist
) => boolean | void,
): boolean => {
try {
const nextThenables = getThenables(fiber);
const prevThenables = fiber.alternate ? getThenables(fiber.alternate) : [];

const maxLength = Math.max(nextThenables.length, prevThenables.length);

for (let i = 0; i < maxLength; i++) {
if (selector(prevThenables[i], nextThenables[i]) === true) return true;
}
} catch {}
return false;
};

/**
* Returns `true` if the {@link Fiber} has rendered. Note that this does not mean the fiber has rendered in the current commit, just that it has rendered in the past.
*/
Expand Down