proof of concept for a module-scoped navigator instance #57
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.
In useEpubNavigator the EpubNavigator object is stored in local scope which means that each component using the hook would see a new instance of this object and could not 'share navigation' with one Navigator:
const nav = useRef<EpubNavigator | null>(null);
There are many ways to potentially lift the scope of this value so that every component could use the same navigator (including using redux, etc.) but it seems like simply lifting the navigator object from local to module scope is exactly what we need. This is done by declaring the var for the EpubNavigator object outside of the hook definition. When a hook is imported into a file, its top-level variables are shared across all files importing the module and JavaScript ensures that the module is loaded only once, thereby creating a singleton pattern for the navigator object. EpubNavigatorLoad is still allowed to create new instances of the object which would not cause a component re-render.
This pull request shows an example of this idea, using the existing TOC Listbox to show a shared Navigator by importing goLink from EpubNavigator to jump to toc locations, to spark discussions.