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

Load files in module only once #59

Closed
rnavarette opened this issue Aug 27, 2014 · 6 comments
Closed

Load files in module only once #59

rnavarette opened this issue Aug 27, 2014 · 6 comments
Labels

Comments

@rnavarette
Copy link

Is there any built in way to restrict a module to loading only once? Not just not reconfiguring, but knowing that a module has already been loaded and not requesting it again? We currently work around this by maintaining a list of modules we've loaded and simply don't request if we've previously loaded.

I understand that this doesn't fit every use case (like when a module is loaded piece-by-piece) but it seems to be pretty handy functionality, especially when you have dependent modules.

@rnavarette
Copy link
Author

Sorry, this may end up being a duplicate of #58, I didn't check for new issues before posting since last time i searched on this.

@ocombe
Copy link
Owner

ocombe commented Aug 27, 2014

Not really a duplicate (#58 seems to be a bug).
The native loaders included in the lib use cache and should not download files with the same path more than one (unless you use cache: false).

The specific part in the code is:

angular.forEach(params.files, function(path) {
  if(angular.isUndefined(filesCache.get(path)) || params.cache === false) {
    if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
      cssFiles.push(path);
    } else if(/\.(htm|html)[^\.]*$/.test(path) && templatesFiles.indexOf(path) === -1) {
      templatesFiles.push(path);
    } else if (jsFiles.indexOf(path) === -1) {
      jsFiles.push(path);
    }
  }
});

Are the paths similar or different in your case ?

And as for modules, they shouldn't be reloaded, the specific code is:

angular.forEach(module.requires, function(requireModule) {
  if(regModules.indexOf(requireModule) === -1) {
    requires.push(requireModule);
  }
});

and also:

// If this module has been loaded before, re-use it.
if(angular.isDefined(moduleName) && moduleExists(moduleName) && regModules.indexOf(moduleName) !== -1) {
  moduleCache.push(moduleName);

[...]

There are more parts of the code to check for this, but I didn't list them all.

Do you have a more detailed example about this problem ?

@chrisknu
Copy link

I have a use case akin to the above request where it would be helpful to expose publicly the moduleExists method already in the code.

The code below is firing just fine on-authentication, however on-unauth, then re-auth (use case -> different user), the 'load' either dies or skips silently. Running a check to see if the modules exist in a different (auth) promise would enable the behaviour I think is requested above.

$ocLazyLoad.load([{
               module1
            }, {
                module2
            }, {
                module3
            }, {
             module4
            }], {
                cache: false,
                reconfig: true
            }).then(function() {
                $rootScope.$broadcast('goToAuthDashboard');
            });

Great tool btw. If you've got a beerware bucket somewhere, let me know

@ocombe
Copy link
Owner

ocombe commented Sep 12, 2014

Thanks, I'll make one :D

The load function should not die silently, it probably rejects the promise, you need to add a second function in the "then" to get the rejects (or you can also use debug: true to see the errors in the console).

.then(function success() {
  $rootScope.$broadcast('goToAuthDashboard');
}, function error(err) {
  console.log(err);
});

@chrisknu
Copy link

Actually, I repurposed what you already had and it seems to work well enough for my purposes:

function moduleExists(moduleName) {
                try {
                    if (!!angular.module(moduleName)) {
                        return true;
                    }
                } catch (e) {
                    if (/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
                        return false;
                    }
                }
            }

@ocombe
Copy link
Owner

ocombe commented Sep 12, 2014

Ok, I could make this function public, it's not like it would pose any kind of problem.

About the donation, you can use Flattr Flattr this or paypal (olivier.combe@gmail.com), but don't feel obligated, I share my work because it helps people and a thank you is already a gift in itself !

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

No branches or pull requests

3 participants