Skip to content

Commit

Permalink
Transclusion unloads properly
Browse files Browse the repository at this point in the history
The transclusion event listener is now removed properly when the
element is no longer rendered and added again when it is rendered anew.

The issue was that I did not send the markdown render child's private
`containerEl` property. If that property is `null`, the component gets
unloaded.

The change fixes #14.

Furthermore, from testing throughout the day, it seems that this commit
together with the earlier `cachedRead` commit e53cdd2 fixes #15.
  • Loading branch information
schemar committed Apr 19, 2021
1 parent e53cdd2 commit b5a778e
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/Tasks/Render/Transclusion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { Sort } from './Sort';

export class Transclusion extends MarkdownRenderChild {
private readonly cache: Cache;
private readonly container: HTMLElement;
private readonly taskItem: TaskItem;
private readonly filters: ((task: Task) => boolean)[];

// private cacheCallbackId: number | undefined;
private cacheCallbackId: number | undefined;

constructor({
cache,
Expand All @@ -26,29 +25,26 @@ export class Transclusion extends MarkdownRenderChild {
super();

this.cache = cache;
this.container = container;
this.containerEl = container;
this.taskItem = taskItem;
this.filters = filters;
}

onload() {
this.render();
// this.cacheCallbackId =
this.cache.register({
this.cacheCallbackId = this.cache.register({
handler: this.render.bind(this),
});
}

// onunload() {
// // TODO: this is not working, it is always immediately unloading
// // https://github.com/schemar/obsidian-tasks/issues/14
// if (this.cacheCallbackId !== undefined) {
// this.cache.unregister(this.cacheCallbackId);
// }
// }
onunload() {
if (this.cacheCallbackId !== undefined) {
this.cache.unregister({id: this.cacheCallbackId});
}
}

public async render() {
const allTaskLists = this.container.createEl('div');
const allTaskLists = this.containerEl.createEl('div');
let tasks = this.cache.getTasks();
for (const filter of this.filters) {
tasks = tasks.filter(filter);
Expand Down Expand Up @@ -96,7 +92,7 @@ export class Transclusion extends MarkdownRenderChild {

allTaskLists.appendChild(taskList);

this.container.firstChild?.replaceWith(allTaskLists);
this.containerEl.firstChild?.replaceWith(allTaskLists);
}

private async listItemFromTask({
Expand Down

0 comments on commit b5a778e

Please sign in to comment.