-
Notifications
You must be signed in to change notification settings - Fork 359
Accept symbol, proc, and nil arguments for broadcasts_refreshes #748
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?
Conversation
| after_create_commit do | ||
| case stream | ||
| when String then broadcast_refresh_later_to(stream) | ||
| when Symbol then broadcast_refresh_later_to(send(stream)) | ||
| else broadcast_refresh_later_to(stream.try(:call, self)) | ||
| end | ||
| end if stream |
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.
where explicitly passing
nilbypasses broadcasting but still triggers theafter_create_commitcallback.
What is the intended behavior of supporting explicitly nil arguments? To me, invoking the after_create_commit seems to be an accidental side-effect.
Rather than suffixing the if stream conditional, would an ArgumentError make sense?
| after_create_commit do | |
| case stream | |
| when String then broadcast_refresh_later_to(stream) | |
| when Symbol then broadcast_refresh_later_to(send(stream)) | |
| else broadcast_refresh_later_to(stream.try(:call, self)) | |
| end | |
| end if stream | |
| raise ArgumentError, "stream is required…" if stream.nil? | |
| after_create_commit do | |
| case stream | |
| when String then broadcast_refresh_later_to(stream) | |
| when Symbol then broadcast_refresh_later_to(send(stream)) | |
| else broadcast_refresh_later_to(stream.try(:call, self)) | |
| end | |
| end |
| case stream | ||
| when String then broadcast_refresh_later_to(stream) | ||
| when Symbol then broadcast_refresh_later_to(send(stream)) | ||
| else broadcast_refresh_later_to(stream.try(:call, self)) |
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.
Since this case statement aims to coerce the argument, but does not invoke different methods across the branches, what do you think about re-structuring it to extract out the method call?
| case stream | |
| when String then broadcast_refresh_later_to(stream) | |
| when Symbol then broadcast_refresh_later_to(send(stream)) | |
| else broadcast_refresh_later_to(stream.try(:call, self)) | |
| stream = send(stream) if Symbol === stream | |
| stream = stream.call(self) if stream.respond_to?(:call) | |
| broadcast_refresh_later_to(stream) |
|
Thanks for your input @seanpdoyle. The intention of If I could break backwards compatibility, I'd probably change the API to the following, which disables the create hook by default, making it optional: broadcasts_refreshes(stream = nil)I think that, in the vast majority of cases, you want to either:
Case for skipping: A Team model would result in a refresh broadcast to the Case for broadcasting to related resources: A Board has many Columns, so if a new Column is created for a given Board, it's nice to be able to call The current default stream name is a static string based on the current model's pluralized name, and it does have use cases, but it seems far less common than either skipping or, for example, broadcasting to a parent. Supporting Symbol and Proc are just quality-of-life improvements, and the ability to skip the create hook to avoid unnecessary broadcasts is just an optimization. Ideally (in my opinion): broadcasts_refreshes # only register update/destroy hooks, skip create hook by defaulting the stream argument to nil
broadcasts_refreshes :board # broadcast create to Board parent of current Column model, in addition to update/destroy hooksPerhaps less ideal from an API design POV, but with backwards compatibility: broadcasts_refreshes skip: :create # skip create hook despite `stream` being set to `model_name.pluralize` by default
broadcasts_refreshes :board # broadcast create to Board parent of current Column model, in addition to update/destroy hooksIf skipping won't be supported for this method, then, worst case, it results in a broadcast that no one will ever listen to, which isn't the end of the world, albeit suboptimal. |
Currently,
broadcasts_refreshesonly accepts values in the form of a string or nil, where explicitly passing nil bypasses broadcasting but still triggers theafter_create_commitcallback.This change allows you to pass a symbol (method name to call) or a proc to evaluate to
broadcasts_refreshesto produce a stream name dynamically at the instance levelafter_create_commit.This allows us to, for example, specify a parent (Board) to broadcast refreshes to on
create, while broadcasting to itself (Column) onupdateanddestroy:Listen for new
Columncreations for@board:Listen for updates and deletion of existing
@column:This changes also disables the entire
after_create_commithook ifnilis passed in explicitly, rather than allowing it to run without broadcasting anything.