-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create callback queue to run callbacks in order (#191)
* Create callback queue to run callbacks in order * _runCallbackAfterEndOfOptionsStream -> _runCallback
- Loading branch information
1 parent
6e541e1
commit 4bfb641
Showing
6 changed files
with
172 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/assets/javascripts/hw_combobox/models/combobox/callbacks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Combobox from "hw_combobox/models/combobox/base" | ||
|
||
const MAX_CALLBACK_ATTEMPTS = 3 | ||
|
||
Combobox.Callbacks = Base => class extends Base { | ||
_initializeCallbacks() { | ||
this.callbackQueue = [] | ||
this.callbackExecutionAttempts = {} | ||
} | ||
|
||
_enqueueCallback() { | ||
const callbackId = crypto.randomUUID() | ||
this.callbackQueue.push(callbackId) | ||
return callbackId | ||
} | ||
|
||
_isNextCallback(callbackId) { | ||
return this._nextCallback === callbackId | ||
} | ||
|
||
_callbackAttemptsExceeded(callbackId) { | ||
return this._callbackAttempts(callbackId) > MAX_CALLBACK_ATTEMPTS | ||
} | ||
|
||
_callbackAttempts(callbackId) { | ||
return this.callbackExecutionAttempts[callbackId] || 0 | ||
} | ||
|
||
_recordCallbackAttempt(callbackId) { | ||
this.callbackExecutionAttempts[callbackId] = this._callbackAttempts(callbackId) + 1 | ||
} | ||
|
||
_dequeueCallback(callbackId) { | ||
this.callbackQueue = this.callbackQueue.filter(id => id !== callbackId) | ||
this._forgetCallbackExecutionAttempts(callbackId) | ||
} | ||
|
||
_forgetCallbackExecutionAttempts(callbackId) { | ||
delete this.callbackExecutionAttempts[callbackId] | ||
} | ||
|
||
get _nextCallback() { | ||
return this.callbackQueue[0] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<%# locals: (for_id:, src:) -%> | ||
<%= tag.li id: hw_pagination_frame_wrapper_id(for_id), class: "hw_combobox__pagination__wrapper", | ||
data: { hw_combobox_target: "endOfOptionsStream", input_type: params[:input_type] }, | ||
data: { hw_combobox_target: "endOfOptionsStream", input_type: params[:input_type], callback_id: params[:callback_id] }, | ||
aria: { hidden: true } do %> | ||
<%= turbo_frame_tag hw_pagination_frame_id(for_id), src: src, loading: :lazy %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters