Register item after validating

This commit is contained in:
Kevin Sawicki 2017-03-02 09:30:21 -08:00
parent 93bbe8e70b
commit 8d716e8b17

View file

@ -3,7 +3,6 @@ const {EventEmitter} = require('events')
let nextItemID = 1 let nextItemID = 1
class TouchBar extends EventEmitter { class TouchBar extends EventEmitter {
// Bind a touch bar to a window // Bind a touch bar to a window
static _setOnWindow (touchBar, window) { static _setOnWindow (touchBar, window) {
if (window._touchBar != null) { if (window._touchBar != null) {
@ -31,6 +30,7 @@ class TouchBar extends EventEmitter {
this.windowListeners = {} this.windowListeners = {}
this.items = {} this.items = {}
this.ordereredItems = [] this.ordereredItems = []
const registerItem = (item) => { const registerItem = (item) => {
this.items[item.id] = item this.items[item.id] = item
item.on('change', () => { item.on('change', () => {
@ -41,15 +41,14 @@ class TouchBar extends EventEmitter {
} }
} }
items.forEach((item) => { items.forEach((item) => {
this.ordereredItems.push(item)
if (!(item instanceof TouchBarItem)) { if (!(item instanceof TouchBarItem)) {
throw new Error('Each item must be an instance of a TouchBarItem') throw new Error('Each item must be an instance of a TouchBarItem')
} }
this.ordereredItems.push(item)
registerItem(item) registerItem(item)
}) })
} }
// Called by BrowserWindow.setTouchBar
_addToWindow (window) { _addToWindow (window) {
const {id} = window const {id} = window
@ -84,7 +83,6 @@ class TouchBar extends EventEmitter {
window._setTouchBarItems(this.ordereredItems) window._setTouchBarItems(this.ordereredItems)
} }
// Called by BrowserWindow.setTouchBar
_removeFromWindow (window) { _removeFromWindow (window) {
const removeListeners = this.windowListeners[window.id] const removeListeners = this.windowListeners[window.id]
if (removeListeners != null) removeListeners() if (removeListeners != null) removeListeners()