Qstore

Javascript library for state management based on RxJS.
It's a simple and lightweight alternative to NGRX, Redux, MobX, etc. Use it if it fits your needs.
Made to work perfectly with Angular.
Usage
You need to install RxJS library first. (Omit for Angular projects)
npm install rxjs
Create store
import {Store} from 'your-path/to/qstore';
interface State {
total: number;
users: string[];
}
const initialState: State = {
total: 0,
users: []
};
export class UsersStore extends Store<State> {
constructor() {
super(initialState);
}
}Add user to store
class UsersStore extends Store<State> {
// constructor...
addUser(user: string): void {
this.set({
total: this.values.total + 1,
users: [...this.values.users, user]
});
}
}Listen to store changes
class UsersStore extends Store<State> {
total$ = this.select(state => state.total);
users$ = this.select(state => state.users);
// ...
}Angular component store usage
@Component({
selector: 'app-users',
template: `
<div *ngIf="usersStore.total$ | async as total">
Total users: {{ total }}
</div>
<div *ngIf="usersStore.users$ | async as users">
Users: {{ users | json }}
</div>
`
})
export class UsersComponent implements OnInit {
constructor(public usersStore: UsersStore) {}
ngOnInit(): void {
this.usersStore.addUser('John');
this.usersStore.addUser('Jane');
}
}Add actions to store
Action is a function that can be executed and listened to. It's useful when you need to listen to actions from other stores or views.
import {Action, ActionWith} from 'your-path/to/qstore';
class UsersStore extends Store<State> {
addUser = ActionWith<string>();
removeUser = ActionWith<string>();
reset = Action();
constructor() {
super(initialState);
this.addUser.listen().subscribe(user => this._addUser(user));
this.removeUser.listen().subscribe(user => this._removeUser(user));
this.reset.listen().subscribe(() => this._reset());
}
private _addUser(user: string): void {
this.set({
total: this.values.total + 1,
users: [...this.values.users, user]
});
}
private _removeUser(user: string): void {
this.set({
total: this.values.total - 1,
users: this.values.users.filter(u => u !== user)
});
}
private _reset(): void {
this.set(initialState);
}
}Listen to actions from other stores
class UsersStore extends Store<State> {
constructor(private authStore: AuthStore) {
super(initialState);
this.authStore.logout.listen().subscribe(() => this.reset());
}
}Execute actions
class UsersComponent implements OnInit {
constructor(public usersStore: UsersStore) {}
ngOnInit(): void {
this.usersStore.addUser.execute('John');
this.usersStore.addUser.execute('Jane');
}
}Install
I closed NPM package - it's so small that you should just copy-paste it to your project.
# Closed NPM packageFor RxJS Version
import {BehaviorSubject, Subject, Observable} from 'rxjs';
import {distinctUntilChanged, map} from 'rxjs/operators';
// Store
export abstract class Store<T> {
private state$: BehaviorSubject<T>;
protected constructor(initialState: T) {
this.state$ = new BehaviorSubject(initialState);
}
select<K>(mapFn: (state: T) => K): Observable<K> {
return this.state$.asObservable().pipe(
map((state: T) => mapFn(state)),
distinctUntilChanged()
);
}
set(state: Partial<T>): void {
this.state$.next({...this.values, ...state});
}
get values(): T {
return this.state$.getValue();
}
}
// Actions
export interface StoreEvent {
execute: () => void;
listen: () => Observable<void>;
}
export interface StoreEventWithPayload<P> {
execute: (payload: P) => void;
listen: () => Observable<P>;
}
export const Action = (): StoreEvent => {
const action$ = new Subject<void>();
return {
listen: (): Observable<void> => action$,
execute: () => action$.next()
}
};
export const ActionWith = <Payload>(): StoreEventWithPayload<Payload> => {
const payload$ = new Subject<Payload>();
return {
listen: (): Observable<Payload> => payload$,
execute: (payload: Payload) => payload$.next(payload)
}
};License
To the code in this page below license applies.
MIT License
Copyright (c) 2023 QUAK Kacper Walczak
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.Made & maintained with ❤️ by QUAK (opens in a new tab)