Skip to content

Commit

Permalink
Fix/bug 424 review next cache race (#821)
Browse files Browse the repository at this point in the history
* Sort reviewdecks with updated due after review

* Comment

* Lint and Format

* Simpler code

* Deal with new notes reviews

* Format

* Comment typo

* Lint fix

* Changelog

---------

Co-authored-by: Stephen Mwangi <[email protected]>
  • Loading branch information
karimodm and st3v3nmw authored Jan 15, 2024
1 parent 9b81cdf commit 17fad0c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [Unreleased]

- Bug fix Note review doesn't open next note automatically after a review [`#424`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/424), [`#582`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/582)
- Feat: Support richer set of flashcard ordering during review; e.g. random card from random deck [`#814`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/814)
- Bug fix Problem with nested list item's indentation [`#800`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/800)
- Bug fix Problem with nested list item's indentation [`#812`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/812)
- Bug Cloze Breaks When }} Encountered [`#799`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/799)
- Bug fix: When reviewing an entire note, the metadata is applied incorrectly (on top of the already existing metadata) [`#776`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/776
- Bug 670 Label customizations not applying to menu items

#### [1.11.0](https://github.com/st3v3nmw/obsidian-spaced-repetition/compare/1.10.5...1.11.0)
Expand Down
81 changes: 58 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,6 @@ export default class SRPlugin extends Plugin {
this.easeByPath = new NoteEaseList(this.data.settings);
this.incomingLinks = {};
this.pageranks = {};
this.dueNotesCount = 0;
this.dueDatesNotes = {};
this.reviewDecks = {};

// reset flashcards stuff
Expand Down Expand Up @@ -449,13 +447,6 @@ export default class SRPlugin extends Plugin {
.moment(frontmatter["sr-due"], ["YYYY-MM-DD", "DD-MM-YYYY", "ddd MMM DD YYYY"])
.valueOf();

for (const matchedNoteTag of matchedNoteTags) {
this.reviewDecks[matchedNoteTag].scheduledNotes.push({ note: noteFile, dueUnix });
if (dueUnix <= now.valueOf()) {
this.reviewDecks[matchedNoteTag].dueNotesCount++;
}
}

let ease: number;
if (this.easeByPath.hasEaseForPath(noteFile.path)) {
ease = (this.easeByPath.getEaseByPath(noteFile.path) + frontmatter["sr-ease"]) / 2;
Expand All @@ -464,15 +455,10 @@ export default class SRPlugin extends Plugin {
}
this.easeByPath.setEaseForPath(noteFile.path, ease);

if (dueUnix <= now.valueOf()) {
this.dueNotesCount++;
}

const nDays: number = Math.ceil((dueUnix - now.valueOf()) / (24 * 3600 * 1000));
if (!Object.prototype.hasOwnProperty.call(this.dueDatesNotes, nDays)) {
this.dueDatesNotes[nDays] = 0;
// schedule the note
for (const matchedNoteTag of matchedNoteTags) {
this.reviewDecks[matchedNoteTag].scheduledNotes.push({ note: noteFile, dueUnix });
}
this.dueDatesNotes[nDays]++;
}

graph.rank(0.85, 0.000001, (node: string, rank: number) => {
Expand All @@ -497,10 +483,6 @@ export default class SRPlugin extends Plugin {
console.log(`SR: ${t("DECKS")}`, this.deckTree);
}

for (const deckKey in this.reviewDecks) {
this.reviewDecks[deckKey].sortNotes(this.pageranks);
}

if (this.data.settings.showDebugMessages) {
console.log(
"SR: " +
Expand All @@ -510,6 +492,36 @@ export default class SRPlugin extends Plugin {
);
}

this.updateAndSortDueNotes();

this.syncLock = false;
}

private updateAndSortDueNotes() {
this.dueNotesCount = 0;
this.dueDatesNotes = {};

const now = window.moment(Date.now());
Object.values(this.reviewDecks).forEach((reviewDeck: ReviewDeck) => {
reviewDeck.dueNotesCount = 0;
reviewDeck.scheduledNotes.forEach((scheduledNote: SchedNote) => {
if (scheduledNote.dueUnix <= now.valueOf()) {
reviewDeck.dueNotesCount++;
this.dueNotesCount++;
}

const nDays: number = Math.ceil(
(scheduledNote.dueUnix - now.valueOf()) / (24 * 3600 * 1000),
);
if (!Object.prototype.hasOwnProperty.call(this.dueDatesNotes, nDays)) {
this.dueDatesNotes[nDays] = 0;
}
this.dueDatesNotes[nDays]++;
});

reviewDeck.sortNotes(this.pageranks);
});

this.statusBar.setText(
t("STATUS_BAR", {
dueNotesCount: this.dueNotesCount,
Expand All @@ -518,7 +530,6 @@ export default class SRPlugin extends Plugin {
);

if (this.data.settings.enableNoteReviewPaneOnStartup) this.reviewQueueView.redraw();
this.syncLock = false;
}

async loadNote(noteFile: TFile, topicPath: TopicPath): Promise<Note> {
Expand Down Expand Up @@ -663,9 +674,33 @@ export default class SRPlugin extends Plugin {
}
await this.app.vault.modify(note, fileText);

// Update note's properties to update our due notes.
this.easeByPath.setEaseForPath(note.path, ease);

Object.values(this.reviewDecks).forEach((reviewDeck: ReviewDeck) => {
let wasDueInDeck = false;
for (const scheduledNote of reviewDeck.scheduledNotes) {
if (scheduledNote.note.path === note.path) {
scheduledNote.dueUnix = due.valueOf();
wasDueInDeck = true;
break;
}
}

// It was a new note, remove it from the new notes and schedule it.
if (!wasDueInDeck) {
reviewDeck.newNotes.splice(
reviewDeck.newNotes.findIndex((newNote: TFile) => newNote.path === note.path),
1,
);
reviewDeck.scheduledNotes.push({ note, dueUnix: due.valueOf() });
}
});

this.updateAndSortDueNotes();

new Notice(t("RESPONSE_RECEIVED"));

await this.sync();
if (this.data.settings.autoNextNote) {
this.reviewNextNote(this.lastSelectedReviewDeck);
}
Expand Down

0 comments on commit 17fad0c

Please sign in to comment.