-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
refactor+perf(study-screen): custom scheduler #19950
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ import com.ichi2.anki.ui.windows.reviewer.autoadvance.AutoAdvance | |
| import com.ichi2.anki.utils.CollectionPreferences | ||
| import com.ichi2.anki.utils.Destination | ||
| import com.ichi2.anki.utils.ext.answerCard | ||
| import com.ichi2.anki.utils.ext.cardStateCustomizer | ||
| import com.ichi2.anki.utils.ext.cardStatsNoCardClean | ||
| import com.ichi2.anki.utils.ext.currentCardStudy | ||
| import com.ichi2.anki.utils.ext.flag | ||
|
|
@@ -72,7 +73,6 @@ import com.ichi2.anki.utils.ext.previousCardStudy | |
| import com.ichi2.anki.utils.ext.setUserFlagForCards | ||
| import kotlinx.coroutines.CompletableDeferred | ||
| import kotlinx.coroutines.Deferred | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.withTimeoutOrNull | ||
|
|
@@ -120,6 +120,7 @@ class ReviewerViewModel( | |
|
|
||
| override val server: AnkiServer = AnkiServer(this, repository.getServerPort()).also { it.start() } | ||
| private val stateMutationKey = TimeManager.time.intTimeMS().toString() | ||
| private val stateMutationJs: Deferred<String> = asyncIO { withCol { cardStateCustomizer } } | ||
| private var typedAnswer = "" | ||
|
|
||
| private val autoAdvance = AutoAdvance(this) | ||
|
|
@@ -130,16 +131,14 @@ class ReviewerViewModel( | |
| * A flag that determines if the SchedulingStates in CurrentQueueState are | ||
| * safe to persist in the database when answering a card. This is used to | ||
| * ensure that the custom JS scheduler has persisted its SchedulingStates | ||
| * back to the Reviewer before we save it to the database. If the custom | ||
| * scheduler has not been configured, then it is safe to immediately set | ||
| * this to true. | ||
| * back to the Reviewer before we save it to the database. | ||
| * | ||
| * This flag should be set to false when we show the front of the card | ||
| * and only set to true once we know the custom scheduler has finished its | ||
| * execution, or set to true immediately if the custom scheduler has not | ||
| * This flag should be reset when we show the front of the card | ||
| * and only complete once we know the custom scheduler has finished its | ||
| * execution, or complete immediately if the custom scheduler has not | ||
| * been configured. | ||
| */ | ||
| private var statesMutated = true | ||
| private var mutationSignal = CompletableDeferred(Unit) | ||
|
|
||
| val answerButtonsNextTimeFlow: MutableStateFlow<AnswerButtonsNextTime?> = MutableStateFlow(null) | ||
| private val shouldShowNextTimes: Deferred<Boolean> = | ||
|
|
@@ -193,9 +192,7 @@ class ReviewerViewModel( | |
| fun onShowAnswer() { | ||
| Timber.v("ReviewerViewModel::onShowAnswer") | ||
| launchCatchingIO { | ||
| while (!statesMutated) { | ||
| delay(50) | ||
| } | ||
| mutationSignal.await() | ||
|
|
||
| val typedAnswerResult = CompletableDeferred<String>() | ||
| if (typeAnswerFlow.value != null) { | ||
|
|
@@ -250,7 +247,7 @@ class ReviewerViewModel( | |
| } | ||
|
|
||
| fun onStateMutationCallback() { | ||
| statesMutated = true | ||
| mutationSignal.complete(Unit) | ||
| } | ||
|
|
||
| private suspend fun emitEditNoteDestination() { | ||
|
|
@@ -411,13 +408,14 @@ class ReviewerViewModel( | |
| } | ||
|
|
||
| private suspend fun runStateMutationHook() { | ||
| val state = queueState.await() ?: return | ||
| val js = state.customSchedulingJs | ||
| val js = stateMutationJs.await() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to the question of hanging completions - looks like the js hook itself is potentially just hanging it's fine to me as a roughly direct / same impact swith of style (without altering risk, seemingly?) from previous style |
||
| if (js.isEmpty()) { | ||
| statesMutated = true | ||
| if (!mutationSignal.isCompleted) { | ||
| mutationSignal.complete(Unit) | ||
| } | ||
| return | ||
| } | ||
| statesMutated = false | ||
| mutationSignal = CompletableDeferred() | ||
| statesMutationEvalFlow.emit( | ||
| "anki.mutateNextCardStates('$stateMutationKey', async (states, customData, ctx) => { $js });", | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Likely off-topic & could be moved to another issue.
I'm concerned there's no cancellation support.
What happens when a card is flipped, but this hasn't run yet? What if there's an error, or the code is slow?
Uh oh!
There was an error while loading. Please reload this page.
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.
Functionally, the only difference in comparison with the previous approach is how it waits for the signal, i.e.
while...delay(50)vsCompletableDeferred.await().Should it have some kind of timeout instead of waiting indefinitely? Perhaps.
Is the
custom schedulerlogic convoluted? Yes. And that is a consequence of how Anki desktop is architectured. I'm mostly following how the desktop version make the reviewer work, even in a barely used setting like this one.ReviewerFragmentTest tests if the custom scheduler is working, so that is covered.
Making the logic safer to future refactors need some integration tests, and I'm still far from them.
So, create another issue if you want. Or simply wait until this turns into a problem (or not).