Skip to content
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

[v3] don't wrap events in slice #4181

Open
wants to merge 1 commit into
base: v3-alpha
Choose a base branch
from
Open

Conversation

tmclane
Copy link
Member

@tmclane tmclane commented Mar 31, 2025

This single commit removes the variadic operator from the input to EmitEvent.
Using a variadic operator for 'data' causes the event object to be wrapped in a single element slice which forces the consumer of the event to operate on a slice of messages even if only a single non-slice object was submitted to the function.

If the variadic operator is intended it would seem that an iteration over the elements might be required and to send multiple events in that case.

Looking for clarification on desired functionality here if this is the wrong solution.

Summary by CodeRabbit

  • Refactor
    • Enhanced the event processing workflow to deliver more consistent and predictable behavior by streamlining data handling for each event.

@tmclane tmclane requested a review from leaanthony March 31, 2025 21:23
Copy link
Contributor

coderabbitai bot commented Mar 31, 2025

Walkthrough

The pull request updates the EmitEvent method in the application module. The method’s signature is changed from accepting a variadic parameter (data ...any) to a single parameter (data any). The internal processing remains the same, with the event being sent via the customEventProcessor and encapsulated in a CustomEvent structure.

Changes

File Change Summary
v3/pkg/application/application.go Updated EmitEvent method signature from data ...any to data any, limiting it to one argument

Possibly related PRs

Poem

I'm a rabbit who loves to code,
Hopping through changes on a winding road.
One argument now, so simple and neat,
Emitting events is ever so sweet.
With whiskers twitching in binary delight,
I celebrate this change late into the night!


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4a2dc28 and 7c9e7d7.

📒 Files selected for processing (1)
  • v3/pkg/application/application.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (1)
v3/pkg/application/application.go (1)

384-389: Good fix to address the event wrapping issue.

This change addresses the issue described in the PR objectives where events were being wrapped in a single-element slice. By removing the variadic parameter (...any) and using a single any parameter instead, consumers of events no longer need to handle a slice when a single object is provided.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@fbbdev
Copy link
Collaborator

fbbdev commented Mar 31, 2025

Hi! I included a similar change in my PR #4161 but an ensuing discussion on discord left me wondering whether it would make for poor DX.

The unexpected slice is definitely bad though, plus it doesn't match the behaviour of the JS runtime, for added confusion.

I was thinking of pushing an update to my PR proposing to emit multiple events as you too suggest, but that would break people's existing code silently as opposed to compile time errors.. not an easy tradeoff.

@fbbdev
Copy link
Collaborator

fbbdev commented Mar 31, 2025

Now that I think of it, emitting multiple events will still cause runtime errors when the data field is asserted to a slice type, so either approach should be safe.

@tmclane
Copy link
Member Author

tmclane commented Apr 1, 2025

I definitely do not like passing a structure and getting a slice of object out on the JS side.
Really non-intuitive.

@fbbdev
Copy link
Collaborator

fbbdev commented Apr 6, 2025

I think I found a better solution than both ones discussed above:

  • if no arguments are provided, use nil data
  • if one argument is provided, use it directly as event data
  • if more than one argument is provided, use the slice as event data

This approach fixes the counterintuitive behaviour in the most common case (one argument) without breaking things for people who might be passing in multiple arguments (I expect there to be a smallish but non-zero number of them). At the same time, it avoids the potentially confusing behaviour of emitting multiple events (one per argument) from a method called EmitEvent.

I included the change in #4161 but I do not consider that PR ready for merging. Feel free to copy the implementation from there if you like the approach.

@tmclane
Copy link
Member Author

tmclane commented Apr 6, 2025

I would argue the most intuitive thing to do with this code is to send exactly what the caller provided.

IE if they provided a nil send the nil.
If it's a struct then send a struct/object.
If it's a slice then send a slice/array.

This is the principle of least surprise, the same object in the same object out to the best of our ability.

@fbbdev
Copy link
Collaborator

fbbdev commented Apr 6, 2025

I agree with you in principle.

There were complaints on Discord about not being able to omit a nil arg, and @leaanthony wrote:

EmitEvent's data has always been optional

My proposal was meant to address those complaints.

I do believe that

app.EmitEvent("myevent", nil)

is a perfectly acceptable level of boilerplate, the simplest option and therefore The Go Way™.

@tmclane
Copy link
Member Author

tmclane commented Apr 6, 2025

Yes that is what I was meaning. Send whatever argument given. If they want a no argument event we could have one more method for that.

EmitEmptyEvent to make it obvious

@tmclane
Copy link
Member Author

tmclane commented Apr 6, 2025

Wait is that the point of the variadic? To allow it to not be specified at all?

Guess that makes it hard to check for not provided versus actually sending nil.

@tmclane
Copy link
Member Author

tmclane commented Apr 6, 2025

I just want it to not become a slice inadvertently.

@fbbdev
Copy link
Collaborator

fbbdev commented Apr 6, 2025

Wait is that the point of the variadic? To allow it to not be specified at all?

Just syntactic sugar for passing nil. A shorthand, for convenience. (At least that's my personal interpretation, which other people on discord appear to share).

Guess that makes it hard to check for not provided versus actually sending nil.

Given that the nil interface is not the same as a typed nil (which translates to a non-nil interface value), I think it can safely fulfill the role of "not provided" (especially once support for strictly typed events gets merged)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants