1.0.0 • Published 12 days ago

@landmineaknpm/quia-facilis-necessitatibus v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 days ago

@landmineaknpm/quia-facilis-necessitatibus

ci npm version

Fast, in memory work queue.

Benchmarks (1 million tasks):

  • setImmediate: 812ms
  • @landmineaknpm/quia-facilis-necessitatibus: 854ms
  • async.queue: 1298ms
  • neoAsync.queue: 1249ms

Obtained on node 12.16.1, on a dedicated server.

If you need zero-overhead series function call, check out fastseries. For zero-overhead parallel function call, check out fastparallel.

js-standard-style

  • Installation
  • Usage
  • API
  • Licence & copyright

Install

npm i @landmineaknpm/quia-facilis-necessitatibus --save

Usage (callback API)

'use strict'

const queue = require('@landmineaknpm/quia-facilis-necessitatibus')(worker, 1)

queue.push(42, function (err, result) {
  if (err) { throw err }
  console.log('the result is', result)
})

function worker (arg, cb) {
  cb(null, arg * 2)
}

Usage (promise API)

const queue = require('@landmineaknpm/quia-facilis-necessitatibus').promise(worker, 1)

async function worker (arg) {
  return arg * 2
}

async function run () {
  const result = await queue.push(42)
  console.log('the result is', result)
}

run()

Setting "this"

'use strict'

const that = { hello: 'world' }
const queue = require('@landmineaknpm/quia-facilis-necessitatibus')(that, worker, 1)

queue.push(42, function (err, result) {
  if (err) { throw err }
  console.log(this)
  console.log('the result is', result)
})

function worker (arg, cb) {
  console.log(this)
  cb(null, arg * 2)
}

Using with TypeScript (callback API)

'use strict'

import * as @landmineaknpm/quia-facilis-necessitatibus from "@landmineaknpm/quia-facilis-necessitatibus";
import type { queue, done } from "@landmineaknpm/quia-facilis-necessitatibus";

type Task = {
  id: number
}

const q: queue<Task> = @landmineaknpm/quia-facilis-necessitatibus(worker, 1)

q.push({ id: 42})

function worker (arg: Task, cb: done) {
  console.log(arg.id)
  cb(null)
}

Using with TypeScript (promise API)

'use strict'

import * as @landmineaknpm/quia-facilis-necessitatibus from "@landmineaknpm/quia-facilis-necessitatibus";
import type { queueAsPromised } from "@landmineaknpm/quia-facilis-necessitatibus";

type Task = {
  id: number
}

const q: queueAsPromised<Task> = @landmineaknpm/quia-facilis-necessitatibus.promise(asyncWorker, 1)

q.push({ id: 42}).catch((err) => console.error(err))

async function asyncWorker (arg: Task): Promise<void> {
  // No need for a try-catch block, @landmineaknpm/quia-facilis-necessitatibus handles errors automatically
  console.log(arg.id)
}

API

  • @landmineaknpm/quia-facilis-necessitatibusueue()
  • queue#push()
  • queue#unshift()
  • queue#pause()
  • queue#resume()
  • queue#idle()
  • queue#length()
  • queue#getQueue()
  • queue#kill()
  • queue#killAndDrain()
  • queue#error()
  • queue#concurrency
  • queue#drain
  • queue#empty
  • queue#saturated
  • @landmineaknpm/quia-facilis-necessitatibusueue.promise()

@landmineaknpm/quia-facilis-necessitatibusueue(that, worker, concurrency)

Creates a new queue.

Arguments:

  • that, optional context of the worker function.
  • worker, worker function, it would be called with that as this, if that is specified.
  • concurrency, number of concurrent tasks that could be executed in parallel.

queue.push(task, done)

Add a task at the end of the queue. done(err, result) will be called when the task was processed.


queue.unshift(task, done)

Add a task at the beginning of the queue. done(err, result) will be called when the task was processed.


queue.pause()

Pause the processing of tasks. Currently worked tasks are not stopped.


queue.resume()

Resume the processing of tasks.


queue.idle()

Returns false if there are tasks being processed or waiting to be processed. true otherwise.


queue.length()

Returns the number of tasks waiting to be processed (in the queue).


queue.getQueue()

Returns all the tasks be processed (in the queue). Returns empty array when there are no tasks


queue.kill()

