All files / core/src event.ts

100% Statements 126/126
100% Branches 70/70
100% Functions 4/4
100% Lines 126/126

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 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 2291x 1x 1x 1x 1x                                                           1x 17x 17x 17x 17x   17x 7x 17x 1x 10x 1x 9x 1x 8x 2x 1x 1x 1x 1x 7x 1x 5x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 17x                                   1x 71x 71x 71x 71x   71x 37x 1x 37x 36x 36x   71x 6x   4x 4x   2x 2x 34x 4x   28x   2x 24x 7x   3x 5x   4x 4x 22x   1x 15x 2x 1x   2x 2x 1x 1x   4x 4x 1x 14x 12x   12x   1x 12x   1x 11x 6x 6x 6x 6x   6x 6x 6x 9x   1x 4x   3x 3x 12x 71x                                 1x 88x 10x 10x   78x 78x   84x 61x 61x 52x 19x 17x 61x 16x 16x   45x 45x   17x 21x 17x   17x 17x                                 37x 37x 1x 1x   36x 36x  
import { anchor } from './anchor.js';
import { ARRAY_MUTATION_KEYS, ARRAY_MUTATIONS, COLLECTION_MUTATION_KEYS } from './constant.js';
import { type ArrayMutations, BatchMutations, MapMutations, ObjectMutations, SetMutations } from './enum.js';
import { assign } from './helper.js';
import { INIT_GATEWAY_REGISTRY, STATE_REGISTRY } from './registry.js';
import type {
  ArrayMutation,
  ArrayMutator,
  KeyLike,
  Linkable,
  MapMutator,
  SetMutator,
  StateChange,
  StateGateway,
} from './types.js';
 
/**
 * Replays a state change event on the given state.
 *
 * This function takes a state object and a state change event, then applies
 * the change described in the event to the state. It handles various types
 * of mutations including object property sets/deletes, map operations,
 * set operations, array mutations, and batch assignments.
 *
 * The function uses a gateway system to perform the actual mutations,
 * ensuring that the appropriate mutator methods are called based on
 * the type of the target object (plain object, Map, Set, or Array).
 *
 * @template T - The type of the state object
 * @param state - The state object to apply the change to
 * @param event - The state change event to replay
 *
 * @internal
 */
export function replay<T>(state: T, event: StateChange) {
  const init = STATE_REGISTRY.get(state as Linkable);
  const { type, prev, value } = event;
  const { key, target } = getEventTarget(init, event);
  const gateway = INIT_GATEWAY_REGISTRY.get(target as Linkable) as StateGateway;
 
  if (type === ObjectMutations.SET) {
    gateway.setter(target, key, value, target);
  } else if (type === MapMutations.SET) {
    (gateway.mutator as MapMutator<unknown, unknown>).set(key, value);
  } else if (type === SetMutations.ADD) {
    (gateway.mutator as SetMutator<unknown>).add(value);
  } else if (type === ObjectMutations.DELETE) {
    gateway.remover(target, key, target);
  } else if (type === MapMutations.DELETE || type === SetMutations.DELETE) {
    if (target instanceof Map) {
      (gateway.mutator as MapMutator<unknown, unknown>).delete(key);
    } else if (target instanceof Set) {
      (gateway.mutator as SetMutator<unknown>).delete(prev);
    }
  } else if (type === BatchMutations.ASSIGN) {
    assign(anchor.find(target) as never, value as never);
  } else if (type === MapMutations.CLEAR || type === SetMutations.CLEAR) {
    if (target instanceof Map) {
      (gateway.mutator as MapMutator<unknown, unknown>).clear();
    } else if (target instanceof Set) {
      (gateway.mutator as SetMutator<unknown>).clear();
    }
  } else if (ARRAY_MUTATIONS.includes(type as ArrayMutations)) {
    ((gateway.mutator as ArrayMutator<unknown>)[type as ArrayMutation] as (...args: unknown[]) => unknown)(
      ...(value as unknown[])
    );
  }
}
 
/**
 * Reverts a state change event on the given state.
 *
 * This function takes a state object and a state change event, then undoes
 * the change described in the event to revert the state back to its previous
 * state. It handles various types of mutations including object property
 * sets/deletes, map operations, set operations, array mutations, and batch assignments.
 *
 * The function uses a gateway system to perform the actual mutations,
 * ensuring that the appropriate mutator methods are called based on
 * the type of the target object (plain object, Map, Set, or Array).
 *
 * @template T - The type of the state object
 * @param state - The state object to revert the change on
 * @param event - The state change event to revert
 */
