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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 4x 4x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 37x 37x 53x 53x 53x 49x 49x 49x 49x 49x 49x 49x 49x 53x 53x 37x 19x 19x 19x 17x 17x 17x 17x 17x 17x 17x 17x 19x 19x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 6x 5x 6x 8x 8x 5x 5x 6x 37x 30x 30x 30x 30x 37x 236x 124x 124x 91x 13x 13x 91x 93x 8x 8x 93x 93x 91x 91x 91x 91x 91x 91x 91x 124x 124x 37x 37x 37x 37x 54x 54x 37x 19x 19x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 57x 57x 57x 57x 57x 29x 1x 29x 28x 28x 57x 5x 3x 3x 2x 2x 28x 3x 23x 2x 20x 5x 2x 4x 3x 3x 18x 1x 13x 2x 1x 2x 2x 1x 1x 4x 4x 1x 12x 10x 10x 1x 10x 1x 9x 5x 5x 5x 5x 5x 5x 5x 8x 1x 3x 2x 2x 10x 57x 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 74x 74x 6x 6x 68x 68x 74x 51x 51x 42x 17x 51x 16x 16x 35x 35x 17x 21x 17x 17x 17x 37x 37x 1x 1x 36x 36x 91x 91x 91x 91x 124x 124x 93x 93x 124x 124x 91x 93x 93x 93x 93x 91x 91x | import type { ArrayMutation, ArrayMutator, KeyLike, Linkable, MapMutator, SetMutator, State, StateChange, StateGateway, } from '../types.js'; import { anchor } from '../anchor.js'; import { derive } from '../derive.js'; import { assign } from '../helper.js'; import { INIT_GATEWAY_REGISTRY, STATE_REGISTRY } from '../registry.js'; import { ARRAY_MUTATION_KEYS, ARRAY_MUTATIONS, COLLECTION_MUTATION_KEYS } from '../constant.js'; import { microtask } from '../utils/index.js'; import { captureStack } from '../exception.js'; import { ArrayMutations, BatchMutations, MapMutations, ObjectMutations, SetMutations } from '../enum.js'; export type HistoryOptions = { debounce?: number; maxHistory?: number; resettable?: boolean; }; export const DEFAULT_HISTORY_OPTION = { debounce: 100, maxHistory: 100, resettable: false, }; export function setDefaultOptions(options: HistoryOptions) { Object.assign(DEFAULT_HISTORY_OPTION, options); } export function getDefaultOptions() { return DEFAULT_HISTORY_OPTION; } export type HistoryState = { readonly backwardList: StateChange[]; readonly forwardList: StateChange[]; canBackward: boolean; canForward: boolean; canReset: boolean; backward(): void; forward(): void; destroy(): void; clear(): void; reset(): void; }; /** * Creates a history management system for a reactive state object. * * This function tracks state changes and provides undo/redo functionality. * It maintains backward and forward stacks of state changes, allowing users * to navigate through the history of modifications. * * @template T - The type of the state object * @param state - The reactive state object to track * @param options - Configuration options for the history management * @param options.debounce - Debounce time in milliseconds for collecting changes (default: 100ms) * @param options.maxHistory - Maximum number of history states to keep (default: 100) * @returns A HistoryState object with methods and properties for history management */ export function history<T extends State>(state: T, options?: HistoryOptions): HistoryState { const { maxHistory = DEFAULT_HISTORY_OPTION.maxHistory, debounce = DEFAULT_HISTORY_OPTION.debounce, resettable = DEFAULT_HISTORY_OPTION.resettable, } = options ?? {}; const [schedule] = microtask<StateChange>(debounce); const changeList: StateChange[] = []; const backwardList: StateChange[] = []; const forwardList: StateChange[] = []; const mergeList = new Set<StateChange>(); const controller = derive.resolve(state); if (!anchor.has(state)) { const error = new Error('Object is not reactive.'); captureStack.error.external('Cannot create history state from non-reactive object.', error, history); } let isBusy = false; const backward = () => { isBusy = true; const event = backwardList.pop(); if (event) { forwardList.unshift(event); undoChange(state, event); assign(historyState, { canForward: forwardList.length > 0, canBackward: backwardList.length > 0, canReset: changeList.length > 0, }); } isBusy = false; }; const forward = () => { isBusy = true; const event = forwardList.shift(); if (event) { backwardList.push(event); redoChange(state, event); assign(historyState, { canForward: forwardList.length > 0, canBackward: backwardList.length > 0, canReset: changeList.length > 0, }); } isBusy = false; }; const clear = () => { isBusy = true; backwardList.length = 0; forwardList.length = 0; assign(historyState, { canForward: forwardList.length > 0, canBackward: backwardList.length > 0, canReset: changeList.length > 0, }); isBusy = false; }; const reset = () => { if (!options?.resettable) return; isBusy = true; while (changeList.length) { undoChange(state, changeList.pop() as StateChange); } clear(); isBusy = false; }; const destroy = () => { changeList.length = 0; unsubscribe?.(); clear(); }; // Subscribe for state changes and push the event to the backward stack, then clears the forward stack. const unsubscribe = controller?.subscribe((snap, event) => { if (event.type !== 'init' && !isBusy) { mergeList.add(event); schedule(() => { if (maxHistory && backwardList.length >= maxHistory) { backwardList.shift(); } for (const change of mergeChanges(mergeList)) { if (resettable) { changeList.push(change); } backwardList.push(change); } forwardList.length = 0; mergeList.clear(); assign(historyState, { canForward: forwardList.length > 0, canBackward: backwardList.length > 0, canReset: changeList.length > 0, }); }, event); } }); const historyState = anchor.raw<HistoryState>( { get backwardList() { return backwardList; }, get forwardList() { return forwardList; }, canBackward: false, canForward: false, canReset: changeList.length > 0, backward, forward, destroy, reset, clear, }, { recursive: false } ); return historyState; } /** * Reverts a state change by applying the previous value to the target. * * This function is used internally by the history management system to undo state changes. * It handles various types of state mutations including set, add, delete, assign, clear, * and array mutations by restoring the previous state. * * @template T - The type of the state object * @param state - The reactive state object * @param event - The state change event containing information about the change to undo * * @internal */ export function undoChange<T>(state: T, event: StateChange) { const init = STATE_REGISTRY.get(state as Linkable) as Linkable; const { type, prev } = event; const { key, target } = getTarget(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[])); } } } /** * Re-applies a state change by applying the new value to the target. * * This function is used internally by the history management system to redo state changes. * It handles various types of state mutations including set, add, delete, assign, clear, * and array mutations by re-applying the change that was previously undone. * * @template T - The type of the state object * @param state - The reactive state object * @param event - The state change event containing information about the change to redo * * @internal */ export function redoChange<T>(state: T, event: StateChange) { const init = STATE_REGISTRY.get(state as Linkable); const { type, prev, value } = event; const { key, target } = getTarget(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[]) ); } } /** * 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 */ function getTarget<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 ) { 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]; } /** * Creates a consolidated list of state changes by merging multiple changes to the same key. * * This function processes a set of state changes and consolidates them so that each unique * key path only appears once in the resulting array. When multiple changes affect the same * key path, only the first change (earliest in the set) is kept, but its value is updated * to the latest value for that key path. * * This is used internally by the history management system to optimize history entries, * ensuring that rapid successive changes to the same property result in a single history * entry with the final value rather than multiple intermediate states. * * @param changeList - A set of state change events to consolidate * @returns An array of consolidated state change events * * @internal */ function mergeChanges(changeList: Set<StateChange>) { const prevChanges = new Map<string, StateChange>(); const nextChanges = new Map<string, unknown>(); for (const change of changeList) { const key = change.keys.join('.'); if (!prevChanges.has(key)) { prevChanges.set(key, change); } nextChanges.set(key, change.value); } return Array.from(prevChanges.entries()).map(([key, change]) => { return { ...change, value: nextChanges.get(key), }; }); } |