Skip to content

Commit e8f5f97

Browse files
authored
Remove everGotGamepadEvent flag and gamepadconnected / gamepaddisconnected (#3189)
handlers (fix #2769) We moved away from using gamepadconnected / gamepaddisconnected events from the rest of the controls. We forgot to remove the handlers from daydream-controls. This was causing problems where sometimes gamepadconnected was fired before tracked-controls had a chance to update controllers and emit controllersupdated. The flag everGotGamepadEvent was set to true leaving onControllersUpdate without effect after tracked-controls had updated the controllers list. The timing of the gamepadconnected event seems to have changed in Chrome Daydream and this problem had not manifested before.
1 parent b52b859 commit e8f5f97

File tree

7 files changed

+6
-139
lines changed

7 files changed

+6
-139
lines changed

src/components/daydream-controls.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ module.exports.Component = registerComponent('daydream-controls', {
4545
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
4646
this.removeControllersUpdateListener = bind(this.removeControllersUpdateListener, this);
4747
this.onAxisMoved = bind(this.onAxisMoved, this);
48-
this.onGamepadConnectionEvent = bind(this.onGamepadConnectionEvent, this);
4948
},
5049

5150
init: function () {
@@ -58,7 +57,6 @@ module.exports.Component = registerComponent('daydream-controls', {
5857
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
5958
this.onAxisMoved = bind(this.onAxisMoved, this);
6059
this.controllerPresent = false;
61-
this.everGotGamepadEvent = false;
6260
this.lastControllerCheck = 0;
6361
this.bindMethods();
6462
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // To allow mock.
@@ -93,27 +91,14 @@ module.exports.Component = registerComponent('daydream-controls', {
9391
this.checkControllerPresentAndSetup(this, GAMEPAD_ID_PREFIX, {hand: this.data.hand});
9492
},
9593

96-
onGamepadConnectionEvent: function (evt) {
97-
this.everGotGamepadEvent = true;
98-
// Due to an apparent bug in FF Nightly
99-
// where only one gamepadconnected / disconnected event is fired,
100-
// which makes it difficult to handle in individual controller entities,
101-
// we no longer remove the controllersupdate listener as a result.
102-
this.checkIfControllerPresent();
103-
},
104-
10594
play: function () {
10695
this.checkIfControllerPresent();
10796
this.addControllersUpdateListener();
108-
window.addEventListener('gamepadconnected', this.onGamepadConnectionEvent, false);
109-
window.addEventListener('gamepaddisconnected', this.onGamepadConnectionEvent, false);
11097
},
11198

11299
pause: function () {
113100
this.removeEventListeners();
114101
this.removeControllersUpdateListener();
115-
window.removeEventListener('gamepadconnected', this.onGamepadConnectionEvent, false);
116-
window.removeEventListener('gamepaddisconnected', this.onGamepadConnectionEvent, false);
117102
},
118103

119104
injectTrackedControls: function () {
@@ -141,7 +126,7 @@ module.exports.Component = registerComponent('daydream-controls', {
141126
},
142127

143128
onControllersUpdate: function () {
144-
if (!this.everGotGamepadEvent) { this.checkIfControllerPresent(); }
129+
this.checkIfControllerPresent();
145130
},
146131

147132
onModelLoaded: function (evt) {

src/components/gearvr-controls.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ module.exports.Component = registerComponent('gearvr-controls', {
5656
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
5757
this.onAxisMoved = bind(this.onAxisMoved, this);
5858
this.controllerPresent = false;
59-
this.everGotGamepadEvent = false;
6059
this.lastControllerCheck = 0;
6160
this.bindMethods();
6261
this.checkControllerPresentAndSetup = checkControllerPresentAndSetup; // To allow mock.

tests/components/daydream-controls.test.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global assert, process, setup, suite, test, Event */
1+
/* global assert, process, setup, suite, test */
22
var entityFactory = require('../helpers').entityFactory;
33

44
suite('daydream-controls', function () {
@@ -177,34 +177,6 @@ suite('daydream-controls', function () {
177177
});
178178
});
179179

180-
suite('gamepaddisconnected', function () {
181-
/**
182-
* Due to an apparent bug in FF Nightly
183-
* where only one gamepadconnected / disconnected event is fired,
184-
* which makes it difficult to handle in individual controller entities,
185-
* we no longer remove the controllersupdate listener as a result.
186-
*/
187-
test('checks present on gamepaddisconnected', function () {
188-
var el = this.el;
189-
var component = el.components['daydream-controls'];
190-
var checkIfControllerPresentSpy = this.sinon.spy(component, 'checkIfControllerPresent');
191-
// Because checkIfControllerPresent may be used in bound form, bind and reinstall.
192-
component.checkIfControllerPresent = component.checkIfControllerPresent.bind(component);
193-
component.pause();
194-
component.play();
195-
196-
el.sceneEl.systems['tracked-controls'].controllers = [];
197-
198-
// Reset everGotGamepadEvent so we don't think we've looked before.
199-
delete component.everGotGamepadEvent;
200-
201-
// Fire emulated gamepaddisconnected event.
202-
window.dispatchEvent(new Event('gamepaddisconnected'));
203-
204-
assert.ok(checkIfControllerPresentSpy.called);
205-
});
206-
});
207-
208180
suite('armModel', function () {
209181
function makePresent (el) {
210182
var component = el.components['daydream-controls'];

tests/components/gearvr-controls.test.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global assert, process, setup, suite, test, Event */
1+
/* global assert, process, setup, suite, test */
22
var entityFactory = require('../helpers').entityFactory;
33

44
suite('gearvr-controls', function () {
@@ -191,31 +191,6 @@ suite('gearvr-controls', function () {
191191
});
192192
});
193193

194-
suite('gamepaddisconnected', function () {
195-
/**
196-
* Due to an apparent bug in FF Nightly
197-
* where only one gamepadconnected / disconnected event is fired,
198-
* which makes it difficult to handle in individual controller entities,
199-
* we no longer remove the controllersupdate listener as a result.
200-
*/
201-
test('check present on gamepaddisconnected', function () {
202-
var el = this.el;
203-
var component = el.components['gearvr-controls'];
204-
var checkIfControllerPresentSpy = this.sinon.spy(component, 'checkIfControllerPresent');
205-
// Because checkIfControllerPresent may be used in bound form, bind and reinstall.
206-
component.checkIfControllerPresent = component.checkIfControllerPresent.bind(component);
207-
component.pause();
208-
component.play();
209-
210-
el.sceneEl.systems['tracked-controls'].controllers = [];
211-
// Reset everGotGamepadEvent so we don't think we've looked before.
212-
delete component.everGotGamepadEvent;
213-
// Fire emulated gamepaddisconnected event.
214-
window.dispatchEvent(new Event('gamepaddisconnected'));
215-
assert.ok(checkIfControllerPresentSpy.called);
216-
});
217-
});
218-
219194
suite('armModel', function () {
220195
function makePresent (el) {
221196
var component = el.components['gearvr-controls'];

tests/components/oculus-touch-controls.test.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global assert, process, setup, suite, test, Event */
1+
/* global assert, process, setup, suite, test */
22
var entityFactory = require('../helpers').entityFactory;
33

44
suite('oculus-touch-controls', function () {
@@ -149,26 +149,4 @@ suite('oculus-touch-controls', function () {
149149
el.emit('buttonchanged', {id: 1, state: {value: 0.5, pressed: true, touched: true}});
150150
});
151151
});
152-
153-
suite('gamepaddisconnected', function () {
154-
/**
155-
* In FF Nightly, only one gamepadconnected/disconnected event is fired,
156-
* which makes it difficult to handle in individual controller entities.
157-
* We no longer remove the controllersupdate listener as a result.
158-
*/
159-
test('checks if present on gamepaddisconnected event', function () {
160-
var checkIfControllerPresentSpy = this.sinon.spy(component, 'checkIfControllerPresent');
161-
// Because checkIfControllerPresent may be used in bound form, bind and reinstall.
162-
component.checkIfControllerPresent = component.checkIfControllerPresent.bind(component);
163-
component.pause();
164-
component.play();
165-
// Mock isControllerPresent to return false.
166-
// Reset everGotGamepadEvent so we don't think we've looked before.
167-
delete component.everGotGamepadEvent;
168-
// Fire emulated gamepaddisconnected event.
169-
window.dispatchEvent(new Event('gamepaddisconnected'));
170-
171-
assert.ok(checkIfControllerPresentSpy.called);
172-
});
173-
});
174152
});

tests/components/vive-controls.test.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global assert, Event, process, setup, suite, test, THREE */
1+
/* global assert, process, setup, suite, test, THREE */
22
var entityFactory = require('../helpers').entityFactory;
33

44
suite('vive-controls', function () {
@@ -177,28 +177,6 @@ suite('vive-controls', function () {
177177
});
178178
});
179179

180-
suite('gamepaddisconnected', function () {
181-
/*
182-
Apparent bug in FF Nightly where only one gamepadconnected/disconnected event is
183-
fired which makes it difficult to handle in individual controller entities. We no
184-
longer remove the controllersupdate listener as a result.
185-
*/
186-
test('checks if controller present on gamepaddisconnected', function () {
187-
var checkIfControllerPresentSpy = this.sinon.spy(component, 'checkIfControllerPresent');
188-
// Because checkIfControllerPresent may be used in bound form, bind and reinstall.
189-
component.checkIfControllerPresent = component.checkIfControllerPresent.bind(component);
190-
component.pause();
191-
component.play();
192-
193-
controlsSystem.controllers = [];
194-
// Reset everGotGamepadEvent to mock not looked before.
195-
delete component.everGotGamepadEvent;
196-
// Fire emulated gamepaddisconnected event.
197-
window.dispatchEvent(new Event('gamepaddisconnected'));
198-
assert.ok(checkIfControllerPresentSpy.called);
199-
});
200-
});
201-
202180
suite('model', function () {
203181
test.skip('loads', function (done) {
204182
component.addEventListeners();

tests/components/windows-motion-controls.test.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global assert, process, setup, suite, test, THREE, Event */
1+
/* global assert, process, setup, suite, test, THREE */
22
var entityFactory = require('../helpers').entityFactory;
33

44
suite('windows-motion-controls', function () {
@@ -521,26 +521,6 @@ suite('windows-motion-controls', function () {
521521
}
522522
});
523523

524-
suite('gamepaddisconnected', function () {
525-
test('checks if present on gamepaddisconnected event', function (done) {
526-
var checkIfControllerPresentSpy = this.sinon.spy(component, 'checkIfControllerPresent');
527-
// Because checkIfControllerPresent may be used in bound form, using this.bind,
528-
// we need to re-create the binding and re-attach the event listeners for our spy to work
529-
component.checkIfControllerPresent = component.checkIfControllerPresent.bind(component);
530-
// Pause and resume to remove and re-attach the event listeners, with our spy.
531-
component.pause();
532-
component.play();
533-
// Reset everGotGamepadEvent so we don't think we've looked before.
534-
delete component.everGotGamepadEvent;
535-
// Fire emulated gamepaddisconnected event.
536-
window.dispatchEvent(new Event('gamepaddisconnected'));
537-
538-
assert.ok(checkIfControllerPresentSpy.called, 'checkIfControllerPresent not called in response to gamepaddisconnected');
539-
540-
setTimeout(() => { done(); });
541-
});
542-
});
543-
544524
suite('setModelVisibility', function () {
545525
test('shows model', function () {
546526
var component = el.components['windows-motion-controls'];

0 commit comments

Comments
 (0)