Improve performance of selecting scripts

Increase performance by only notifying GUI about changes in selection
when there really is a change. It removes extra processing from all
event listeners that act on selection state change.
This commit is contained in:
undergroundwires
2022-01-07 22:06:05 +01:00
parent 99fb4c73f5
commit 8e96c19126
2 changed files with 157 additions and 74 deletions

View File

@@ -109,6 +109,9 @@ export class UserSelection implements IUserSelection {
.getAllScripts() .getAllScripts()
.filter((script) => !this.scripts.exists(script.id)) .filter((script) => !this.scripts.exists(script.id))
.map((script) => new SelectedScript(script, false)); .map((script) => new SelectedScript(script, false));
if (scriptsToSelect.length === 0) {
return;
}
for (const script of scriptsToSelect) { for (const script of scriptsToSelect) {
this.scripts.addItem(script); this.scripts.addItem(script);
} }
@@ -116,6 +119,9 @@ export class UserSelection implements IUserSelection {
} }
public deselectAll(): void { public deselectAll(): void {
if (this.scripts.length === 0) {
return;
}
const selectedScriptIds = this.scripts.getItems().map((script) => script.id); const selectedScriptIds = this.scripts.getItems().map((script) => script.id);
for (const scriptId of selectedScriptIds) { for (const scriptId of selectedScriptIds) {
this.scripts.removeItem(scriptId); this.scripts.removeItem(scriptId);
@@ -127,20 +133,35 @@ export class UserSelection implements IUserSelection {
if (!scripts || scripts.length === 0) { if (!scripts || scripts.length === 0) {
throw new Error('Scripts are empty. Use deselectAll() if you want to deselect everything'); throw new Error('Scripts are empty. Use deselectAll() if you want to deselect everything');
} }
// Unselect from selected scripts let totalChanged = 0;
if (this.scripts.length !== 0) { totalChanged += this.unselectMissingWithoutNotifying(scripts);
this.scripts.getItems() totalChanged += this.selectNewWithoutNotifying(scripts);
.filter((existing) => !scripts.some((script) => existing.id === script.id)) if (totalChanged > 0) {
.map((script) => script.id)
.forEach((scriptId) => this.scripts.removeItem(scriptId));
}
// Select from unselected scripts
const unselectedScripts = scripts
.filter((script) => !this.scripts.exists(script.id))
.map((script) => new SelectedScript(script, false));
for (const toSelect of unselectedScripts) {
this.scripts.addItem(toSelect);
}
this.changed.notify(this.scripts.getItems()); this.changed.notify(this.scripts.getItems());
} }
} }
private unselectMissingWithoutNotifying(scripts: readonly IScript[]): number {
if (this.scripts.length === 0 || scripts.length === 0) {
return 0;
}
const existingItems = this.scripts.getItems();
const missingIds = existingItems
.filter((existing) => !scripts.some((script) => existing.id === script.id))
.map((script) => script.id);
for (const id of missingIds) {
this.scripts.removeItem(id);
}
return missingIds.length;
}
private selectNewWithoutNotifying(scripts: readonly IScript[]): number {
const unselectedScripts = scripts
.filter((script) => !this.scripts.exists(script.id))
.map((script) => new SelectedScript(script, false));
for (const newScript of unselectedScripts) {
this.scripts.addItem(newScript);
}
return unselectedScripts.length;
}
}

View File

@@ -38,7 +38,8 @@ describe('UserSelection', () => {
.expectFinalScripts(scripts); .expectFinalScripts(scripts);
}); });
}); });
describe('deselectAll removes all items', () => { describe('deselectAll', () => {
describe('removes existing items', () => {
// arrange // arrange
const allScripts = [ const allScripts = [
new SelectedScriptStub('s1', false), new SelectedScriptStub('s1', false),
@@ -61,33 +62,20 @@ describe('UserSelection', () => {
.expectFinalScripts([]) .expectFinalScripts([])
.expectFinalScriptsInEvent(0, []); .expectFinalScriptsInEvent(0, []);
}); });
describe('selectOnly selects expected', () => { describe('does not notify if nothing is selected', () => {
// arrange
const allScripts = [
new SelectedScriptStub('s1', false),
new SelectedScriptStub('s2', false),
new SelectedScriptStub('s3', false),
new SelectedScriptStub('s4', false),
];
const selectedScripts = allScripts.filter(
(s) => ['s1', 's2', 's3'].includes(s.id),
);
const scriptsToSelect = allScripts.filter(
(s) => ['s2', 's3', 's4'].includes(s.id),
);
new UserSelectionTestRunner() new UserSelectionTestRunner()
.withSelectedScripts(selectedScripts) // arrange
.withCategory(1, allScripts.map((s) => s.script)) .withSelectedScripts([])
// act // act
.run((sut) => { .run((sut) => {
sut.selectOnly(scriptsToSelect.map((s) => s.script)); sut.deselectAll();
}) })
// assert // assert
.expectTotalFiredEvents(1) .expectTotalFiredEvents(0);
.expectFinalScripts(scriptsToSelect)
.expectFinalScriptsInEvent(0, scriptsToSelect);
}); });
describe('selectAll selects as expected', () => { });
describe('selectAll', () => {
describe('selects as expected', () => {
// arrange // arrange
const expected = [ const expected = [
new SelectedScriptStub('s1', false), new SelectedScriptStub('s1', false),
@@ -105,6 +93,80 @@ describe('UserSelection', () => {
.expectFinalScripts(expected) .expectFinalScripts(expected)
.expectFinalScriptsInEvent(0, expected); .expectFinalScriptsInEvent(0, expected);
}); });
describe('does not notify if nothing new is selected', () => {
const allScripts = [new ScriptStub('s1'), new ScriptStub('s2')];
const selectedScripts = allScripts.map((s) => s.toSelectedScript(false));
new UserSelectionTestRunner()
// arrange
.withSelectedScripts(selectedScripts)
.withCategory(0, allScripts)
// act
.run((sut) => {
sut.selectAll();
})
// assert
.expectTotalFiredEvents(0);
});
});
describe('selectOnly', () => {
describe('selects as expected', () => {
// arrange
const allScripts = [
new SelectedScriptStub('s1', false),
new SelectedScriptStub('s2', false),
new SelectedScriptStub('s3', false),
new SelectedScriptStub('s4', false),
];
const getScripts = (...ids: string[]) => allScripts.filter((s) => ids.includes(s.id));
const testCases = [
{
name: 'adds as expected',
preSelected: getScripts('s1'),
toSelect: getScripts('s1', 's2'),
},
{
name: 'removes as expected',
preSelected: getScripts('s1', 's2', 's3'),
toSelect: getScripts('s1'),
},
{
name: 'adds and removes as expected',
preSelected: getScripts('s1', 's2', 's3'),
toSelect: getScripts('s2', 's3', 's4'),
},
];
for (const testCase of testCases) {
describe(testCase.name, () => {
new UserSelectionTestRunner()
.withSelectedScripts(testCase.preSelected)
.withCategory(1, testCase.toSelect.map((s) => s.script))
// act
.run((sut) => {
sut.selectOnly(testCase.toSelect.map((s) => s.script));
})
// assert
.expectTotalFiredEvents(1)
.expectFinalScripts(testCase.toSelect)
.expectFinalScriptsInEvent(0, testCase.toSelect);
});
}
});
describe('does not notify if selection does not change', () => {
const allScripts = [new ScriptStub('s1'), new ScriptStub('s2'), new ScriptStub('s3')];
const toSelect = [allScripts[0], allScripts[1]];
const preSelected = toSelect.map((s) => s.toSelectedScript(false));
new UserSelectionTestRunner()
// arrange
.withSelectedScripts(preSelected)
.withCategory(0, allScripts)
// act
.run((sut) => {
sut.selectOnly(toSelect);
})
// assert
.expectTotalFiredEvents(0);
});
});
describe('addOrUpdateSelectedScript', () => { describe('addOrUpdateSelectedScript', () => {
describe('adds when item does not exist', () => { describe('adds when item does not exist', () => {
// arrange // arrange