Skip to content

Commit

Permalink
docs: imporve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbe812 committed Jul 10, 2024
1 parent 8258f4f commit 5cfcfbf
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions libs/effects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ Tooling to handle your effects (subscriptions)!
```


## Included

- `rxEffect`

### `rxEffect`
## `rxEffect`

`rxEffect` is a standalone convenience function to take care of a subscription and
execute side effects.
Expand Down Expand Up @@ -41,8 +37,62 @@ const effects = rxEffect(({register}) => {
*Note* that you need to use `rxEffect` within an injection context. If you want to
use it outside an injection context you can pass the `Ìnjector` as argument.

#### Run Code when an effect is cleaned up
### Run Code on Clean up

#### `runOnInstanceDestroy`- Run code when the `rxEffect` instance is destroyed

When a `rxEffect`-instance is destroyed you can execute code which is registered in the `runOnInstanceDestroy`-hook.

`runOnInstanceDestroy` is executed whenever the repsective `DestroyRef.onDestroy`-callback is executed.

Example for standalone function
```ts
const effects = rxEffect()

effects.runOnInstanceDestroy(() => // do something e.g. interact with local storage)
```
Example for factory function
```ts
const effects = rxEffect(({runOnInstanceDestroy}) => {
runOnInstanceDestroy(() =>
// do something e.g. interact with local storage
)
})

```
Todo add docs
#### Run Code when a single effect is cleaned up
When creating an effect:
```ts
const effects = rxEffect()
const logEffect = effects.run(of(1), console.log)

```
You can optionally specify a callback which is executed **one time** if **either** cleanUp() is called on this single
effect or the `DestroyRef.onDestroy()`-callback iun the current scope executed. Whatever comes first will be executed.
You do this by:
```ts
const effects = rxEffect()
const logEffect = effects.run(of(1), console.log, {onCleanUp: () => {}})

```
### Manually destroy `rxEffect`
You can call `cleanUp()` on the `rxEffect` instance to destroy the instance.
### Manually clean up/ destroy a single effect
When creating an effect:
```ts
const effects = rxEffect()
const logEffect = effects.run(of(1), console.log)

```
You get a `EffectCleanUpRef` which exposes a `cleanUp`-function. You can call this function and
destroy this single effect.

0 comments on commit 5cfcfbf

Please # to comment.