{"id":22349136,"url":"https://github.com/mnasyrov/rx-effects","last_synced_at":"2025-08-25T03:36:09.613Z","repository":{"id":57355887,"uuid":"383312036","full_name":"mnasyrov/rx-effects","owner":"mnasyrov","description":"Reactive state and effect management with RxJS","archived":false,"fork":false,"pushed_at":"2024-07-12T21:42:16.000Z","size":2714,"stargazers_count":36,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"1.x","last_synced_at":"2025-04-14T18:51:16.923Z","etag":null,"topics":["effects","react","reactive","state-management"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mnasyrov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-07-06T02:05:28.000Z","updated_at":"2025-01-20T08:10:12.000Z","dependencies_parsed_at":"2024-01-02T23:42:36.210Z","dependency_job_id":"42d1fb98-0a35-46d7-8db0-8bb0b1b77b5d","html_url":"https://github.com/mnasyrov/rx-effects","commit_stats":{"total_commits":152,"total_committers":3,"mean_commits":"50.666666666666664","dds":"0.019736842105263164","last_synced_commit":"f77f8b0447e086d8780676a00f3c449c52f5b837"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnasyrov%2Frx-effects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnasyrov%2Frx-effects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnasyrov%2Frx-effects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnasyrov%2Frx-effects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mnasyrov","download_url":"https://codeload.github.com/mnasyrov/rx-effects/tar.gz/refs/heads/1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251342724,"owners_count":21574245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["effects","react","reactive","state-management"],"created_at":"2024-12-04T11:07:26.820Z","updated_at":"2025-04-28T16:10:22.665Z","avatar_url":"https://github.com/mnasyrov.png","language":"TypeScript","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"# RxEffects\n\n\u003cimg alt=\"rocket\" src=\"rocket.svg\" width=\"120\" /\u003e\n\nReactive state and effect management with RxJS.\n\n[![npm](https://img.shields.io/npm/v/rx-effects.svg)](https://www.npmjs.com/package/rx-effects)\n[![downloads](https://img.shields.io/npm/dt/rx-effects.svg)](https://www.npmjs.com/package/rx-effects)\n[![types](https://img.shields.io/npm/types/rx-effects.svg)](https://www.npmjs.com/package/rx-effects)\n[![licence](https://img.shields.io/github/license/mnasyrov/rx-effects.svg)](https://github.com/mnasyrov/rx-effects/blob/master/LICENSE)\n[![Coverage Status](https://coveralls.io/repos/github/mnasyrov/rx-effects/badge.svg?branch=main)](https://coveralls.io/github/mnasyrov/rx-effects?branch=main)\n\n## Overview\n\nThe library provides a way to describe business and application logic using MVC-like architecture. Core elements include actions and effects, states and stores. All of them are optionated and can be used separately. The core package is framework-agnostic and can be used in different cases: libraries, server apps, web, SPA and micro-frontends apps.\n\nThe library is inspired by [MVC](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), [RxJS](https://github.com/ReactiveX/rxjs), [Akita](https://github.com/datorama/akita), [JetState](https://github.com/mnasyrov/jetstate) and [Effector](https://github.com/effector/effector).\n\nIt is recommended to use RxEffects together with [Ditox.js] – a dependency injection container.\n\n### Features\n\n- Reactive state and store\n- Declarative actions and effects\n- Effect container\n- Framework-agnostic\n- Functional API\n- Typescript typings\n\n### Breaking changes\n\nVersion 1.0 contains breaking changes due stabilizing API from the early stage. The previous API is available in 0.7.2 version.\n\nDocumentation is coming soon.\n\n### Packages\n\n| Package                                       | Description                                | Links                                                      |\n| --------------------------------------------- | ------------------------------------------ | ---------------------------------------------------------- |\n| [**rx-effects**][rx-effects/docs]             | Core elements, state and effect management | [Docs][rx-effects/docs], [API][rx-effects/api]             |\n| [**rx-effects-react**][rx-effects-react/docs] | Tooling for React.js                       | [Docs][rx-effects-react/docs], [API][rx-effects-react/api] |\n\n## Usage\n\n### Installation\n\n```\nnpm install rx-effects rx-effects-react --save\n```\n\n### Concepts\n\nThe main idea is to use the classic MVC pattern with event-based models (state stores) and reactive controllers (actions and effects). The view subscribes to model changes (state queries) of the controller and requests the controller to do some actions.\n\n\u003cimg alt=\"concept-diagram\" src=\"docs/concept-diagram.svg\" width=\"400\" /\u003e\n\nCore elements:\n\n- `State` – a data model.\n- `Query` – a getter and subscriber for data of the state.\n- `StateMutation` – a pure function which changes the state.\n- `Store` – a state storage, it provides methods to update and subscribe the state.\n- `Action` – an event emitter.\n- `Effect` – a business logic which handles the action and makes state changes and side effects.\n- `Controller` – a controller type for effects and business logic\n- `Scope` – a controller-like boundary for effects and business logic\n\n### Example\n\nBelow is an implementation of the pizza shop, which allows order pizza from the menu and to submit the cart. The controller orchestrate the state store and side effects. The component renders the state and reacts on user events.\n\n```ts\n// pizzaShop.ts\n\nimport {\n  Controller,\n  createAction,\n  createScope,\n  declareStateUpdates,\n  EffectState,\n  Query,\n  withStoreUpdates,\n} from 'rx-effects';\nimport { delay, filter, map, mapTo, of } from 'rxjs';\n\n// The state\ntype CartState = Readonly\u003c{ orders: Array\u003cstring\u003e }\u003e;\n\n// Declare the initial state.\nconst CART_STATE: CartState = { orders: [] };\n\n// Declare updates of the state.\nconst CART_STATE_UPDATES = declareStateUpdates\u003cCartState\u003e({\n  addPizzaToCart: (name: string) =\u003e (state) =\u003e ({\n    ...state,\n    orders: [...state.orders, name],\n  }),\n\n  removePizzaFromCart: (name: string) =\u003e (state) =\u003e ({\n    ...state,\n    orders: state.orders.filter((order) =\u003e order !== name),\n  }),\n});\n\n// Declaring the controller.\n// It should provide methods for triggering the actions,\n// and queries or observables for subscribing to data.\nexport type PizzaShopController = Controller\u003c{\n  ordersQuery: Query\u003cArray\u003cstring\u003e\u003e;\n\n  addPizza: (name: string) =\u003e void;\n  removePizza: (name: string) =\u003e void;\n  submitCart: () =\u003e void;\n  submitState: EffectState\u003cArray\u003cstring\u003e\u003e;\n}\u003e;\n\nexport function createPizzaShopController(): PizzaShopController {\n  // Creates the scope to track subscriptions\n  const scope = createScope();\n\n  // Creates the state store\n  const store = withStoreUpdates(\n    scope.createStore(CART_STATE),\n    CART_STATE_UPDATES,\n  );\n\n  // Creates queries for the state data\n  const ordersQuery = store.query((state) =\u003e state.orders);\n\n  // Introduces actions\n  const addPizza = createAction\u003cstring\u003e();\n  const removePizza = createAction\u003cstring\u003e();\n  const submitCart = createAction();\n\n  // Handle simple actions\n  scope.handle(addPizza, (order) =\u003e store.updates.addPizzaToCart(order));\n\n  scope.handle(removePizza, (name) =\u003e store.updates.removePizzaFromCart(name));\n\n  // Create a effect in a general way\n  const submitEffect = scope.createEffect\u003cArray\u003cstring\u003e\u003e((orders) =\u003e {\n    // Sending an async request to a server\n    return of(orders).pipe(delay(1000), mapTo(undefined));\n  });\n\n  // Effect can handle `Observable` and `Action`. It allows to filter action events\n  // and transform data which is passed to effect's handler.\n  submitEffect.handle(\n    submitCart.event$.pipe(\n      map(() =\u003e ordersQuery.get()),\n      filter((orders) =\u003e !submitEffect.pending.get() \u0026\u0026 orders.length \u003e 0),\n    ),\n  );\n\n  // Effect's results can be used as actions\n  scope.handle(submitEffect.done$, () =\u003e store.set(CART_STATE));\n\n  return {\n    ordersQuery,\n    addPizza,\n    removePizza,\n    submitCart,\n    submitState: submitEffect,\n    destroy: () =\u003e scope.destroy(),\n  };\n}\n```\n\n```tsx\n// pizzaShopComponent.tsx\n\nimport React, { FC, useEffect } from 'react';\nimport { useConst, useObservable, useQuery } from 'rx-effects-react';\nimport { createPizzaShopController } from './pizzaShop';\n\nexport const PizzaShopComponent: FC = () =\u003e {\n  // Creates the controller and destroy it on unmounting the component\n  const controller = useConst(() =\u003e createPizzaShopController());\n  useEffect(() =\u003e controller.destroy, [controller]);\n\n  // The same creation can be achieved by using `useController()` helper:\n  // const controller = useController(createPizzaShopController);\n\n  // Using the controller\n  const { ordersQuery, addPizza, removePizza, submitCart, submitState } =\n    controller;\n\n  // Subscribing to state data and the effect stata\n  const orders = useQuery(ordersQuery);\n  const isPending = useQuery(submitState.pending);\n  const submitError = useObservable(submitState.error$, undefined);\n\n  return (\n    \u003c\u003e\n      \u003ch1\u003ePizza Shop\u003c/h1\u003e\n\n      \u003ch2\u003eMenu\u003c/h2\u003e\n      \u003cul\u003e\n        \u003cli\u003e\n          Pepperoni\n          \u003cbutton disabled={isPending} onClick={() =\u003e addPizza('Pepperoni')}\u003e\n            Add\n          \u003c/button\u003e\n        \u003c/li\u003e\n\n        \u003cli\u003e\n          Margherita\n          \u003cbutton disabled={isPending} onClick={() =\u003e addPizza('Margherita')}\u003e\n            Add\n          \u003c/button\u003e\n        \u003c/li\u003e\n      \u003c/ul\u003e\n\n      \u003ch2\u003eCart\u003c/h2\u003e\n      \u003cul\u003e\n        {orders.map((name) =\u003e (\n          \u003cli\u003e\n            {name}\n            \u003cbutton disabled={isPending} onClick={() =\u003e removePizza(name)}\u003e\n              Remove\n            \u003c/button\u003e\n          \u003c/li\u003e\n        ))}\n      \u003c/ul\u003e\n\n      \u003cbutton disabled={isPending || orders.length === 0} onClick={submitCart}\u003e\n        Submit\n      \u003c/button\u003e\n\n      {submitError \u0026\u0026 \u003cdiv\u003eFailed to submit the cart\u003c/div\u003e}\n    \u003c/\u003e\n  );\n};\n```\n\n---\n\n[rx-effects/docs]: packages/rx-effects/README.md\n[rx-effects/api]: packages/rx-effects/docs/README.md\n[rx-effects-react/docs]: packages/rx-effects-react/README.md\n[rx-effects-react/api]: packages/rx-effects-react/docs/README.md\n[ditox.js]: https://github.com/mnasyrov/ditox\n\n\u0026copy; 2021 [Mikhail Nasyrov](https://github.com/mnasyrov), [MIT license](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnasyrov%2Frx-effects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmnasyrov%2Frx-effects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnasyrov%2Frx-effects/lists"}