Skip to content

Commit f5f2790

Browse files
5144 rename throttle component changed (#5151)
* rename throttleComponentChanged -> throttleLeadingAndTrailing * document throttleLeadingAndTrailing * @dmarcos says we don't need to preserve 1.3.0 interface * No need to define this MD variable twice.
1 parent c1998a7 commit f5f2790

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

docs/core/utils.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,20 @@ AFRAME.registerComponent('foo', {
212212
});
213213
```
214214

215+
### `AFRAME.utils.throttleLeadingAndTrailing (function, minimumInterval [, optionalContext])`
216+
217+
Returns a throttled function that is called at most once every `minimumInterval` milliseconds, but ensures that the very last call of a burst gets deferred until the end of the interval. This is useful when an event is used to trigger synchronization of state, and there is a need to converge to the correct final state following a burst of events.
218+
219+
Example use cases:
220+
221+
* synchronizing state based on the componentchanged event
222+
* following a mouse pointer using the mousemove event
223+
* integrating with [THREE.TransformControls](https://threejs.org/docs/#examples/en/controls/TransformControls), via the objectChange event.
224+
225+
A context such as `this` can be provided to handle function binding for convenience.
226+
227+
The same as [lodash's`throttle`][lodash], with `leading` and `trailing` options both set to `true`
228+
215229
## Miscellaneous
216230

217231
### `AFRAME.utils.getUrlParameter (name)`

src/core/component.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ var Component = module.exports.Component = function (el, attrValue, id) {
7272
}
7373

7474
// Last value passed to updateProperties.
75-
this.throttledEmitComponentChanged = utils.throttleComponentChanged(function emitChange () {
75+
// This type of throttle ensures that when a burst of changes occurs, the final change to the
76+
// component always triggers an event (so a consumer of this event will end up reading the correct
77+
// final state, following a burst of changes).
78+
this.throttledEmitComponentChanged = utils.throttleLeadingAndTrailing(function emitChange () {
7679
el.emit('componentchanged', self.evtDetail, false);
7780
}, 200);
7881
this.updateProperties(attrValue);

src/utils/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,21 @@ module.exports.throttle = function (functionToThrottle, minimumInterval, optiona
6868
* Returns throttle function that gets called at most once every interval.
6969
* If there are multiple calls in the last interval we call the function one additional
7070
* time.
71-
* It behaves like a throttle except for the very last call that gets deferred until the end of the interval.
71+
* It behaves like throttle except for the very last call that gets deferred until the end of the interval.
72+
* This is useful when an event is used to trigger synchronization of state, and there is a need to converge
73+
* to the correct final state following a burst of events.
74+
*
75+
* Example use cases:
76+
* - synchronizing state based on the componentchanged event
77+
* - following a mouse pointer using the mousemove event
78+
* - integrating with THREE.TransformControls, via the objectChange event.
7279
*
7380
* @param {function} functionToThrottle
7481
* @param {number} minimumInterval - Minimal interval between calls (milliseconds).
7582
* @param {object} optionalContext - If given, bind function to throttle to this context.
7683
* @returns {function} Throttled function.
7784
*/
78-
module.exports.throttleComponentChanged = function (functionToThrottle, minimumInterval, optionalContext) {
85+
module.exports.throttleLeadingAndTrailing = function (functionToThrottle, minimumInterval, optionalContext) {
7986
var lastTime;
8087
var deferTimer;
8188
if (optionalContext) {

0 commit comments

Comments
 (0)