From 0cbee3da1d1b3fcbc4837b166a0aa4fcd767cab5 Mon Sep 17 00:00:00 2001 From: Dean Radcliffe <24406+deanrad@users.noreply.github.com> Date: Sat, 20 Jan 2024 18:15:08 -0600 Subject: [PATCH] rxfx/effect 1.0.1 README --- effect/README.md | 167 +++++++++++++++++++++++++++++++++++++++++++- effect/package.json | 2 +- 2 files changed, 167 insertions(+), 2 deletions(-) diff --git a/effect/README.md b/effect/README.md index 41e803f..33c139a 100644 --- a/effect/README.md +++ b/effect/README.md @@ -2,4 +2,169 @@ A Vanilla JS container for Effect Management, based on RxJS. Supports cancelation, concurrency modes (queueing, throttling, debouncing), and TypeScript. -Part of the [𝗥𝘅𝑓𝑥](https://github.com/deanrad/rxfx) family of libraries. \ No newline at end of file +Part of the [𝗥𝘅𝑓𝑥](https://github.com/deanrad/rxfx) family of libraries. + +# What Is It Good For? + +When you have an async effect, embodied in a function that returns a Promise or an Observable, but you want it to: + +- not run too often +- be cancelable +- automatically track whether it is active + +# Usage + +Treat `createEffect` (or a concurrency-controlled version like `createQueueingEffect`) as a higher-order function which returns a concurrency-controlled, cancelable version of that function + +``` +npm i -S @rxfx/effect +``` + +```ts +import { createEffect, createQueueingEffect } from '@rxfx/effect'; + +const ringBell = () => { + /* returns a Promise for when a bell has completed ringing */ +}; + +// The RxFx effect with no concurrency control +const ringEffect = createEffect(ringBell); +// An RxFx effect that queues ringing, with the same API as createEffect +const queuedRing = createQueueingEffect(ringBell); + +queuedRing(); // ring it now +queuedRing(); // ring after the first +queuedRing.request(); // alternate way to ring + +queuedRing.cancelCurrent(); // cancels this ring, begins the next +queuedRing.cancelCurrentAndQueued(); // cancels this ring, empties the queue + +// Query if active now, or subscribe to all activity updates +queuedRing.isActive.value +queuedRing.isActive.subscribe + +// The current error (cleared when a new execution begins), or all errors +queuedRing.currentError.value +queuedRing.errors.subscribe + +// See @rxfx/service for more +``` + +# Usage in React + +The `useService` hook from `@rxfx/react` brings the current `isActive` status and `currentError` into React. + +```ts +function BellComponent() { + const { isActive, currentError } = useService(queuedRing); + // render 🛎️ if active +} +``` + +# Usage in Angular + +```ts +import { queuedRing } from '../services/ringer' + +export class BellComponent { + isActive$: Observable; + + constructor() { + this.isActive$ = queuedRing.isActive; + } + + ring() { + queuedRing(); + } + +} + +// html +