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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | 1x 1x 1x 1x 1x 1x 327x 4x 4x 327x 4x 4x 319x 319x 319x 319x 319x 302x 302x 319x 319x 319x 318x 327x 800x 2x 2x 2x 2x 2x 800x 2x 798x 796x 796x 367x 367x 796x 796x 800x 318x 318x 318x 318x 318x 318x 318x 327x 327x 327x 301x 301x 327x 1x 1x 327x 1x 17x 4x 4x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 17x 23x 2x 2x 2x 2x 2x 23x 1x 21x 20x 15x 15x 20x 20x 15x 15x 20x 23x 17x 3x 1x 2x 2x 2x 2x 14x 10x 10x 2x 2x 3x 13x 13x 13x 13x 13x 13x 13x 17x 17x 17x 13x 13x 17x 1x 1x 17x 1x 16x 4x 4x 12x 12x 12x 12x 12x 12x 12x 12x 16x 2x 2x 16x 1x 10x 3x 3x 7x 6x 12x 12x 12x 6x 12x 12x 12x 12x 12x 12x 16x 16x 16x 12x 12x 16x 1x 1x 16x 1x 16x 2x 2x 14x 16x 2x 2x 16x 2x 2x 10x 10x 10x 10x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 10x 10x 16x 1x 1x 16x 1x 1x 16x 1x 8x 8x 1x 8x 8x 10x 10x 10x 10x 10x 10x 10x 1521x 1521x 1521x | import { getDevTool } from './dev.js';
import { BatchMutations, StringMutations } from './enum.js';
import { BROADCASTER_REGISTRY, META_REGISTRY, STATE_BUSY_LIST, STATE_REGISTRY } from './registry.js';
import type { Assignable, AssignablePart, Broadcaster, Linkable, StateChange } from './types.js';
import { softEntries, softKeys } from './utils/clone.js';
import { isArray, isDefined, isMap, isSet, isString } from './utils/index.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> = {};
const entries = softEntries(source);
if (!entries.length) return;
const changes: unknown[] = [];
for (const [key, val] of softEntries(source)) {
if (isMap(target)) {
prev[key as never] = target.get(key) as never;
if (prev[key as never] !== val) {
changes.push(key);
}
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];
if (prev[key as keyof T] !== val) {
changes.push(key);
}
target[key as never] = val;
}
}
const event = {
type: BatchMutations.ASSIGN,
prev,
keys: [],
changes,
value: source,
} as StateChange;
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>;
const changes: unknown[] = [];
for (const key of keys) {
if (isMap(target)) {
if (target.has(key)) {
changes.push(key);
}
prev[key as never] = target.get(key) as never;
target.delete(key);
} else if (isSet(target)) {
target.delete(key);
} else if (isSafeObject(target) || isArray(target)) {
if (typeof target[key] !== 'undefined') {
changes.push(key);
}
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 = {
type: BatchMutations.REMOVE,
prev,
keys: [],
changes,
value: keys,
} as StateChange;
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);
}
let changes: unknown[] = [];
if (isMap(target)) {
changes = [...target.keys()];
target.clear();
} else if (isSet(target)) {
target.clear();
} else if (isArray(target)) {
changes = Array.from(target.keys());
target.length = 0;
} else if (isSafeObject(target)) {
for (const key of softKeys(target)) {
changes.push(key);
delete target[key];
}
}
const event = {
type: BatchMutations.CLEAR,
prev: {},
keys: [],
changes,
} as StateChange;
broadcaster?.emit(event);
broadcaster?.broadcast(init, event, meta?.id);
if (isDefined(init)) {
STATE_BUSY_LIST.delete(init);
}
if (meta && devTool?.onClear) {
devTool?.onClear(meta);
}
};
/**
* Merges text to an existing string property in the target object.
*
* This function prepends or appends the given value to the existing string property.
* It handles state management by notifying subscribers of the change.
*
* @template T - The type of the target object
* @template K - The type of the property key
* @param {T} target - The target object containing the string property
* @param {K} prop - The property key of the string to modify
* @param {T[K]} value - The string value to prepend or append
* @param {boolean} isPrepend - Whether to prepend (true) or append (false) the value
* @throws {Error} If the target is not a safe object, or if the property is not a string, or if the value is not a string
*/
const mergeText = <T, K extends keyof T>(target: T, prop: K, value: T[K], isPrepend: boolean = false) => {
if (!isSafeObject(target) && !isArray(target)) {
throw new Error(`Cannot ${isPrepend ? 'prepend' : 'append'} to non-object target.`);
}
const prev = target[prop];
if (!isString(prev)) {
throw new Error(`Cannot ${isPrepend ? 'prepend' : 'append'} to non-string property.`);
}
if (!isString(value)) {
throw new Error(`Cannot ${isPrepend ? 'prepend' : 'append'} non-string value.`);
}
const { init, meta, devTool, broadcaster } = targetInfo(target as Linkable);
if (isDefined(init)) {
STATE_BUSY_LIST.add(init);
}
target[prop as never] = (isPrepend ? value + prev : prev + value) as never;
const event = {
type: isPrepend ? StringMutations.PREPEND : StringMutations.APPEND,
prev,
keys: [prop],
value,
} as StateChange;
broadcaster?.emit(event);
broadcaster?.broadcast(init, event, meta?.id);
if (isDefined(init)) {
STATE_BUSY_LIST.delete(init);
}
if (meta && isPrepend && devTool?.onPrepend) {
devTool?.onPrepend(meta, prop, value);
}
if (meta && !isPrepend && devTool?.onAppend) {
devTool?.onAppend(meta, prop, value);
}
};
/**
* Appends a string value to an existing string property in the target object.
*
* This function appends the given value to the end of the existing string property.
* It handles state management by notifying subscribers of the change.
*
* @template T - The type of the target object
* @template K - The type of the property key
* @param {T} target - The target object containing the string property
* @param {K} prop - The property key of the string to modify
* @param {T[K]} value - The string value to append
*/
export function append<T, K extends keyof T>(target: T, prop: K, value: T[K]) {
mergeText(target, prop, value);
}
/**
* Prepends a string value to an existing string property in the target object.
*
* This function prepends the given value to the beginning of the existing string property.
* It handles state management by notifying subscribers of the change.
*
* @template T - The type of the target object
* @template K - The type of the property key
* @param {T} target - The target object containing the string property
* @param {K} prop - The property key of the string to modify
* @param {T[K]} value - The string value to prepend
*/
export function prepend<T, K extends keyof T>(target: T, prop: K, value: T[K]) {
mergeText(target, prop, value, true);
}
function targetInfo(target: Linkable) {
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;
return { init, meta, devTool, broadcaster };
}
function isSafeObject(value: unknown): value is object {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
|