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

@rosey-ai/merge

v0.0.7

Published

Merge your front end and back end development experience.

Downloads

7

Readme

Merge

Merge your front end and back end development experience.

A small library that allows you to invoke functions from your server to your client or vice versa.

Merge is designed to work with any system that provides a message pump style information transfer. We specifically built it around socket.io but other message pumps should work as well (such as webworkers).

Installation

Using yarn:

yarn add @rosey-ai/Merge

Using npm:

npm -i --save @rosey-ai/Merge

Exposing Functions

Merge uses promises internally to manage the inherient asyncronous nature of these systems. Thus when you expose a function there are a couple of requirements.

  1. Writing the exposed function (The function that will be called) must be written taking two callbacks, the resolve and reject handlers for the promise. The exposed function can take any number of arguments, provided that the last two arguments are the resolve and reject handlers. For example:
function CallMe(one,two,resolve,reject){
  if ( ... ){
    // We succeeded and want to return that success
    resolve({"return":"values","go":"here"})
  }else{
    // We failed or errored and want to call that reject
    reject(["something","went wrong"])
  }
}

Your resolve and reject handlers can 'return' any kind of data.

  1. Calling the exposed function. When calling the function you must pass the correct number of arguments or merge will break. This is a a known bug. Otherwise you can use .then promise notation to complete. await also works.
ToDo: Reject correctly on incorrect number of arguments passed

For example:

merge[id].CallMe("First",{second:"args"}).then(
  (result)=>{
    console.log(result);
  },(error)=>{
    console.log(error);    
  })

OR

  let result = await merge[id].CallMe("First",{second:"args"});

Frontend & Backend

Frontend script is designed to be loaded as an es6 module, either through a <script> tag or with a dynmaic module import.

Backend script is designed to be required by a node application.

Both frontend and backend modules are designed to be "singleton" instances that live inside of some other singleton style object. You can create multiple instances by passing in other parent objects. (On the frontend this could be window on the backend it could be your app (or some object you set/get in your app))

Multiple message pumps can be stored and utilized inside a single Merge instance, each with their own set of exposed functions.

Example Usage

Frontend (Browser)

<script type="text/javascript" src="/@rosey-ai/merge/frontend/Merge.js"></script>
<script>
  let io_pump = io("/merge-pump");
  let the_server = Merge(window);
  the_server["example"] = io_pump;
  //Declare a function for the server to be able to call.
  the_server["example"] .CallMeSomeTime = (args,resolve,reject)=>{
                console.log("The server called me: ",args);
                resolve("Argumens and results can be of any serializeable type.");
            };
the_server["example"].DoStuff(data:{msg:"Hello"},stuff:[1,2,3],"third argument").then(
  (results)=>{
    console.log("The server finished and sent back: ",results)
  });
</script>

Backend (Node)

let merge = require("@rosey-ai/merge")(app);
let io_pump = io.of('/merge-pump');
io_pump.on("connection",(socket)=>{
  //Each socket communicates over a unique room. We could use the default room, but you can specify a room as well.
  //This way only messages for the intended target are sent and recieved.
  //This also means that if all of a single users connections from multiple devices are placed in the same room, all of the devices
  //will be kept in sync.
    socket.join(socket.id);
    //Its possible that this socket is simply reconnecting due to some network error. It should just be in the same room.
    // (or a single user might log in with multiple devices)
    if(merge[socket.id] == undefined){
      //Each user gets a unique outgoing room. (This will notify everyone in that room)
      merge[socket.id] = io_pump.to(socket.id);
    }
    //We need to know about each client connection.
    merge[socket.id] = socket;
    /* Function just for this socket */
    merge[socket.id].DoStuff = (first,second,third,resolve,reject)=>{
        console.log("The client told me to do stuff: ",args);
        //The server can do some time intesive processing if it wants.
        setTimeout(()=>{resolve("I waited for you");},3000);
    };
    /* Declaring a function for all sockets that can be called from any client */
    merge.DoOtherStuff = (first,second,third,resolve,reject)=>{
            console.log("The client told me to other do stuff: ",args);
            //The server can do some time intesive processing if it wants.
            setTimeout(()=>{resolve("I waited for you");},3000);
      };
    merge[socket.id].CallMeSomeTime(["Args",{can:"be"},"anything","serializable"]).then((result)=>{
        console.log("The client returned",result);
    });
  });