diff --git a/libs/effects/README.md b/libs/effects/README.md index e74ed77..3ce7e5e 100644 --- a/libs/effects/README.md +++ b/libs/effects/README.md @@ -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. @@ -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.