Skip to content
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

proof of concept for a module-scoped navigator instance #57

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

bluefirepatrick
Copy link
Member

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.

@JayPanoz
Copy link
Contributor

As discussed during yesterday’s call, this sounds good to anyone involved in the project.

I think I will merge this immediately following the merge of the Sheets branch (#51) + the toc tree Component (#37) so that the ToC can actually be usable as well., and it’s no concern for the Navigator’s custom hook that is bound to evolve dramatically with the implementation of the Preferences API in the ts-toolkit.

Thanks a lot for taking the time to open this PR. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Draft
Development

Successfully merging this pull request may close these issues.

2 participants