Determine whether there is a circular dependency when a module is loaded

Source: Internet
Author: User

Loop dependency is a big problem for all language loaders. Unless your modules use IOC for control inversion like spring, loop dependency will end up. The only thing we can do is to notify users immediately to view their dependency list when the first circular dependency occurs.

After amd popularized the benefits of dynamic loading, we should hide two ways to do this. One defines the define method of the module, and the other is the require method of loading the module.

The parameter of the define method is define (name ?, Deps? In other words, neither the module name nor the dependency list is required. The require parameter is require (names, callback ). When we use a group of require modules to pass their return values to the callback, it actually does a lot of things. To implement cross-origin loading scripts, we use the script tag for loading. But this is not important. The script loaded from the script tag has a strict format, which is a define method. This is very similar to jsonp. In order to implement anonymous modules, we usually do not use 1st parameters, but 2nd parameters cannot be omitted sometimes. But there is no problem. We can restore the module name from the request URL, so we finally get three parameters. If the second parameter dependency list does not exist, we will set it to an empty array. When a module is loaded back and its dependency list is empty, we can say it is available. If a module has a dependency list, it is available only when all its modules are available. When there is a circular dependency between two modules, it will become you and me, and I will wait for your status. Therefore, every time a module is loaded back, it will detect its dependency list, trace back its parent dependency to the parent dependency, but whether the current Module name exists in these links. If a circular dependency exists, an error is thrown immediately. We can also optimize this check. Because this is a graph, the path will be very complex. We can skip all modules that have sent requests but not loaded back or all available modules.

// These are defined in the seed module (Core Module) var loadings = []; var modules ={}; function define (name, deps, factory) {var El = modules [name] = {deps: deps, name: name, factory: factory, status :! Deps. Length // available} If (! El. status) {// enter the queue to detect loadings. push (EL) ;}_ checkdeps (); If (_ checkcircle (deps, name )) {Throw new error (name + "circular dependency between the module and some previous modules")} function _ checkcircle (list, Nick) {// check whether there is a circular dependency for (VAR I = 0, name; name = list [I ++];) {var El = modules [name]; If (EL) {If (El. name = Nick | el. deps. length & _ checkcircle (El. deps, Nick) {return true ;}}} function _ checkdeps () {// check whether the module has been loaded successfully. Loop: For (VAR I = Loadings. length, El; El = loadings [-- I];) {var deps = el. deps; var OK = true; For (VAR J = 0, name; name = deps [J ++];) {If (! Modules [name] |! Modules [name]. status) {OK = false; Continue loop} If (OK) {el. status = true; loadings. splice (I, 1); _ checkdeps () }}// this is. JS content define ("A", ["B"]) // This is B. JS content define ("B", ["C"]) // This is C. JS content define ("C", ["A"]) // easily confirm the console with the naked eye of firebug. log (modules) console. log (loadings)

the above is only the Code used to determine the dependency. In essence, the module loading system is very complex, which can be divided into the shard Module name and alias ing, convert to URL, create the loaded IFRAME and script (IFRAME is used to better determine the dead chain in opera and old IE), and process the last parameter factory of the define method loaded back, restore Its Module name, determine whether it is available, whether it is a circular dependency, and load its dependency if there is a dependency, search for other articles related to module loading in my blog.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.