Skip to content

Commit

Permalink
Adjust useAsyncValue typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Aug 26, 2024
1 parent d4d4be1 commit a09ccd2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/hooks/useAsyncValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,24 @@ import { useAsyncCallback, type AsyncCallbackState } from './useAsyncCallback';
export const useAsyncValue = <A extends Array<unknown>, R>(
callback: ( ...args: Array<A> ) => Promise<R>,
deps: DependencyList
): AsyncCallbackState<R> => {
): AsyncCallbackHookResult<R> => {
const [ asyncCallback, asyncState ] = useAsyncCallback( callback );

useInstantEffect( asyncCallback, deps );

// There might be short delay between the effect and the state update.
// So it is possible that the status is still 'idle' after the effect.
// In such case, we should return 'loading' status because the effect is already queued to be executed.
if ( asyncState.status === 'idle' ) {
return {
status: 'loading'
};
}

return asyncState;
};

/**
* The result of the `useAsyncValue` hook.
*/
type AsyncCallbackHookResult<R> = Exclude<AsyncCallbackState<R>, { status: 'idle' }>;
2 changes: 1 addition & 1 deletion tests/cloud/withCKEditorCloud.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe( 'withCKEditorCloud', () => {
deferredPlugin.resolve( 123 );

expect( await findByText( 'Your Editor 1' ) ).toBeVisible();
expect( lastRenderedMockProps.current?.cloud.CKPlugins?.Plugin ).toBe( 123 );
expect( lastRenderedMockProps.current?.cloud.loadedPlugins?.Plugin ).toBe( 123 );
} );

it( 'should show error message when cloud loading fails', async () => {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"DOM.Iterable"
],
"types": [
"./vite-env.d.ts"
"./vite-env.d.ts",
"@testing-library/jest-dom/vitest"
],
"skipLibCheck": true,
"jsx": "react",
Expand Down

0 comments on commit a09ccd2

Please sign in to comment.