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
6 changed files
with
105 additions
and
1 deletion.
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
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
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
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
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,80 @@ | ||
--- | ||
category: Watch | ||
--- | ||
|
||
# whenever | ||
|
||
Shorthand for watching value to be truthy. | ||
|
||
## Usage | ||
|
||
```js | ||
import { whenever, useAsyncState } from '@vueuse/core' | ||
|
||
const { state, ready } = useAsyncState( | ||
fetch('https://jsonplaceholder.typicode.com/todos/1').then(t => t.json()), | ||
{}, | ||
) | ||
|
||
whenever(ready, () => console.log(state)) | ||
``` | ||
|
||
```ts | ||
// this | ||
whenever(ready, () => console.log(state)) | ||
|
||
// is equivalent to: | ||
watch(ready, (isReady) => { | ||
if (isReady) { | ||
console.log(state) | ||
} | ||
}) | ||
``` | ||
|
||
### Computed | ||
|
||
Same as `watch`, you can pass a getter function to calculate on each change. | ||
|
||
```ts | ||
// this | ||
whenever( | ||
() => counter.value === 7, | ||
() => console.log('counter is 7 now!'), | ||
) | ||
``` | ||
|
||
### Options | ||
|
||
Options and defaults are same with `watch`. | ||
|
||
```ts | ||
// this | ||
whenever( | ||
() => counter.value === 7, | ||
() => console.log('counter is 7 now!'), | ||
{ flush: 'sync' } | ||
) | ||
``` | ||
|
||
<!--FOOTER_STARTS--> | ||
## Type Declarations | ||
|
||
```typescript | ||
/** | ||
* Shorthand for watching value to be truthy | ||
* | ||
* @link https://vueuse.js.org/whenever | ||
*/ | ||
export declare function whenever<T = boolean>( | ||
source: WatchSource<T>, | ||
cb: Fn, | ||
options?: WatchOptions | ||
): WatchStopHandle | ||
``` | ||
|
||
## Source | ||
|
||
[Source](https://github.com/vueuse/vueuse/blob/main/packages/shared/whenever/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/shared/whenever/index.md) | ||
|
||
|
||
<!--FOOTER_ENDS--> |
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,15 @@ | ||
import { WatchOptions, watch, WatchSource } from 'vue-demi' | ||
import { Fn } from '../utils' | ||
|
||
/** | ||
* Shorthand for watching value to be truthy | ||
* | ||
* @link https://vueuse.js.org/whenever | ||
*/ | ||
export function whenever<T = boolean>(source: WatchSource<T>, cb: Fn, options?: WatchOptions) { | ||
return watch( | ||
source, | ||
(v) => { if (v) cb() }, | ||
options, | ||
) | ||
} |