JS-Aspect Demo 4

Using Aspect with a ES2015 class, With mod in js-Aspect Lib code to work with ES2015 Classes

by Ben Clayton

JavaScript

! function(host) {
  function joinPointName(joinPointId) {
    return joinPointId.toLowerCase().replace(/_[a-z]/, function(match) {
      return match[1].toUpperCase()
    })
  }

  function injectAdvice(target, methodName, advice, joinPoint) {
    isFunction(target[methodName]) && (jsAspect.JOIN_POINT.AROUND === joinPoint && (advice = wrapAroundAdvice(advice)), target[methodName][adviceEnhancedFlagName] || (enhanceWithAdvices(target, methodName), target[methodName][adviceEnhancedFlagName] = !0), target[methodName][joinPoint].unshift(advice))
  }

  function wrapAroundAdvice(advice) {
    var wrappedAdvice = function(executionContext, leftAroundAdvices) {
      var oThis = this,
        nextWrappedAdvice = leftAroundAdvices.shift(),
        args = toArray(arguments).slice(2);
      if (!executionContext.isStopped) {
        if (nextWrappedAdvice) {
          var nextUnwrappedAdvice = function() {
            var argsForWrapped = toArray(arguments);
            return argsForWrapped.unshift(leftAroundAdvices), argsForWrapped.unshift(executionContext), nextWrappedAdvice.apply(oThis, argsForWrapped)
          };
          args.unshift(nextUnwrappedAdvice)
        }
        return advice.originalMethodFlagName || args.unshift(executionContext), advice.apply(this, args)
      }
    };
    return wrappedAdvice.__originalAdvice = advice, wrappedAdvice
  }

  function enhanceWithAdvices(target, methodName) {
    var originalMethod = target[methodName];
    originalMethod.originalMethodFlagName = !0, target[methodName] = function() {
      var returnValue, self = this,
        method = target[methodName],
        args = toArray(arguments),
        executionContext = new ExecutionContext(target, methodName, args);
      applyBeforeAdvices(self, method, args, executionContext);
      try {
        returnValue = applyAroundAdvices(self, method, args, executionContext)
      } catch (exception) {
        throw applyAfterThrowingAdvices(self, method, exception,...