Skip to content

feat(useEventSource): add transformer #4953

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

imddc
Copy link

@imddc imddc commented Aug 10, 2025

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.
⚠️ Slowing down new functions

Warning: Slowing down new functions

As the VueUse audience continues to grow, we have been inundated with an overwhelming number of feature requests and pull requests. As a result, maintaining the project has become increasingly challenging and has stretched our capacity to its limits. As such, in the near future, we may need to slow down our acceptance of new features and prioritize the stability and quality of existing functions. Please note that new features for VueUse may not be accepted at this time. If you have any new ideas, we suggest that you first incorporate them into your own codebase, iterate on them to suit your needs, and assess their generalizability. If you strongly believe that your ideas are beneficial to the community, you may submit a pull request along with your use cases, and we would be happy to review and discuss them. Thank you for your understanding.


Description

  • JSON Parsing: Automatically parse JSON strings from EventSource into JavaScript objects
  • Custom Transformations: Apply user-defined transformation functions to incoming data
  • Backward Compatibility: All existing functionality remains unchanged, serialization is opt-in

Additional context

This enhancement addresses a common pain point when working with EventSource APIs that send JSON data as strings, requiring manual parsing in every usage. The implementation follows VueUse's existing patterns and maintains full backward compatibility.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Aug 10, 2025
Comment on lines 51 to 68

/**
* Data serialization options
*/
serialization?: {
/**
* Whether to automatically parse JSON data
* @default false
*/
parseJSON?: boolean

/**
* Custom data transformer function
* @param data Raw data from EventSource
* @returns Transformed data
*/
transform?: (data: any) => any
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should only have the transform/serializer.

we should add an optional generic to the options and use this for the serializer

  serializer?: (v: unknown) => T

the serializer should default to:

  serializer: (v) => v

The reason I don't like the parseJSON option is that it doesn't save much.

  serialization?: {
    parseJSON: true
  }

vs

  serializer: (v) => JSON.parse(v)

It is not clear to the user what should happen if we pass both.

  serialization?: {
    parseJSON: true,
    transform: (v)=> /* ... */
  }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the feedback!

I modified the code according to your suggestion

how is it this time?

@OrbisK
Copy link
Collaborator

OrbisK commented Aug 10, 2025

Thank you for your PR.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Aug 10, 2025
@imddc imddc changed the title Feat/enhance useEventSource feat(useEventSource): add transformer Aug 11, 2025
Copy link
Collaborator

@OrbisK OrbisK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far. Some small adjustments.

Maybe we should rename it to: serializer like useBase64

@imddc
Copy link
Author

imddc commented Aug 11, 2025

Looks good so far. Some small adjustments.

Maybe we should rename it to: serializer like useBase64

Thank you!
I have adopted your suggestion, and it looks much better!

@imddc imddc requested a review from OrbisK August 11, 2025 08:14
@OrbisK
Copy link
Collaborator

OrbisK commented Aug 11, 2025

There are no new commits. Maybe you forgot to push 😅

@imddc
Copy link
Author

imddc commented Aug 12, 2025

There are no new commits. Maybe you forgot to push 😅

I forgot last time. Could you please do the code review again?

@@ -183,14 +186,14 @@ export function useEventSource<Events extends string[], Data = any>(

es.onmessage = (e: MessageEvent) => {
event.value = null
data.value = e.data
data.value = serialization(e.data) || null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data.value = serialization(e.data) || null
data.value = serialization(e.data) ?? null

Comment on lines 196 to 197
data.value = serialization(e.data) || null
lastEventId.value = e.lastEventId || null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data.value = serialization(e.data) || null
lastEventId.value = e.lastEventId || null
data.value = serialization(e.data) ?? null
lastEventId.value = e.lastEventId ?? null

const event: ShallowRef<string | null> = shallowRef(null)
const data: ShallowRef<Data | null> = shallowRef(null)
const data: ShallowRef<Data | TransformedData | null> = shallowRef(null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const data: ShallowRef<Data | TransformedData | null> = shallowRef(null)
const data: ShallowRef<TransformedData | null> = shallowRef(null)

@imddc
Copy link
Author

imddc commented Aug 12, 2025

how about now?

/**
* Reference to the latest data received via the EventSource,
* can be watched to respond to incoming messages
*/
data: ShallowRef<Data | null>
data: ShallowRef<Data | TransformedData | null>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data: ShallowRef<Data | TransformedData | null>
data: ShallowRef<TransformedData | null>

sorry, overlooked this one 🫠

@imddc imddc requested a review from OrbisK August 12, 2025 08:14
Copy link
Collaborator

@OrbisK OrbisK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Thank you!

@OrbisK OrbisK requested review from 43081j and ilyaliao August 12, 2025 08:17
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Aug 12, 2025
'https://event-source-url',
[],
{
serialization: (rawData) => JSON.parse(rawData),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We held an internal discussion. We want to use the serialiser to ensure consistency with the other composables.

Could you please adjust the PR?

@dosubot dosubot bot removed the lgtm This PR has been approved by a maintainer label Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants