From 55c6841fae3e6a730f8448aafd0a5efd6217237a Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 1 Mar 2022 11:58:50 -0600 Subject: [PATCH] fileStorage/reducers: drop use of Set Using set causes problems with tooling that mocks the global app state, so stick with arrays. --- src/editor/sagas.test.ts | 6 +++--- src/editor/sagas.ts | 2 +- src/fileStorage/reducers.test.ts | 16 ++++++++-------- src/fileStorage/reducers.ts | 12 ++++++++---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/editor/sagas.test.ts b/src/editor/sagas.test.ts index afa73f0f..0ef67005 100644 --- a/src/editor/sagas.test.ts +++ b/src/editor/sagas.test.ts @@ -140,7 +140,7 @@ describe('setEditSession', () => { it('should wait for storage to be initialized', async () => { const mockEditor = mock(); const saga = new AsyncSaga(editor, { - fileStorage: { isInitialized: false, fileNames: new Set() }, + fileStorage: { isInitialized: false, fileNames: [] }, }); saga.put(setEditSession(mockEditor)); @@ -155,7 +155,7 @@ describe('setEditSession', () => { it('should load main.py', async () => { const mockEditor = mock(); const saga = new AsyncSaga(editor, { - fileStorage: { isInitialized: true, fileNames: new Set(['main.py']) }, + fileStorage: { isInitialized: true, fileNames: ['main.py'] }, }); saga.put(setEditSession(mockEditor)); @@ -175,7 +175,7 @@ describe('setEditSession', () => { it('should not raise error if main.py does not exist', async () => { const mockEditor = mock(); const saga = new AsyncSaga(editor, { - fileStorage: { isInitialized: true, fileNames: new Set() }, + fileStorage: { isInitialized: true, fileNames: [] }, }); saga.put(setEditSession(mockEditor)); diff --git a/src/editor/sagas.ts b/src/editor/sagas.ts index f977bdff..fababe90 100644 --- a/src/editor/sagas.ts +++ b/src/editor/sagas.ts @@ -115,7 +115,7 @@ function* handleSetEditSession(action: ReturnType): Gener const fileList = yield* select((s: RootState) => s.fileStorage.fileNames); - if (!fileList.has(currentFileName)) { + if (!fileList.includes(currentFileName)) { // The file doesn't exist in storage, so don't try to open it. yield* put(didSetEditSession(action.editSession)); return; diff --git a/src/fileStorage/reducers.test.ts b/src/fileStorage/reducers.test.ts index cc7e2017..402d3b88 100644 --- a/src/fileStorage/reducers.test.ts +++ b/src/fileStorage/reducers.test.ts @@ -14,7 +14,7 @@ type State = ReturnType; test('initial state', () => { expect(reducers(undefined, {} as AnyAction)).toMatchInlineSnapshot(` Object { - "fileNames": Set {}, + "fileNames": Array [], "isInitialized": false, } `); @@ -33,31 +33,31 @@ test('fileNames', () => { // initialization populates file list expect( reducers( - { fileNames: new Set() } as State, + { fileNames: [] as ReadonlyArray } as State, fileStorageDidInitialize([testFileName]), ).fileNames, - ).toEqual(new Set([testFileName])); + ).toEqual([testFileName]); // if item is not in set, add it expect( reducers( - { fileNames: new Set() } as State, + { fileNames: [] as ReadonlyArray } as State, fileStorageDidChangeItem(testFileName), ).fileNames, - ).toEqual(new Set([testFileName])); + ).toEqual([testFileName]); // if item is already in set, there should not be duplicates expect( reducers( - { fileNames: new Set([testFileName]) } as State, + { fileNames: [testFileName] as ReadonlyArray } as State, fileStorageDidChangeItem(testFileName), ).fileNames, - ).toEqual(new Set([testFileName])); + ).toEqual([testFileName]); // if item is in set, it should be removed expect( reducers( - { fileNames: new Set([testFileName]) } as State, + { fileNames: [testFileName] as ReadonlyArray } as State, fileStorageDidRemoveItem(testFileName), ).fileNames, ).not.toContain(testFileName); diff --git a/src/fileStorage/reducers.ts b/src/fileStorage/reducers.ts index 5986405a..ff1a73b4 100644 --- a/src/fileStorage/reducers.ts +++ b/src/fileStorage/reducers.ts @@ -16,17 +16,21 @@ const isInitialized: Reducer = (state = false, action) => { return state; }; -const fileNames: Reducer> = (state = new Set(), action) => { +const fileNames: Reducer> = (state = [], action) => { if (fileStorageDidInitialize.matches(action)) { - return new Set(action.fileNames); + return [...action.fileNames]; } if (fileStorageDidChangeItem.matches(action)) { - return new Set([...state, action.fileName]); + if (state.includes(action.fileName)) { + return state; + } + + return [...state, action.fileName]; } if (fileStorageDidRemoveItem.matches(action)) { - return new Set([...state].filter((value) => value !== action.fileName)); + return [...state].filter((value) => value !== action.fileName); } return state;