-
Notifications
You must be signed in to change notification settings - Fork 2
Debouncing #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Debouncing #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,13 +112,39 @@ exports.Target = class Target extends Montage { | |
| * https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath | ||
| * @property {Array<Target>} | ||
| */ | ||
| composedPath: {value: undefined} | ||
| composedPath: { value: undefined }, | ||
|
|
||
| /** | ||
| * Map storing the last timestamp when each interaction type was processed | ||
| * @type {Map<string, number>} | ||
| * @private | ||
| */ | ||
| __debouncedInteractionTimestamps: { value: null }, | ||
|
|
||
| /** | ||
| * Map of threshold durations (in milliseconds) for each interaction type | ||
| * @type {Map<string, number>} | ||
| * @private | ||
| */ | ||
| __debouncedInteractionThresholds: { value: null }, | ||
| }); | ||
| } | ||
|
|
||
| get _debouncedInteractionTimestamps() { | ||
| if (!this.__debouncedInteractionTimestamps) { | ||
| this.__debouncedInteractionTimestamps = new Map(); | ||
| } | ||
|
|
||
| return this.__debouncedInteractionTimestamps; | ||
| } | ||
|
|
||
| get _debouncedInteractionThresholds() { | ||
| if (!this.__debouncedInteractionThresholds) { | ||
| this.__debouncedInteractionThresholds = new Map(); | ||
| } | ||
|
|
||
| return this.__debouncedInteractionThresholds; | ||
| } | ||
|
|
||
| /** | ||
| * Whether or not this is the activeTarget | ||
|
|
@@ -133,7 +159,31 @@ exports.Target = class Target extends Montage { | |
| return this === defaultEventManager.activeTarget; | ||
| } | ||
|
|
||
| registerInteractionForDebounce(interactionKey, threshold) { | ||
| this._debouncedInteractionThresholds.set(interactionKey, threshold); | ||
| } | ||
|
|
||
| unregisterInteractionForDebounce(interactionKey) { | ||
| this._debouncedInteractionThresholds.delete(interactionKey); | ||
| this._debouncedInteractionTimestamps.delete(interactionKey); | ||
| } | ||
|
|
||
| shouldPreventInteraction(interactionKey) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marchant if you come up with a better name |
||
| if (!this._debouncedInteractionThresholds.has(interactionKey)) return false; | ||
|
|
||
| const currentTime = Date.now(); | ||
| const lastTime = this._debouncedInteractionTimestamps.get(interactionKey) || 0; | ||
| const threshold = this._debouncedInteractionThresholds.get(interactionKey) || 0; | ||
| const delta = currentTime - lastTime; | ||
|
|
||
| // Check if we need to debounce | ||
| if (lastTime > 0 && delta < threshold) return true; | ||
|
|
||
| // Update timestamp for this interaction type | ||
| this._debouncedInteractionTimestamps.set(interactionKey, currentTime); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Ask this target to surrender its activeTarget status. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,50 @@ const Button = (exports.Button = class Button extends Control { | |
|
|
||
| static VisualPosition = VisualPosition; | ||
|
|
||
| /** | ||
| * How long to wait (in milliseconds) between allowing press events | ||
| * @type {number} | ||
| */ | ||
| _debounceThreshold = 300; | ||
|
|
||
| /** | ||
| * Whether debouncing is enabled for press events | ||
| * @type {boolean} | ||
| */ | ||
| _debounceEnabled = false; | ||
|
|
||
| get debounceEnabled() { | ||
| return this._debounceEnabled; | ||
| } | ||
|
|
||
| set debounceEnabled(enabled) { | ||
| enabled = !!enabled; | ||
|
|
||
| if (enabled !== this._debounceEnabled) { | ||
| this._debounceEnabled = enabled; | ||
| this._updateActionDebounceRegistration(); | ||
| } | ||
| } | ||
|
|
||
| get debounceThreshold() { | ||
| return this._debounceThreshold; | ||
| } | ||
|
|
||
| set debounceThreshold(threshold) { | ||
| if (typeof threshold === "number" && threshold >= 0 && this._debounceThreshold !== threshold) { | ||
| this._debounceThreshold = threshold; | ||
| this._updateActionDebounceRegistration(); | ||
| } | ||
| } | ||
|
|
||
| _updateActionDebounceRegistration() { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marchant if you come up with a better name |
||
| if (this.debounceEnabled ) { | ||
| this.registerInteractionForDebounce("action", this.debounceThreshold); | ||
| } else { | ||
| this.unregisterInteractionForDebounce("action"); | ||
| } | ||
| } | ||
|
|
||
| // <---- Properties ----> | ||
|
|
||
| _visualPosition = VisualPosition.start; | ||
|
|
@@ -275,7 +319,10 @@ const Button = (exports.Button = class Button extends Control { | |
|
|
||
| if (identifier === "space" || identifier === "enter") { | ||
| this.active = false; | ||
| this.dispatchActionEvent(); | ||
|
|
||
| if (!this.shouldPreventInteraction("action")) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marchant I tried to add it to the press composer and the key composers and it worked fine only when the source was the same composer. |
||
| this.dispatchActionEvent(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -300,7 +347,11 @@ const Button = (exports.Button = class Button extends Control { | |
| handlePress(mutableEvent) { | ||
| if (!this._promise) { | ||
| this.active = false; | ||
| this.dispatchActionEvent(event.details); | ||
|
|
||
| if (!this.shouldPreventInteraction("action")) { | ||
| this.dispatchActionEvent(event.details); | ||
| } | ||
|
|
||
| this._removeEventListeners(); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marchant if you come up with a better name