-
Notifications
You must be signed in to change notification settings - Fork 242
Add event handler system #398
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
Conversation
schloerke
left a comment
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.
Caching within package is ok. We do this for global setup chunk and exercise chunks.
Let's add a "reset event handlers cache" when starting a learnr tutorial in tutorial().
Let's also reset the global setup chunk and exercise chunk cache when resetting the registered event handlers.
Co-authored-by: Carson Sievert <cpsievert1@gmail.com>
Co-authored-by: Carson Sievert <cpsievert1@gmail.com> Co-authored-by: Barret Schloerke <barret@rstudio.com>
* master: Add events to website (#423) Add a checker test with error as solution (#422) Fixes for learnr on external evaluator (#420) Add shinytest support (#407) Validate the ability to create an idb store (#417) Disable completion of R code inside quotes, closes #401 (#413) Bump version (#414) Save tutorial output to temp folder when tutorial folder does not have write permissions (#412) Throw an informative error if an exercise chunk is NULL (#411) Remove outdated exercise option Checking for exercise errors (#403) Add event handler system (#398) External evaluator fixes (#399)
This adds a general-purpose event handler system. Closes #357.
It adds:
event_register_handler()one_time()It also converts many of the existing event-like functions into to true event handlers.
It adds a new event,
"section_viewed", which is triggered every time a section becomes visible.The
event_register_handler()function can be used to execute code every time an event occurs. For example, this will print a message to the console every time any section becomes visible.The
one_time()function can be used to only execute code the first time an event happens AND some condition is satisfied. This can be used to execute code the only the first time a section is viewed:Usage notes
If a section doesn't have a name, then one will be auto-generated. In the example below, the first one will trigger a
section_viewedevent wheredata$sectionIdis"section-section"and the second will be"section-section-1".You can also add specific section IDs without having any text show up. This is better in practice, so that adding a section in the middle doesn't alter up later section IDs that you're looking for. In the example below, the first one will be
"section-first"and the second will be"section-second".Implementation notes
The events starts in tutorial-format.js. There are
shownandhiddenevents for the topics and sections. Unfortunately, they fire too often, and sometimes (for example) ashownevent occurs when a section isn't actually visible. The code in that file converts those trigger-happy events into state, in the form a Shiny input value. It setsinput$`tutorial-visible-sections`to a character vector that contains the IDs of the visible sections.On the R side, the states from
input$`tutorial-visible-sections`are converted back to events, using an observer which callsevent_trigger("section_viewed", ....).