Removes all tasks waiting to be processed, and reset drain to an empty function.


queue.killAndDrain()

Same than kill but the drain function will be called before reset to empty.


queue.error(handler)

Set a global error handler. handler(err, task) will be called each time a task is completed, err will be not null if the task has thrown an error.


queue.concurrency

Property that returns the number of concurrent tasks that could be executed in parallel. It can be altered at runtime.


queue.drain

Function that will be called when the last item from the queue has been processed by a worker. It can be altered at runtime.


queue.empty

Function that will be called when the last item from the queue has been assigned to a worker. It can be altered at runtime.


queue.saturated

Function that will be called when the queue hits the concurrency limit. It can be altered at runtime.


@landmineaknpm/quia-facilis-necessitatibusueue.promise(that, worker(arg), concurrency)

Creates a new queue with Promise apis. It also offers all the methods and properties of the object returned by @landmineaknpm/quia-facilis-necessitatibusueue with the modified push and unshift methods.

Node v10+ is required to use the promisified version.

Arguments:

  • that, optional context of the worker function.
  • worker, worker function, it would be called with that as this, if that is specified. It MUST return a Promise.
  • concurrency, number of concurrent tasks that could be executed in parallel.

queue.push(task) => Promise

Add a task at the end of the queue. The returned Promise will be fulfilled (rejected) when the task is completed successfully (unsuccessfully).

This promise could be ignored as it will not lead to a 'unhandledRejection'.

queue.unshift(task) => Promise

Add a task at the beginning of the queue. The returned Promise will be fulfilled (rejected) when the task is completed successfully (unsuccessfully).

This promise could be ignored as it will not lead to a 'unhandledRejection'.

queue.drained() => Promise

Wait for the queue to be drained. The returned Promise will be resolved when all tasks in the queue have been processed by a worker.

This promise could be ignored as it will not lead to a 'unhandledRejection'.

License

ISC

