fix: guard against duplicate TouchBarItem IDs (#22644)

This commit is contained in:
Erick Zhao
2020-03-11 18:48:17 -07:00
committed by GitHub
parent a76ea622b9
commit 1114954cbf
2 changed files with 21 additions and 1 deletions

View File

@@ -51,13 +51,25 @@ class TouchBar extends EventEmitter {
item.child.ordereredItems.forEach(registerItem)
}
}
const idSet = new Set()
items.forEach((item) => {
if (!(item instanceof TouchBarItem)) {
throw new Error('Each item must be an instance of TouchBarItem')
}
if (!idSet.has(item.id)) {
idSet.add(item.id)
} else {
throw new Error('Cannot add a single instance of TouchBarItem multiple times in a TouchBar')
}
})
// register in separate loop after all items are validated
for (const item of items) {
this.ordereredItems.push(item)
registerItem(item)
})
}
}
set escapeItem (item) {

View File

@@ -32,6 +32,14 @@ describe('TouchBar module', () => {
}).to.throw('Escape item must be an instance of TouchBarItem')
})
it('throws an error if the same TouchBarItem is added multiple times', () => {
expect(() => {
const item = new TouchBarLabel({ label: 'Label' })
const touchBar = new TouchBar({ items: [item, item] })
touchBar.toString()
}).to.throw('Cannot add a single instance of TouchBarItem multiple times in a TouchBar')
})
describe('BrowserWindow behavior', () => {
let window: BrowserWindow