6.0.4 • Published 4 years ago

generic-data-chamber v6.0.4

Weekly downloads
1,467
License
ISC
Repository
github
Last release
4 years ago

generic-data-chamber

NPM version NPM license NPM total downloads NPM monthly downloads

A global data store that is library agnostic.

Installation

npm install generic-data-chamber

Importing

import { Store } from "generic-data-chamber";

Usage

1. Create a Store

import { Store } from "generic-data-chamber";
import userService from "./services/user";
import userType from "./types/user";

const store = new Store({
  name: "app",
  services: { user: userService },
  types: { user: userType },
});

2. Create a Type

import actions from "./actions";

const user = {
  name: "user",
  state: {
    id: null,
    firstName: "",
    lastName: "",
  },
  actions: {
    getByIdAsync: {
      reducer: actions.getByIdAsync,
      configs: {
        isPending: true,
        shouldThrowErrors: false,
        shouldTrackAsyncState: false,
      },
    },
    update: actions.update,
  },
};

3. Create an Action

const getByIdAsync = ({ services, prevState }, userId) => {
  return services.user.getByIdAsync(userId).then((user) => {
    return { ...prevState, ...user };
  });
};

4. Subscribe/Unsubscribe to the Store

import appStore from "./stores/app";

const subscription = appStore.subscribe((store) => {
  const { firstName, lastName } = store.getState("user");
  console.log(`${firstName} ${lastName}`);
});

subscription.unsubscribe();

5. Dispatch Actions

import appStore from "./stores/app";

appStore.dispatchAsync("user.getByIdAsync", 1182);
appStore.dispatch("user.update", { firstName: "Scotty" });

6. Get Status of Async Actions

import appStore from "./stores/app";

const isPending = appStore.isPending("user.getByIdAsync");
const isError = appStore.isError("user.getByIdAsync");
const error = appStore.getError("user.getByIdAsync");
6.0.4

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

3.0.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago