docs: add a guide for using Components#204
Conversation
| temporarilyUnloadWidget() { | ||
| // You can also unload it temporarily if needed | ||
| this.widget.unload(); | ||
| window.setTimeout(() => this.widget.load(), 5000); | ||
| } |
There was a problem hiding this comment.
I'm not sure this is a good example for plugins. Usually Components should not be calling load/unload themselves. If they are properly attached to a parent, then the parent Component is responsible for loading and unloading
There was a problem hiding this comment.
I've removed this example for now. The main idea behind this is that you can fully unload and re-load the component, and everything will be handled properly.
| window.addEventListener("resize", onResize); | ||
| } | ||
|
|
||
| onunload() { | ||
| window.removeEventListener("resize", onResize); | ||
| } |
There was a problem hiding this comment.
This example is awkward since onResize is undefined and its very easy to mess up removeEventListener (if you don't pass in the exact instance, it won't be properly removed).
There was a problem hiding this comment.
I am not sure if there is a different, but still simple, example that can be used for this. For now, I have changed the example such that onResize is a method on the class.
| } | ||
| ``` | ||
|
|
||
| While this works, it quickly becomes repetitive and error-prone. Fortunately, Obsidian's `Component` system offers a better solution. |
There was a problem hiding this comment.
This seems a little awkward since the example with onload and onunload is already using functions of Component.
There was a problem hiding this comment.
I think for the purposes of this example, that is fine. The main point to demonstrate here is that explicitly registering something to the Component is safer, rather than handling the loading/unloading yourself (be it in a Plugin, a custom class that you manage the lifecycle of, etc...). The Plugin just happens to be the most well known construction for this 😅
What do you think of:
While this works fine, you need to remember to remove your listener in the
onunloadcall. Obsidian fortunately offers a better and simpler methods to register events via theComponentsystem:
| createEl("button", { text: "Click me!" }) | ||
| ); | ||
| // Good | ||
| button.addEventListener("click", onButtonClick); |
There was a problem hiding this comment.
The Obsidian native approach for these would be:
new ButtonComponent(this.containerEl).setButtonText('Click me!').onClick(() => {
new Notice('Button clicked!');
});
// keyboard events
this.scope = new Scope(this.app.scope); //this does not need to be unloaded, Obsidian takes care of it
this.scope.register(['Ctrl'], 's', () => {
new Notice('Shortcut pressed!');
})There was a problem hiding this comment.
I intentionally chose this construction with button and window to showcase the parallels for registering listeners, and the difference in how long these listeners would live for.
Do you think this should be added as a separate example? Or maybe as an additional note clarifying that there is a more correct implementation available using Obsidian API's.
…hitespaces) docs: remove passive voice constructions docs: improve comments for codeblocks
liamcain
left a comment
There was a problem hiding this comment.
This is great! Very informative, and easy to follow.
Co-authored-by: Liam Cain <hi@liamca.in>
|
|
||
| ### Solution: Add or use an existing component | ||
|
|
||
| Instead, you should pass a component that has a clearly defined lifecycle, one that will be loaded and unloaded properly. Since _every_ view is a `Component`, we can simply pass the `View` instance itself via `this`: |
There was a problem hiding this comment.
we might want to mention that using the plugin / app instance as the compoent is a bad idea here.
There was a problem hiding this comment.
I think the current phrasing is clear enough. Here's my attempt to add more info:
Instead, you should pass a component that has a clearly defined lifecycle, one that will be loaded and unloaded at an appropriate time. Both
PluginandVieware components; however,Viewis a more appropriate choice since its lifespan matches with the MarkdownRenderer. We can simply pass theViewinstance itself viathis:
Might a bit wordy though...
There was a problem hiding this comment.
I hadn't even considered that people might use plugin or app... (is App a component though?) Regardless, I think this is an useful addition.
There was a problem hiding this comment.
Added the suggestion!
This is a preliminary PR for adding a guide on how to use the
Componentclass for managing lifecycles and events.This guide aims to give developers a tool to avoid common mistakes made during plugin development, including:
window,document,view, ..., but not removing them up when the plugin/view/... is unloadedComponentfor aMarkdownRenderer.rendercall, but never attaching it to a parentComponentAny input would be very welcome: