All files / xstate/src State.ts

67.92% Statements 36/53
63.89% Branches 23/36
58.33% Functions 7/12
70% Lines 35/50

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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                    1x 1x     1x                           1x     1x       1372x 965x 4x                         961x     407x                     1x       498x 498x 474x   24x                                 1588x 1588x 1588x 1588x 3362x 3583x 3769x       3200x     1588x           1x 5x       5x     1x                               1x 3x     1x 7x 3x     4x                 1x  
import {
  StateValue,
  ActivityMap,
  EventObject,
  StateInterface,
  HistoryValue,
  ActionObject,
  EventType,
  StateValueMap
} from './types';
import { EMPTY_ACTIVITY_MAP } from './constants';
import { matchesState, keys } from './utils';
import { StateTree } from './StateTree';
 
export function stateValuesEqual(a: StateValue, b: StateValue): boolean {
  if (a === b) {
    return true;
  }
 
  const aKeys = keys(a as StateValueMap);
  const bKeys = keys(b as StateValueMap);
 
  return (
    aKeys.length === bKeys.length &&
    aKeys.every(key => stateValuesEqual(a[key], b[key]))
  );
}
 
export class State<TContext, TEvents extends EventObject = EventObject>
  implements StateInterface<TContext> {
  public tree?: StateTree;
  public static from<TC, TE extends EventObject = EventObject>(
    stateValue: State<TC, TE> | StateValue,
    context: TC
  ): State<TC, TE> {
    if (stateValue instanceof State) {
      if (stateValue.context !== context) {
        return new State<TC, TE>(
          stateValue.value,
          context,
          stateValue.historyValue,
          stateValue.history,
          [],
          stateValue.activities,
          {},
          [],
          stateValue.tree
        );
      }
 
      return stateValue;
    }
 
    return new State<TC, TE>(
      stateValue,
      context,
      undefined,
      undefined,
      [],
      undefined,
      undefined,
      []
    );
  }
  public static inert<TC, TE extends EventObject = EventObject>(
    stateValue: State<TC> | StateValue,
    context: TC
  ): State<TC, TE> {
    Eif (stateValue instanceof State) {
      if (!stateValue.actions.length) {
        return stateValue as State<TC, TE>;
      }
      return new State(
        stateValue.value,
        context,
        stateValue.historyValue,
        stateValue.history,
        undefined,
        stateValue.activities,
        undefined,
        undefined,
        stateValue.tree
      );
    }
 
    return State.from<TC, TE>(stateValue, context);
  }
 
  constructor(
    public value: StateValue,
    public context: TContext,
    public historyValue?: HistoryValue | undefined,
    public history?: State<TContext>,
    public actions: Array<ActionObject<TContext>> = [],
    public activities: ActivityMap = EMPTY_ACTIVITY_MAP,
    public data: Record<string, any> = {},
    /**
     * Internal event queue
     */
    public events: TEvents[] = [],
    tree?: StateTree
  ) {
    Object.defineProperty(this, 'tree', {
      value: tree,
      enumerable: false
    });
  }
 
  public get nextEvents(): EventType[] {
    Iif (!this.tree) {
      return [];
    }
 
    return this.tree.nextEvents;
  }
 
  public toStrings(
    value: StateValue = this.value,
    delimiter: string = '.'
  ): string[] {
    if (typeof value === 'string') {
      return [value];
    }
    const valueKeys = keys(value);
 
    return valueKeys.concat(
      ...valueKeys.map(key =>
        this.toStrings(value[key]).map(s => key + delimiter + s)
      )
    );
  }
 
  public matches(parentStateValue: StateValue): boolean {
    return matchesState(parentStateValue, this.value);
  }
 
  public get changed(): boolean | undefined {
    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))
    );
  }
}