Skip to content

Commit

Permalink
Use Colin's latest changes with a reexport and fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
mofojed committed Dec 12, 2023
1 parent 638f4d1 commit d120b83
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
6 changes: 5 additions & 1 deletion plugins/ui/src/js/src/ObjectView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ function ObjectView(props: ObjectViewProps) {
const { object } = props;
log.info('Object is', object);

const fetch = useCallback(() => object.fetch() as Promise<Widget>, [object]);
const fetch = useCallback(async () => {
// We reexport the object in case this object is used in multiplate places or closed/opened multiple times
const reexportedObject = await object.reexport();
return reexportedObject.fetch() as Promise<Widget>;
}, [object]);

const plugins = usePlugins();

Expand Down
10 changes: 9 additions & 1 deletion plugins/ui/src/js/src/UITable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ function UITable({ element }: UITableProps) {

// Just load the object on mount
useEffect(() => {
let isCancelled = false;
async function loadModel() {
log.debug('Loading table from props', element.props);
const newTable = (await element.props.table.fetch()) as Table;
const reexportedTable = await element.props.table.reexport();
const newTable = (await reexportedTable.fetch()) as Table;
if (isCancelled) {
newTable.close();
}
setTable(newTable);
}
loadModel();
return () => {
isCancelled = true;
};
}, [dh, element]);

const irisGridProps: Partial<IrisGridProps> = useMemo(() => {
Expand Down
25 changes: 15 additions & 10 deletions plugins/ui/src/js/src/WidgetHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ function WidgetHandler({ onClose, widget: wrapper }: WidgetHandlerProps) {
if (widget == null) {
return;
}
// Need to reset the exported object map and count
const widgetExportedObjectMap = new Map<number, WidgetExportedObject>();
exportedObjectMap.current = widgetExportedObjectMap;
exportedObjectCount.current = 0;
function receiveData(
data: string,
newExportedObjects: WidgetExportedObject[]
Expand All @@ -184,8 +188,14 @@ function WidgetHandler({ onClose, widget: wrapper }: WidgetHandlerProps) {
receiveData(widget.getDataAsString(), widget.exportedObjects);

return () => {
log.debug('Cleaning up listener');
log.debug('Cleaning up widget', widget);
cleanup();
widget.close();

// Clean up any exported objects that haven't been closed yet
Array.from(widgetExportedObjectMap.values()).forEach(exportedObject => {
exportedObject.close();
});
};
}, [dh, jsonClient, parseDocument, updateExportedObjects, widget]);

Expand All @@ -194,9 +204,12 @@ function WidgetHandler({ onClose, widget: wrapper }: WidgetHandlerProps) {
log.debug('loadWidget', wrapper.id, wrapper.definition);
let isCancelled = false;
async function loadWidgetInternal() {
const newWidget = await wrapper.fetch(false);
const newWidget = await wrapper.fetch();
if (isCancelled) {
newWidget.close();
newWidget.exportedObjects.forEach(exportedObject => {
exportedObject.close();
});
return;
}
log.debug('newWidget', wrapper.id, wrapper.definition, newWidget);
Expand All @@ -210,14 +223,6 @@ function WidgetHandler({ onClose, widget: wrapper }: WidgetHandlerProps) {
[wrapper]
);

useEffect(
() =>
function closeWidget() {
widget?.close();
},
[widget]
);

const handleDocumentClose = useCallback(() => {
log.debug('Widget document closed', wrapper.id);
onClose?.(wrapper.id);
Expand Down

0 comments on commit d120b83

Please sign in to comment.