rxstor
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

rxstor (reactive store)

a lightweight store based on RxJS for state management

Usage

import { createValueStore, createEntityStore } from "storerx";

/** value store */
const foo = createValueStore(0);

const foo$ = foo.get$; // Observable<number>

foo.get(); // 0
foo.set(2);
foo.get(); // 2;
foo.clear();
foo.get(); // null;

foo.complete(); // completes all subscriptions to the store

/** entity store */
interface Bar {
  id: string;
  a: any;
  b: any;
}

const bars = createEntityStore<Bar>();

bars.set([
  { id: "1", a: 1, b: 1 },
  { id: "2", a: 2, b: 2 },
  { id: "3", a: 3, b: 3 }
]);

bars.list();
/**
[
  { id: "1", a: 1, b: 1 },
  { id: "2", a: 2, b: 2 },
  { id: "3", a: 3, b: 3 }
]
*/

bars.ids(); // ["1", "2", "3"]
bars.entity("2"); // { id: "2", a: 2, b: 2 }
bars.query(e => e.a === 3); // [{ id: "3", a: 3, b: 3 }]
bars.length(); // 3
bars.map();
/**
{
  1: { id: "1", a: 1, b: 1 },
  2: { id: "2", a: 2, b: 2 },
  3: { id: "3", a: 3, b: 3 }
}
*/

bars.insert("5", { id: "5", a: 5, b: 5 });
bars.length(); // 4

bars.delete("2");
bars.length(); // 3
bars.delete("2");
bars.length(); // 3, still 3 because entity with id="2" doesn't exist anymore

bars.update("1", { a: 10 });
bars.entity("1"); // { id: "1", a: 10, b: 1 };

bars.clear();
bars.length(); // 0

bars.entity$("1"); // Observable<Bar>
bars.list$; // Observable<Bar[]>
bars.query$(e => e.a > 1); // Observable<Bar[]>
bars.ids$; // Observable<string[]>
bars.length$; // Observable<number>
bars.map$; // Observable<Dictionary<Bar>>
bars.changes$; // Observable<EntityChange>

bars.complete(); // completes all subscriptions to the store

Readme

Keywords

Package Sidebar

Install

npm i rxstor

Weekly Downloads

7

Version

1.0.6

License

MIT

Unpacked Size

11.3 kB

Total Files

12

Last publish

Collaborators

  • delashum
  • hivewire-dev