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

Add ability to invalidate cache of require() #198

Open
AlexTrotsenko opened this issue Jun 15, 2015 · 3 comments
Open

Add ability to invalidate cache of require() #198

AlexTrotsenko opened this issue Jun 15, 2015 · 3 comments
Labels
CommonJS Issues related to the CommonJS implementation in Rhino docs Issues containing stuff that ought to be documented

Comments

@AlexTrotsenko
Copy link

Preconditions:

  1. ModuleSourceProvider has ability to return NOT_MODIFIED if source was not modified to prevent compiling code 2nd time.
  2. CachingModuleScriptProviderBase has ability to obtain cached ModuleScript if NOT_MODIFIED returned by ModuleSourceProvider. In such case the returned ModuleScript was already compiled previously.

The problem, that Require do not take into account NOT_MODIFIED, that is returned by ModuleScriptProvider when same JS code is run 2nd time:

  1. if for the 2nd run of JS the same ModuleScriptProvider is used with same Require: moduleId was already loaded and exports where obtained previously - then it never checks if module was changed. It never do call to ModuleSourceProvider to check for NOT_MODIFIED.
  2. If the same ModuleScriptProvider is used with another Require then executeModuleScript(...) is always called and NOT_MODIFIED is not taken into the account.

The concrete problem, that I have for now is following:

  • I run same JS code in Rhino in Android several times. It contains several require() and source code of these modules could be changed.
  • When I execute JS code 1st time then result of require() become cached in Require object in exportedModuleInterfaces field.
  • When I execute the same code 2nd time I want cached exports will be used if NOT_MODIFIED is returned by ModuleSourceProvider.

But if I use same Require object - it does not check for NOT_MODIFIED at all.
If I use another Require object then exports calculated for nothing : NOT_MODIFIED was returned by ModuleSourceProvider before.

Because of this either executing of exports of moment-timezone took 10 seconds every time I run JS code or changes to source of my modules are not visible in JS runtime.

@p-bakker p-bakker added the CommonJS Issues related to the CommonJS implementation in Rhino label Jun 29, 2021
@szegedi
Copy link
Contributor

szegedi commented Feb 8, 2023

A single Require object will not reload modules, that is by design. Once a module is loaded in a single require instance, it's loaded, period. There's no way to reexecute it, and it would in fact not be correct to do so and come with a bunch of stability issues. (Imagine other modules depended on it, would they need to be reexecuted as well? In what order? If some code managed to hold on to old exports you'd have both old and new exports floating around, it'd be a nightmare to debug. There'd be a lot of problems with that approach.)

Using a single ModuleSourceProvider with multiple Require instances (one per global) is the right thing to do. Yes, each require will execute the script to create its exports object that belongs to that one Require (again, this is by design), but the scripts won't be recompiled each time. As long as the script isn't changed, the same compiled script will be re-executed by each Require. If executing a module takes 10 seconds, then you have one very expensive to initialize module.

@p-bakker p-bakker added the docs Issues containing stuff that ought to be documented label Feb 9, 2023
@p-bakker
Copy link
Collaborator

p-bakker commented Feb 9, 2023

Sounds like everything is working as it should, leaving this open tagged with the docs label, so the documentation can be updted with some info about this before closing this issue

@AlexTrotsenko
Copy link
Author

Thanks for providing the information.
This issue can be closed for sure given the time, which was passed from the time when the issue was created :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CommonJS Issues related to the CommonJS implementation in Rhino docs Issues containing stuff that ought to be documented
Projects
None yet
Development

No branches or pull requests

3 participants