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

megastores

v1.5.0

Published

Store manager with realtime exchanges between server and client

Downloads

6

Readme

MEGASTORES

Store manager with realtime exchanges between client and server

Tests : Build Status

Dependances

Requirement

  • nodejs >= 4.0 for server only

Installation

$ npm install --save megastores

How to use it

Server

const { Store, Megastores } = require('megastores');

var myStore = new Store('my-store');
var megastores = new Megastores();

megastores.attach(myStore).listen(8080);

myStore.subscribe(state => {
    console.log('new state :', state);
});

Client

<script src="node_modules/megastores/dist/megastores-client.js">
<script>
    var store = new Store('my-store');
    var megastores = new Megastores();

    megastores.attach(store).connect('ws://localhost:8080');

    store.put({ text: 'Hello world!' });
</script>

Features

  • Realtime exchanges between client and server, every action dispatched is synchronized between server and client.
  • Automatic reconnection and resynchronization if connection with server is lost
  • Offline mode with local storage persistence

Documentation

Server

Events

  • connection (client) Called every time when client opens connection with server
  • close Called every time when client loses connection with server
  • message (message) Called every time when client exchanges with server
// Exemple
var megastores = new Megastores();
megastores.listen(8080).on('open', () => {
    console.log('New connection openned.');
});

Methods

  • () Constructor, return an instance of Megastores
  • attach(store|[stores]) Attach a Store or a list of stores, also create redux store, so you need to attach all stores before starting server
  • listen(port) Start server and listen on port port
  • on(event|custom-event, callback) Listen event
  • send(custom-event, data, client = null) Send event to one client or all of them
// Exemple
var store1 = new Store('store1');
var store2 = new Store('store2');

var server = new Megastores();
server.attach([store1, store2]).listen(8080);

server.on('custom-event', message => {
    let client = message.client;
    let data = message.data;

    server.send('response-custom-event', 'hello world!', client);
});

Server - Store

Property

  • items Get data stored

Methods

  • (name, initialStore = []) Constructor, return an instance of Store
    • initialStore can be an array or an Object
  • put(item|property) Put an item or a property of Object into Store
  • update(index, item|property) Update item or property at index
  • remove(index|property) Remove item or property
  • use(callback) Add middleware called every time when action is dispatching
    • callback(action, oldState, newState, next)
  • subscribe(callback) Add listener called each time action is dispatched
    • callback(items) With items is the current state of Store
// Exemple
var fruits = new Store('fruits', ['apple', 'lemon', 'cherry']);

var server = new Megastores();
server.attach(fruits).listen(8080);

fruits.subscribe((items) => {
    console.log(items); // display current state
});

fruits.use(() => console.log('juste say hello when state changes'));

fruits.put('raspberry');
fruits.update(1, 'strawberry'); // change lemon to strawberry
fruits.remove(0); // delete apple

Client

Events

  • open Called every time when connection with server is open
  • close Called every time when client loses connection with server
  • message (message) Called every time when server exchanges with client
// Exemple
var client = new Megastores();
client.connect('ws://localhost:8080').on('open', () => {
    console.log('Connected with server.');
});

Methods

  • () Constructor, return an instance of Megastores
  • attach(store|[stores]) Attach a Store or a list of stores, also create redux store, so you need to attach all stores before starting a connection with server
  • connect(url, port) Connect to server with url and port
  • on(event|custom-event, callback) Listen event
  • send(custom-event, data) Send event to server

Client - Store

Properties

  • items Get data stored

Methods

  • (name, initialStore = [], options = {}) Constructor, return an instance of Store
    • initialStore can be an array or an Object
    • options Object of options
      • offline Default false, enable offline mode, there are no exchanges with server, and data are stored into local storage
      • enableCache Default true, enable catching items when connection with server is lost
  • put(item|property) Put an item or a property of Object into Store
  • update(index, item|property) Update item or property at index
  • remove(index|property) Remove item or property
  • use(callback) Add middleware called every time when action is dispatching
    • callback(action, oldState, newState, next)
  • subscribe(callback) Add listener called each time action is dispatched
    • callback(items) With items is the current state of Store
// Exemple
var fruits = new Store('fruits', ['apple', 'lemon', 'cherry']);

var client = new Megastores();
client.attach(fruits).connect('ws://localhost:8080');

fruits.subscribe((items) => {
    console.log(items); // display current state
});

fruits.use(() => console.log('juste say hello when state changes'));

fruits.put('raspberry');
fruits.update(1, 'strawberry'); // change lemon to strawberry
fruits.remove(0); // delete apple

Example

Todolist client

<script src="node_modules/megastores/dist/megastores-client.js">

<ul id="list">
</ul>
<form id="todo-form">
    <input type="text"/>
    <button type="submit">Envoyer</button>
</form>

<script>
    const form = document.querySelector('#todo-form');
    const list = document.querySelector('#list');

    const todoStore = new Store('todo');
    const client = new Megastores();

    const render = function(items) {
        list.innerHTML = '';
        items.forEach(function(item) {
            var li = document.createElement('li');
            li.innerHTML = item.do;
            list.appendChild(li);
        });
    }

    client.attach(todoStore).connect('ws://localhost:8080').on('open', function() {
        render(todoStore.items);
    });

    todoStore.subscribe(function(items) {
        render(items);
    });

    form.addEventListener('submit', function(e) {
        e.preventDefault();

        var input = form.querySelector('input');
        todoStore.put({ do: input.value });

        input.value = null;
    });
</script>

Todolist server

const { Store, Megastores } = require('megastores');

var items = [] // Get items from database

const todoStore = new Store('todo', items);
const server = new Megastores();

server.attach(todoStore).listen(8080);

todoStore.use((action, oldState, newState, next) => {
    // Persist data into database

    next();
});

License

MIT

Test

$ npm test

Have fun