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

one-handle

v2.0.0

Published

多次调用,响应一次,可开缓存

Downloads

10

Readme

Features

多次调用,响应一次,可开缓存

Scene

one-handle接受一个return Promise的函数生成一个闭包, 里面缓存了一个队列,当并发调用的时候,只会同时触发一次 函数,后面函数都会归到队列里面,等待第一次函数完成,当 然,第一个的Promise状态也会影响到队列里面的状态 (resolve还是reject)

通过oneHandle可以创造出4中时效性的缓存

  • oneHandle(fn): 并发时效性
  • oneHandle(fn, true):内存时效性(例如刷新页面就没了)
  • oneHandle(fn, '本地缓存的key名', 'sessionStorage'): sessionStorage时效性
  • oneHandle(fn, '本地缓存的key名'): localStorage时效性

Introduction

下载方式

npm i one-handle

yarn add one-handle

// 引入方式
// esm
import oneHandle from 'one-handle'
// cjs
const oneHandle = require('one-handle')
// script
<script src="https://unpkg.com/one-handle"></script>
window.oneHandle

使用方式

import oneHandle from 'one-handle'
function wait (time, data = 0) {
  return new Promise(resolve => {
    setTimeout(() => resolve(data), time)
  })
}
const $wait1 = oneHandle(wait)
$wait1(1000, 1).then(data => console.log('只触发一次', data)) // 只触发一次 1
$wait1(4000, 2).then(data => console.log('只触发一次', data)) // 只触发一次 1
$wait1(1, 3).then(data => console.log('只触发一次', data)) // 只触发一次 1
// 使用缓存,第二个参数传true
const $wait2 = oneHandle(wait, true)
// 第一次调用成功返回的值缓存起来,下次调用都会取这个值
$wait2(1, false).then(data => {
    console.log('缓存起来', data) // 缓存起来 false
    return $wait2(1, 50)
  })
    .then(data => console.log('使用缓存', data)) // 使用缓存 false

值得留意的时,当去取本地缓存的时候,oneHandle会试图去JSON.parse

// 通过设置catch为字符串开启本地缓存
const $wait3 = oneHandle(wait, 'key')
let $wait4
$wait3(1, {a: 1}).then(data => {
    console.log(data) // {a: 1}
    $wait4 = oneHandle(wait, 'key')
    // key一样,随意初始化的缓存一样
    console.log('同步缓存', $wait4.$getData()) // 同步缓存 {a: 1}
    $wait4.$clear() // 清除缓存
    $wait3.$update() // 更新缓存
    // $wait3.$update()更新到的缓存已经被$wait4.$clear()清除了
    console.log($wait3.$getData()) // null
  })

Options

/**
 * 多次调用,响应一次,可开缓存
 * 
 * @param {Function} fn 一个返回Promise的函数 | 是否开启缓存
 * @param {boolean | string} cache 是否开启闭包缓存 | 开启localStorage的key(默认为localStorage,你可以通过storageType来进行选择是localStorage|sessionStorage)
 * @param {string | object} [storageType] 本地缓存方式 | 上下文
 * @param {object | string} context 上下文 | 本地缓存方式
 * @returns {()=>Promise} return Promise的闭包
 */
function oneHandle (fn, cache, storageType, context) {

API

通过传参方式,可以开启额外的api

开启cache

返回的包装函数附带$getData和$clear

const $wait = oneHandle(wait, true)
$wait.$getData() // 返回缓存
$wait.$clear() // 清除缓存

开启本地缓存(cache参数为string)

返回的包装函数附带$update

const $wait = oneHandle(wait, 'key')
$wait.$update() // 从本地缓存中更新
$wait.$cacheKey // oneHandle-key。缓存的真正key