All files / core/src helper.ts

100% Statements 135/135
100% Branches 62/62
100% Functions 4/4
100% Lines 135/135

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  1x 1x 1x 1x 1x                             1x 315x 4x 4x   315x 4x 4x   307x 307x 307x 307x   307x 299x 299x   307x   315x 780x 2x 2x 780x 2x 778x 776x 776x 776x 780x   307x 307x 307x 307x 307x 307x   315x 315x   315x 299x 299x   315x 1x 1x 315x                           1x 16x 4x 4x   12x 12x 12x 12x   12x 12x 12x 12x   12x   16x 22x 2x 2x 22x 20x   20x 15x 15x 20x 22x   16x 3x 1x 2x 2x 2x   2x 14x 10x 10x 2x 2x 3x   12x 12x 12x 12x 12x 12x   16x 16x   16x 12x 12x   16x 1x 1x 16x                         1x 16x 4x 4x   12x 12x 12x 12x   12x 12x 12x   16x 2x 16x 3x 10x 7x 12x 12x 7x   12x 12x 12x 12x 12x   16x 16x   16x 12x 12x   16x 1x 1x 16x   1461x 1461x 1461x  
import type { Assignable, AssignablePart, Broadcaster, Linkable, StateChange } from './types.js';
import { BROADCASTER_REGISTRY, META_REGISTRY, STATE_BUSY_LIST, STATE_REGISTRY } from './registry.js';
import { isArray, isDefined, isMap, isSet } from '@beerush/utils';
import { softEntries, softKeys } from './utils/clone.js';
import { getDevTool } from './dev.js';
import { BatchMutations } from './enum.js';
 
/**
 * Assigns a partial state to the given state.
 *
 * This function updates the target state object with values from the source object.
 * It supports objects, arrays, and maps. The function also handles state management
 * by notifying subscribers of the changes.
 *
 * @template T - The type of the target state object
 * @template P - The type of the source partial object
 * @param {T} target - The target state object to be updated
 * @param {P} source - The partial object containing the new values
 * @throws {Error} If the target is not an assignable state or if the source is not an object-like value
 */
export const assign = <T extends Assignable, P extends AssignablePart<T>>(target: T, source: P) => {
  if (!isSafeObject(target) && !isArray(target)) {
    throw new Error('Cannot assign to non-assignable state.');
  }
 
  if (!isSafeObject(source) && !isArray(source)) {
    throw new Error('Cannot assign using non-object value.');
  }
 
  const init = STATE_REGISTRY.get(target) as Linkable;
  const meta = META_REGISTRY.get(init as Linkable);
  const devTool = getDevTool();
  const broadcaster = BROADCASTER_REGISTRY.get(init) as Broadcaster;
 
  if (isDefined(init)) {
    STATE_BUSY_LIST.add(init);
  }
 
  const prev: AssignablePart<T> = {};
 
  for (const [key, val] of softEntries(source)) {
    if (isMap(target)) {
      prev[key as never] = target.get(key) as never;
      target.set(key, val);
    } else if (isSet(target)) {
      target.add(val);
    } else if (isSafeObject(target) || isArray(target)) {
      prev[key as keyof T] = target[key as keyof T];
      target[key as never] = val;
    }
  }
 
  const event: StateChange = {
    type: BatchMutations.ASSIGN,
    prev,
    keys: [],
    value: source,
  };
 
  broadcaster?.emit(event);
  broadcaster?.broadcast(init, event, meta?.id);
 
  if (isDefined(init)) {
    STATE_BUSY_LIST.delete(init);
  }
 
  if (meta && devTool?.onAssign) {
    devTool?.onAssign(meta, source);
  }
};
 
/**
 * Removes the given keys from the given state.
 *
 * This function removes specified keys from the target state object.
 * It supports objects, arrays, and maps. The function also handles state management
 * by notifying subscribers of the changes.
 *
 * @template T - The type of the target state object
 * @param {T} target - The target state object from which keys will be removed
 * @param {...keyof T} keys - The keys to be removed from the target object
 * @throws {Error} If the target is not an assignable state
 */
export const remove = <T extends Assignable>(target: T, ...keys: Array<keyof T>) => {
  if (!isSafeObject(target) && !isArray(target)) {
    throw new Error('Cannot remove from non-assignable state.');
  }
 
  const init = STATE_REGISTRY.get(target) as Linkable;
  const meta = META_REGISTRY.get(init as Linkable);
  const devTool = getDevTool();
  const broadcaster = BROADCASTER_REGISTRY.get(init) as Broadcaster;
 
  if (isDefined(init)) {
    target = init as T;
    STATE_BUSY_LIST.add(init);
  }
 
  const prev = {} as AssignablePart<T>;
 
  for (const key of keys) {
    if (isMap(target)) {
      prev[key as never] = target.get(key) as never;
      target.delete(key);
    } else if (isSafeObject(target) || isArray(target)) {
      prev[key] = target[key];
 
      if (!isArray(target)) {
        delete target[key];
      }
    }
  }
 
  if (isArray(target)) {
    if (keys.length === 1) {
      target.splice(keys[0] as never, 1);
    } else {
      const values = [...target];
      target.length = 0;
 
      values.forEach((v, i) => {
        if (!keys.includes(String(i) as keyof T)) {
          target.push(v);
        }
      });
    }
  }
 
  const event: StateChange = {
    type: BatchMutations.REMOVE,
    prev,
    keys: [],
    value: keys,
  };
 
  broadcaster?.emit(event);
  broadcaster?.broadcast(init, event, meta?.id);
 
  if (isDefined(init)) {
    STATE_BUSY_LIST.delete(init);
  }
 
  if (meta && devTool?.onRemove) {
    devTool?.onRemove(meta, keys);
  }
};
 
/**
 * Clears the given state.
 *
 * This function clears the target state object, removing all its contents.
 * It supports objects, arrays, and maps. The function also handles state management
 * by notifying subscribers of the changes.
 *
 * @template T - The type of the target state object
 * @param {T} target - The target state object to be cleared
 * @throws {Error} If the target is not an assignable state
 */
export const clear = <T extends Assignable>(target: T) => {
  if (!isSafeObject(target) && !isArray(target)) {
    throw new Error('Cannot clear non-assignable state.');
  }
 
  const init = STATE_REGISTRY.get(target) as Linkable;
  const meta = META_REGISTRY.get(init as Linkable);
  const devTool = getDevTool();
  const broadcaster = BROADCASTER_REGISTRY.get(init) as Broadcaster;
 
  if (isDefined(init)) {
    STATE_BUSY_LIST.add(init);
  }
 
  if (isMap(target)) {
    target.clear();
  } else if (isArray(target)) {
    target.length = 0;
  } else if (isSafeObject(target)) {
    for (const key of softKeys(target)) {
      delete target[key];
    }
  }
 
  const event: StateChange = {
    type: BatchMutations.CLEAR,
    prev: {},
    keys: [],
  };
 
  broadcaster?.emit(event);
  broadcaster?.broadcast(init, event, meta?.id);
 
  if (isDefined(init)) {
    STATE_BUSY_LIST.delete(init);
  }
 
  if (meta && devTool?.onClear) {
    devTool?.onClear(meta);
  }
};
 
function isSafeObject(value: unknown): value is object {
  return typeof value === 'object' && value !== null && !Array.isArray(value);
}