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

feat(createEventHook): allow trigger to optionally have no parameters #3507

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

curtgrimes
Copy link
Contributor

@curtgrimes curtgrimes commented Oct 27, 2023

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.

Description

Previously, createEventHook()'s trigger() would always require at least one parameter:

const { trigger } = createEventHook();

trigger();
//      ^
// TypeScript error: Expected 1 arguments, but got 0

Now, parameters are optional. This allows a use case where there is no other information to send when triggering an event, and simply knowing that an event was triggered is all that is needed:

const { on, trigger } = createEventHook();

// This is now possible:
trigger(/* having no arguments is now allowed */);

// Elsewhere, we can listen for the event:
on((/* no arguments */) => {})

Additional context

I ran into a need for this when I was using createEventHook() for some kind of simple event where I had no other information to provide on trigger(), and I only cared about the fact that an event was triggered.


🤖 Generated by Copilot at 251b985

This pull request enhances the createEventHook function and its interface to support events without parameters. It also adds a new test case and refactors the type definitions for the event hook callbacks.

🤖 Generated by Copilot at 251b985

  • Simplify type signatures of event hook functions by introducing a new type alias Callback<T> that can handle both parameterized and parameterless callbacks (link,link)
  • Allow event hook to trigger events with or without parameters by making the trigger function accept an optional parameter of type T and invoking the callbacks accordingly (link)
  • Add a new test case in createEventHook/index.test.ts to verify that the event hook can be triggered without any parameters and that the registered callbacks are fired (link)

@curtgrimes curtgrimes force-pushed the create-event-hook-no-params branch 3 times, most recently from 8402ab4 to 6f98fad Compare October 27, 2023 06:18
Previously, `createEventHook()`'s `trigger()` would always require at
least one parameter:

```ts
const { trigger } = createEventHook();

trigger();
//      ^
// TypeScript error: Expected 1 arguments, but got 0
```

Now, parameters are optional. This allows a use case where there is no
other information to send when triggering an event, and simply knowing
that an event was triggered is all that is needed:

```ts
const { on, trigger } = createEventHook();

// This is now possible:
trigger(/* having no arguments is now allowed */);

// Elsewhere, we can listen for the event:
on((/* no arguments */) => {})
```
@curtgrimes curtgrimes marked this pull request as ready for review October 27, 2023 06:28
@antfu antfu enabled auto-merge November 9, 2023 14:54
@antfu antfu added this pull request to the merge queue Nov 9, 2023
Merged via the queue into vueuse:main with commit 6040e1c Nov 9, 2023
5 checks passed
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