# Code coverage report for lib/Actions.js

## Statements: 96.55% (56 / 58) Branches: 76.92% (20 / 26) Functions: 100% (15 / 15) Lines: 100% (51 / 51) Ignored: none

All files » lib/ » Actions.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148

    1

    1

    39

    1

    1
    1
    39

    39

    39
    39
    69
    69

    39

    1

    5
    5
    17
    17

    44
    44
    130

    69
    69
    69

    69
    28
    24

    28

    28
    7
    7

    21

    28

    69

    69

    69

    21
    20
    19

    1
    1

    21

    7
    6

    1
    1

    1

    1

    1

    1
    28

     "use strict";

    var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

    var _prototypeProperties = function (child, staticProps, instanceProps) { Iif (staticProps) Object.defineProperties(child, staticProps); Eif (instanceProps) Object.defineProperties(child.prototype, instanceProps); };

    var _classCallCheck = function (instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };

    /**
     * Actions
     *
     * Instances of the Actions class represent a set of actions. (In Flux parlance,
     * these might be more accurately denoted as Action Creators, while Action
     * refers to the payload sent to the dispatcher, but this is... confusing. We
     * will use Action to mean the function you call to trigger a dispatch.)
     *
     * Create actions by extending from the base Actions class and adding methods.
     * All methods on the prototype (except the constructor) will be
     * converted into actions. The return value of an action is used as the body
     * of the payload sent to the dispatcher.
     */

    var uniqueId = _interopRequire(require("uniqueid"));

    var Actions = (function () {
      function Actions() {
        _classCallCheck(this, Actions);

        this._baseId = uniqueId();

        var methodNames = this._getActionMethodNames();
        for (var i = 0; i < methodNames.length; i++) {
          var methodName = methodNames[i];
          this._wrapAction(methodName);
        }

        this.getConstants = this.getActionIds;
      }

      _prototypeProperties(Actions, null, {
        getActionIds: {
          value: function getActionIds() {
            var _this = this;
            return this._getActionMethodNames().reduce(function (result, actionName) {
              result[actionName] = _this[actionName]._id;
              return result;
            }, {});
          },
          writable: true,
          configurable: true
        },
        _getActionMethodNames: {
          value: function _getActionMethodNames(instance) {
            var _this = this;
            return Object.getOwnPropertyNames(this.constructor.prototype).filter(function (name) {
              return name !== "constructor" && typeof _this[name] === "function";
            });
          },
          writable: true,
          configurable: true
        },
        _wrapAction: {
          value: function _wrapAction(methodName) {
            var _this = this;
            var originalMethod = this[methodName];
            var actionId = this._createActionId(methodName);

            var action = function () {
              for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
                args[_key] = arguments[_key];
              }

              var body = originalMethod.apply(_this, args);

              if (isPromise(body)) {
                var promise = body;
                _this._dispatchAsync(actionId, promise, args, methodName);
              } else {
                _this._dispatch(actionId, body, args, methodName);
              }

              // Return original method's return value to caller
              return body;
            };

            action._id = actionId;

            this[methodName] = action;
          },
          writable: true,
          configurable: true
        },
        _createActionId: {

          /**
           * Create unique string constant for an action method, using
           * @param {string} methodName - Name of the action method
           */
          value: function _createActionId(methodName) {
            return "" + this._baseId + "-" + methodName;
          },
          writable: true,
          configurable: true
        },
        _dispatch: {
          value: function _dispatch(actionId, body, args, methodName) {
            if (typeof this.dispatch === "function") {
              if (typeof body !== "undefined") {
                this.dispatch(actionId, body, args);
              }
            } else {
              Eif (process.env.NODE_ENV !== "production") {
                console.warn("You've attempted to perform the action " + ("" + this.constructor.name + "#" + methodName + ", but it hasn't been added ") + "to a Flux instance.");
              }
            }

            return body;
          },
          writable: true,
          configurable: true
        },
        _dispatchAsync: {
          value: function _dispatchAsync(actionId, promise, args, methodName) {
            if (typeof this.dispatchAsync === "function") {
              return this.dispatchAsync(actionId, promise, args);
            } else {
              Eif (process.env.NODE_ENV !== "production") {
                console.warn("You've attempted to perform the asynchronous action " + ("" + this.constructor.name + "#" + methodName + ", but it hasn't been added ") + "to a Flux instance.");
              }

              return promise;
            }
          },
          writable: true,
          configurable: true
        }
      });

      return Actions;
    })();

    module.exports = Actions;

    function isPromise(value) {
      return value && typeof value.then === "function";
    }
    //# sourceMappingURL=data:application/json;base64,