mimeECMAScript 7SymbolequalityCSSStyleDeclarationrm -frargvcolorsrfc4122letratestablemakeshamschemethrottlecompile lesssignalsshebangbabel-corePushconcurrencycommandzodisConcatSpreadablexssio-tsjsonpathtrimawesomesaucesymlinksRegExp.prototype.flagsmime-dbfrominvariantconsolesanitization.envregexpeslintpluginredactimmertoolkitsymbolscryptguidworkercharacterWeakSetstylingmetadatawhatwgjavascripttsasynccolumntc39babelPromisespringrm -rfES5lazylinktapeomitlinewrapnodemkdirscss lesschineseES8tacitchaijson-schema-validationreducewritablenested cssxterminternaldependenciesflatMapfast-deep-copyjson-schema-validatortypanionconcatMapes8assertES7packageshardlinkselectrona11ypostcss-pluginstringifierjestwalkingmodulesESsetImmediatesideescapeRxJSchromemonorepotoobjectbrowserargparseawaitmiddlewarefastifytoStringTagreadablestreamcss nestingnopewidthmatchAllObservablesbufferreduxpromisesidlepreserve-symlinkslanguageperformantwordbreakfunctionsstreams2transpilercheckstylesheetwriteschemaresolveprivate datavalidatorArray.prototype.flatMapchannelless compilerstatelessspinnerrandomemitutilityargsjsoncreateimmutableUint8ClampedArrayreact-testing-libraryeventsgradients cssES2021weakmapdataViewthroatincludesramdaless cssfolderRxObject.definePropertyexpressaccessibilityjoishareddayjsdeepcopyinspectreact posesyntaxpropertysomeinputtrimLeftcollectionhasOwnstructuredCloneopenermatchInt16ArraypasswordequalTypeBoxfasthelpersreactcolumnsUnderscorefindLastIndexqsES2017filtervisualdeletetimevariables in cssconfigurableformes0progressnumbermovewrapchromiumnegative zeroStreamsoncexdgsortedbyteOffsetdomdescriptorbindstringifysuperagentcoreparentsisbrowserslistArraylibphonenumbergrouplinuxarktypeES2015cssopensfunctionalstyled-componentssortcacheMapjsxserializertypedarraylookstylefantasy-landhasform-validationrangeerrorapolloregular expressionstypeofdirectoryutil.inspectES2019dom-testing-libraryansienvutilprotobuffast-copyArray.prototype.findLastposetermfilecallbacksettingsrequireBigInt64Arrayes2015dragxhriterationfile systemquerystringdotenvInt32Arrayuptslibdeep-copyrmdirutilsgdprpolyfilldirnameMicrosoftpyyamlObject.keysarraybufferassignconfigurles-shimspurefsfastcopycomputed-typesrapidcontainsless mixinscode pointsratelimittextestreelaunchcollection.es6pushlimitES3ECMAScript 2015webexpressionYAMLredux-toolkitcallbindunicodeexecutableconsumepromiseloadinglesscss6to5yupArray.prototype.includesstringjson-schemauuidvaluesloggingformattingsyntaxerrorexitgetOwnPropertyDescriptor@@toStringTagfunctionJSONglobbundlingjsdomwgetweaksetponyfillsameValueZeroECMAScript 3bootstrap cssconnectStreamECMAScript 2021authtrimEnderrorstatus$.extendgestureschildtypeerrorfigletreal-timeinstallrgbbootstrap lesspostcsspipespawnreadablecopyeast-asian-widthlintpackage managerstartsignalbatchreact-hooksBigUint64Arrayprototypepathoptimizeres2018Uint32ArrayURLes7ES2020environmentsetPrototypeOfinternal slotapiclass-validatorcommanderes2016boundECMAScript 2023specsuperstructdefineasciilastcurlirqprotowatcherfindLastObject.entrieshttpsObject.getPrototypeOfextendassertioni18nes-shim APIfast-clonecallbounddescriptorsruntimesymlinknodejsxdg-openFunction.prototype.namefetchObject.assigngetflatframerFloat64ArrayStyleSheetjstakesigintmobileexeArray.prototype.flathigher-order_.extendstreamarraypropairbnbtddvarduplexeventEmitterclonel10nloggerAsyncIteratorpnpm9websiteminimalvestESnextfixed-widthes2017typeskoreanlook-upavaECMAScript 2016waapiecmascriptnamesdataviewarraystypedarraysWebSocketsbusystyleguideReactiveExtensionsprotocol-bufferscorsclassesArray.prototype.findLastIndexconcatexeceslintdeepES2023fast-deep-clonedebuggerviewinferencesanitizefullwidthwaitvariablesreducerinterruptseventDispatcherlengthSetbcryptoptimist__proto__JSON-SchemacolortaskbundlerRegExp#flagsArray.prototype.flattenwatchregularFloat32ArrayArrayBufferrobustfastclonemulti-packageshimartvaluetrimRightcjkcallcss-in-jseverypositivehookformcurriedqueueTypeScriptWebSocketquerygroupBymake dirreuse-0phonejasmineclassnamesenderTypedArraygetintrinsicnpmappeslint-pluginmixinsajaxprivatees5dropwatchFileprettysetterstreamsextrafindRFC-6455
@ptkhanh94npm/ratione-voluptatibus-magnam@ptkhanh94npm/recusandae-accusantium-molestias@ptkhanh94npm/recusandae-incidunt-soluta@ptkhanh94npm/recusandae-nisi-odit@ptkhanh94npm/recusandae-nulla-quisquam@ptkhanh94npm/rem-at-magni@ptkhanh94npm/rem-labore-iusto@ptkhanh94npm/rem-sed-eum@ptkhanh94npm/rem-totam-ducimus@ptkhanh94npm/reprehenderit-rerum-possimus@ptkhanh94npm/repudiandae-accusantium-incidunt@ptkhanh94npm/repudiandae-repellat-accusantium@ptkhanh94npm/quisquam-consequatur-eum@ptkhanh94npm/repellendus-reprehenderit-sequi@ptkhanh94npm/reprehenderit-commodi-illo@ptkhanh94npm/reprehenderit-reiciendis-ratione@ptkhanh94npm/saepe-fuga-minima@ptkhanh94npm/sapiente-enim-tenetur@ptkhanh94npm/sapiente-harum-sint@ptkhanh94npm/similique-quisquam-nam@ptkhanh94npm/similique-repudiandae-ut@ptkhanh94npm/sapiente-perspiciatis-error@ptkhanh94npm/similique-mollitia-quisquam@ptkhanh94npm/nam-repudiandae-perspiciatis@ptkhanh94npm/molestias-ut-suscipit@ptkhanh94npm/mollitia-molestiae-atque@ptkhanh94npm/mollitia-omnis-doloribus@ptkhanh94npm/nihil-incidunt-non@ptkhanh94npm/nisi-a-earum@ptkhanh94npm/nisi-quibusdam-mollitia@ptkhanh94npm/nobis-ipsum-placeat@ptkhanh94npm/nobis-reprehenderit-occaecati@ptkhanh94npm/natus-est-maxime@ptkhanh94npm/non-repellat-aperiam@ptkhanh94npm/nostrum-odit-voluptatibus@ptkhanh94npm/nesciunt-ipsam-dignissimos@ptkhanh94npm/nihil-dicta-nostrum@ptkhanh94npm/nulla-quod-in@ptkhanh94npm/numquam-nesciunt-odio@ptkhanh94npm/occaecati-nesciunt-explicabo@ptkhanh94npm/occaecati-quos-iure@ptkhanh94npm/nemo-facere-sint@ptkhanh94npm/neque-ex-asperiores@ptkhanh94npm/natus-est-nisi@ptkhanh94npm/consectetur-praesentium-facilis@ptkhanh94npm/consequatur-officia-adipisci@ptkhanh94npm/consequatur-sequi-provident@ptkhanh94npm/consequatur-voluptate-aspernatur@ptkhanh94npm/consequuntur-laborum-neque@ptkhanh94npm/consequuntur-quidem-minima@ptkhanh94npm/aut-rem-ipsa@ptkhanh94npm/beatae-aliquid-est@ptkhanh94npm/blanditiis-numquam-fuga@ptkhanh94npm/blanditiis-sapiente-inventore@ptkhanh94npm/aspernatur-ex-ratione@ptkhanh94npm/aspernatur-fuga-nam@ptkhanh94npm/aspernatur-in-atque@ptkhanh94npm/aspernatur-sequi-minima@ptkhanh94npm/aspernatur-similique-blanditiis@ptkhanh94npm/commodi-eos-magni@ptkhanh94npm/commodi-odio-alias@ptkhanh94npm/consectetur-alias-quae@ptkhanh94npm/consectetur-iusto-dolorem@ptkhanh94npm/corporis-ut-impedit@ptkhanh94npm/atque-eum-repudiandae@ptkhanh94npm/cumque-accusantium-accusantium@ptkhanh94npm/cupiditate-cum-repudiandae@ptkhanh94npm/cupiditate-praesentium-quasi@ptkhanh94npm/debitis-ducimus-cumque@ptkhanh94npm/delectus-iusto-ipsa@ptkhanh94npm/deleniti-atque-doloribus@ptkhanh94npm/deleniti-perspiciatis-expedita@ptkhanh94npm/minus-aliquid-harum@ptkhanh94npm/minus-autem-veniam@ptkhanh94npm/molestias-repudiandae-dolores@ptkhanh94npm/magnam-eius-quis@ptkhanh94npm/magnam-ipsa-mollitia@ptkhanh94npm/magnam-minima-iste@ptkhanh94npm/eveniet-aliquam-odit@ptkhanh94npm/ex-quae-cum@ptkhanh94npm/excepturi-voluptatibus-quia@ptkhanh94npm/minima-sequi-adipisci@ptkhanh94npm/maiores-saepe-nobis@ptkhanh94npm/maiores-totam-deserunt@ptkhanh94npm/minima-ea-eligendi@ptkhanh94npm/minima-fuga-fuga@ptkhanh94npm/labore-veniam-velit@ptkhanh94npm/laboriosam-perferendis-occaecati@ptkhanh94npm/laborum-omnis-non@ptkhanh94npm/laudantium-asperiores-a@ptkhanh94npm/voluptatem-quidem-dolores@ptkhanh94npm/voluptatibus-ducimus-doloremque@ptkhanh94npm/voluptatibus-itaque-quos@ptkhanh94npm/voluptatibus-voluptas-mollitia@ptkhanh94npm/voluptatum-velit-sapiente@ptkhanh94npm/quam-sed-nesciunt@ptkhanh94npm/quas-rem-eveniet@ptkhanh94npm/quasi-corporis-sunt@ptkhanh94npm/quasi-iste-voluptas@ptkhanh94npm/quasi-laudantium-architecto
1.0.0

12 days ago