All files / xstate/lib State.js

17.86% Statements 15/84
10.71% Branches 6/56
7.14% Functions 1/14
21.13% Lines 15/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  1x                               1x       1x 1x 1x                   1x 1x                                             1x                 1x                 1x                   1x                       1x     1x                             1x   1x  
"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spread = (this && this.__spread) || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var constants_1 = require("./constants");
var utils_1 = require("./utils");
function stateValuesEqual(a, b) {
    if (a === b) {
        return true;
    }
    var aKeys = utils_1.keys(a);
    var bKeys = utils_1.keys(b);
    return (aKeys.length === bKeys.length &&
        aKeys.every(function (key) { return stateValuesEqual(a[key], b[key]); }));
}
exports.stateValuesEqual = stateValuesEqual;
var State = /** @class */ (function () {
    function State(value, context, historyValue, history, actions, activities, data, 
    /**
     * Internal event queue
     */
    events, tree) {
        if (actions === void 0) { actions = []; }
        if (activities === void 0) { activities = constants_1.EMPTY_ACTIVITY_MAP; }
        if (data === void 0) { data = {}; }
        if (events === void 0) { events = []; }
        this.value = value;
        this.context = context;
        this.historyValue = historyValue;
        this.history = history;
        this.actions = actions;
        this.activities = activities;
        this.data = data;
        this.events = events;
        Object.defineProperty(this, 'tree', {
            value: tree,
            enumerable: false
        });
    }
    State.from = function (stateValue, context) {
        if (stateValue instanceof State) {
            if (stateValue.context !== context) {
                return new State(stateValue.value, context, stateValue.historyValue, stateValue.history, [], stateValue.activities, {}, [], stateValue.tree);
            }
            return stateValue;
        }
        return new State(stateValue, context, undefined, undefined, [], undefined, undefined, []);
    };
    State.inert = function (stateValue, context) {
        if (stateValue instanceof State) {
            if (!stateValue.actions.length) {
                return stateValue;
            }
            return new State(stateValue.value, context, stateValue.historyValue, stateValue.history, undefined, stateValue.activities, undefined, undefined, stateValue.tree);
        }
        return State.from(stateValue, context);
    };
    Object.defineProperty(State.prototype, "nextEvents", {
        get: function () {
            if (!this.tree) {
                return [];
            }
            return this.tree.nextEvents;
        },
        enumerable: true,
        configurable: true
    });
    State.prototype.toStrings = function (value, delimiter) {
        var _this = this;
        if (value === void 0) { value = this.value; }
        if (delimiter === void 0) { delimiter = '.'; }
        if (typeof value === 'string') {
            return [value];
        }
        var valueKeys = utils_1.keys(value);
        return valueKeys.concat.apply(valueKeys, __spread(valueKeys.map(function (key) {
            return _this.toStrings(value[key]).map(function (s) { return key + delimiter + s; });
        })));
    };
    State.prototype.matches = function (parentStateValue) {
        return utils_1.matchesState(parentStateValue, this.value);
    };
    Object.defineProperty(State.prototype, "changed", {
        get: function () {
            if (!this.history) {
                return undefined;
            }
            return (!!this.actions.length ||
                (typeof this.history.value !== typeof this.value
                    ? true
                    : typeof this.value === 'string'
                        ? this.value !== this.history.value
                        : stateValuesEqual(this.value, this.history.value)));
        },
        enumerable: true,
        configurable: true
    });
    return State;
}());
exports.State = State;