-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,37 @@ | ||
# define-emitter-composable | ||
Creates a Vue composable function for providing or injecting a mitt event emitter. | ||
|
||
## Install | ||
```bash | ||
$ npm install define-emitter-composable | ||
``` | ||
|
||
```js | ||
import defineEmitterComposable from 'define-emitter-composable' | ||
``` | ||
|
||
## Usage | ||
```js | ||
// Creates a composable function for providing or injecting a mitt event emitter. | ||
const useFooEmitter = defineEmitterComposable<{ bar: string, baz: number }>({ key: Symbol('bar') , throwOnNoProvider: () => new Error('No provider for bar') }) | ||
const useBarEmitter = defineEmitterComposable() | ||
|
||
// In a Vue component setup | ||
setup() { | ||
const fooEmitter = useFooEmitter('provide') // Provides an emitter | ||
fooEmitter.on('bar', (payload) => { | ||
// Handle the event | ||
}) | ||
|
||
// Or inject an existing event emitter | ||
const injectedBarEmitter = useFooEmitter() // Or `useFooEmitter('inject')` | ||
injectedFooEmitter.emit('bar', 'bar') | ||
injectedFooEmitter.emit('baz', 123) | ||
|
||
// If `injectDefault` or `throwOnNoProvider` options are not set, the return value may be undefined | ||
const undefinedEmitter = useBarEmitter() // undefined | ||
} | ||
``` | ||
|
||
## License | ||
[MIT](./LICENSE) |