Skip to content

Commit

Permalink
docs: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
MuXiu1997 committed Nov 25, 2023
1 parent 1a89bc4 commit d3fc50a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
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)

0 comments on commit d3fc50a

Please sign in to comment.