Skip to content

Commit

Permalink
0.17.1-obsidian-30 (memory leak)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Jul 4, 2024
1 parent 7467358 commit 07ecb0b
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 73 deletions.
26 changes: 2 additions & 24 deletions packages/excalidraw/animated-trail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,13 @@ export class AnimatedTrail implements Trail {
}

stop() {
this.animationFrameHandler?.stop(this); //zsviczian
this.animationFrameHandler.stop(this);

if (this.trailElement?.parentNode === this.container) { //zsviczian
if (this.trailElement.parentNode === this.container) {
this.container?.removeChild(this.trailElement);
}
}

terminate() { //zsviczian
if(this.trailElement?.parentElement) {//zsviczian
this.trailElement.parentElement.removeChild(this.trailElement);
}
if(this.container?.parentElement) { //zsviczian
this.container.parentElement.removeChild(this.container);
}
//@ts-ignore
this.trailElement = null; //zsviczian
//@ts-ignore
this.container = null; //zsviczian
//@ts-ignore
this.app = null; //zsviczian
//@ts-ignore
this.animationFrameHandler = null; //zsviczian
//@ts-ignore
this.options = null; //zsviczian
//@ts-ignore
this.currentTrail = null; //zsviczian
this.pastTrails = []; //zsviczian
}

startPath(x: number, y: number) {
//zsviczian (fix offset)
if (this.container) {
Expand Down
13 changes: 5 additions & 8 deletions packages/excalidraw/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ import { AnimatedTrail } from "../animated-trail";
import { LaserTrails } from "../laser-trails";
import { withBatchedUpdates, withBatchedUpdatesThrottled } from "../reactUtils";
import { getRenderOpacity } from "../renderer/renderElement";
import { destroyObsidianUtils, getExcalidrawContentEl, hideFreedrawPenmodeCursor, hostPlugin, initializeObsidianUtils } from "../obsidianUtils";
import { getExcalidrawContentEl, hideFreedrawPenmodeCursor, hostPlugin, initializeObsidianUtils } from "../obsidianUtils";
import {
hitElementBoundText,
hitElementBoundingBoxOnly,
Expand Down Expand Up @@ -2563,7 +2563,9 @@ class App extends React.Component<AppProps, AppState> {
}

public componentWillUnmount() {
(window as any).launchQueue?.setConsumer(() => {});
this.renderer.destroy();
this.scene.destroy();
this.scene = new Scene();
this.fonts = new Fonts({ scene: this.scene });
this.renderer = new Renderer(this.scene);
Expand All @@ -2572,7 +2574,6 @@ class App extends React.Component<AppProps, AppState> {
this.resizeObserver?.disconnect();
this.unmounted = true;
this.removeEventListeners();
this.scene.destroy(true); //zsviczian
this.library.destroy();
this.laserTrails.stop();
this.eraserTrail.stop();
Expand Down Expand Up @@ -2608,8 +2609,6 @@ class App extends React.Component<AppProps, AppState> {
this.actionManager.app = null; //zsviczian
this.actionManager.actions = {} as Record<ActionName, Action>; //zsviczian
this.history.clear(); //zsviczian
//@ts-ignore
this.history = null; //zsviczian
this.store.clear(); //zsviczian
//@ts-ignore
this.store = null; //zsviczian
Expand All @@ -2624,10 +2623,8 @@ class App extends React.Component<AppProps, AppState> {
this.nearestScrollableContainer = undefined; //zsviczian
this.excalidrawContainerValue = { container: null, id:"unknown" }; //zsviczian
//@ts-ignore
this.props = null; //zsviczian
this.laserTrails.terminate(); //zsviczian
this.eraserTrail.terminate(); //zsviczian
destroyObsidianUtils(); //zsviczian
// this.laserTrails.terminate(); //zsviczian
// this.eraserTrail.terminate(); //zsviczian
/*
Object.keys(this).forEach((key) => {
//@ts-ignore
Expand Down
3 changes: 3 additions & 0 deletions packages/excalidraw/data/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ export const getFileHandle = async (
if (nativeFileSystemSupported) {
try {
const item = event.dataTransfer.items[0];
if (!item) { //zsviczian (Obsidian move tab to new pane generates a drop event without an item)
return null;
}
const handle: FileSystemHandle | null =
(await (item as any).getAsFileSystemHandle()) || null;

Expand Down
2 changes: 0 additions & 2 deletions packages/excalidraw/data/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@ class Library {
// isInitialized: false,
// libraryItems: [],
// });
//@ts-ignore
this.app = null; //zsviczian
};

resetLibrary = () => {
Expand Down
1 change: 1 addition & 0 deletions packages/excalidraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export {
getFontString, //zsviczian
getBoundTextMaxWidth, //zsviczian
mermaidToExcalidraw, //zsviczian
destroyObsidianUtils, //zsviczian
} from "../utils/export";

export {
Expand Down
22 changes: 2 additions & 20 deletions packages/excalidraw/laser-trails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,8 @@ export class LaserTrails implements Trail {
}

stop() {
this.animationFrameHandler?.stop(this); //zsviczian
this.localTrail?.stop(); //zsviczian
}

terminate() { //zsviczian
this.localTrail.terminate();
//@ts-ignore
this.localTrail = null;
//@ts-ignore
this.collabTrails = null;

if (this.container?.parentElement) {
this.container.parentElement.removeChild(this.container);
}
//@ts-ignore
this.container = null;
//@ts-ignore
this.animationFrameHandler = null;
//@ts-ignore
this.app = null;
this.animationFrameHandler.stop(this);
this.localTrail.stop();
}

onFrame() {
Expand Down
27 changes: 12 additions & 15 deletions packages/excalidraw/obsidianUtils.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@

//zsviczian, my dirty little secrets. These are hacks I am not proud of...
export let hostPlugin: any = null;

export const initializeObsidianUtils = (obsidianPlugin: any) => {
hostPlugin = obsidianPlugin;
}

export const destroyObsidianUtils = () => {
export function destroyObsidianUtils() {
hostPlugin = null;
}

const getExcalidrawConfig = () => hostPlugin.excalidrawConfig;
export function initializeObsidianUtils (obsidianPlugin: any) {
hostPlugin = obsidianPlugin;
}

export const getAreaLimit = () => {
return getExcalidrawConfig().areaLimit ?? 16777216;
export function getAreaLimit() {
return hostPlugin.excalidrawConfig.areaLimit ?? 16777216;
}

export const getWidthHeightLimit = () => {
return getExcalidrawConfig().widthHeightLimit ?? 32767;
export function getWidthHeightLimit() {
return hostPlugin.excalidrawConfig.widthHeightLimit ?? 32767;
}

export const isExcaliBrainView = () => {
export function isExcaliBrainView() {
const excalidrawView = hostPlugin.activeExcalidrawView;
if(!excalidrawView) return false;
return excalidrawView.linksAlwaysOpenInANewPane && excalidrawView.allowFrameButtonsInViewMode;
}

export const getExcalidrawContentEl = ():HTMLElement => {
export function getExcalidrawContentEl():HTMLElement {
const excalidrawView = hostPlugin.activeExcalidrawView;
if(!excalidrawView) return document.body;
return excalidrawView.contentEl as HTMLElement;
}

export const hideFreedrawPenmodeCursor = () => {
export function hideFreedrawPenmodeCursor() {
return !hostPlugin.settings.penModeCrosshairVisible;
}
}
2 changes: 1 addition & 1 deletion packages/excalidraw/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zsviczian/excalidraw",
"version": "0.17.1-obsidian-29",
"version": "0.17.1-obsidian-30",
"main": "main.js",
"types": "types/excalidraw/index.d.ts",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions packages/excalidraw/scene/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class Scene {
};
}

destroy(force: boolean = false) { //zsviczian
destroy() {
this.elements = [];
this.nonDeletedElements = [];
this.nonDeletedFramesLikes = [];
Expand All @@ -332,7 +332,7 @@ class Scene {
this.selectedElementsCache.cache.clear();

Scene.sceneMapById.forEach((scene, elementKey) => {
if (force || (scene === this)) { //zsviczian
if (scene === this) {
Scene.sceneMapById.delete(elementKey);
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,5 @@ export {
} from "../excalidraw/element/textElement"; //zsviczian
export { getFontString } from "../excalidraw/utils"; //zsviczian
export { getBoundTextMaxWidth } from "../excalidraw/element/textElement"; //zsviczian
export { mermaidToExcalidraw } from "../excalidraw/components/TTDDialog/MermaidToExcalidraw"; //zsviczian
export { mermaidToExcalidraw } from "../excalidraw/components/TTDDialog/MermaidToExcalidraw"; //zsviczian
export { destroyObsidianUtils } from "../excalidraw/obsidianUtils"; //zsviczian

0 comments on commit 07ecb0b

Please sign in to comment.