# Code coverage report for lib/Flux.js

## Statements: 95.04% (134 / 141) Branches: 73.97% (54 / 73) Functions: 100% (24 / 24) Lines: 99.16% (118 / 119) Ignored: none

All files » lib/ » Flux.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
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303

    3

    98

    1

    1

    56

    1

    1

    1
    1

    1
    1
    56

    56

    56
    56

    1

    1

    69
    41

    69
    1

    1

    68
    1

    67
    67

    67
    67

    67

    67

    38

    33

    33
    1

    1

    32
    1

    31
    31
    31

    31

    31

    62

    4

    4
    2
    2

    21

    12
    12

    12

    12

    12
    7

    6

    5

    4

    6

    6

    45
    43

    1

    1
    1

    1

    1

    1
    2

    2

    2

    4

    4
    8

    8

    8

    8

    7

    7
    1

    1
    1

    7

    7
    1

    1
    1

    4

    4

    4
    4

    1

    1
    1

    3
    7

    7

    7

    7

    6
    6

    6

    6
    1

    1
    1

    1

    1

    1
    1
    1

    1
    2

    1

    1
    1
    1
    1
    1

     "use strict";

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

    var _applyConstructor = function (Constructor, args) { var instance = Object.create(Constructor.prototype); var result = Constructor.apply(instance, args); return result != null && (typeof result == "object" || typeof result == "function") ? result : instance; };

    var _prototypeProperties = function (child, staticProps, instanceProps) { Iif (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"); } };

    /**
     * Flux
     *
     * The main Flux class.
     */

    var Store = _interopRequire(require("./Store"));

    var Actions = _interopRequire(require("./Actions"));

    var Dispatcher = require("flux").Dispatcher;
    var EventEmitter = _interopRequire(require("eventemitter3"));

    var Flux = (function (EventEmitter) {
      function Flux() {
        _classCallCheck(this, Flux);

        this.dispatcher = new Dispatcher();

        this._stores = {};
        this._actions = {};
      }

      _inherits(Flux, EventEmitter);

      _prototypeProperties(Flux, null, {
        createStore: {
          value: function createStore(key, _Store) {
            for (var _len = arguments.length, constructorArgs = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
              constructorArgs[_key - 2] = arguments[_key];
            }

            if (!(_Store.prototype instanceof Store)) {
              var className = getClassName(_Store);

              throw new Error("You've attempted to create a store from the class " + className + ", which " + "does not have the base Store class in its prototype chain. Make sure " + ("you're using the `extends` keyword: `class " + className + " extends ") + "Store { ... }`");
            }

            if (this._stores.hasOwnProperty(key) && this._stores[key]) {
              throw new Error("You've attempted to create multiple stores with key " + key + ". Keys must " + "be unique.");
            }

            var store = _applyConstructor(_Store, constructorArgs);
            var token = this.dispatcher.register(store.handler.bind(store));

            store._waitFor = this.waitFor.bind(this);
            store._token = token;

            this._stores[key] = store;

            return store;
          },
          writable: true,
          configurable: true
        },
        getStore: {
          value: function getStore(key) {
            return this._stores.hasOwnProperty(key) ? this._stores[key] : undefined;
          },
          writable: true,
          configurable: true
        },
        createActions: {
          value: function createActions(key, _Actions) {
            for (var _len = arguments.length, constructorArgs = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
              constructorArgs[_key - 2] = arguments[_key];
            }

            if (!(_Actions.prototype instanceof Actions) && _Actions !== Actions) {
              var className = getClassName(_Actions);

              throw new Error("You've attempted to create actions from the class " + className + ", which " + "does not have the base Actions class in its prototype chain. Make " + ("sure you're using the `extends` keyword: `class " + className + " ") + "extends Actions { ... }`");
            }

            if (this._actions.hasOwnProperty(key) && this._actions[key]) {
              throw new Error("You've attempted to create multiple actions with key " + key + ". Keys " + "must be unique.");
            }

            var actions = _applyConstructor(_Actions, constructorArgs);
            actions.dispatch = this.dispatch.bind(this);
            actions.dispatchAsync = this.dispatchAsync.bind(this);

            this._actions[key] = actions;

            return actions;
          },
          writable: true,
          configurable: true
        },
        getActions: {
          value: function getActions(key) {
            return this._actions.hasOwnProperty(key) ? this._actions[key] : undefined;
          },
          writable: true,
          configurable: true
        },
        getActionIds: {
          value: function getActionIds(key) {
            var actions = this.getActions(key);

            if (!actions) {
              return;
            }return actions.getConstants();
          },
          writable: true,
          configurable: true
        },
        dispatch: {
          value: function dispatch(actionId, body) {
            this._dispatch({ actionId: actionId, body: body });
          },
          writable: true,
          configurable: true
        },
        dispatchAsync: {
          value: function dispatchAsync(actionId, promise, actionArgs) {
            var _this = this;
            var payload = {
              actionId: actionId,
              async: "begin" };

            if (actionArgs) payload.actionArgs = actionArgs;

            this._dispatch(payload);

            return promise.then(function (body) {
              _this._dispatch({
                actionId: actionId,
                body: body,
                async: "success"
              });

              return body;
            }, function (error) {
              _this._dispatch({
                actionId: actionId,
                error: error,
                async: "failure" });

              return Promise.reject(error);
            })["catch"](function (error) {
              _this.emit("error", error);

              return Promise.reject(error);
            });
          },
          writable: true,
          configurable: true
        },
        _dispatch: {
          value: function _dispatch(payload) {
            this.dispatcher.dispatch(payload);
            this.emit("dispatch", payload);
          },
          writable: true,
          configurable: true
        },
        waitFor: {
          value: function waitFor(tokensOrStores) {
            Eif (!Array.isArray(tokensOrStores)) tokensOrStores = [tokensOrStores];

            var ensureIsToken = function (tokenOrStore) {
              return tokenOrStore instanceof Store ? tokenOrStore._token : tokenOrStore;
            };

            var tokens = tokensOrStores.map(ensureIsToken);

            this.dispatcher.waitFor(tokens);
          },
          writable: true,
          configurable: true
        },
        removeAllStoreListeners: {
          value: function removeAllStoreListeners(event) {
            for (var key in this._stores) {
              Iif (!this._stores.hasOwnProperty(key)) continue;

              var store = this._stores[key];

              store.removeAllListeners(event);
            }
          },
          writable: true,
          configurable: true
        },
        serialize: {
          value: function serialize() {
            var stateTree = {};

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

              var store = this._stores[key];

              var serialize = store.constructor.serialize;

              if (typeof serialize !== "function") continue;

              var serializedStoreState = serialize(store.state);

              if (typeof serializedStoreState !== "string") {
                var className = store.constructor.name;

                Eif (process.env.NODE_ENV !== "production") {
                  console.warn("The store with key '" + key + "' was not serialized because the static " + ("method `" + className + ".serialize()` returned a non-string with type ") + ("'" + typeof serializedStoreState + "'."));
                }
              }

              stateTree[key] = serializedStoreState;

              if (typeof store.constructor.deserialize !== "function") {
                var className = store.constructor.name;

                Eif (process.env.NODE_ENV !== "production") {
                  console.warn("The class `" + className + "` has a `serialize()` method, but no " + "corresponding `deserialize()` method.");
                }
              }
            }

            return JSON.stringify(stateTree);
          },
          writable: true,
          configurable: true
        },
        deserialize: {
          value: function deserialize(serializedState) {
            var stateMap = undefined;

            try {
              stateMap = JSON.parse(serializedState);
            } catch (error) {
              var className = this.constructor.name;

              Eif (process.env.NODE_ENV !== "production") {
                throw new Error("Invalid value passed to `" + className + "#deserialize()`: " + ("" + serializedState));
              }
            }

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

              var store = this._stores[key];

              var deserialize = store.constructor.deserialize;

              if (typeof deserialize !== "function") continue;

              var storeStateString = stateMap[key];
              var storeState = deserialize(storeStateString);

              store.replaceState(storeState);

              if (typeof store.constructor.serialize !== "function") {
                var className = store.constructor.name;

                Eif (process.env.NODE_ENV !== "production") {
                  console.warn("The class `" + className + "` has a `deserialize()` method, but no " + "corresponding `serialize()` method.");
                }
              }
            }
          },
          writable: true,
          configurable: true
        }
      });

      return Flux;
    })(EventEmitter);

    exports["default"] = Flux;

    // Aliases
    Flux.prototype.getConstants = Flux.prototype.getActionIds;
    Flux.prototype.dehydrate = Flux.prototype.serialize;
    Flux.prototype.hydrate = Flux.prototype.deserialize;

    function getClassName(Class) {
      return Class.prototype.constructor.name;
    }

    var Flummox = Flux;

    exports.Flux = Flux;
    exports.Flummox = Flummox;
    exports.Store = Store;
    exports.Actions = Actions;
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    //# sourceMappingURL=data:application/json;base64,