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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 4x 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 1x 1x 12x 1x 1x 1x 1x 11x 11x 15x 11x 11x 11x 12x 1x 12x 11x 11x 11x 12x 14x 12x 12x 12x 11x 1x 1x 11x 11x 91x 91x 91x 91x 124x 124x 93x 93x 124x 124x 91x 93x 93x 93x 93x 91x 91x | import { anchor } from '../anchor.js';
import { setInspector } from '../broadcast.js';
import { replay, rollback } from '../event.js';
import { captureStack } from '../exception.js';
import { assign } from '../helper.js';
import { mutable } from '../ref.js';
import { subscribe } from '../subscription.js';
import type { Linkable, State, StateChange } from '../types.js';
import { microtask } from '../utils/index.js';
/**
* @deprecated Use `rollback()` instead.
* @type {<T>(state: T, event: StateChange) => void}
*/
export const undoChange = rollback;
/**
* @deprecated Use `replay()` instead.
* @type {<T>(state: T, event: StateChange) => void}
*/
export const redoChange = replay;
export type HistoryOptions = {
debounce?: number;
maxHistory?: number;
resettable?: boolean;
};
export const DEFAULT_HISTORY_OPTION = {
debounce: 100,
maxHistory: 100,
resettable: false,
};
function setDefaultOptions(options: HistoryOptions) {
Object.assign(DEFAULT_HISTORY_OPTION, options);
}
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 = subscribe.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);
rollback(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);
replay(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) {
rollback(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 = mutable<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;
}
history.setDefaultOptions = setDefaultOptions;
history.getDefaultOptions = getDefaultOptions;
/**
* Creates an undoable operation that can be executed and later reverted.
*
* This function wraps a given function execution and captures any state changes
* that occur during its execution. It returns a tuple containing two functions:
* the first one undoes the changes, and the second one clears the captured changes.
*
* @param operation - The function to execute and make undoable
* @returns A tuple containing two functions: [undo, clear]
* - undo: A function that, when called, will undo the changes made by the original function
* - clear: A function that, when called, will clear the captured changes (marking as completed)
*/
export function undoable(operation: () => void): [() => void, () => void] {
if (typeof operation !== 'function') {
const error = new Error('Invalid argument.');
captureStack.error.argument('The given argument is not a function.', error, undoable);
return [() => {}, () => {}];
}
const changes: { state: State; change: StateChange }[] = [];
setInspector((init: Linkable, event: StateChange) => {
changes.unshift({ state: anchor.find(init), change: event });
});
try {
operation();
} catch (error) {
captureStack.error.external('Undoable execution failed.', error as Error);
} finally {
setInspector(undefined);
}
const undo = () => {
changes.forEach(({ state, change }) => {
rollback(state, change);
});
changes.length = 0;
};
const clear = () => {
changes.length = 0;
};
return [undo, clear];
}
/**
* 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),
};
});
}
|