Skip to content

chore(clerk-js,shared): Improve createEventBus and move it to shared #5546

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

Merged
merged 12 commits into from
Apr 9, 2025

Conversation

panteliselef
Copy link
Member

@panteliselef panteliselef commented Apr 6, 2025

Description

Extract createEventBus changes from #5476.

New APIs

  • onBefore() is triggered after .emit() but its listeners are executed before the listeners of on().
  • offBefore() unregisters onBefore
  • internal.retrieveListeners() useful of lopping through listeners and attaching them to a new event bus instance. Exactly what we need when "hydrating" in isomorphic clerk

Replaced APIs

- dispatch
+ emit

Example

// Create a type-safe event bus
const bus = createEventBus<{
  'user:login': { id: string };
  'error': Error;
}>();

// Subscribe to events
const onLogin = ({ id }: { id: string }) => console.log('User logged in:', id);
bus.on('user:login', onLogin);

// Subscribe with priority (runs before regular handlers)
bus.onBefore('error', (error) => console.error('Error occurred:', error));

// Emit events
bus.emit('user:login', { id: 'user_123' });

// Unsubscribe specific handler
bus.off('user:login', onLogin);

// Unsubscribe all handlers
bus.off('error');

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

changeset-bot bot commented Apr 6, 2025

🦋 Changeset detected

Latest commit: c9167b8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 19 packages
Name Type
@clerk/clerk-js Patch
@clerk/shared Minor
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch
@clerk/agent-toolkit Patch
@clerk/astro Patch
@clerk/backend Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/clerk-react Patch
@clerk/remix Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Apr 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 9, 2025 1:55pm

'@clerk/shared': minor
---

Export `createEventBus` from `@clerk/shared/eventBus`.
Copy link
Member

Choose a reason for hiding this comment

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

❓ Why does this need to be public now?

Copy link
Member Author

Choose a reason for hiding this comment

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

In Clerk.status, isomorphic Clerk also uses an event bus until clerk-js hydrates.

@panteliselef panteliselef changed the title chore(clerk-js,shared): Improve eventBus and move it to shared chore(clerk-js,shared): Improve createEventBus and move it to shared Apr 7, 2025
bus.on('user:login', onLogin);

// Subscribe with priority (runs before regular handlers)
bus.onBefore('error', (error) => console.error('Error occurred:', error));
Copy link
Member

Choose a reason for hiding this comment

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

Not a fan of the naming here, I read it as "on 'before' event". How about preOn or similar?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not a fan of the current name either. How about prioritizedOn/Off ?

# Conflicts:
#	packages/shared/package.json
on: (...args) => _on(eventToHandlersMap, latestPayloadMap, ...args),
// Subscribe to an event with priority
// Registered handlers with `prioritizedOn` will be called before handlers registered with `on`
prioritizedOn: (...args) => _on(eventToPredispatchHandlersMap, latestPayloadMap, ...args),
Copy link
Member

Choose a reason for hiding this comment

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

Could this just be an option that is set on the regular on and off methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely, and I tried that, it would make it go over 400bytes, and i decided to revert it. Do you feel strongly for about it ?

Copy link
Member Author

@panteliselef panteliselef Apr 9, 2025

Choose a reason for hiding this comment

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

@jacekradko Keep in mind this will still be internal, it's just exported by shared.

Developers doing clerk.on() will not be able to do clerk. prioritizedOn()

Copy link
Member

Choose a reason for hiding this comment

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

@panteliselef I don't think it's too problematic, just a a larger API surface. We can always revisit in the future

@jacekradko jacekradko merged commit 8186cb5 into main Apr 9, 2025
30 checks passed
@jacekradko jacekradko deleted the elef/clerk-event-bus branch April 9, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants