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

@jerryc/concurrent-merger

v0.0.3

Published

Merger concurrent called of a function

Downloads

4

Readme

Concurrent Merger - 并发调用合并器

CircleCI

NPM Version NPM Downloads Coverage Status npm bundle size

说明

在一次异步调用完成之前,所有后续调用进行等待,直到函数完成,共享调用结果。

设计图

注意:该设计模式,由于多次调用共享一次调用结果,对函数有幂等和入参固定的要求。

Quick Usage

  1. 安装
npm i @jerryc/concurrent-merger
  1. 使用
import { ConcurrentMerger } from '@jerryc/concurrent-merger';

// 一个请求远程资源的异步函数
const getAssets = async () => API.requestAssets();

// 创建 ConcurrentMerger 实例
const concurrentMerger = new ConcurrentMerger({ name: 'test-merger' });
// 代理原函数
const getAssetsProxy = concurrentMerger.proxy(getAssets);

// 高频并发调用 getAssetsProxy,在第一次请求回调之前,后续请求会入队进行等待,直到请求完之后,释放队列,共享结果。
getAssetsProxy().then(result => ...);
getAssetsProxy().then(result => ...);
getAssetsProxy().then(result => ...);
getAssetsProxy().then(result => ...);

// 输出 log
// [concurrent-merger:info] test-merger-任务队列-入列(1/100, id: db3116bd-e4e7-4c6e-9397-32ce015e6225)
// [concurrent-merger:info] test-merger-任务队列-执行目标方法
// [concurrent-merger:info] test-merger-任务队列-入列(2/100, id: a0b206ba-a3af-4e54-9f99-13206595d1df)
// [concurrent-merger:info] test-merger-任务队列-入列(3/100, id: 2dc2f30b-66ff-4101-9eff-56530131689c)
// [concurrent-merger:info] test-merger-任务队列-入列(4/100, id: 580fa99c-8794-495c-b805-8c72978e3fa1)
// [concurrent-merger:info] test-merger-任务队列-消费-resolve(id: 2dc2f30b-66ff-4101-9eff-56530131689c)
// [concurrent-merger:info] test-merger-任务队列-消费-resolve(id: 580fa99c-8794-495c-b805-8c72978e3fa1)
// [concurrent-merger:info] test-merger-任务队列-消费-resolve(id: fa597867-38ef-4e84-ae21-6760a136ab15)
// [concurrent-merger:info] test-merger-任务队列-消费-resolve(id: a9b1f027-c6ba-4895-a5a1-41be45853ba9)

Senior Usage

1. Decorator

如果你的项目中支持 TS 或者 ES Decorator,那么 ConcurrentMerger 提供了快捷使用的装饰器。

import { decorator as concurrentMerger } from '@jerryc/concurrent-merger';

@concurrentMerger({ name: 'test-merger' })
async function getAsset() {
  return API.requestAssets();
};

2. Modify Log Level

ConcurrentMerger 内置了一个迷你 logger(power by @jerryc/mini-logger),方便内部日志打印,外部可以获得 Logger 的实例,进行 log level 的控制。

import {
  logger,
  LoggerLevel,
  ConcurrentMerger,
} from '@jerryc/concurrent-merger';

// 创建 ConcurrentMerger 实例
const concurrentMerger = new ConcurrentMerger({ name: 'test-merger' });

// 下调 Log level
logger.level = LoggerLevel.ERROR;

关于 Log Level 的详细,查看:@jerryc/mini-logger

API

详见:https://jerryc8080.github.io/concurrent-merger/

测试覆盖率

详见:https://jerryc8080.github.io/concurrent-merger/coverage/index.html