aidam38 / roamsr

Spaced Repetition in Roam Research
https://roamresearch.com/#/app/roam-depot-developers/page/uQSCwVKx0
98 stars 18 forks source link

refactoring ideas #16

Open hungriesthippo opened 3 years ago

hungriesthippo commented 3 years ago

Just want to say this is not a criticism of the code! It's not unmanageable. Mostly I think it's a fun example for a more functional/reactive style.

First: defining the state

{
  inSession: boolean
  cardsToReview: Card[], // current card at 0
  answerRevealed: boolean
}

a sketch of the main loop

state.cardsToReview = loadCards();
while (state.inSession && state.cardsToReview.length) {
  await render(state);
  const card = state.cardsToReview[0];
  let [answerRevealed, signal] = await showAnswerOrSkip(card);
  if (answerRevealed !== state.answerRevealed) {
    state.answerRevealed = answerRevealed
    await render(state);
  }
  signal ||= await getResponse(card);
  updateHistory(card, signal);
  state.cardsToReview.splice(0, 1);  // or pass cardsToReview to updateHistory
  state.answerRevealed = false;
}
render(state);

Let me know what you think. My customization stuff coming soon may help motivate the changes.

LuccaHellriegel commented 3 years ago

Hey @hungriesthippo,

I just had a call with @aidam38 and we agreed that I would do a minimally-intrusive refactoring/preparing for the script to be able to be unit-tested so we can make future changes with more confidence. A more functional style is a huge part of that and I plan to extract some pure functions as a first step (+ add npm).

https://github.com/LuccaHellriegel/roamsr/tree/refactoring-for-unit-tests

Maybe we can integrate your ideas after I have put some unit-tests into place so we can be sure that it generally still works? Happy to chat about possible refactorings.