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

@insertish/mutable

v1.1.0

Published

Mutable object utilities.

Downloads

10

Readme

npm

Why?

The use-case of this library is for when you need to monitor mutations for a set of objects.

  • When taking an object from the map, you will get a reference as JS passes objects by reference.
  • This means everywhere you use the object will have the latest version of the object.
  • Then to monitor mutations, we use a Proxy on the object internally which feeds into an EventEmitter on the map.

You may be wondering if something like Redux would do the job, probably, but it gets a bit messy when you also want to persist all the data, i.e. you're serializing and writing huge amounts of data for every small little change.

The solution is to wrap every object internally and catch any mutations on the objects.

  • For any mutation, events are emitted which can be used to re-render React elements which use that relevant data.
  • And for any mutation, we can also update IndexedDB with the new partial data, instead of re-saving everything.

Example Usage

import { MutableMap } from '@insertish/mutable';

type User = {
    _id: string
    username: string
    online: boolean
    flag: number
}

class Users extends MutableMap<User> {
    updateUsername(id: string, username: string) {
        // we would call an API here to verify we can do this
        // and then use Reflect to set the correct field
        this.map[id].username = username;
    }
}

let map = new Users();

map.on('create', item => console.log('Created user', item));
map.on('delete', id => console.log('Deleted user with id', id));
map.on('mutation', (user: User, property: string) => console.log('User', user._id, 'has had property', property, 'changed!'));

map.create({ _id: '123', username: 'aaa', online: true, flag: 0 });

let user = map.get('123');
map.updateUsername('123', 'bbb');
map.updateUsername('123', 'bbb'); // Internally, we use lodash.isEqual to prevent multiple events firing.
console.log(JSON.stringify(user)); // Since it's a normal object, we can just serialize it if we want.

map.set({ _id: '456', username: 'bbb', online: false, flag: 0 });
map.set({ _id: '456', username: 'bbb', online: false, flag: 8 });
map.patch('456', { online: true });
map.patch('123', { flag: 1 });
map.delete('456');

Usage with IndexedDB

To use with IDB, simply pass a ZangoDB collection in. (reliance on this library isn't necessary, but I am already using it elsewhere in the project this is going to be used in, feel free to PR to use just IDB)

import { Db } from '@insertish/zangodb';
let db = new Db('state', 1, [ 'users' ]);

let map = new MutableMap<T>(db.collection('users'));

// After constructing, always reload state first.
map.restore(); // -> Promise<void>

// All changes are automatically saved to IDB.