Skip to content

Commit

Permalink
feat(ClientSideAPI): Hold React-baked API methods undefined until rea…
Browse files Browse the repository at this point in the history
…dy (#70)

* feat(ClientSideAPI): Hold API methods undefined until ready

* chore(ClientSideAPI): set static (non-React) APIs with static mappings

---------

Co-authored-by: Darran Boyd <drboyd@amazon.com>
  • Loading branch information
dboyd13 and Darran Boyd authored Jan 17, 2024
1 parent ea41400 commit 824df9b
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import { useCallback, FC, PropsWithChildren, useEffect } from 'react';
import { useWorkspacesContext } from '../../../contexts';
import { ThreatComposerNamespace } from '../../../customTypes/dataExchange';
import useExportImport, {
PLACEHOLDER_EXCHANGE_DATA,
PLACEHOLDER_EXCHANGE_DATA_FOR_WORKSPACE,
} from '../../../hooks/useExportImport';
import useExportImport from '../../../hooks/useExportImport';
import useRemoveData from '../../../hooks/useRemoveData';
import EventController from '../../../utils/EventController';

Expand All @@ -36,17 +33,9 @@ const stringifyWorkspaceData = (data: any) => {
const eventController = new EventController();

window.threatcomposer = {
getWorkspaceList: () => [PLACEHOLDER_EXCHANGE_DATA_FOR_WORKSPACE],
getCurrentWorkspaceMetadata: () => PLACEHOLDER_EXCHANGE_DATA_FOR_WORKSPACE,
getCurrentWorkspaceData: () => PLACEHOLDER_EXCHANGE_DATA,
stringifyWorkspaceData,
setCurrentWorkspaceData: () => Promise.resolve(),
switchWorkspace: () => {},
createWorkspace: () =>
Promise.resolve(PLACEHOLDER_EXCHANGE_DATA_FOR_WORKSPACE),
deleteWorkspace: () => Promise.resolve(),
renameWorkspace: () => Promise.resolve(),
addEventListener: (eventName, eventHandler) => eventController.addEventListener(eventName, eventHandler),
addEventListener: (eventName, eventHandler) =>
eventController.addEventListener(eventName, eventHandler),
dispatchEvent: (event) => eventController.dispatchEvent(event),
};

Expand Down Expand Up @@ -104,6 +93,10 @@ const WindowExporter: FC<PropsWithChildren<{}>> = ({ children }) => {
window.threatcomposer.renameWorkspace = renameWorkspace;
}, [renameWorkspace]);

useEffect(() => {
window.threatcomposer.stringifyWorkspaceData = stringifyWorkspaceData;
}, []);

return <>{children}</>;
};

Expand Down
Loading

0 comments on commit 824df9b

Please sign in to comment.