Skip to content

Commit a64f9db

Browse files
author
Brandon Tilley
committed
Replace event bus with property-based function references
1 parent 2145be5 commit a64f9db

6 files changed

Lines changed: 30 additions & 52 deletions

File tree

src/js/client/bus.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/js/client/status_bar.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var bus = require('./bus');
2-
31
module.exports = React.createClass({
42
render: function() {
53
return (
@@ -14,6 +12,6 @@ module.exports = React.createClass({
1412
},
1513

1614
onChange: function(evt) {
17-
bus.emit('change:searchAllWindows', evt.target.checked);
15+
this.props.changeSearchAllWindows(evt.target.checked);
1816
}
1917
});

src/js/client/tab_item.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var bus = require('./bus');
21
var stringSpanner = require('./string_spanner');
32

43
var MATCH_START = '<span class="match">';
@@ -39,10 +38,10 @@ module.exports = React.createClass({
3938
},
4039

4140
onMouseEnter: function(evt) {
42-
bus.emit('change:selected', this.props.tab);
41+
this.props.changeSelected(this.props.tab);
4342
},
4443

4544
onClick: function(evt) {
46-
bus.emit('action:activate');
45+
this.props.activateSelected();
4746
}
4847
});

src/js/client/tab_list.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ module.exports = React.createClass({
77
<ul>
88
{this.props.tabs.map(function(tab, i) {
99
return <TabItem tab={tab} key={tab.id} filter={this.props.filter}
10-
selected={this.props.selectedTab === tab} />;
10+
selected={this.props.selectedTab === tab}
11+
changeSelected={this.props.changeSelected}
12+
activateSelected={this.props.activateSelected} />;
1113
}.bind(this))}
1214
</ul>
1315
/* jshint ignore:end */

src/js/client/tab_search_box.jsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var bus = require('./bus');
2-
31
var KEY_ENTER = 13;
42
var KEY_ESC = 27;
53
var KEY_UP = 38;
@@ -22,24 +20,24 @@ module.exports = React.createClass({
2220
onKeydown: function(evt) {
2321
switch (evt.which) {
2422
case KEY_ESC:
25-
bus.emit('exit');
23+
this.props.exit();
2624
break;
2725
case KEY_ENTER:
28-
bus.emit('action:activate');
26+
this.props.activateSelected();
2927
break;
3028
case KEY_UP:
31-
bus.emit('select:previous');
29+
this.props.modifySelected(-1);
3230
evt.preventDefault();
3331
break;
3432
case KEY_DOWN:
35-
bus.emit('select:next');
33+
this.props.modifySelected(1);
3634
evt.preventDefault();
3735
break;
3836
}
3937
},
4038

4139
onChange: function(evt) {
4240
if (event.target.value !== this.props.filter)
43-
bus.emit('change:filter', event.target.value);
41+
this.props.changeFilter(event.target.value);
4442
}
4543
});

src/js/client/tab_switcher.jsx

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var bus = require('./bus');
21
var stringScore = require('../../../vendor/string_score');
32
var tabBroker = require('./tab_broker')(chrome);
43
var tabFilter = require('./tab_filter')(stringScore);
@@ -7,24 +6,6 @@ var TabSearchBox = require('./tab_search_box.jsx');
76
var TabList = require('./tab_list.jsx');
87
var StatusBar = require('./status_bar.jsx');
98

10-
/**
11-
* TabSwitcher is the main component of our application. It contains
12-
* all the state, and all other components communicate their intent
13-
* to change that state via events on the bus (which is a Node.js
14-
* EventEmitter). All child components receive their data via properties.
15-
*
16-
* Bus events:
17-
* - change:searchAllWindows(boolean) - the 'search all windows'
18-
* option was toggled
19-
* - change:filter(string) - the filter text was changed
20-
* - change:selected(tab) - the selected tab was changed
21-
* - action:activate - the user wishes to swich to the currently
22-
* selected tab
23-
* - select:previous - the tab above the selected one should be selected
24-
* - select:next - the tab below the selected one should be selected
25-
* - exit - the extension should exit, closing the window
26-
*/
27-
289
module.exports = React.createClass({
2910
getInitialState: function() {
3011
// TODO: move into a model
@@ -44,26 +25,29 @@ module.exports = React.createClass({
4425
},
4526

4627
componentDidMount: function() {
47-
bus.on('change:filter', this.changeFilter);
48-
bus.on('change:selected', this.changeSelected);
49-
bus.on('change:searchAllWindows', this.changeSearchAllWindows);
50-
bus.on('select:previous', this.moveSelection.bind(this, -1));
51-
bus.on('select:next', this.moveSelection.bind(this, 1));
52-
bus.on('action:activate', this.activateSelection);
53-
bus.on('exit', this.close);
5428
window.onblur = this.close;
55-
5629
this.refreshTabs();
5730
},
5831

5932
render: function() {
6033
return (
6134
/* jshint ignore:start */
6235
<div>
63-
<TabSearchBox filter={this.state.filter} />
64-
<TabList tabs={this.filteredTabs()} filter={this.state.filter}
65-
selectedTab={this.getSelected()} />
66-
<StatusBar searchAllWindows={this.state.searchAllWindows} />
36+
<TabSearchBox
37+
filter={this.state.filter}
38+
exit={this.close}
39+
changeFilter={this.changeFilter}
40+
activateSelected={this.activateSelected}
41+
modifySelected={this.modifySelected} />
42+
<TabList
43+
tabs={this.filteredTabs()}
44+
filter={this.state.filter}
45+
selectedTab={this.getSelected()}
46+
changeSelected={this.changeSelected}
47+
activateSelected={this.activateSelected} />
48+
<StatusBar
49+
searchAllWindows={this.state.searchAllWindows}
50+
changeSearchAllWindows={this.changeSearchAllWindows} />
6751
</div>
6852
/* jshint ignore:end */
6953
);
@@ -94,11 +78,11 @@ module.exports = React.createClass({
9478
return this.state.selected || this.filteredTabs()[0];
9579
},
9680

97-
activateSelection: function() {
81+
activateSelected: function() {
9882
var selected = this.getSelected();
9983
if (selected) {
10084
tabBroker.switchTo(selected);
101-
bus.emit('exit');
85+
this.close();
10286
}
10387
},
10488

@@ -110,7 +94,7 @@ module.exports = React.createClass({
11094
this.setState({selected: tab});
11195
},
11296

113-
moveSelection: function(change) {
97+
modifySelected: function(change) {
11498
var filteredTabs = this.filteredTabs();
11599
if (!filteredTabs.length) return;
116100

@@ -119,7 +103,7 @@ module.exports = React.createClass({
119103
if (newIndex < 0) newIndex = 0;
120104
if (newIndex >= filteredTabs.length) newIndex = filteredTabs.length - 1;
121105
var newTab = filteredTabs[newIndex];
122-
bus.emit('change:selected', newTab);
106+
this.changeSelected(newTab);
123107
},
124108

125109
changeSearchAllWindows: function(value) {

0 commit comments

Comments
 (0)