Skip to content

Commit bc3461e

Browse files
authored
Use native bind instead of custom implementation (#5461)
Co-authored-by: Noeri Huisman <[email protected]>
1 parent 54bd997 commit bc3461e

28 files changed

+115
-153
lines changed

docs/core/component.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ AFRAME.registerComponent('cursor', {
287287
// Set up initial state and variables.
288288
this.intersection = null;
289289
// Bind methods.
290-
this.onIntersection = AFRAME.utils.bind(this.onIntersection, this);
290+
this.onIntersection = this.onIntersection.bind(this);
291291
// Attach event listener.
292292
this.el.addEventListener('raycaster-intersection', this.onIntersection);
293293
}

src/components/cursor.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
var registerComponent = require('../core/component').registerComponent;
33
var utils = require('../utils/');
44

5-
var bind = utils.bind;
6-
75
var EVENTS = {
86
CLICK: 'click',
97
FUSING: 'fusing',
@@ -76,12 +74,12 @@ module.exports.Component = registerComponent('cursor', {
7674
this.intersectedEventDetail = {cursorEl: this.el};
7775

7876
// Bind methods.
79-
this.onCursorDown = bind(this.onCursorDown, this);
80-
this.onCursorUp = bind(this.onCursorUp, this);
81-
this.onIntersection = bind(this.onIntersection, this);
82-
this.onIntersectionCleared = bind(this.onIntersectionCleared, this);
83-
this.onMouseMove = bind(this.onMouseMove, this);
84-
this.onEnterVR = bind(this.onEnterVR, this);
77+
this.onCursorDown = this.onCursorDown.bind(this);
78+
this.onCursorUp = this.onCursorUp.bind(this);
79+
this.onIntersection = this.onIntersection.bind(this);
80+
this.onIntersectionCleared = this.onIntersectionCleared.bind(this);
81+
this.onMouseMove = this.onMouseMove.bind(this);
82+
this.onEnterVR = this.onEnterVR.bind(this);
8583
},
8684

8785
update: function (oldData) {

src/components/generic-tracked-controller-controls.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var registerComponent = require('../core/component').registerComponent;
2-
var bind = require('../utils/bind');
32

43
var trackedControlsUtils = require('../utils/tracked-controls');
54
var checkControllerPresentAndSetup = trackedControlsUtils.checkControllerPresentAndSetup;
@@ -51,15 +50,15 @@ module.exports.Component = registerComponent('generic-tracked-controller-control
5150
mapping: INPUT_MAPPING,
5251

5352
bindMethods: function () {
54-
this.onControllersUpdate = bind(this.onControllersUpdate, this);
55-
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
56-
this.removeControllersUpdateListener = bind(this.removeControllersUpdateListener, this);
57-
this.onAxisMoved = bind(this.onAxisMoved, this);
53+
this.onControllersUpdate = this.onControllersUpdate.bind(this);
54+
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
55+
this.removeControllersUpdateListener = this.removeControllersUpdateListener.bind(this);
56+
this.onAxisMoved = this.onAxisMoved.bind(this);
5857
},
5958

6059
init: function () {
6160
var self = this;
62-
this.onButtonChanged = bind(this.onButtonChanged, this);
61+
this.onButtonChanged = this.onButtonChanged.bind(this);
6362
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self); };
6463
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self); };
6564
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self); };

src/components/hand-tracking-controls.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* global THREE, XRHand */
22
var registerComponent = require('../core/component').registerComponent;
3-
var bind = require('../utils/bind');
43

54
var AEntity = require('../core/a-entity').AEntity;
65

@@ -58,9 +57,9 @@ module.exports.Component = registerComponent('hand-tracking-controls', {
5857
},
5958

6059
bindMethods: function () {
61-
this.onControllersUpdate = bind(this.onControllersUpdate, this);
62-
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
63-
this.removeControllersUpdateListener = bind(this.removeControllersUpdateListener, this);
60+
this.onControllersUpdate = this.onControllersUpdate.bind(this);
61+
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
62+
this.removeControllersUpdateListener = this.removeControllersUpdateListener.bind(this);
6463
},
6564

6665
addEventListeners: function () {

src/components/hp-mixed-reality-controls.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var bind = require('../utils/bind');
21
var registerComponent = require('../core/component').registerComponent;
32
var THREE = require('../lib/three');
43

@@ -55,7 +54,7 @@ module.exports.Component = registerComponent('hp-mixed-reality-controls', {
5554
var self = this;
5655
this.controllerPresent = false;
5756
this.lastControllerCheck = 0;
58-
this.onButtonChanged = bind(this.onButtonChanged, this);
57+
this.onButtonChanged = this.onButtonChanged.bind(this);
5958
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self, self.data.hand); };
6059
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self, self.data.hand); };
6160
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self, self.data.hand); };
@@ -81,11 +80,11 @@ module.exports.Component = registerComponent('hp-mixed-reality-controls', {
8180
},
8281

8382
bindMethods: function () {
84-
this.onModelLoaded = bind(this.onModelLoaded, this);
85-
this.onControllersUpdate = bind(this.onControllersUpdate, this);
86-
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
87-
this.removeControllersUpdateListener = bind(this.removeControllersUpdateListener, this);
88-
this.onAxisMoved = bind(this.onAxisMoved, this);
83+
this.onModelLoaded = this.onModelLoaded.bind(this);
84+
this.onControllersUpdate = this.onControllersUpdate.bind(this);
85+
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
86+
this.removeControllersUpdateListener = this.removeControllersUpdateListener.bind(this);
87+
this.onAxisMoved = this.onAxisMoved.bind(this);
8988
},
9089

9190
addEventListeners: function () {

src/components/light.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var bind = require('../utils/bind');
21
var utils = require('../utils');
32
var diff = utils.diff;
43
var debug = require('../utils/debug');
@@ -102,7 +101,7 @@ module.exports.Component = registerComponent('light', {
102101
if (value.hasLoaded) {
103102
self.onSetTarget(value, light);
104103
} else {
105-
value.addEventListener('loaded', bind(self.onSetTarget, self, value, light));
104+
value.addEventListener('loaded', self.onSetTarget.bind(self, value, light));
106105
}
107106
}
108107
break;
@@ -299,7 +298,7 @@ module.exports.Component = registerComponent('light', {
299298
if (target.hasLoaded) {
300299
this.onSetTarget(target, light);
301300
} else {
302-
target.addEventListener('loaded', bind(this.onSetTarget, this, target, light));
301+
target.addEventListener('loaded', this.onSetTarget.bind(this, target, light));
303302
}
304303
}
305304
return light;
@@ -320,7 +319,7 @@ module.exports.Component = registerComponent('light', {
320319
if (target.hasLoaded) {
321320
this.onSetTarget(target, light);
322321
} else {
323-
target.addEventListener('loaded', bind(this.onSetTarget, this, target, light));
322+
target.addEventListener('loaded', this.onSetTarget.bind(this, target, light));
324323
}
325324
}
326325
return light;

src/components/look-controls.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
var registerComponent = require('../core/component').registerComponent;
33
var THREE = require('../lib/three');
44
var utils = require('../utils/');
5-
var bind = utils.bind;
65

76
// To avoid recalculation at every mouse movement tick
87
var PI_2 = Math.PI / 2;
@@ -118,16 +117,16 @@ module.exports.Component = registerComponent('look-controls', {
118117
},
119118

120119
bindMethods: function () {
121-
this.onMouseDown = bind(this.onMouseDown, this);
122-
this.onMouseMove = bind(this.onMouseMove, this);
123-
this.onMouseUp = bind(this.onMouseUp, this);
124-
this.onTouchStart = bind(this.onTouchStart, this);
125-
this.onTouchMove = bind(this.onTouchMove, this);
126-
this.onTouchEnd = bind(this.onTouchEnd, this);
127-
this.onEnterVR = bind(this.onEnterVR, this);
128-
this.onExitVR = bind(this.onExitVR, this);
129-
this.onPointerLockChange = bind(this.onPointerLockChange, this);
130-
this.onPointerLockError = bind(this.onPointerLockError, this);
120+
this.onMouseDown = this.onMouseDown.bind(this);
121+
this.onMouseMove = this.onMouseMove.bind(this);
122+
this.onMouseUp = this.onMouseUp.bind(this);
123+
this.onTouchStart = this.onTouchStart.bind(this);
124+
this.onTouchMove = this.onTouchMove.bind(this);
125+
this.onTouchEnd = this.onTouchEnd.bind(this);
126+
this.onEnterVR = this.onEnterVR.bind(this);
127+
this.onExitVR = this.onExitVR.bind(this);
128+
this.onPointerLockChange = this.onPointerLockChange.bind(this);
129+
this.onPointerLockError = this.onPointerLockError.bind(this);
131130
},
132131

133132
/**
@@ -150,7 +149,7 @@ module.exports.Component = registerComponent('look-controls', {
150149

151150
// Wait for canvas to load.
152151
if (!canvasEl) {
153-
sceneEl.addEventListener('render-target-loaded', bind(this.addEventListeners, this));
152+
sceneEl.addEventListener('render-target-loaded', this.addEventListeners.bind(this));
154153
return;
155154
}
156155

src/components/magicleap-controls.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var bind = require('../utils/bind');
21
var registerComponent = require('../core/component').registerComponent;
32

43
var trackedControlsUtils = require('../utils/tracked-controls');
@@ -50,7 +49,7 @@ module.exports.Component = registerComponent('magicleap-controls', {
5049
var self = this;
5150
this.controllerPresent = false;
5251
this.lastControllerCheck = 0;
53-
this.onButtonChanged = bind(this.onButtonChanged, this);
52+
this.onButtonChanged = this.onButtonChanged.bind(this);
5453
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self); };
5554
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self); };
5655
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
@@ -76,11 +75,11 @@ module.exports.Component = registerComponent('magicleap-controls', {
7675
},
7776

7877
bindMethods: function () {
79-
this.onModelLoaded = bind(this.onModelLoaded, this);
80-
this.onControllersUpdate = bind(this.onControllersUpdate, this);
81-
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
82-
this.removeControllersUpdateListener = bind(this.removeControllersUpdateListener, this);
83-
this.onAxisMoved = bind(this.onAxisMoved, this);
78+
this.onModelLoaded = this.onModelLoaded.bind(this);
79+
this.onControllersUpdate = this.onControllersUpdate.bind(this);
80+
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
81+
this.removeControllersUpdateListener = this.removeControllersUpdateListener.bind(this);
82+
this.onAxisMoved = this.onAxisMoved.bind(this);
8483
},
8584

8685
addEventListeners: function () {

src/components/oculus-go-controls.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var registerComponent = require('../core/component').registerComponent;
2-
var bind = require('../utils/bind');
32

43
var trackedControlsUtils = require('../utils/tracked-controls');
54
var checkControllerPresentAndSetup = trackedControlsUtils.checkControllerPresentAndSetup;
@@ -67,16 +66,16 @@ module.exports.Component = registerComponent('oculus-go-controls', {
6766
mapping: INPUT_MAPPING,
6867

6968
bindMethods: function () {
70-
this.onModelLoaded = bind(this.onModelLoaded, this);
71-
this.onControllersUpdate = bind(this.onControllersUpdate, this);
72-
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
73-
this.removeControllersUpdateListener = bind(this.removeControllersUpdateListener, this);
74-
this.onAxisMoved = bind(this.onAxisMoved, this);
69+
this.onModelLoaded = this.onModelLoaded.bind(this);
70+
this.onControllersUpdate = this.onControllersUpdate.bind(this);
71+
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
72+
this.removeControllersUpdateListener = this.removeControllersUpdateListener.bind(this);
73+
this.onAxisMoved = this.onAxisMoved.bind(this);
7574
},
7675

7776
init: function () {
7877
var self = this;
79-
this.onButtonChanged = bind(this.onButtonChanged, this);
78+
this.onButtonChanged = this.onButtonChanged.bind(this);
8079
this.onButtonDown = function (evt) { onButtonEvent(evt.detail.id, 'down', self); };
8180
this.onButtonUp = function (evt) { onButtonEvent(evt.detail.id, 'up', self); };
8281
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self); };

src/components/oculus-touch-controls.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var bind = require('../utils/bind');
21
var registerComponent = require('../core/component').registerComponent;
32
var THREE = require('../lib/three');
43

@@ -201,12 +200,12 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
201200
mapping: INPUT_MAPPING,
202201

203202
bindMethods: function () {
204-
this.onButtonChanged = bind(this.onButtonChanged, this);
205-
this.onThumbstickMoved = bind(this.onThumbstickMoved, this);
206-
this.onModelLoaded = bind(this.onModelLoaded, this);
207-
this.onControllersUpdate = bind(this.onControllersUpdate, this);
208-
this.checkIfControllerPresent = bind(this.checkIfControllerPresent, this);
209-
this.onAxisMoved = bind(this.onAxisMoved, this);
203+
this.onButtonChanged = this.onButtonChanged.bind(this);
204+
this.onThumbstickMoved = this.onThumbstickMoved.bind(this);
205+
this.onModelLoaded = this.onModelLoaded.bind(this);
206+
this.onControllersUpdate = this.onControllersUpdate.bind(this);
207+
this.checkIfControllerPresent = this.checkIfControllerPresent.bind(this);
208+
this.onAxisMoved = this.onAxisMoved.bind(this);
210209
},
211210

212211
init: function () {

0 commit comments

Comments
 (0)