mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-22 21:08:12 +08:00
Restructure frontend TS files so managers/stores export destructors instead of returning them from their constructors (#3919)
* Replace parameter passing with getContext and extract destroy functions to module-level exports * Resend layouts from Rust when editor is re-mounted on HMR * Code review
This commit is contained in:
@@ -1,25 +1,22 @@
|
||||
import type { Editor } from "@graphite/editor";
|
||||
|
||||
let currentCleanup: (() => void) | undefined;
|
||||
let currentArgs: [Editor] | undefined;
|
||||
let editorRef: Editor | undefined = undefined;
|
||||
|
||||
export function createLocalizationManager(editor: Editor) {
|
||||
currentArgs = [editor];
|
||||
editorRef = editor;
|
||||
|
||||
// Subscribe to process backend event
|
||||
editor.subscriptions.subscribeFrontendMessage("TriggerAboutGraphiteLocalizedCommitDate", (data) => {
|
||||
const localized = localizeTimestamp(data.commitDate);
|
||||
editor.handle.requestAboutGraphiteDialogWithLocalizedCommitDate(localized.timestamp, localized.year);
|
||||
});
|
||||
|
||||
function destroy() {
|
||||
editor.subscriptions.unsubscribeFrontendMessage("TriggerAboutGraphiteLocalizedCommitDate");
|
||||
}
|
||||
|
||||
currentCleanup = destroy;
|
||||
return { destroy };
|
||||
}
|
||||
export type LocalizationManager = ReturnType<typeof createLocalizationManager>;
|
||||
|
||||
export function destroyLocalizationManager() {
|
||||
const editor = editorRef;
|
||||
if (!editor) return;
|
||||
|
||||
editor.subscriptions.unsubscribeFrontendMessage("TriggerAboutGraphiteLocalizedCommitDate");
|
||||
}
|
||||
|
||||
function localizeTimestamp(utc: string): { timestamp: string; year: string } {
|
||||
// Timestamp
|
||||
@@ -38,6 +35,6 @@ function localizeTimestamp(utc: string): { timestamp: string; year: string } {
|
||||
|
||||
// Self-accepting HMR: tear down the old instance and re-create with the new module's code
|
||||
import.meta.hot?.accept((newModule) => {
|
||||
currentCleanup?.();
|
||||
if (currentArgs) newModule?.createLocalizationManager(...currentArgs);
|
||||
destroyLocalizationManager();
|
||||
if (editorRef) newModule?.createLocalizationManager(editorRef);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user