Skip to content

Commit 5cfcfbf

Browse files
committed
docs: imporve docs
1 parent 8258f4f commit 5cfcfbf

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

libs/effects/README.md

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ Tooling to handle your effects (subscriptions)!
99
```
1010

1111

12-
## Included
13-
14-
- `rxEffect`
15-
16-
### `rxEffect`
12+
## `rxEffect`
1713

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

44-
#### Run Code when an effect is cleaned up
40+
### Run Code on Clean up
41+
42+
#### `runOnInstanceDestroy`- Run code when the `rxEffect` instance is destroyed
43+
44+
When a `rxEffect`-instance is destroyed you can execute code which is registered in the `runOnInstanceDestroy`-hook.
45+
46+
`runOnInstanceDestroy` is executed whenever the repsective `DestroyRef.onDestroy`-callback is executed.
47+
48+
Example for standalone function
49+
```ts
50+
const effects = rxEffect()
51+
52+
effects.runOnInstanceDestroy(() => // do something e.g. interact with local storage)
53+
54+
```
55+
56+
Example for factory function
57+
```ts
58+
const effects = rxEffect(({runOnInstanceDestroy}) => {
59+
runOnInstanceDestroy(() =>
60+
// do something e.g. interact with local storage
61+
)
62+
})
63+
64+
```
4565
46-
Todo add docs
4766
67+
#### Run Code when a single effect is cleaned up
68+
When creating an effect:
4869
70+
```ts
71+
const effects = rxEffect()
72+
const logEffect = effects.run(of(1), console.log)
73+
74+
```
75+
You can optionally specify a callback which is executed **one time** if **either** cleanUp() is called on this single
76+
effect or the `DestroyRef.onDestroy()`-callback iun the current scope executed. Whatever comes first will be executed.
77+
78+
You do this by:
79+
```ts
80+
const effects = rxEffect()
81+
const logEffect = effects.run(of(1), console.log, {onCleanUp: () => {}})
82+
83+
```
84+
85+
### Manually destroy `rxEffect`
86+
87+
You can call `cleanUp()` on the `rxEffect` instance to destroy the instance.
88+
89+
### Manually clean up/ destroy a single effect
90+
When creating an effect:
91+
92+
```ts
93+
const effects = rxEffect()
94+
const logEffect = effects.run(of(1), console.log)
95+
96+
```
97+
You get a `EffectCleanUpRef` which exposes a `cleanUp`-function. You can call this function and
98+
destroy this single effect.

0 commit comments

Comments
 (0)