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

preactor

v0.8.3

Published

preactors

Downloads

8

Readme

preactor

build status AppVeyor Build Status Security Responsible Disclosure


preactor allows to develop event-heavy and reactive applications in a seemless manner. Its manifestation, the Preactor type, an EventEmitter, has a variety of methods for manipulating the process of an event being emitted. The project is inspired by the evolving Observable type. Apart from initial inspiration there is no further relation to Observable implementations out in the wild. Note that preactor can be used in node or a browser.


Get it!

node:

npm install --save preactor

browser:

Source the browser bundle from the filesystem or unpkg cdn:

<!-- <script src="./browser.js"></script> -->
<script src="https://unpkg.com/preactor"></script>

Find the UMD build @ window.preactor.


Usage

Wrap any EventEmitter, EventTarget, or Promise instance with the Preactor type, then manipulate its events with the various Preactor prototype methods, transducers.

Note that all transducers return this, meaning you can simply chain them, allowing for straight-forward event processing.

There is one example for a browser context and another one for node.

To check out the browser demo run npm run browser-demo or open ./browser_usage.html with a browser.

Check out the node demo by running npm run node-demo or node ./node_usage.js.

node demo:

const preactor = require('preactor')

console.log('type sth and hit enter multiple times...')

preactor(process.stdin, 'data')
  .accumulate(2, true, (accu = [ '' ], args) => {
    return [ accu[0] + args[0].toString().trim() ]
  })
  .on('data', accu => console.log('accumulated data:', accu))

API

new Preactor(emitter, eventName[, errorName])

Create a preactor that wraps an EventEmitter, EventTarget or Promise.

For those that don't like new:

preactor(emitter, eventName[, errorName])

The returned Preactor instance itself is just a simple EventEmitter. Passing eventName is required for all input emitters that are not a Promise. For a Promise eventName defaults to 'resolved', whereas errorName defaults to 'rejected'. When wrapping an EventEmitter or EventTarget eventName is required and indicates the event to preact upon, whereas errorName is optional and indicates the name of the event that signals failure. Specifying errorName allows to listen for errors directly on the preactor instance as it simply forwards failures of its wrapped subject.

Preactor.prototype.accumulate(n[, repeat][, argsReducer])

Accumulate n emits of the preactor's event using argsReducer to handle the propagation of arguments. n must be an unsigned integer, repeat a boolean, and argsReducer a function. The latter must have arity two, take two arrays as inputs, whereby the first parameter should have a default argument, i.e. argsReducer(a = [], b), and return an array. argsReducer defaults to a latestWin implementation. repeat indicates whether to repeat accumulation or only accumulate the first n emits, defaults to false.

Preactor.prototype.accumulateInterval(ms[, unref][, argsReducer])

Accumulate emits of the preactor's event within a recurring interval. ms must be an unsigned integer and indicates the interval duration. unref must be a boolean and indicates whether to call Timeout.prototype.unref in a node context, defaults to false. argsReducer should be a function and defaults to a latestWin implementation. It must have arity two, take two arrays as inputs, whereby the first parameter should have a default argument, i.e. argsReducer(a = [], b), and return an array.

Preactor.prototype.debounce(ms[, unref][, argsReducer])

Debounces the preactor's event by ms. Unreference internal timers by passing a truthy non-function second argument which defaults to false. If passed argsReducer must be a function and defaults to a latestWin implementation. It must have arity two, take two arrays as inputs, whereby the first parameter should have a default argument, i.e. argsReducer(a = [], b), and return an array.

Preactor.prototype.delay(ms[, unref])

Delay events by ms. Pass unref truthy and the internal timers will be unreferenced, defaults to false.

Preactor.prototype.distinct(pred)

Only allow emits that pass a predicate test. pred must be a function and defaults to a naiveNeverBefore implementation. It must have arity two, take two arrays as inputs, whereby the first parameter should have a default argument, i.e. pred(accu = [], args), and return a boolean.

Preactor.prototype.filter(pred)

Only allow emits that pass a predicate test. pred must be a function, there is no default implementation. pred gets passed all arguments that a subject emits with as an event, and must return a boolean that indicates whether to have the preactor reemit the event and corresponding arguments.

Preactor.prototype.limit(n)

Limit the number of emits of this preactor instance to n.

Preactor.prototype.mask(mask, recycle)

Control whether a Preactor instance reemits its emitter's events according to a boolean sequence. mask must be an array, which is used as a boolean schedule that indicates whether to or not have the preactor reemit its emitter's events. recycle indicates whether the preactor should repeat reemitting events according to mask or to have it stop reemitting for good.

Preactor.prototype.notWhitin(start, end)

Have a Preactor instance reemit events of its subject only if these are not emitted within the specified time frame. start and end must be unsigned integers representing the start and end of the ignore time frame as unix timestamps.

Preactor.prototype.onlyWhitin(start, end)

Have a Preactor instance reemit events of its subject only if these are emitted within the specified time frame. start and end must be unsigned integers representing the start and end of the time frame as unix timestamps.

Preactor.prototype.clearOwnTimeout()

Clear the current internal timeout timer. Useful for teardown work.

Preactor.prototype.clearOwnInterval()

Clear the current internal interval timer. Useful for teardown work.

Preactor.prototype.reset(index)

Reset the Preactor instance's effective transducer to a previous one. index must be an unsigned integer as it indicates the index of the transducer to put into effect (indexing is zero-based), thus the first transducer that was applied on the Preactor instance has index 0, the second 1, and so forth.

Preactor.prototype.transducers

Get the internal transducers array. Useful for inferring the current transducer index in prep for using Preactor.prototype.reset(index).


License

MIT