Skip to content

Commit bfc798d

Browse files
committed
[checkpoints] clearForward
1 parent 0590e2c commit bfc798d

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

src/checkpoints.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ export const createCheckpoints = getCreateFunction(
301301
return checkpoints;
302302
};
303303

304+
const clearForward = (): Checkpoints => {
305+
if (!arrayIsEmpty(forwardIds)) {
306+
clearCheckpointIds(forwardIds);
307+
callListeners(checkpointIdsListeners);
308+
}
309+
return checkpoints;
310+
};
311+
304312
const destroy = (): void => {
305313
store.delListener(cellListenerId);
306314
store.delListener(valueListenerId);
@@ -334,6 +342,7 @@ export const createCheckpoints = getCreateFunction(
334342
delListener,
335343

336344
clear,
345+
clearForward,
337346
destroy,
338347
getListenerStats,
339348
};

src/types/checkpoints.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export interface Checkpoints {
7777
/// Checkpoints.clear
7878
clear(): Checkpoints;
7979

80+
/// Checkpoints.clearForward
81+
clearForward(): Checkpoints;
82+
8083
/// Checkpoints.destroy
8184
destroy(): void;
8285

src/types/docs/checkpoints.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,8 @@
758758
* removing all the checkpoints it has been managing.
759759
*
760760
* Obviously this method should be used with caution as it destroys the
761-
* ability to undo recent changes to the Store (though of course the Store
762-
* itself is not reset by this method).
761+
* ability to undo or redo recent changes to the Store (though of course the
762+
* Store itself is not reset by this method).
763763
*
764764
* This method can be useful when a Store is being loaded via a Persister
765765
* asynchronously after the Checkpoints object has been attached, and you
@@ -807,6 +807,59 @@
807807
* @category Lifecycle
808808
*/
809809
/// Checkpoints.clear
810+
/**
811+
* The clearForward method resets just the 'redo' checkpoints it has been
812+
* managing.
813+
*
814+
* Obviously this method should be used with caution as it destroys the
815+
* ability to redo recent changes to the Store (though of course the Store
816+
* itself is not reset by this method).
817+
*
818+
* This method can be useful when you want to prohibit a user from redoing
819+
* changes they have undone. The 'backward' redo stack, and current checkpoint
820+
* are not affected.
821+
* @returns A reference to the Checkpoints object.
822+
* @example
823+
* This example creates a Store, a Checkpoints object, adds a listener, makes
824+
* a change and then clears the forward checkpoints.
825+
*
826+
* ```js
827+
* const store = createStore().setTables({pets: {fido: {sold: false}}});
828+
*
829+
* const checkpoints = createCheckpoints(store);
830+
* console.log(checkpoints.getCheckpointIds());
831+
* // -> [[], '0', []]
832+
*
833+
* const listenerId = checkpoints.addCheckpointIdsListener(() => {
834+
* console.log('checkpoints changed');
835+
* });
836+
*
837+
* store.setCell('pets', 'fido', 'color', 'brown');
838+
* // -> 'checkpoints changed'
839+
* checkpoints.addCheckpoint();
840+
* // -> 'checkpoints changed'
841+
* store.setCell('pets', 'fido', 'sold', true);
842+
* // -> 'checkpoints changed'
843+
* checkpoints.addCheckpoint();
844+
* // -> 'checkpoints changed'
845+
* checkpoints.goBackward();
846+
* // -> 'checkpoints changed'
847+
*
848+
* console.log(store.getTables());
849+
* // -> {pets: {fido: {color: 'brown', sold: false}}}
850+
* console.log(checkpoints.getCheckpointIds());
851+
* // -> [['0'], '1', ['2']]
852+
*
853+
* checkpoints.clearForward();
854+
* // -> 'checkpoints changed'
855+
*
856+
* console.log(checkpoints.getCheckpointIds());
857+
* // -> [['0'], '1', []]
858+
* ```
859+
* @category Lifecycle
860+
* @since v4.5.3
861+
*/
862+
/// Checkpoints.clearForward
810863
/**
811864
* The destroy method should be called when this Checkpoints object is no
812865
* longer used.

src/types/with-schemas/checkpoints.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export interface Checkpoints<in out Schemas extends OptionalSchemas> {
7878
/// Checkpoints.clear
7979
clear(): Checkpoints<Schemas>;
8080

81+
/// Checkpoints.clearForward
82+
clearForward(): Checkpoints<Schemas>;
83+
8184
/// Checkpoints.destroy
8285
destroy(): void;
8386

test/unit/other/checkpoints.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,33 @@ describe('Miscellaneous', () => {
509509
]);
510510
});
511511

512+
test('clearForward', () => {
513+
expect(checkpoints.getCheckpointIds()).toEqual([[], '0', []]);
514+
const [[id0], [id1], [id2], [id3], [id4]] = setContent();
515+
const listener = createCheckpointsListener(checkpoints);
516+
const listenerId = listener.listenToCheckpoints('/');
517+
checkpoints.goBackward().goBackward();
518+
expect(checkpoints.getCheckpointIds()).toEqual([
519+
[id0, id1],
520+
id2,
521+
[id3, id4],
522+
]);
523+
expectChanges(
524+
listener,
525+
'/',
526+
[[id0, id1, id2], id3, [id4]],
527+
[[id0, id1], id2, [id3, id4]],
528+
);
529+
checkpoints.clearForward();
530+
expect(checkpoints.getCheckpointIds()).toEqual([[id0, id1], id2, []]);
531+
expectChanges(listener, '/', [[id0, id1], id2, []]);
532+
expectNoChanges(listener);
533+
checkpoints.clearForward();
534+
expect(checkpoints.getCheckpointIds()).toEqual([[id0, id1], id2, []]);
535+
expectNoChanges(listener);
536+
checkpoints.delListener(listenerId);
537+
});
538+
512539
test('listen to and change labels', () => {
513540
expect(checkpoints.getCheckpointIds()).toEqual([[], '0', []]);
514541
const listener = createCheckpointsListener(checkpoints);

0 commit comments

Comments
 (0)