Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
{
"path": "lib/components/internal/widget-exports.js",
"brotli": false,
"limit": "890 kB",
"limit": "891 kB",
"ignore": "react-dom"
}
],
Expand Down
21 changes: 20 additions & 1 deletion src/app-layout/__tests__/runtime-drawers-widgetized.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,36 @@ describeEachAppLayout({ themes: ['refresh-toolbar'] }, ({ size }) => {
expect(globalDrawersWrapper.findDrawerById(drawerDefaults.id)!.isActive()).toBe(true);
});

test('isAppLayoutReady returns true when app layout is ready', () => {
test('isAppLayoutReady returns true when app layout is ready', async () => {
expect(awsuiWidgetPlugins.isAppLayoutReady()).toBe(false);
const { rerender } = renderComponent(<AppLayout />);

expect(awsuiWidgetPlugins.isAppLayoutReady()).toBe(true);
await expect(awsuiWidgetPlugins.whenAppLayoutReady()).resolves.toBe(undefined);

rerender(<></>);

expect(awsuiWidgetPlugins.isAppLayoutReady()).toBe(false);
});

test('whenAppLayoutReady resolves when app layout is ready', async () => {
const readyPromise = awsuiWidgetPlugins.whenAppLayoutReady();

let isResolved = false;
readyPromise.then(() => {
isResolved = true;
});

expect(isResolved).toBe(false);

const { rerender } = renderComponent(<AppLayout />);

rerender(<></>);

await readyPromise;
expect(isResolved).toBe(true);
});

test('adds ai drawer to an already rendered component', () => {
const { globalDrawersWrapper } = renderComponent(<AppLayout />);
expect(globalDrawersWrapper.findAiDrawerTrigger()).toBeFalsy();
Expand Down
2 changes: 1 addition & 1 deletion src/internal/plugins/widget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
export * from './widget/interfaces';
export { registerLeftDrawer, updateDrawer, isAppLayoutReady } from './widget/internal';
export { registerLeftDrawer, updateDrawer, isAppLayoutReady, whenAppLayoutReady } from './widget/internal';
16 changes: 16 additions & 0 deletions src/internal/plugins/widget/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { AppLayoutMessage, AppLayoutUpdateMessage, DrawerPayload, RegisterDrawer

const storageKeyMessageHandler = Symbol.for('awsui-widget-api-message-handler');
const storageKeyInitialMessages = Symbol.for('awsui-widget-api-initial-messages');
const storageKeyReadyDeferCallbacks = Symbol.for('awsui-widget-api-ready-defer');

interface WindowWithApi extends Window {
[storageKeyMessageHandler]: AppLayoutHandler | undefined;
[storageKeyInitialMessages]: Array<RegisterDrawerMessage> | undefined;
[storageKeyReadyDeferCallbacks]: Array<(value?: unknown) => void> | undefined;
}

type AppLayoutHandler = (event: AppLayoutMessage) => void;
Expand All @@ -33,6 +35,8 @@ export function registerAppLayoutHandler(handler: AppLayoutHandler) {
reportRuntimeApiWarning('AppLayoutWidget', 'Double registration attempt, the old handler will be overridden');
}
win[storageKeyMessageHandler] = handler;
win[storageKeyReadyDeferCallbacks]?.forEach(fn => fn());
win[storageKeyReadyDeferCallbacks] = [];
return () => {
win[storageKeyMessageHandler] = undefined;
};
Expand All @@ -49,6 +53,18 @@ export function isAppLayoutReady() {
return !!getAppLayoutMessageHandler();
}

/**
* Returns a promise that resolves once the app layout has loaded
*/
export function whenAppLayoutReady() {
if (isAppLayoutReady()) {
return Promise.resolve();
}
const win = getWindow();
win[storageKeyReadyDeferCallbacks] = win[storageKeyReadyDeferCallbacks] ?? [];
return new Promise(resolve => win[storageKeyReadyDeferCallbacks]?.push(resolve));
}

/**
* Registers a new runtime drawer to app layout
* @param drawer
Expand Down
Loading