Gathering detailed insights and metrics for @hutechtechnical/impedit-beatae-repudiandae-minima
Gathering detailed insights and metrics for @hutechtechnical/impedit-beatae-repudiandae-minima
Gathering detailed insights and metrics for @hutechtechnical/impedit-beatae-repudiandae-minima
Gathering detailed insights and metrics for @hutechtechnical/impedit-beatae-repudiandae-minima
npm install @hutechtechnical/impedit-beatae-repudiandae-minima
Typescript
Module System
Node Version
NPM Version
65.4
Supply Chain
48.1
Quality
75.9
Maintenance
100
Vulnerability
100
License
Total Downloads
100
Last Day
1
Last Week
1
Last Month
2
Last Year
100
Latest Version
1.0.0
Package Id
@hutechtechnical/impedit-beatae-repudiandae-minima@1.0.0
Unpacked Size
17.40 kB
Size
6.38 kB
File Count
8
NPM Version
10.5.0
Node Version
20.12.2
Publised On
04 May 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
1
Compared to previous week
Last month
0%
2
Compared to previous month
Last year
0%
100
Compared to previous year
27
Reactive state and effect management with RxJS.
The 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.
The library is inspired by MVC, RxJS, Akita, JetState and Effector.
It is recommended to use RxEffects together with Ditox.js – a dependency injection container.
Version 1.0 contains breaking changes due stabilizing API from the early stage. The previous API is available in 0.7.2 version.
Documentation is coming soon.
Package | Description | Links |
---|---|---|
@hutechtechnical/impedit-beatae-repudiandae-minima | Core elements, state and effect management | Docs, API |
@hutechtechnical/impedit-beatae-repudiandae-minima-react | Tooling for React.js | Docs, API |
npm install @hutechtechnical/impedit-beatae-repudiandae-minima @hutechtechnical/impedit-beatae-repudiandae-minima-react --save
The 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.
Core elements:
State
– a data model.Query
– a getter and subscriber for data of the state.StateMutation
– a pure function which changes the state.Store
– a state storage, it provides methods to update and subscribe the state.Action
– an event emitter.Effect
– a business logic which handles the action and makes state changes and side effects.Controller
– a controller type for effects and business logicScope
– a controller-like boundary for effects and business logicBelow 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.
1// pizzaShop.ts 2 3import { 4 Controller, 5 createAction, 6 createScope, 7 declareStateUpdates, 8 EffectState, 9 Query, 10 withStoreUpdates, 11} from '@hutechtechnical/impedit-beatae-repudiandae-minima'; 12import { delay, filter, map, mapTo, of } from 'rxjs'; 13 14// The state 15type CartState = Readonly<{ orders: Array<string> }>; 16 17// Declare the initial state. 18const CART_STATE: CartState = { orders: [] }; 19 20// Declare updates of the state. 21const CART_STATE_UPDATES = declareStateUpdates<CartState>({ 22 addPizzaToCart: (name: string) => (state) => ({ 23 ...state, 24 orders: [...state.orders, name], 25 }), 26 27 removePizzaFromCart: (name: string) => (state) => ({ 28 ...state, 29 orders: state.orders.filter((order) => order !== name), 30 }), 31}); 32 33// Declaring the controller. 34// It should provide methods for triggering the actions, 35// and queries or observables for subscribing to data. 36export type PizzaShopController = Controller<{ 37 ordersQuery: Query<Array<string>>; 38 39 addPizza: (name: string) => void; 40 removePizza: (name: string) => void; 41 submitCart: () => void; 42 submitState: EffectState<Array<string>>; 43}>; 44 45export function createPizzaShopController(): PizzaShopController { 46 // Creates the scope to track subscriptions 47 const scope = createScope(); 48 49 // Creates the state store 50 const store = withStoreUpdates( 51 scope.createStore(CART_STATE), 52 CART_STATE_UPDATES, 53 ); 54 55 // Creates queries for the state data 56 const ordersQuery = store.query((state) => state.orders); 57 58 // Introduces actions 59 const addPizza = createAction<string>(); 60 const removePizza = createAction<string>(); 61 const submitCart = createAction(); 62 63 // Handle simple actions 64 scope.handle(addPizza, (order) => store.updates.addPizzaToCart(order)); 65 66 scope.handle(removePizza, (name) => store.updates.removePizzaFromCart(name)); 67 68 // Create a effect in a general way 69 const submitEffect = scope.createEffect<Array<string>>((orders) => { 70 // Sending an async request to a server 71 return of(orders).pipe(delay(1000), mapTo(undefined)); 72 }); 73 74 // Effect can handle `Observable` and `Action`. It allows to filter action events 75 // and transform data which is passed to effect's handler. 76 submitEffect.handle( 77 submitCart.event$.pipe( 78 map(() => ordersQuery.get()), 79 filter((orders) => !submitEffect.pending.get() && orders.length > 0), 80 ), 81 ); 82 83 // Effect's results can be used as actions 84 scope.handle(submitEffect.done$, () => store.set(CART_STATE)); 85 86 return { 87 ordersQuery, 88 addPizza, 89 removePizza, 90 submitCart, 91 submitState: submitEffect, 92 destroy: () => scope.destroy(), 93 }; 94}
1// pizzaShopComponent.tsx 2 3import React, { FC, useEffect } from 'react'; 4import { useConst, useObservable, useQuery } from '@hutechtechnical/impedit-beatae-repudiandae-minima-react'; 5import { createPizzaShopController } from './pizzaShop'; 6 7export const PizzaShopComponent: FC = () => { 8 // Creates the controller and destroy it on unmounting the component 9 const controller = useConst(() => createPizzaShopController()); 10 useEffect(() => controller.destroy, [controller]); 11 12 // The same creation can be achieved by using `useController()` helper: 13 // const controller = useController(createPizzaShopController); 14 15 // Using the controller 16 const { ordersQuery, addPizza, removePizza, submitCart, submitState } = 17 controller; 18 19 // Subscribing to state data and the effect stata 20 const orders = useQuery(ordersQuery); 21 const isPending = useQuery(submitState.pending); 22 const submitError = useObservable(submitState.error$, undefined); 23 24 return ( 25 <> 26 <h1>Pizza Shop</h1> 27 28 <h2>Menu</h2> 29 <ul> 30 <li> 31 Pepperoni 32 <button disabled={isPending} onClick={() => addPizza('Pepperoni')}> 33 Add 34 </button> 35 </li> 36 37 <li> 38 Margherita 39 <button disabled={isPending} onClick={() => addPizza('Margherita')}> 40 Add 41 </button> 42 </li> 43 </ul> 44 45 <h2>Cart</h2> 46 <ul> 47 {orders.map((name) => ( 48 <li> 49 {name} 50 <button disabled={isPending} onClick={() => removePizza(name)}> 51 Remove 52 </button> 53 </li> 54 ))} 55 </ul> 56 57 <button disabled={isPending || orders.length === 0} onClick={submitCart}> 58 Submit 59 </button> 60 61 {submitError && <div>Failed to submit the cart</div>} 62 </> 63 ); 64};
© 2021 Mikhail Nasyrov, MIT license
No vulnerabilities found.
No security vulnerabilities found.