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

pm-rpc

v3.2.7

Published

[![Maintenance Status][status-image]][status-url] [![NPM version][npm-image]][npm-url] [![Dependencies][deps-image]][deps-url] [![Build Status][build-image]][build-url]

Downloads

577

Readme

Maintenance Status NPM version Dependencies Build Status

RPC calls via PostMessage (pm-rpc)

This project allows defining and using a promise-based API between different browser frames or workers.

Usage

RPC calls are defined between a callee and one or several callers

API definition

The callee must first set an API for consumption. This makes the API available for any callers requesting for an API with that ID.

The API can be any object containing functions and/or namespaces, and namespaces can be functions as well.
e.g.:

const api = {
  syncFunc(...args) {
    return someComputation(...args)
  },
  asyncFunc(...args) {
    return performSomeAjaxRequest(...args)
  },
  
  someNamespace: {
    namespacedFunction(...args) {
      doSomething(...args)
    }
  },
  
  add(a, b) { 
    return a + b
  }
};

api.add.one = a => 1 + a; //api.add is both a function and a namespace!
rpc.api.set(appId, api);

Let's look at a real example:

Let the callee expose a 'maxOdd' function that recives as arguments a getNumbers and a filterOdd functions, and as a result return the max number.

Please note that the callee expose it's API by calling pmrpc.api.set

const api = {
  maxOdd(getNumbers, getOdd) {
    return getNumbers()
      .then(getOdd)
      .then(odds => Math.max(...odds))
  }
}
pmrpc.api.set('functions', api)

The caller code will look as follow:

pmrpc.api.request('functions', {target: iframe.contentWindow})
  .then(api => {
    const filterOdd = arr => arr.filter(x => x % 2)
    const getNumbers = () => [1, 2, 3, 4, 5, 6]

    api.maxOdd(getNumbers, filterOdd)
      .then(result => {
        //result is 5 try it!
    })
})

Removing a set API

The callee may remove an API, and stop listening for requests to it. This makes the API no longer available, and rejects any other requests for the API with an error message. e.g.:

rpc.api.unset(appId)

Using onApiCall

When setting an API, you can also pass an options parameter with an onApiCall option. This callback will be called whenever any API method is invoked from any caller, with the following object:

  • appId - The ID of the app passed
  • call - The name of the function to invoke
  • args - An array of arguments passed

e.g.:

const api = {
  syncFunc(...args) {
    return someComputation(...args);
  },
  asyncFunc(...args) {
    return performSomeAjaxRequest(args)
  }
}
rpc.api.set(appId, api, {onApiCall: function (data) {
  console.log(data); // {appId: 'someAppId', call: 'theMethodCalled', args:['argument1', 'argument2']} 
}});

Using onApiSettled

When setting an API, you can also pass an options parameter with an onApiSettled option. This callback will be called whenever any API method is invoked from any caller after the api has settled, with the following object:

  • appId - The ID of the app passed
  • call - The name of the function to invoke
  • args - An array of arguments passed
  • onApiCallResult - The result from the onApiCall callback

e.g.:

const api = {
  syncFunc(...args) {
    return someComputation(...args);
  },
  asyncFunc(...args) {
    return performSomeAjaxRequest(args)
  }
}
rpc.api.set(appId, api, {
    onApiCall:() => performance.now(),
    onApiSettled: message => {
      console.log(`Method: ${message.call} was executed in ${performance.now() - message.onApiCallResult} ms`)
    }
});

Using with WebWorker

When setting an API, you can specify workers that may consume requested API. Inside worker, you can request

const api = {
  asyncFunc(...args) {
    return performSomeAjaxRequest(args)
  }
}

const worker = new Worker('dowork.js')

rpc.api.set('appId', api, {workers: [worker]});

Inside web worker:

rpc.api.request('appId')
  .then(api => api.asyncFunc())

API usage:

To use an API, the caller must request it from the callee. The API is then returned in a promise, and all API calls return promises.

e.g.:

import rpc from 'pm-rpc';
rpc.api.request(
  appId, 
  {
    target //The callee window, usually parent
  }
)
.then(api => api.syncFunc(...someArgs))
.then(result => {
    // Do something with the results
});