From 6e4fcbdae3853ba7da5a8f6b5db40acab2b4c6df Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 16 Mar 2021 14:54:53 -0700 Subject: [PATCH] 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 --- .../src/state/datasource/DataSource.tsx | 22 +++++++++++++ .../__tests__/datasource-basics.node.tsx | 31 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/desktop/flipper-plugin/src/state/datasource/DataSource.tsx b/desktop/flipper-plugin/src/state/datasource/DataSource.tsx index f2f57b502..12ea16b39 100644 --- a/desktop/flipper-plugin/src/state/datasource/DataSource.tsx +++ b/desktop/flipper-plugin/src/state/datasource/DataSource.tsx @@ -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) { this.dataUpdateQueue.push(event); // TODO: schedule diff --git a/desktop/flipper-plugin/src/state/datasource/__tests__/datasource-basics.node.tsx b/desktop/flipper-plugin/src/state/datasource/__tests__/datasource-basics.node.tsx index ecc7dab98..50868d6d5 100644 --- a/desktop/flipper-plugin/src/state/datasource/__tests__/datasource-basics.node.tsx +++ b/desktop/flipper-plugin/src/state/datasource/__tests__/datasource-basics.node.tsx @@ -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([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([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]); +});