npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@brainstack/store

v1.0.149

Published

An Micro Pub Sub Event Driven State Management

Downloads

420

Readme

@brainstack/store

A package that combines state management with event handling, providing a convenient solution for managing application state and responding to state changes using event-driven programming.

Installation

To use @brainstack/store, install it using npm or yarn:

npm install @brainstack/store

or

yarn add @brainstack/store

Usage

@brainstack/store offers a way to create a microstore that integrates @brainstack/state and @brainstack/hub libraries to handle state management and event handling.

Here's how you can use @brainstack/store:

import { createStore } from '@brainstack/store';

// Create a microstore instance
const microstore = createStore();

// Subscribe to state changes
const unsubscribe = microstore.subscribe((currentState) => {
  console.log('State changed:', currentState);
});

// Update the state using the mutate method
microstore.mutate((currentState) => {
  return { ...currentState, key: 'new value' };
});

// Unsubscribe when done
unsubscribe();

API

createStore(options?: TCreateStoreOptions)

Creates a store instance with integrated state management and event handling.

  • options: An optional object containing the following properties:
    • initializer: A function that initializes the initial state.
    • eventHubOptions: Options for configuring the event hub.

Store Instance Methods

  • mutate(mutator: (_state: ReturnType<typeof state.getState>) => any): Updates the microstore state with a new value and emits a "state.changed" event.

  • subscribe(callback: (_state: ReturnType<typeof state.getState>) => void): Subscribes to changes in the microstore state and invokes the provided callback when the state changes.

createCRUDObject(domain: keyof typeof state)

Creates a set of CRUD operations for a specific domain in the state which is expected to be an object.

CRUD Object Methods

  • create(item: any): Adds a new item to the domain.
  • read(item: any): Reads an item from the domain by its ID.
  • update(item: any): Updates an item in the domain by its ID.
  • delete(item: { id: string }): Deletes an item from the domain by its ID.
  • list(): Lists all items in the domain.
  • search(keyword: string): Searches items in the domain by a keyword.

createCRUDArray(domain: keyof typeof state)

Creates a set of CRUD operations for a specific domain in the state which is expected to be an array.

CRUD Array Methods

  • create(item: any): Adds a new item to the domain.
  • read(item: { id: string }): Reads an item from the domain by its ID.
  • update(updatedItem: any): Updates an item in the domain by its ID.
  • delete(item: { id: string }): Deletes an item from the domain by its ID.
  • list(): Lists all items in the domain.
  • search(keyword: string): Searches items in the domain by a keyword.

Comprehensive Usage Example

In this example, we'll demonstrate how to use @brainstack/store along with the createCRUDObject and createCRUDArray utility functions. We'll create a store with two domains: profiles (using an object structure) and tasks (using an array structure).

1. Setting Up

First, import the necessary functions and set up your initial state:

import { createStore } from '@brainstack/store';
// ... (import other necessary utilities such as createCRUDObject and createCRUDArray)

const initialState = {
  profiles: {},
  tasks: []
};

const store = createStore({ initializer: initialState });

2. Create CRUD for Each Domain

Now, we'll generate CRUD operations for both profiles and tasks:

const profilesCRUD = createCRUDObject('profiles');
const tasksCRUD = createCRUDArray('tasks');

3. Using the CRUD Operations

Profiles (Object structure)

// Create
const newProfile = { name: 'Alice Brown', age: 25 };
profilesCRUD.create(newProfile);

// Read
const profile = profilesCRUD.read({ id: '123' });
console.log(profile);

// Update
const updatedProfile = { id: '123', name: 'Alice B.', age: 26 };
profilesCRUD.update(updatedProfile);

// Delete
profilesCRUD.delete({ id: '124' });

// List
const allProfiles = profilesCRUD.list();
console.log(allProfiles);

// Search
const foundProfiles = profilesCRUD.search('Alice');
console.log(foundProfiles);

Tasks (Array structure)

// Create
const newTask = { title: 'Go for a walk', completed: false };
const taskId = tasksCRUD.create(newTask);
console.log(taskId);

// Read
const task = tasksCRUD.read({ id: 't1' });
console.log(task);

// Update
const updatedTask = { id: 't1', title: 'Read two books', completed: true };
tasksCRUD.update(updatedTask);

// Delete
tasksCRUD.delete({ id: 't2' });

// List
const allTasks = tasksCRUD.list();
console.log(allTasks);

// Search
const searchTasks = tasksCRUD.search('walk');
console.log(searchTasks);

Contributing

Contributions are welcome! If you would like to contribute to this module, please follow these guidelines:

  • Fork the repository
  • Create a new branch for your changes
  • Make your changes and commit them with descriptive commit messages
  • Push your changes to your fork
  • Submit a pull request

License

This module is released under the MIT License.