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($try-on-mount): init #7

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions indexes.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
"category": "Component",
"description": "safe `onDestroy`"
},
{
"name": "tryOnMount",
"package": "shared",
"docs": "/shared/tryOnMount/",
"category": "Component",
"description": "safe `onMount`"
},
{
"name": "utils",
"package": "shared",
Expand Down
7 changes: 7 additions & 0 deletions packages/core/indexes.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
"category": "Component",
"description": "safe `onDestroy`"
},
{
"name": "tryOnMount",
"package": "shared",
"docs": "/shared/tryOnMount/",
"category": "Component",
"description": "safe `onMount`"
},
{
"name": "utils",
"package": "shared",
Expand Down
1 change: 1 addition & 0 deletions packages/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Component
- [`tryOnDestroy`](/shared/tryOnDestroy/) — safe `onDestroy`
- [`tryOnMount`](/shared/tryOnMount/) — safe `onMount`

### Utilities
- [`whenever`](/shared/whenever/) — shorthand for watching value to be truthy
Expand Down
1 change: 1 addition & 0 deletions packages/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './tryOnDestroy'
export * from './tryOnMount'
export * from './utils'
export * from './whenever'
15 changes: 15 additions & 0 deletions packages/shared/tryOnMount/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
category: Component
---

# tryOnMount

Safe `onMount`. Call `onMount()` if it's inside a component lifecycle, if not, run just call the function.

## Usage

```js
import { tryOnMount } from '@svelte-use/core'

tryOnMount(() => {})
```
19 changes: 19 additions & 0 deletions packages/shared/tryOnMount/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { onMount } from 'svelte'
import { tryOnDestroy } from '../tryOnDestroy'
import { Fn, noop, tryGetCurrentComponent } from '../utils'

export type OnMountFn = Fn | (() => Fn)

/**
* Safe `onMount`. Call `onMount()` if it's inside a component lifecycle, if not, run just call the function.
*
* @param fn
*/
export function tryOnMount(fn: OnMountFn) {
if (tryGetCurrentComponent()) {
onMount(fn)
} else {
const cb = fn() || noop
tryOnDestroy(cb)
}
}