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-list

v2.0.0

Published

A List with tools to easily keep replicas in sync

Downloads

5

Readme

replicated-list

ReplicatedList is an Array/List-like structure, but with helper methods that make it easy to replicate it's state from one list to the next. Even across the network!

Basic List Example

var ReplicatedList = require('replicated-list');

// Initial state can be passed during construction
var list = new ReplicatedList(['some', 'initial', 'items']);

list.push('fourthvalue');

console.log(list.get(1)); // initial

console.log(list.pop()); // fourthvalue

console.log(list.shift()); // some

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

Replication Example

Leader

var list = new ReplicatedList();

list.push(0);

// Start replicating
list.replicate(function(type, index, item) {
	// 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, index, item]));
});

// All mutating commands will be replicated to the follower
// keeping it in a consistent state with this one
list.unshift('another_new_element');

Follower

var list = new ReplicatedList();

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

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

// list 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(list.length); // 2

	console.log(list.get(0)); // another_new_element

	console.log(list.get(1)); // 0
}, 100);

Events

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

list.replicate(function(type, index, item) {
	// type will be 'add' or 'remove'
	switch(type) {
	case 'add':
		addElementToView(item, index);
		break;
	case 'remove':
		removeElementFromView(index);
	}
});

Methods

  • .push(value) - Pushes a value onto the end of the list
  • .pop() - Removes and returns the last value in the list
  • .unshift(value) - Adds a value to the beginning of the list
  • .shift() - Removes and returns the first element in the list
  • .splice(startIndex, count, elements...) - Removes count elements starting at startIndex while adding additional elements
  • .clear() - Clears all elements from the list
  • .forEach(fn) - fn(value, index) is executed once for each item in the list
  • .map(fn) - fn(value, index) is executed once for each item in the list returning an array of the results of fn
  • .mutate(type, index, item) - Performs the given mutating on this list
  • .replicate(fn) - Calls fn(type, key, value) once for each mutate event needed to replicate the state of this list to another list

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 push 3 and push 7. If they are out of the order the older the array contents will be out of order as well.

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

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