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

immutable-item-store

v1.0.0

Published

Store your items of data in a store powered by immutability

Downloads

7

Readme

🗃️ Immutable Item Store

NPM version npm

Store your items of data in a store powered by immutability

Install

$ npm install immutable-item-store

Usage

import { ItemStore }  from 'immutable-item-store';

// Declare your item class
class User {
  id = 'some-id';
  name = 'user name';
  isAdmin = false;
}

// Create an instance and pass item interface
const userStore = new ItemStore<User>();
const newUser = new User();

// Add new item
const { itemsDraft, patches, inversePatches, confirm } = userStore.add(newUser);
console.log('items size:', userStore.items.size); // items size: 0
console.log('items draft size:', itemsDraft.size); // items draft size: 1

confirm(); // or userStore.applyPatches(patches)
console.log('items size:', userStore.items.size); // items size: 1

userStore.applyPatches(inversePatches);
console.log('items size:', userStore.items.size); // items size: 0

// Update item
userStore.update('some-id', { isAdmin: true }).confirm();

// Batch update items
const updates: BatchUpdate<User>[] = [
  { id: 'some-id', itemProps: { name: 'user name 2' } },
  { id: 'some-id-2', itemProps: { name: 'user name 3', isAdmin: true } },
];
userStore.batchUpdate(updates).confirm();

// Remove item
userStore.remove('some-id').confirm();

Advanced usage

import { ItemStore }  from 'immutable-item-store';

// Declare your item class
class User {
  id = 'id';
  name = 'user name';
  isAdmin = false;
}

class UserStore extends ItemStore<User> {
  public applyPatches(patches: Patch[]): User {
    const userId = patch.path[0] as string;
    const fieldName = patch.path[1];
    // Whenever user data changes we can identify what exactly has changed
    if (userId && fieldName) {
      console.log(`UserClient "${clientId}" changed "${fieldName}" to `, patch.value);
    } else if (clientId) {
      console.log(`UserClient "${clientId}" was ${patch.op}(e)d`);
    }

    // Do some data hanling (e.g. validation, emit event) and then apply the patches
    // if (validate(patches)) {
      const user = super.applyPatches(patches);
    // } else {
    //  throw new Error('Not valid');
    // }

    return user;
  }
}

const userStore = new UserStore();
const newUser = new User();

userStore.add(newUser).confirm();
const { itemsDraft, patches, inversePatches, confirm } = userStore.update(newUser.id, { name: 'new name' });
// Optimistic update
confirm();
api.users.create(newUser).catch((e) => {
  // If operation failed - reverse changes
  userStore.applyPatches(inversePatches);
});

API

.get(id: string)

Gets an item by id

.add(id: string, item: ItemType)

Creates a draft of adding a new item to the store. Returned value.

.update(id: string, itemProps: Partial)

Creates a draft of updating an item with new values. Returned value.

.remove(id: string)

Creates a draft of removing an item from the store. Returned value.

.batchUpdate(updates: BatchUpdate[])

Creates a draft of batch updating items with new values. Returned value.

.applyPatches(patches: Patch)

Applies patches to stored items. More on patches.

.itemsArray()

Transforms map of items to an array when required

Returned value

of add, update, remove, batchUpdate methods

type OpReturnType<T> = {
    itemsDraft: Items<Immutable<ItemType>>;
    patches: Patch[];
    inversePatches: Patch[];
    confirm: () => Items<ItemType>;
};

itemsDraft

The draft of changes that were initiated

patches

Describes changes that were initiated

inversePatches

Describes inverse changes

confirm

Saves the draft by appling patches

Type tips

Patch

interface Patch {
    op: "replace" | "remove" | "add";
    path: (string | number)[];
    value?: any;
}