export function rollback<T>(state: T, event: StateChange) {
  const init = STATE_REGISTRY.get(state as Linkable) as Linkable;
  const { type, prev } = event;
  const { key, target } = getEventTarget(init, event);
  const gateway = INIT_GATEWAY_REGISTRY.get(target as Linkable) as StateGateway;
 
  if (type === ObjectMutations.SET) {
    if (typeof prev === 'undefined') {
      gateway.remover(target, key, target);
    } else {
      gateway.setter(target, key, prev, target);
    }
    // target[key as never] = prev as never;
  } else if (type === MapMutations.SET) {
    if (typeof prev === 'undefined') {
      // target.delete(key);
      (gateway.mutator as MapMutator<unknown, unknown>).delete(key);
    } else {
      // target.set(key, prev);
      (gateway.mutator as MapMutator<unknown, unknown>).set(key, prev);
    }
  } else if (type === SetMutations.ADD) {
    (gateway.mutator as SetMutator<unknown>).delete(event.value);
    // (target[key as never] as Set<unknown>).delete(event.value);
  } else if (type === ObjectMutations.DELETE) {
    // (target as ObjLike)[key] = prev;
    gateway?.setter(target, key, prev, target);
  } else if (type === MapMutations.DELETE || type === SetMutations.DELETE) {
    if (target instanceof Map) {
      // target.set(key, prev);
      (gateway.mutator as MapMutator<unknown, unknown>).set(key, prev);
    } else if (target instanceof Set) {
      // (target[key as never] as Set<unknown>).add(prev);
      (gateway.mutator as SetMutator<unknown>).add(prev);
    }
  } else if (type === BatchMutations.ASSIGN) {
    // assign(target as never, prev as never);
    assign(anchor.find(target) as never, prev as never);
  } else if (type === MapMutations.CLEAR || type === SetMutations.CLEAR) {
    if (target instanceof Map) {
      for (const [key, value] of prev as [[unknown, unknown]]) {
        // target.set(key as never, value);
        (gateway.mutator as MapMutator<unknown, unknown>).set(key, value);
      }
    } else if (target instanceof Set) {
      for (const value of prev as [unknown]) {
        // target.add(value);
        (gateway.mutator as SetMutator<unknown>).add(value);
      }
    }
  } else if (ARRAY_MUTATIONS.includes(type as never)) {
    const items = target as unknown[];
 
    if (type === 'shift') {
      // items.unshift(prev);
      (gateway.mutator as ArrayMutator<unknown>).unshift(prev);
    } else if (type === 'pop') {
      // items.push(prev);
      (gateway.mutator as ArrayMutator<unknown>).push(prev);
    } else if (type === 'push') {
      const initItems = (anchor.get as (item: unknown, silent: boolean) => unknown[])(items, true);
      for (const item of event.value as unknown[]) {
        const index = initItems.indexOf(item);
        if (index >= 0) {
          // items.splice(index, 1);
          (gateway.mutator as ArrayMutator<unknown>).splice(index, 1);
        }
      }
    } else if (type === 'unshift') {
      // items.shift();
      (gateway.mutator as ArrayMutator<unknown>).shift();
    } else {
      // items.splice(0, items.length, ...(prev as unknown[]));
      (gateway.mutator as ArrayMutator<unknown>).splice(0, items.length, ...(prev as unknown[]));
    }
  }
}
 
/**
 * Retrieves the target object and key for a nested property access.
 *
 * This function is used internally by the history management system to
 * locate the specific object and property that was modified. It takes
 * a state object and a path of keys, then traverses the object hierarchy
 * to find the parent object and the final key.
 *
 * @template T - The type of the state object
 * @param state - The root state object to traverse
 * @param event - The state change event containing information about the modified property
 * @returns An object containing the final key and its parent target object
 *
 * @internal
 */
export function getEventTarget<T>(state: T, event: StateChange) {
  if (!event.keys.length) {
    return { key: '', target: state as Linkable };
  }
 
  const parentKeys = [...event.keys];
  const key = parentKeys.pop() as KeyLike;
 
  if (!parentKeys.length) {
    if (
      (ARRAY_MUTATION_KEYS.has(event.type as ArrayMutations) ||
        COLLECTION_MUTATION_KEYS.has(event.type as SetMutations)) &&
      event.type !== MapMutations.SET &&
      event.type !== MapMutations.DELETE
    ) {
      return { key: '', target: getValue(state, key) as Linkable };
    }
 
    return { key, target: state as Linkable };
  }
 
  const target = parentKeys.reduce((parent, key) => {
    return getValue(parent, key) as T;
  }, state) as Linkable;
 
  return { key, target };
}
 
/**
 * Retrieves a value from a target object using a specified key.
 *
 * This function is used internally by the history management system to
 * access values in various data structures including Maps and plain objects.
 * It provides a unified interface for value retrieval regardless of the
 * underlying data structure.
 *
 * @template T - The type of the target object
 * @param target - The target object from which to retrieve the value
 * @param key - The key used to access the value
 * @returns The value associated with the key, or undefined if not found
 *
 * @internal
 */
function getValue<T>(target: T, key: KeyLike) {
  if (target instanceof Map) {
    return target.get(key);
  }
 
  return (target as Record<KeyLike, unknown>)[key];
}