# Code coverage report for lib/Store.js

## Statements: 94.95% (94 / 99) Branches: 76.79% (43 / 56) Functions: 100% (16 / 16) Lines: 97.67% (84 / 86) Ignored: none

All files » lib/ » Store.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
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219

    2

    1

    1

    87

    1

    1

    1

    1
    87

    87

    87
    87

    1

    1

    90

    40
    3

    3

    40
    30
    30

    10
    10

    10
    10

    15
    6
    6

    9
    9

    6
    4

    2

    53

    53
    1
    52

    3

    3

    3
    9

    9

    9
    5

    4

    3

    1

    42
    42
    42
    42
    42

    42
    42

    42
    10

    5
    2

    5

    2
    2

    1

    3
    2

    3

    35

    35

    39
    41

    39
    39
    39

    39
    39

    39
    29
    29

    39
    39
    39

    1

    1

    1
    56

     "use strict";

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

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

    var _inherits = function (subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) subClass.__proto__ = superClass; };

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

    /**
     * Store
     *
     * Stores hold application state. They respond to actions sent by the dispatcher
     * and broadcast change events to listeners, so they can grab the latest data.
     * The key thing to remember is that the only way stores receive information
     * from the outside world is via the dispatcher.
     */

    var EventEmitter = _interopRequire(require("eventemitter3"));

    var assign = _interopRequire(require("object-assign"));

    var Store = (function (EventEmitter) {
      /**
       * Stores are initialized with a reference
       * @type {Object}
       */
      function Store() {
        _classCallCheck(this, Store);

        this.state = null;

        this._handlers = {};
        this._asyncHandlers = {};
      }

      _inherits(Store, EventEmitter);

      _prototypeProperties(Store, {
        assignState: {
          value: function assignState(oldState, newState) {
            return assign({}, oldState, newState);
          },
          writable: true,
          configurable: true
        }
      }, {
        setState: {
          value: function setState(newState) {
            // Do a transactional state update if a function is passed
            if (typeof newState === "function") {
              var prevState = this._isHandlingDispatch ? this._pendingState : this.state;

              newState = newState(prevState);
            }

            if (this._isHandlingDispatch) {
              this._pendingState = this.constructor.assignState(this._pendingState, newState);
              this._emitChangeAfterHandlingDispatch = true;
            } else {
              Eif (process.env.NODE_ENV !== "production") {
                console.warn("Store#setState() called from outside an action handler. This is likely " + "a mistake. Flux stores should manage their own state.");
              }
              this.state = this.constructor.assignState(this.state, newState);
              this.emit("change");
            }
          },
          writable: true,
          configurable: true
        },
        replaceState: {
          value: function replaceState(newState) {
            if (this._isHandlingDispatch) {
              this._pendingState = this.constructor.assignState(undefined, newState);
              this._emitChangeAfterHandlingDispatch = true;
            } else {
              this.state = this.constructor.assignState(undefined, newState);
              this.emit("change");
            }
          },
          writable: true,
          configurable: true
        },
        forceUpdate: {
          value: function forceUpdate() {
            if (this._isHandlingDispatch) {
              this._emitChangeAfterHandlingDispatch = true;
            } else {
              this.emit("change");
            }
          },
          writable: true,
          configurable: true
        },
        register: {
          value: function register(actionId, handler) {
            actionId = ensureActionId(actionId);

            if (typeof handler !== "function") {
              return;
            }this._handlers[actionId] = handler.bind(this);
          },
          writable: true,
          configurable: true
        },
        registerAsync: {
          value: function registerAsync(actionId, beginHandler, successHandler, failureHandler) {
            actionId = ensureActionId(actionId);

            var asyncHandlers = {
              begin: beginHandler,
              success: successHandler,
              failure: failureHandler };

            for (var key in asyncHandlers) {
              Iif (!asyncHandlers.hasOwnProperty(key)) continue;

              var handler = asyncHandlers[key];

              if (typeof handler === "function") {
                asyncHandlers[key] = handler.bind(this);
              } else {
                delete asyncHandlers[key];
              }
            }

            this._asyncHandlers[actionId] = asyncHandlers;
          },
          writable: true,
          configurable: true
        },
        waitFor: {
          value: function waitFor(tokensOrStores) {
            this._waitFor(tokensOrStores);
          },
          writable: true,
          configurable: true
        },
        handler: {
          value: function handler(payload) {
            var body = payload.body;
            var actionId = payload.actionId;
            var _async = payload.async;
            var actionArgs = payload.actionArgs;
            var error = payload.error;

            var _handler = this._handlers[actionId];
            var _asyncHandler = this._asyncHandlers[actionId] && this._asyncHandlers[actionId][_async];

            if (_async) {
              switch (_async) {
                case "begin":
                  if (typeof _asyncHandler === "function") {
                    this._performHandler.apply(this, [_asyncHandler].concat(actionArgs));
                  }
                  return;
                case "failure":
                  Eif (typeof _asyncHandler === "function") {
                    this._performHandler(_asyncHandler, error);
                  }
                  return;
                case "success":
                  if (typeof _asyncHandler === "function") {
                    _handler = _asyncHandler;
                  }
                  break;
                default:
                  return;
              }
            }

            Iif (typeof _handler !== "function") {
              return;
            }this._performHandler(_handler, body);
          },
          writable: true,
          configurable: true
        },
        _performHandler: {
          value: function _performHandler(_handler) {
            for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
              args[_key - 1] = arguments[_key];
            }

            this._isHandlingDispatch = true;
            this._pendingState = this.constructor.assignState(undefined, this.state);
            this._emitChangeAfterHandlingDispatch = false;

            try {
              _handler.apply(this, args);
            } finally {
              if (this._emitChangeAfterHandlingDispatch) {
                this.state = this._pendingState;
                this.emit("change");
              }

              this._isHandlingDispatch = false;
              this._pendingState = undefined;
              this._emitChangeAfterHandlingDispatch = false;
            }
          },
          writable: true,
          configurable: true
        }
      });

      return Store;
    })(EventEmitter);

    module.exports = Store;

    function ensureActionId(actionOrActionId) {
      return typeof actionOrActionId === "function" ? actionOrActionId._id : actionOrActionId;
    }

    //# sourceMappingURL=data:application/json;base64,