Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

16 Commits

Repository files navigation

Dcor

JavaScript function decoration based on Aspect-oriented programming

A simple JavaScript library for decorating functions with common and useful cross-cutting concerns like logging, authorization, caching, etc. It is heavily based on closures and functional aspects of JavaScript.

Note: While the library has been tested in browser, it's not tested on Node.js yet.

Dcor predefined aspects:

  • logged: Allows logging function calls along with the passed arguments and the return values
  • secured: Secures a function by ignoring (or breaking) unauthorized calls
  • cached: Stores the function return value to cache (memory) and use it for later calls
  • performanceLogged: Measures the runtime duration of the function
  • singleton: Makes the function singleton. All future instances will be the same.
  • safeConstructor: Protects constructor functions against NOT using new. Any call will be considered as an instantiation.
  • async: Makes synchronous functions asynchronous to prevent UI blocking

Examples:

  • Make a function loggable:
//define a functionfunctionfoo{}//decorate itDcor.logged("foo",function(fname,fargs,res){console.log("Log> function "+fname+" called with: ",fargs," and returned: ",res);});//run itfoo();//args and result value should be logged from now on 

Spy on all functions!

logged can be used to log all functions in the browser for any website. To do that:

  • Copy/paste contents of Dcor.js in the browser console
  • Decorate all functions like this:
Dcor.logged(".",function(fname,fargs,res){console.log("Dcor Log> "+fname+" called with: ",fargs," and returned: ",res);});

Other Examples:

  • Make a synchronous funtion asynchronous:
//define a functionfunctionlengthyOperation(){//a long running synchrounous task...};//decorate itDcor.decorate("lengthyOperation",[{//or simply user Dcor.asynctype: "async",callback: function(fname,fargs,res,f,ns){console.log("Async> function "+fname+" called with: ",fargs," and returned: ",res);}}]);//run it lengthyOperation();//it now runs asynchronously and will not block the UI

See more examples

About

JavaScript function decoration based on AOP

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages