Skip to content

Adds Custom Event Map#221

Merged
marklundin merged 7 commits intomainfrom
feat-custom-app-events
Sep 8, 2025
Merged

Adds Custom Event Map#221
marklundin merged 7 commits intomainfrom
feat-custom-app-events

Conversation

@marklundin
Copy link
Collaborator

Adds a Custom Event Map to useAppEvent which allows subscribing to user defined events. Fixes #203

// Custom events (with generic)
interface MyEventMap {
  levelComplete: (level: number, score: number) => void;
}

useAppEvent<MyEventMap>('levelComplete', (level, score) => { /* level logic */ });

Tests included. Docs updated

- Updated the `useAppEvent` hook to allow for custom events with TypeScript generics, enabling users to define their own event maps without extending the base callback map.
- Added documentation for custom events in the API reference and created an example demonstrating their usage.
- Expanded unit tests to validate the functionality of custom events and ensure proper type safety during event registration and cleanup.
@marklundin marklundin requested a review from Copilot September 3, 2025 15:04
@marklundin marklundin self-assigned this Sep 3, 2025
@marklundin marklundin added enhancement New feature or request React labels Sep 3, 2025
@changeset-bot
Copy link

changeset-bot bot commented Sep 3, 2025

🦋 Changeset detected

Latest commit: 28e1096

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

This PR includes changesets to release 2 packages
Name Type
@playcanvas/react Patch
@playcanvas/blocks 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
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds custom event map support to the useAppEvent hook, allowing developers to define and subscribe to custom application events with full TypeScript type safety. The hook now accepts generic type parameters to support user-defined events beyond the built-in PlayCanvas events.

Key changes:

  • Redesigned the hook's type system to support custom event maps via generics
  • Updated the event handler to accept variable arguments for custom events
  • Added comprehensive documentation and examples for custom event usage

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
packages/lib/src/hooks/use-app-event.ts Core hook implementation with generic type support and updated event handling
packages/lib/src/hooks/use-app-event.test.tsx Test cases for custom event map functionality
packages/docs/content/examples/custom-events.mdx Complete example documentation for custom events usage
packages/docs/content/docs/api/hooks/index.mdx Updated API documentation with custom events section

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Sep 3, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@playcanvas/blocks@221
npm i https://pkg.pr.new/@playcanvas/react@221

commit: 28e1096

marklundin and others added 3 commits September 3, 2025 16:13
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Changed the callback invocation for custom events to no longer forward arguments, ensuring it is called without parameters.
@marklundin marklundin marked this pull request as ready for review September 5, 2025 15:52
@marklundin marklundin requested a review from a team September 8, 2025 12:58
@marklundin marklundin merged commit f5f3001 into main Sep 8, 2025
5 of 6 checks passed
@marklundin marklundin deleted the feat-custom-app-events branch September 8, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request React

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Ability to extend useAppEvent

2 participants