Skip to content

Commit

Permalink
fix: clear cache after leaf view unload
Browse files Browse the repository at this point in the history
  • Loading branch information
Z233 committed Sep 16, 2023
1 parent 6ad2e8f commit d218585
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Platform,
TFile,
type MarkdownPostProcessorContext,
WorkspaceLeaf,
WorkspaceTabs,
} from 'obsidian'
import { defaultSettings, SuperPlanSettings } from './setting/settings'
import { SuperPlanSettingsTab } from './setting/settings-tab'
Expand Down Expand Up @@ -126,36 +128,44 @@ export default class SuperPlan extends Plugin {

let activeFile: Maybe<TFile> = this.app.workspace.getActiveFile()

if (!activeFile) {
this.registerEvent(
this.app.workspace.on('active-leaf-change', (leaf) => {
activeFile = this.app.workspace.getActiveFile()
if (queue.size) {
queue.forEach((fn) => fn())
queue.clear()
this.registerEvent(
this.app.workspace.on('active-leaf-change', (leaf) => {
const leafFile = this.app.workspace.getActiveFile()
activeFile = leafFile

if (leaf) {
leaf.view.onunload = () => {
if (leafFile && filesMap.has(leafFile)) {
filesMap.delete(leafFile)
}
}
})
)
}
}

if (queue.size) {
queue.forEach((fn) => fn())
queue.clear()
}
})
)

this.registerEditorExtension(
EditorView.updateListener.of((update) => {
if (!update.docChanged) return

const doc = update.state.doc
update.changes.iterChanges((fromA, toA, fromB, toB) => {
// Get the starting and ending line numbers for the changed lines
const changedLineStart = doc.lineAt(fromB).number - 1
const changedLineEnd = doc.lineAt(toB).number - 1

// Notify the change to the sync.
if (activeFile && filesMap.has(activeFile)) {
const { sync } = filesMap.get(activeFile)!
const { lineStart, lineEnd } = sync.getInfo()

if (changedLineStart >= lineStart + 1 && changedLineEnd <= lineEnd - 1) {
sync.notify({
source: [...doc.iterLines(lineStart + 2, lineEnd + 1)].join('\n')
source: [...doc.iterLines(lineStart + 2, lineEnd + 1)].join('\n'),
})
}
}
Expand Down

0 comments on commit d218585

Please sign in to comment.