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

message2call

v1.1.0

Published

Convert Send Message and on Message to asynchronous get and call style

Downloads

457

Readme

Message to call

Convert Send Message and on Message to asynchronous get and call style.

+--------------------------+           +--------------------------+
|        Main Thread       |           |       Worker Thread      |
+--------------------------+           +--------------------------+
|                          |           |      Expose Object {     |
|   await remote.hello()  ---------------->     hello() {}        |
|                          |           |      }                   |
+--------------------------+           +--------------------------+  

It supports message sending and receiving scenarios in workers, websockets, and more.

Installation

  • Use npm install
# install
npm install message2call
// import
import * as message2call from 'message2call'
  • Use script link
<script src="./message2call.min.js"></script>

How to use

see demo

index.html File:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script src="../dist/message2call.min.js"></script>
  <script>
    const worker = new Worker('./worker.js')
    const exposeObj = {
      getName(fromName) {
        return 'hello ' + fromName + ', I am index.html'
      },
    }
    const message2call = Message2call.createMsg2call({
      /**
       * required proxy object
       */
      funcsObj: exposeObj,
      /**
       * send message function
       */
      sendMessage: function(data) {
        worker.message(data)
      }
    })
    worker.onmessage = (event) => {
      message2call.message(event.data)
    }

    (async() => {
      const remote = message2call.remote
      const remoteName = await remote.name
      console.log('[index]', 'remote name is ' + remoteName)

      const result = await remote.count(6)
      console.log('[index]', result)
    })()
  </script>
</body>
</html>

worker.js File:

importScripts('../dist/message2call.min.js')

const exposeObj = {
  name: 'worker.js',
  async count(num) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(num + 2)
      }, 2000);
    })
  },
}
const message2call = Message2call.createMsg2call({
  /**
   * required proxy object
   */
  funcsObj: exposeObj,
  /**
   * send message function
   */
  sendMessage: function(data) {
    postMessage(data)
  },
})
onmessage = (event) => {
  message2call.message(event.data)
}

(async() => {
  const name = await message2call.remote.getName('worker.js')
  console.log('[worker]', name)
})()

Options

interface Options {
  /**
   * required proxy object
   */
  funcsObj: Readonly<Record<string, ((...args: any[]) => any) | string | number | object>>
  /**
   * send message function
   */
  sendMessage: (data: Record<string, unknown>) => void
  /**
   * on call error hook
   */
  onError?: (err: Error, path: string[], groupName: string | null) => viod
  /**
   * call timeout, 0 will be no timeout
   */
  timeout?: number
  /**
   * whether the call fails to send the call stack
   */
  isSendErrorStack?: boolean
  /**
   * convert call params
   */
  onCallBeforeParams?: (rawArgs: any[]) => any[]
}
type createMsg2call = <T>(options: Options) => {
  /**
   * remote proxy object
   */
  remote: T;
  /**
   * create remote proxy object of group calls
   */
  createRemoteGroup<T_1>(groupName: string, options?: {
    /**
     * call timeout, 0 will be no timeout, default use global timeout
     */
    timeout?: number;
    /**
     * whether to use queue calls
     */
    queue?: boolean;
  }): T_1;
  /**
   * on message function
   */
  message: ({ name, path, error, data }: any) => void;
  /**
   * destroy
   */
  destroy: () => void;
}
/**
 * create a proxy callback
 */
type createProxyCallback = <T extends Function>(callback: T) => T & { releaseProxy: () => void };
/**
 * release all created proxy callback
 */
type releaseAllProxyCallback = () => void;

CHANGELOG

See CHANGELOG.md

Thanks

Inspired by comlink: https://github.com/GoogleChromeLabs/comlink

LICENSE

MIT