You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is the use case: When deleting an object the list must be refreshed and the counter updated. As this object can be manipulated by other processes that are not calling the controller. This forces Turbo Stream calls to be executed from with the model.
Now, when the object is destroyed its reference will be lost... making the following code impossible to run:
after_destroy_commit do
broadcast_update_later_to [self.room_id, :questions], target: "question_counter", html: Question.approved_questions_for_room(self.room_id).count
broadcast_remove_to [self.room_id, :questions], target: self
end
The error is then ERROR: Discarded Turbo::Streams::ActionBroadcastJob due to a ActiveJob::DeserializationError.
(the same sequence called from the controller does work as expected with no error)
I do understand the process behind the scene, but wouldn't it be better to properly "serialize" the active job so that it can be run once the object does not exist so we can keep the async performance benefits.
This question could also be extended to the broadcast_remove_to as it does not exist a broadcast_remove_later_to (same, I understand the why, but I also think that developers have solutions in their arsenal to circumvent the potential issues - for instance Realm database is fully async and you quickly learn how to properly delete data and reflect these changes on the UI)
The text was updated successfully, but these errors were encountered:
While the developers may come back with a better solution I think the following hackish pseudo-code will work
before_destroy do
Thread.current_thread[:broadcast_room_id] = self.room_id
# OR store the whole record in current thread
Thread.current_thread[:selfish] = self
end
then
after_destroy_commit do
broadcast_update_later_to [Thread.current_thread[:broadcast_room_id], :questions], target: "question_counter", html: Question.approved_questions_for_room(Thread.current_thread[:selfish].room_id]).count
broadcast_remove_to [Thread.current_thread[:selfish].room_id, :questions], target: Thread.current_thread[:selfish]
end
which adds a destroyed instance to locals. So in order to avoid ActiveJob::DeserializationError, you can set locals like { model_name.element.to_sym => nil }:
Here is the use case: When deleting an object the list must be refreshed and the counter updated. As this object can be manipulated by other processes that are not calling the controller. This forces Turbo Stream calls to be executed from with the model.
Now, when the object is destroyed its reference will be lost... making the following code impossible to run:
The error is then ERROR: Discarded Turbo::Streams::ActionBroadcastJob due to a ActiveJob::DeserializationError.
(the same sequence called from the controller does work as expected with no error)
I do understand the process behind the scene, but wouldn't it be better to properly "serialize" the active job so that it can be run once the object does not exist so we can keep the async performance benefits.
This question could also be extended to the broadcast_remove_to as it does not exist a broadcast_remove_later_to (same, I understand the why, but I also think that developers have solutions in their arsenal to circumvent the potential issues - for instance Realm database is fully async and you quickly learn how to properly delete data and reflect these changes on the UI)
The text was updated successfully, but these errors were encountered: