-
Notifications
You must be signed in to change notification settings - Fork 29
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
Send updates when NoteID is populated #106
Conversation
Hey @benjamin-weller sorry for being slow to look at this. I've been away i will try and take a look this week |
SG, no worries. I'm relatively green WRT to JavaScript async + await, so I think that's likely where I could have missed something. Additionally how would you suggest I write unit tests for these changes? |
src/models/Deck.ts
Outdated
async createAndUpdateCards() { | ||
// Push the updated cards (based on noteID) | ||
const updateCards = this.cards.filter((v) => v.noteId); | ||
this._pushUpdatedCardsToAnki(updateCards); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could probably use reduce so you only cycle through the cards once
this.cards.reduce((acc, v) => {v.noteId ? acc[update].push(v) : acc[new].push(v) }, {})
Or something like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll going to use forEach
here because that's what makes sense in my brain.
src/models/Deck.ts
Outdated
async pushNewCardsToAnki() { | ||
// Calls anki to update the fields of all the passed cards. | ||
private async _pushUpdatedCardsToAnki(cards: Card[]) { | ||
cards?.map((card) => this.ankiService?.updateFields(card)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is async but there's no await
being used.
You're also not returning any value so map
is redundant here, you may instead just want to use forEach.
Do you want to wait for this function to finish? If so you could return the promises into a Promise.all() and await that
cards?.map((card) => this.ankiService?.updateFields(card)); | |
const cardsPromise = cards?.map((card) => this.ankiService?.updateFields(card)); | |
await Promise.all(cardsPromise); |
The ?
on cards would be redundant as it should always be an array coming into this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to drop the async
on this function as I don't care exactly when this finishes.
I'll also switch to forEach
.
I'll remove the optional chaining you mentioned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we do need to care when operations like this finish though. Due to the progress bar that’s shown in VSCode, it needs to know when to stop showing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've taken your advice and ensured I'm await
ing the async _pushUpdatedCardsToAnki
function now.
Yeah so far looks good. So far I see support for parsing the note and updating/instead of pushing if there is a noteid (this PR) For tests: |
Yes I have some "working" code for this on my computer. I want to roll this change out first because I perceive that change to be higher risk/would clog up the review for this change. WRT tests, I manually tested. Idk how exactly I would mock updating a card in Anki here? Unless you have a good idea now on how to do that I'm leaning towards not implementing them for this feature. That won't be the case for the next one (updating the markdown) as that's higher risk imo. Proof of manual testing (works on my machine): Content of MD file:
Ran: I want to point out that my changes allow you to update a notecard regardless of which deck it exists in. This may feel like a bug at first, but it's what I want, my use case; I may relocate an entire subsection of markdown document, but that doesn't mean when I re-push to Anki that I want to re-create new cards, I just want to know that the particular cards is up to date. |
sure this sounds fair, but it may conflict with the “Send dir to Anki” command. Which sends cards to Anki based on their directory location. If I have noteids on the cards and move them which operation wins out? We may want to think about and how we respond to it |
Hi, awesome job! Updates are the one feature I've been missing, so very happy this is getting tackled. Thank you so much! In eager awaiting of this, I've written a script to update an existing markdown file (without note ids) with the note ids from Anki, in a similar format as #106 (comment) does (though I'm using You can find it in this gist https://gist.github.com/mkraenz/a7e0473ca80c07522ff75dc2fd9e52ba Maybe it becomes a reference for you or somebody else. In particular, the sanitization of the card front is not trivial. The summary at the end looks like this for 526 cards with backticks, question marks, parentheses in the card front all being gracefully handled. Only one of the cards cannot be resolved to its note id.
PS: Sorry for randomly dropping this into this PR. |
I'm not certain exactly which one wins out tbh. I'm willing to feature flag this (only have cards be updated if the user opts in), this ensures that they explicitly agree that things might not be 100% compatible. Is that fair for you? |
@mkraenz It's great to hear that Yes the card sanitation is currently the most tricky part, I assume that the text of the header is equivalent to the text of the front of the card (I ask the HTML for the inner content but even then there can be stuff like |
Ok lets feature flag it for now. |
Is there anything else I need to do such that these changes are ready to merge? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I’ve been slow on this, LGTM
@benjamin-weller how has this been for you? Have you been testing it? |
Hey, I've created a new PR, let's talk there. |
Are you guys approaching a release soon or do we need to install from main? |
Jason can you please take a look at generally what I'm thinking about doing and give me a LGTM or not. I just don't want to get going on these changes and be barking up the wrong tree.
Continuation of PR 92.