-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed views not reopening on restart by fixing PlatformEventEmitter b…
…ug (#947)
- Loading branch information
Showing
13 changed files
with
174 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lib/platform-bible-utils/src/platform-event-emitter.model.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import PlatformEventEmitter from './platform-event-emitter.model'; | ||
|
||
describe('unsubscribing', () => { | ||
it('does not prevent other subscribers from running when unsubscribing in a callback', () => { | ||
const shouldUnsubscribeSubscriptions = [false, true, false, true, true, false]; | ||
// Map of event number to which event subscription indices ran for that event number | ||
// Purposely making this an array, not a Set, to make sure we catch duplicate runs | ||
const subscriptionResults: { [eventNum: string]: number[] } = {}; | ||
|
||
const numEventsToEmit = 3; | ||
// Array of each event number that should have been run: [0, 1, 2, ..., numEventsToEdit - 1] | ||
const eventNumArray = [...Array(numEventsToEmit).keys()]; | ||
// Array of each event number that should have been run after unsubscribing | ||
// (basically eventNumArray without the first event) | ||
const [, ...eventNumArrayAfterUnsubscribing] = eventNumArray; | ||
let nextEventNum = 0; | ||
const emitter = new PlatformEventEmitter<number>(); | ||
const emitEvent = () => { | ||
emitter.emit(nextEventNum); | ||
nextEventNum += 1; | ||
}; | ||
const unsubscribers = shouldUnsubscribeSubscriptions.map((shouldUnsubscribe, i) => | ||
emitter.subscribe((eventNum) => { | ||
const subscriptionResultsForEventNum = subscriptionResults[eventNum] ?? []; | ||
if (!subscriptionResults[eventNum]) | ||
subscriptionResults[eventNum] = subscriptionResultsForEventNum; | ||
|
||
subscriptionResultsForEventNum.push(i); | ||
|
||
if (shouldUnsubscribe) unsubscribers[i](); | ||
}), | ||
); | ||
|
||
for (let i = 0; i < numEventsToEmit; i += 1) emitEvent(); | ||
|
||
// There should be results for each event that was run | ||
expect( | ||
Object.keys(subscriptionResults) | ||
.map((eventNumString) => parseInt(eventNumString, 10)) | ||
.sort(), | ||
).toEqual(eventNumArray); | ||
|
||
// All should have run the first time | ||
expect(subscriptionResults[0]).toEqual( | ||
shouldUnsubscribeSubscriptions.map((_shouldUnsubscribe, i) => i), | ||
); | ||
eventNumArrayAfterUnsubscribing.forEach((eventNum) => { | ||
// Only the `false` ones (didn't unsubscribe) should have run after the first time | ||
expect(subscriptionResults[eventNum]).toEqual( | ||
shouldUnsubscribeSubscriptions | ||
.map((shouldUnsubscribe, i) => (shouldUnsubscribe ? undefined : i)) | ||
.filter((i) => i !== undefined), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.