Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(createEventHook): new function (#497)
- Loading branch information
Showing
3 changed files
with
111 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
category: Utilities | ||
--- | ||
|
||
# createEventHook | ||
|
||
Utility for creating event hooks | ||
|
||
## Usage | ||
|
||
|
||
Creating a function that uses `createEventHook` | ||
```ts | ||
import { createEventHook } from '@vueuse/core' | ||
|
||
export function useMyFetch(url) { | ||
const fetchResult = createEventHook<Response>() | ||
const fetchError = createEventHook<any>() | ||
|
||
fetch(url) | ||
.then(result => fetchResult.trigger(result)) | ||
.catch(error => fetchError.trigger(error.message)) | ||
|
||
return { | ||
onResult: fetchResult.on, | ||
onError: fetchError.on | ||
} | ||
} | ||
``` | ||
|
||
Using a function that uses `createEventHook` | ||
```html | ||
<script setup lang="ts"> | ||
import { useMyFetch } from './my-fetch-function' | ||
const { onResult, onError } = useMyFetch('my api url') | ||
onResult((result) => { | ||
console.log(result) | ||
}) | ||
onError((error) => { | ||
console.errr(error) | ||
}) | ||
</script> | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createEventHook } from '.' | ||
|
||
describe('createEventHook', () => { | ||
it('should be defined', () => { | ||
expect(createEventHook).toBeDefined() | ||
}) | ||
|
||
it('should trigger event', () => { | ||
let message = '' | ||
|
||
const myFunction = () => { | ||
const resultEvent = createEventHook<string>() | ||
const exec = () => resultEvent.trigger('Hello World') | ||
return { | ||
exec, | ||
onResult: resultEvent.on, | ||
} | ||
} | ||
|
||
const { exec, onResult } = myFunction() | ||
onResult(result => message = result) | ||
exec() | ||
|
||
setTimeout(() => {}, 0) | ||
|
||
expect(message).toBe('Hello World') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* The source code for this function was inspiried by vue-apollo's `useEventHook` util | ||
* https://github.com/vuejs/vue-apollo/blob/v4/packages/vue-apollo-composable/src/util/useEventHook.ts | ||
*/ | ||
|
||
/** | ||
* Utility for creating event hooks | ||
* | ||
* @see https://vueuse.org/createEventHook | ||
*/ | ||
export function createEventHook<T = any>() { | ||
const fns: Array<(param: T) => void> = [] | ||
|
||
const off = (fn: (param: T) => void) => { | ||
const index = fns.indexOf(fn) | ||
if (index !== -1) | ||
fns.splice(index, 1) | ||
} | ||
|
||
const on = (fn: (param: T) => void) => { | ||
fns.push(fn) | ||
|
||
return { | ||
off: () => off(fn), | ||
} | ||
} | ||
|
||
const trigger = (param: T) => { | ||
fns.forEach(fn => fn(param)) | ||
} | ||
|
||
return { | ||
on, | ||
off, | ||
trigger, | ||
} | ||
} |