Serenity

by Administrator 15. September 2013 02:43

My new album is out on Jamendo. Check it out here if you like.

Tags: ,

Music | My Stuff

RequireJS Interception

by Administrator 5. September 2013 13:20

RequireJS is sort of the counterpart to dependency injection in .NET/Java. It allows to wireup javascript modules creating loosely coupled modules - really neat when your codebase starts to grow. Looking at dependency injection frameworks like Castle Windsor or Unity you often get interception support as this fits really nice into the dependency injection concept. Interception is a way to wrap the actual code of injected components into a custom code fragement to deal with cross cutting concerns. Good uses for interception are things like:

  • Logging
  • Exception handling
  • Caching
  • Tracing (e.g. Performance)
  • Authentication/authorization

RequireJS supports plugins that allow to customize the way modules are bound. We came up with the idea to implement a plugin that provides interception for RequireJS and here it is!

First of all to provide a plugin for RequireJS, you just define a module the way you would define any other module. The module has to at least implement the load method. The parameters of this method are:

  • The name of the module to load
  • The standard require function
  • A callback to be invoked with the loaded module.

Have a look at this one:

define(
  [],
  function () {
    "use strict";

    var module = {
      load: function (name, parentRequire, load) {
        parentRequire([name], function (module) {

          // Iterate over functions on the given module and wrap
          // them in a proxy function.
          _(module).functions().each(function (functionName) {
            var originalFunction = module[functionName];
            var operationId = _.uniqueId();
            var timeStampBefore = new Date().toLocaleTimeString();
            var timeStampAfter;

            module[functionName] = function () {
              //
              // Actions BEFORE executing the original function.
              //


              // Original method call.
              var result = originalFunction.apply(module, arguments);

              //
              // Actions AFTER executing the original function.
              //

              return result;
            };
          });

          // Notify require that the module has loaded.
          load(module);
        });
      }
    };

    return module;
  });

What't happening here is really simple, first we invoke to original function to get to the raw module. Then we iterate over the defined functions and wrap them in a new function that may execute code before/after the original function call.To use this plugin, you can use the <plugin>! prefix:


require (["intercept!myModuile"], function (myModule) {
});


That's all. Perhaps you'll find ways to do something similar in your projects. We use the interception mainly for tracing at this point.














Tags: ,

Javascript

About the author

for comments and suggestions contact:

 

Month List