Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks #328

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added plugin/.DS_Store
Binary file not shown.
Binary file added plugin/src/.DS_Store
Binary file not shown.
Binary file added plugin/src/api/.DS_Store
Binary file not shown.
Binary file added plugin/src/ui/.DS_Store
Binary file not shown.
Binary file added plugin/src/ui/components/.DS_Store
Binary file not shown.
14 changes: 12 additions & 2 deletions plugin/src/ui/components/markdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RenderChildContext } from "@/ui/context";
import { MarkdownRenderer } from "obsidian";
import { MarkdownRenderer, Component } from "obsidian";
import React, { useEffect, useRef } from "react";

interface MarkdownProps {
Expand All @@ -10,14 +10,24 @@ interface MarkdownProps {
const Markdown: React.FC<MarkdownProps> = ({ content, className }) => {
const renderChild = RenderChildContext.use();
const ref = useRef<HTMLDivElement>(null);
let componentRef = useRef<Component | null>(null);

useEffect(() => {
const renderMarkdown = async () => {
if (ref.current === null) {
return;
}

await MarkdownRenderer.renderMarkdown(content, ref.current, "", renderChild);
// Create a new component or use existing one
if (componentRef.current === null) {
componentRef.current = new Component();
}

// Attach renderChild to the component
componentRef.current.addChild(renderChild);
Comment on lines +22 to +27
Copy link
Owner

@jamiebrynes7 jamiebrynes7 Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still unsure that this is correct. renderChild is a reference to the Obsidian component wrapping the injected React component, so the relationship feels like it should be the other way around, i.e. - renderChild is the parent of the new component.

Looking at the docs for MarkdownRenderer.renderMarkdown:

     * @param component - A parent component to manage the lifecycle of the rendered child components.

Why doesn't renderChild satisfy this, it is a subclass of Component already - https://github.com/jamiebrynes7/obsidian-todoist-plugin/blob/master/plugin/src/query/injector.tsx#L47 (MarkdownRenderChild is a Component)?

Can you reproduce a situation whereby we can produce this error on the current state on master? I've been paying attention to the dev console since you've opened this PR and still haven't encountered it


// Render markdown content
await MarkdownRenderer.renderMarkdown(content, ref.current, "", componentRef.current);

if (ref.current.childElementCount > 1) {
return;
Expand Down