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

replicated-map

v2.0.0

Published

A Map object with tools for easily keeping replicas in sync across a data stream

Downloads

6

Readme

replicated-map

ReplicatedMap is your standard HashMap-type dealio, but with helper methods that make it easy to replicate it's state from one map to the next. Even across the network!

Basic Map Example

var ReplicatedMap = require('replicated-map');

var rm = new ReplicatedMap();

rm.set('testkey', 'somevalue');

console.log(rm.get('testkey')); // somevalue

rm.delete('testkey');

console.log(rm.has('testkey')); // false

Pretty standard stuff for a map type. Here's what makes this one interesting though.

Replication Example

Leader

// Can also be called without new
var rm = require('replicated-map')();

rm.set('some_existing', 'value');

// Start replicating
rm.replicate(function(type, key, value) {
	// This function will be called every time
	// there's a new command to replicate

	// Just pretend we set this network connection up earlier
	network.write(JSON.stringify([type, key, value]));
});

// All set and delete commands will be replicated to the follower
// keeping it in a consistent state with this one
rm.set('another_new', 'val');

Follower

var rm = require('replicated-map')();

// Again, just pretend
network.on('data', function(message) {
	message = JSON.parse(message);

	rm.mutate(message[0], message[1], message[2]);
});

// rm will now follow the leader at the other end of the
// network stream and stay in sync with it

// Give it some time to replicate
setTimeout(function() {
	console.log(rm.get('some_existing')); // value

	console.log(rm.get('another_new')); // val
}, 100);

Events

You may also listen to changes in the map for arbitrary purposes.

map.replicate(function(type, key, value) {
	switch(type) {
	case 'add':
		// Do something with the added key/value pair
		break;
	case 'remove':
		// A key/value pair has been deleted
	}
});

Methods

  • .has(key) - Returns whether or not the map contains a key
  • .get(key) - Returns value of key
  • .set(key, value) - Set value of key
  • .delete(key) - Removes a key from the map
  • .clear() - Clears all key/value pairs from the map
  • .keys() - Returns an array of the map keys
  • .values() - Returns an array of the map values
  • .forEach(fn) - fn(value, key) is executed once for each key/value pair
  • .mutate(type, key, value) - Performs the specified mutate operation on the map
  • .replicate(fn) - Calls fn(type, key, value) once for each mutate operating needed to replicate the state of this map to another map

Notes

The method used to stream data must carry the messages IN ORDER. If the messages are out of order the accuracy of the replication cannot be guaranteed. Imagine doing set key 3 and set key 7. If they are out of the order the older command may overwrite the newer one.

Do not call the mutating methods on a following map, this will result in the follower being out of sync with the leader.

This module depends on Map and Set being available, so if you are targeting a browser without support you may need to require('core-js/fn/map') and require('core-js/fn/set') the polyfill.