Introduce clear() & reset()

Summary:
clear will drop all current records but keep any view preferences. This typically relates to the "clear" button in Flipper.

reset on the other hand will keep the current records, but just reset the view preferences to the default, dropping any filters and sorting criteria

Reviewed By: nikoant

Differential Revision: D25975612

fbshipit-source-id: 5b419f259bffc049daf125090c6754aa6528919b
This commit is contained in:
Michel Weststrate
2021-03-16 14:54:53 -07:00
committed by Facebook GitHub Bot
parent 66864b8f54
commit 6e4fcbdae3
2 changed files with 53 additions and 0 deletions

View File

@@ -231,6 +231,28 @@ class DataSource<
}
}
/**
* The clear operation removes any records stored, but will keep the current view preferences such as sorting and filtering
*/
clear() {
this._records = [];
this._recordsById = new Map();
this.idToIndex = new Map();
this.dataUpdateQueue = [];
if (this._sortedRecords) this._sortedRecords = [];
if (this._reversedRecords) this._reversedRecords = [];
}
/**
* The reset operation resets any view preferences such as sorting and filtering, but keeps the current set of records.
*/
reset() {
this.sortBy = undefined;
this._sortedRecords = undefined;
this.reverse = false;
this._reversedRecords = undefined;
}
emitDataEvent(event: DataEvent<T>) {
this.dataUpdateQueue.push(event);
// TODO: schedule

View File

@@ -255,3 +255,34 @@ test('reverse with sorting', () => {
expect(ds.sortedRecords).toEqual([a, b3r, b1, b2r, b4, c]);
expect(ds.reversedRecords).toEqual([c, b4, b2r, b1, b3r, a]);
});
test('reset', () => {
const ds = createDataSource<Todo>([submitBug, drinkCoffee, eatCookie], 'id');
ds.setSortBy('title');
ds.toggleReversed();
expect(ds.reversedRecords).toEqual([submitBug, eatCookie, drinkCoffee]);
expect([...ds.recordsById.keys()]).toEqual(['bug', 'coffee', 'cookie']);
ds.reset();
expect(ds.reversedRecords).toEqual([submitBug, drinkCoffee, eatCookie]);
expect([...ds.recordsById.keys()]).toEqual(['bug', 'coffee', 'cookie']);
});
test('clear', () => {
const ds = createDataSource<Todo>([submitBug, drinkCoffee, eatCookie], 'id');
ds.setSortBy('title');
ds.toggleReversed();
expect(ds.reversedRecords).toEqual([submitBug, eatCookie, drinkCoffee]);
expect([...ds.recordsById.keys()]).toEqual(['bug', 'coffee', 'cookie']);
ds.clear();
expect(ds.reversedRecords).toEqual([]);
expect([...ds.recordsById.keys()]).toEqual([]);
ds.append(eatCookie);
ds.append(drinkCoffee);
ds.append(submitBug);
expect([...ds.recordsById.keys()]).toEqual(['cookie', 'coffee', 'bug']);
// resets in the same ordering as view preferences were preserved
expect(ds.reversedRecords).toEqual([submitBug, eatCookie, drinkCoffee]);
});