Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

generic-data-chamber

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

generic-data-chamber

A global data store that is library agnostic.

  • 6.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
increased by181.76%
Maintainers
1
Weekly downloads
 
Created
Source

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");

Keywords

FAQs

Package last updated on 05 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc