A global data store that is library agnostic.
npm install generic-data-chamber!NPM version
!NPM license
!NPM total downloads
!NPM monthly downloads
A global data store that is library agnostic.
``bash`
npm install generic-data-chamber
`js`
import { Store } from "generic-data-chamber";
#### 1. Create a Store
`js
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
`js
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
`js`
const getByIdAsync = ({ services, prevState }, userId) => {
return services.user.getByIdAsync(userId).then((user) => {
return { ...prevState, ...user };
});
};
#### 4. Subscribe/Unsubscribe to the Store
`js
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
`js
import appStore from "./stores/app";
appStore.dispatchAsync("user.getByIdAsync", 1182);
appStore.dispatch("user.update", { firstName: "Scotty" });
`
#### 6. Get Status of Async Actions
`js
import appStore from "./stores/app";
const isPending = appStore.isPending("user.getByIdAsync");
const isError = appStore.isError("user.getByIdAsync");
const error = appStore.getError("user.getByIdAsync");
``