Gathering detailed insights and metrics for @xania/state
Gathering detailed insights and metrics for @xania/state
Gathering detailed insights and metrics for @xania/state
Gathering detailed insights and metrics for @xania/state
npm install @xania/state
Typescript
Module System
Min. Node Version
Node Version
NPM Version
74.6
Supply Chain
98.5
Quality
76.1
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
5,867
Last Day
1
Last Week
5
Last Month
143
Last Year
919
MIT License
8 Stars
64 Commits
3 Watchers
1 Branches
1 Contributors
Updated on Jul 13, 2023
Minified
Minified + Gzipped
Latest Version
0.2.0-alpha.1
Package Id
@xania/state@0.2.0-alpha.1
Unpacked Size
145.61 kB
Size
37.71 kB
File Count
39
NPM Version
9.6.0
Node Version
19.3.0
Published on
Mar 31, 2023
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-78.3%
5
Compared to previous week
Last Month
393.1%
143
Compared to previous month
Last Year
-81%
919
Compared to previous year
No dependencies detected.
Is blazingly fast javascript library for managing reactive state.
Visit the docs website to learn more about how to use @xania/state in your project.
1npm install @xania/state
1import { State } from "@xania/state" 2 3const a = new State(1); 4const b = a.map(x => x + 1); 5const c = a.map(x => x * 2); 6const d = a.bind(x => x % 2 === ? b : c);
@xania/state is intended to be complementary to RxJS and is especially an alternative to BehaviorSubject and it's main goal to provide reactivity for the View library. RxJS is on the other hand better suited for handling events coming from view, e.g. for debounce, async, etc...
;
In realworld, a state is practically never isolated from other parts of the state. e.g. firstName
can be represented by a state object but it is also part of the Employee
state and the two and all there observers needs to be in sync. Most used pattern for updating firstName
in other libraries use a coarse grained approach where the whole person updates is updates. Biggest disadvantage is that the handling of firstName
gets entangled with the structure of Person
, which also means that this affected by any changes in structure.
@xania/state provides a different pattern using fine-grained approach. @xania/state provides a prop
method to create an isolated state object and uses (internal) operators to keep the parent en it's property in sync.
1const person = new State({ 2 firstName: 'Ibrahim', 3}); 4const firstName = person.prop('firstName'); 5firstName.set('Ramy'); 6 7console.log(person.get().firstName); // prints 'Ramy'
1import * as Rx from 'rxjs'; 2 3const s = new State(1); 4const x = Rx.from(s);
1const x = Rx.timer(0, 1000); 2const s = State.from(x);
State objects encapsulate values and are responsible for synchronisation with derived state and derived state of derived state etc..
1new State(1).set(2);
The are three core methods of creating a derived state.
prop
: property of a root objectmap
: monadic mapbind
: monadic bindState mutation is done by calling set
on root object. Derived states do not allow for direct mutations. Changes can only flow from root states with one exception, namely a property state mapped directly of a root state in which case we can guarantee consistency for all derived states.
Xania does not ignore the fact that we can mutate the state outside the formal set operation. That's where sync
comes into play.
1const data = { 2 firstName: 'Ibrahim', 3}; 4const person = new State(data); 5const firstName = state.prop('firstName'); 6firstName.subscribe({ 7 next(x) { 8 console.log('Hello, ', x); 9 }, 10}); 11// console output: Hello, Ibrahim 12 13// external mutation 14data.firstName = 'Ramy'; 15sync(person); 16// console output: Hello, Ramy
To maintain
Xania ensures correct order in which the updates are propagated in a single roundtrip. This is especially important in case of the known diamond problem.
No vulnerabilities found.
No security vulnerabilities found.