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

web_worker_rpc

v1.1.13

Published

a safe and easy to use RPC script for web

Downloads

6

Readme

web_worker_rpc

一个安全,易用的web worker rpc脚本

description

web_worker_rpc,提供宿主与worker通信的RPC能力,并保证了以下情况时宿主的安全性
1. worker死循环或者任何超出了worker负荷能力 # CONFIG.HEARTBEAT
2. worker讳规过于频繁地向宿主发生请求 # CONFIG.QPS
3. worker内部错误达到限制数量 # CONFIG.ONERROR_LIMIT
4. 单次完成任务时间过长 # CONFIG.TIMEOUT

example

demo

install

npm install web_worker_rpc

or

yarn add web_worker_rpc

usage

  1. import it in script tag.
<script src="web_worker_rpc.js"></script>
  1. RPC in your host.js
// host.js
const rpc = window['__web_worker_rpc']
// 创建一个新的worker
// 第一参数传入 url
// 第二参数转入需要暴露给worker的方法
let worker = rpc.create('worker.js', {
    hostFun() {
        console.log('log from host but call in worker');
    },
    others_api: {
        add(a, b) {
            return a+b;
        },
        // you can even RPC with callback function!
        addTickListener(tickTime, handle) {
            let tid = setInterval(handle, tickTime);
            // removeTickListener
            return ()=>{
                clearInterval(tid);
            }
        }
    }
})
// host.js
// 调用worker的方法
worker.remote.workerFun().then(r=>console.log(r))
// host.js
// 销毁
worker.dispose();
  1. RPC in worker.js, you can call host api remotely.
// 远程提供给host方法
rpc.exports = {
    workerFun(){
        return 'from worker';
    }
}
// 远程调用host方法
rpc.remote.hostFun(__distance, id);
// 所有远端方法都是 promise
let r = await rpc.remote.others_api.add(1, 2);
// you can even RPC with callback function!
let canceller = rpc.remote.addTickListener(3000, ()=>{
    console.log('tick from worker');
})
// removeListener
canceller()

configuration

谨慎修改配置

{
    // how long is a remote call timeout, 
    // default 42000, 42 sec
    TIMEOUT: number; 

    // heartbeat to detect if worker is still alive, 
    // default 4200, 4.2 sec
    HEARTBEAT: number; 

    // requested limit per second, when reach, the worker restart, 
    // default 1000, worker can request at most 1000 times in a second
    QPS: number; 

    // when errors counter reached, the worker restart, 
    // default 64
    ONERROR_LIMIT: number; 
};
// in <host.js>
const rpc = window['__web_worker_rpc']
rpc.CONFIG.TIMEOUT = 22;// 设置调用 host API超时为22ms, 抛出超时异常

// in <worker.js>
// no need to declare rpc, internal
rpc.TIMEOUT = 22;// 设置调用超时22ms, 抛出超时异常

FAQ

当worker发生异常时,如何处理

worker将自动关闭,你可以获得异常和关闭的事件,

worker.onFail = ()=>{
    console.log('something bad happen in worker, id of', worker.id);
}

你也可以将它重启,简单地,重新create一个

worker.onFail = ()=>{
    console.log('something bad happen in worker, id of', worker.id);
    rpc.create(workerUrl, worker.exports).then(
        newWorker=> worker = newWorker
    )
}