Dojo/aspect Source Code Analysis

Source: Internet
Author: User

The Dojo/aspect module is an implementation of AOP in the Dojo framework. For a detailed explanation of AOP please read the other information separately, here is a brief review of the basic concepts of AOP:

    1. Facets (Aspect): In fact, the realization of common functions. such as log plane, permission plane, transaction plane, and so on.
    2. Notification (Advice): is the specific implementation of facets. The target method is the reference point, depending on where it is placed, it can be divided into pre-notification (before), post-notification (after) and surround notification (Around).
    3. Connection point (Joinpoint): The place where the program is able to insert slices during operation.
    4. Target object: The object that is about to cut into the plane, that is, the object that is being notified. All of these objects are left with clean core business logic code, and all common functionality code waits for an AOP container to cut in.
    5. Proxy object: An object that is dynamically created after the notification is applied to the target object. It can be simply understood that the function of the proxy object equals the core business logic functionality of the target object plus the shared functionality. The proxy object is transparent to the user, and is the product of the process of running the program.
    6. Weaving (Weaving): The process of applying facets to a target object to create a new proxy object. This process can occur in the compilation period, the class loading period and the operating period, of course, different points of occurrence have different preconditions. For example, in the compilation period, a special compiler is required to support this AOP implementation, and in the class loading period, a special class loader supporting AOP implementation is required, which can be implemented dynamically by the reflection mechanism of Java language and dynamic agent mechanism.

The process of building a proxy object can be understood as follows:

  

  

The Dojo/aspect module code is mainly divided into two parts:

  • The advise method constructs a "notification" chain by using a closure-and-chain model.
    "Use Strict"; varUndefined, NextID = 0; functionadvise (dispatcher, type, advice, receivearguments) {varPrevious =Dispatcher[type]; vararound = Type = = "Around"; varsignal; if(around) {varadvised = Advice (function(){                returnPrevious.advice ( This, arguments);            }); Signal={remove:function(){                    if(advised) {advised= Dispatcher = Advice =NULL; }}, advice:function(target, args) {returnAdvised?advised.apply (target, args)://called the advised functionPrevious.advice (target, args);//cancelled, skip to next one                }            }; }Else{            //Create the Remove handlerSignal ={remove:function(){                    if(signal.advice) {varPrevious =signal.previous; varNext =Signal.next; if(!next &&!)Previous) {                            DeleteDispatcher[type]; }Else{                            if(previous) {Previous.next=Next; }Else{Dispatcher[type]=Next; }                            if(next) {next.previous=previous; }                        }                        //remove the advice to signal it signal has been removedDispatcher = Advice = Signal.advice =NULL; }}, Id:nextid++, Advice:advice, receivearguments:receivearguments}; }        if(Previous &&!)around) {            if(type = = "after"){                //Add the Listener to the end of the list                //Note that we had to the change of this loop a little bit to workaround a bizarre IE10 JIT bug                 while(Previous.next && (previous =Previous.next)) {} Previous.next=signal; Signal.previous=previous; }Else if(Type = = "Before"){                //Add to beginningDispatcher[type] =signal; Signal.next=previous; Previous.previous=signal; }        }Else{            //around or first one just replacesDispatcher[type] =signal; }        returnsignal; }
    View Code
  • Aspect method, this function returns a closure. The function of closure is to weave the "notification" method into the target function, the runtime of Java in the way of reflection to weave, and JS in the dynamic change the target function to achieve the weaving process, then call this method can make the tangent function and business logic simultaneously.
    functionaspect (type) {return function(target, methodName, advice, receivearguments) {varexisting =Target[methodname], dispatcher; if(!existing | | Existing.target! =target) {                //no dispatcher in placeTarget[methodname] = Dispatcher =function(){                    varExecutionid =NextID; //before advice                    varargs =arguments; varBefore =Dispatcher.before;  while(before) {args= Before.advice.apply ( This, args) | |args; Before=Before.next; }                    //Around Advice                    if(dispatcher.around) {varResults = Dispatcher.around.advice ( This, args); }                    //After advice                    varafter =Dispatcher.after;  while(After && After.id <Executionid) {                        if(after.receivearguments) {varNewresults = After.advice.apply ( This, args); //Change the return value is only if a new value is returnedResults = Newresults = = = undefined?Results:newresults; }Else{Results= After.advice.call ( This, results, args); } after=After.next; }                    returnresults;                }; if(existing) {Dispatcher.around= {advice:function(target, args) {returnexisting.apply (target, args);                }}; } dispatcher.target=Target; }            varResults = Advise ((Dispatcher | |existing), type, advice, receivearguments); Advice=NULL; returnresults;    }; }
    View Code

  Note: The process of dojo does not generate proxy objects, but instead changes the methods of the original objects directly.

See This article for an explanation of the Aspect.after method (the Before method is similar):   JavaScript event mechanism compatibility solution; The origin of aspect.around in this article JavaScript AOP (aspect-oriented programming) of the Around (surround) has its evolutionary process of step-by-step.

This article gives the following aspect module calls:

Before and after functions:

  

Around function:

  

varadvised = Advice (function(){                returnPrevious.advice ( This, arguments);            }); Signal={remove:function(){                    if(advised) {advised= Dispatcher = Advice =NULL; }}, advice:function(target, args) {returnAdvised?//once the call remove,adviced becomes empty, it skips this surround notification and goes to the previous layer's advice method. Advised.apply (target, args)://called the advised functionPrevious.advice (target, args);//cancelled, skip to next one                }            };

You can see that the around function borrows the closure to form the wrapping function chain. After calling the Remove method, there is no complete removal of the notification method like before and after, and the registered wrapping method is still in memory, so this method cannot remove the surround notification, only avoids executing it in the function chain. Memory cannot be freed, too much is not recommended.

Dojo/aspect Source Code Analysis

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.