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

@berlysia/p-fied

v0.0.2

Published

PromisiFIED asynchronous utility module.

Downloads

6

Readme

p-fied

PromisiFIED asynchronous utility module.

Motivation

async.js proposed a lot of asynchronous way in JavaScript. But now we have a Promise.

Some of async.js functions can be easily expressed with the Promise (e.g. series, parallel...), so p-fied doesn't have these functions.

ここまで書いて async.js と Bluebird で足りたんじゃないのという気持ちになった.元気がなくなったので日本語で書く.

pfor(initialState, test, update, routine)

for([initialState], [test], [update]){
	[routine]
}

arguments

  • initialState - 初期状態を示すObject

  • test - (state: Object) => bool | Promise<bool>trueならループ続行,falseでループ終了.

  • update - (state: Object) => state.状態を更新する.返値を更新後の値とする.返さなかった場合はroutineの返値

  • routine - (state: Object) => state | Promise<state> | true | Promise<true> | any.ループのメイン処理を表現する.返値が状態オブジェクトやそのPromiseであるならば,updateに渡す値はここで返した値となる.ループ中のいわゆるcontinueを,routineがsynchronousである場合はreturnで,asynchronousである場合はreturn resolvedPromiseで表現する.breakreturn rejectedPromise(true)で表現する.

  • returns Promise<state>. エラー終了した場合はrejected,その他の場合はresolved

pfied.pfor({i: 0}, s => s.i < 5, s => {i: s.i+1}, s => {
	setImmediate(_ => console.log(s.i));
}).then(s => console.log("done! %d", s.i));
/*
0
1
2
3
4
done! 5
*/

動作の詳細例はテストコードが参考になるだろう.

whilst(test, routine, initialState?)

doWhilst(test, routine, initialState?)

while文に対応.testtrue || Promise<true>な間,routineを実行する.initialStateは省略可能.routineの挙動はpforのものに従う.

doWhilstはそのままdo-while文に対応する.

until(test, routine, initialState?)

doUntil(test, routine, initialState?)

while文に対応するが,testfalse || Promise<false>である間にroutineを実行する.他はwhilstdoWhilstに従う.

forever(routine, initialState)

無限ループ.routinepforのものと同様の動作を期待する.

new Queue(worker, concurrency = 1)

async.jsのQueueと同様の機能を提供する.

  • worker - (task: Object) => any.キューからタスクを受け取って処理する.

  • concurrency - 並行して最大いくつまでworkerを実行するか指定する.初期値は1.

  • Queue#push(task: Object) => Promise<worker returning value>

  • Queue#unshift(task: Object) => Promise<worker returning value> キューにタスクを追加する.pushはFIFO,unshiftはLIFO.返値はそのタスクが処理されたときに解決されるPromise

  • Queue#pushBulk(tasks: [Object]) => [Promise<worker returning value>]

  • Queue#unshiftBulk(tasks: [Object]) => [Promise<worker returning value>] キューにタスクを一度に複数個追加する.返値は単体時と同様のPromiseからなるArray.与えたタスク列の順に対応している.

  • running() - ワーカーが処理中か否か true/false

  • idle() - !running()

  • pause() - ワーカーの追加を中止する.現在処理中のものは続行する.

  • resume() - ワーカーの追加を再開する.

  • kill() - ワーカーに渡されていないタスクをドロップする.

  • saturated - タスクを追加する操作をしたとき,保持するタスク数がcurrencyを超えた場合にコールバックする関数を指定する.

  • empty - タスク保持数がゼロになったときにコールバックする関数を指定する.

  • drain - ワーカー稼働数がゼロになったときにコールバックする関数を指定する.

  • error - ワーカー稼働中にエラーが発生した場合にコールバックする関数を指定する.エラーを第一引数で受け取れる.

new Cargo(worker, payload = 1)

Queueに近いが,ワーカーに渡すときpayloadの数を最大値として取れるだけ取り,配列に詰めて渡す.結果のPromiseはワーカーが正常終了すれば個別に返る.ワーカーが失敗すると一緒に渡されたタスクの結果Promiseは失敗原因でrejectされる.

new ProirityQueue(worker, concurrency, comperator)

Queueに近いが,タスク間の順序を定義しておくことで,その順にワーカーに渡す.先頭や末尾に追加するという概念がないので,unshiftを持たない.順序定義用の関数comperatorは,Array.prototype.sortが引数として要求する形式を満たすこと.

async.jsのものは固定値で優先度を定義してその順に取り出していたため,優先度の与え方がまったく異なる.