All files / xstate/es State.js

0% Statements 0/79
0% Branches 0/56
0% Functions 0/14
0% Lines 0/74

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 120 121 122 123 124 125 126 127 128 129                                                                                                                                                                                                                                                                 
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;
};
import { EMPTY_ACTIVITY_MAP } from './constants';
import { matchesState, keys } from './utils';
export function stateValuesEqual(a, b) {
    if (a === b) {
        return true;
    }
    var aKeys = keys(a);
    var bKeys = keys(b);
    return aKeys.length === bKeys.length && aKeys.every(function (key) {
        return stateValuesEqual(a[key], b[key]);
    });
}
var State = /** @class */ /*#__PURE__*/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 = 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 = 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 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;
}();
export { State };