mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: guard against duplicate TouchBarItem IDs (#22644)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user