-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: canvas rerender/update-settings hooks (#400)
- Loading branch information
1 parent
dc49f26
commit c8bf6b1
Showing
11 changed files
with
141 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./useRemoveWidgetsListener"; | ||
export * from "./useRenderListener"; | ||
export * from "./useRenderWidgetsListener"; | ||
export * from "./useShowToastListener"; | ||
export * from "./useTheme"; | ||
export * from "./useUpdateSettingsListener"; | ||
export * from "./useWidgetsStore"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { listenToRenderWidgets } from "../../events"; | ||
import { invokeBundleWidget, invokeSetRenderReady } from "../../commands"; | ||
import { | ||
Widget, | ||
updateWidgetRender, | ||
updateWidgetRenderError, | ||
useWidgetsStore, | ||
} from "./useWidgetsStore"; | ||
|
||
const BASE_URL = new URL(import.meta.url).origin; | ||
const RAW_APIS_URL = new URL("/generated/raw-apis.js", BASE_URL).href; | ||
|
||
export function useRenderWidgetsListener() { | ||
const hasInited = useRef(false); | ||
|
||
useEffect(() => { | ||
const unlisten = listenToRenderWidgets(async (event) => { | ||
const widgets = useWidgetsStore.getState().widgets; | ||
|
||
const promises = event.payload.map(async ({ id, settings, code }) => { | ||
let apisBlobUrl; | ||
if (id in widgets) { | ||
// APIs blob URL can be reused because the contents are dependent only | ||
// on widget ID; the code blob URL will definitely change on re-render | ||
// so we revoke it here | ||
const widget = widgets[id]; | ||
apisBlobUrl = widget.apisBlobUrl; | ||
if (widget.moduleBlobUrl !== undefined) { | ||
URL.revokeObjectURL(widget.moduleBlobUrl); | ||
} | ||
} else { | ||
const apisCode = window.__DESKULPT_CANVAS_INTERNALS__.apisWrapper | ||
.replace("__DESKULPT_WIDGET_ID__", id) | ||
.replace("__RAW_APIS_URL__", RAW_APIS_URL); | ||
const apisBlob = new Blob([apisCode], { | ||
type: "application/javascript", | ||
}); | ||
apisBlobUrl = URL.createObjectURL(apisBlob); | ||
} | ||
|
||
if (code === undefined) { | ||
// If code is not provided, we need to bundle the widget | ||
try { | ||
code = await invokeBundleWidget({ | ||
id, | ||
baseUrl: BASE_URL, | ||
apisBlobUrl, | ||
}); | ||
} catch (error) { | ||
updateWidgetRenderError(id, error, apisBlobUrl, settings); | ||
return; | ||
} | ||
} | ||
|
||
const moduleBlob = new Blob([code], { type: "application/javascript" }); | ||
const moduleBlobUrl = URL.createObjectURL(moduleBlob); | ||
let module; | ||
try { | ||
module = await import(/* @vite-ignore */ moduleBlobUrl); | ||
} catch (error) { | ||
URL.revokeObjectURL(moduleBlobUrl); | ||
updateWidgetRenderError(id, error, apisBlobUrl, settings); | ||
return; | ||
} | ||
|
||
const widget = module.default as Widget; | ||
if (widget === undefined || widget.Component === undefined) { | ||
URL.revokeObjectURL(moduleBlobUrl); | ||
updateWidgetRenderError( | ||
id, | ||
"The widget must provide a default export with a `Component` property.", | ||
apisBlobUrl, | ||
settings, | ||
); | ||
return; | ||
} | ||
|
||
updateWidgetRender(id, widget, moduleBlobUrl, apisBlobUrl, settings); | ||
}); | ||
|
||
await Promise.all(promises); | ||
}); | ||
|
||
if (!hasInited.current) { | ||
invokeSetRenderReady() | ||
.then(() => { | ||
hasInited.current = true; | ||
}) | ||
.catch(console.error); | ||
} | ||
|
||
return () => { | ||
unlisten.then((f) => f()).catch(console.error); | ||
}; | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useEffect } from "react"; | ||
import { listenToUpdateSettings } from "../../events"; | ||
import { updateWidgetSettings } from "./useWidgetsStore"; | ||
|
||
export function useUpdateSettingsListener() { | ||
useEffect(() => { | ||
const unlisten = listenToUpdateSettings((event) => { | ||
const { id, settings } = event.payload; | ||
updateWidgetSettings(id, settings); | ||
}); | ||
|
||
return () => { | ||
unlisten.then((f) => f()).catch(console.error); | ||
}; | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters