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

0.0.7 • Public • Published

issues forks stars license tested with jest codecov powered by MobX build with Parcel

perfection-itemstore

This itemstore is an opinionated way to manage all kinds of items in a store. It is intended to be extendable and simple to use in all kinds of reactive applications.

How to use

Create an interface, which is used to construct data.

interface ApiExampleItem {
  id: string; // ID Is required by BaseItem.
  value: number;
}

Use that interface in an Item like the following example. The second generic parameter is the type of the key, which could be number, string or guid for example. This can be helpful for determining strong types.

By default, id is exposed. Other members can be exposed with MobX like in the example as value.

class ExampleItem extends BaseItem<ApiExampleItem, string> {
  // MobX computed. this._x is ApiExampleItem.
  @computed
  public get value(): number {
    return this._x.value;
  }
}

Alternatively, AsyncItem can be constructed with an additional resolver and optionally a default state, i.e. State.Partial.

class ExampleAsyncItem extends AsyncItem<ApiExampleItem, string> {
  // MobX computed. this._x is ApiExampleItem.
  @computed
  public get value(): number {
    return this._x.value;
  }
}

Finally you can define your ItemStore. It will try to infer types from the resolver functions. But you can explicitly set the BaseItem type and key type.

// Explicit example 'new ItemStore<ExampleItem, string>(a, b);
const exampleStore = new ItemStore(
  () => {
    // You could use somekind of async library here.
    return Promise.resolve([
      ["some-key-1", new ExampleItem({ id: "some-key-1", value: 0 })],
      ["some-key-2", new ExampleItem({ id: "some-key-2", value: 1 })],
      ["some-key-3", new ExampleItem({ id: "some-key-3", value: 2 })],
    ]);
  },
  (key) => {
    // Key would be of type string in this example.
    // You could use somekind of async library here.
    return Promise.resolve([key, new ExampleItem({ id: key, value: 0 })]);
  }
);

Get started with developing

Install packages.

yarn

Running linter.

yarn lint

Running tests.

yarn test

Build and watch for development.

yarn dev

Build for production.

yarn build

License

This project is licensed under the MIT License - see the LICENSE file for details

Readme

Keywords

none

Package Sidebar

Install

npm i perfection-itemstore

Weekly Downloads

0

Version

0.0.7

License

MIT

Unpacked Size

109 kB

Total Files

19

Last publish

Collaborators

  • perfectionvr