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

@nik-kita/promitter

v1.3.1

Published

--- ## NAMING: * Prefixes: * I - interface `interface ICool { ... }` * T - type `type TMessage = { ... }`

Downloads

37

Readme

Promitter

[For this moment it is an amateur library, use it on your own risk..]

This class provide a simple API for symbioses EventEmitter's and Promise's natures Inspired by RXJS

Updates

wait() and emitAndWaitComplete has additional optional parameter resolveIfWasInPast_:boolean = false Default behavior is like in previous versions. Behavior for true value: If event on which first emittion you want resolve your promise was emitted before this waiting - it will be resolve instansly. Why it may be needed? Example - wait on Socket connection. But if connection is already open you will be resolve even if hang this promise after it; P. S. You may clean all memory about past completed mimimum one time events by calling 'resetCompleted()' method.

Declaration:

Promitter<TLabel extends string = string>

You may use 'TLabel' for more implicitly type declaration of your code


emit(label: TLabel, data?: any): Promitter

Like 'EventEmitter.emit(...)'


emitReject(label: TLabel, data?: any): Promitter;

The pending promises, given by 'some label' from 'promitter.wait('some label')' or from 'promitter.emitAndWaitComplete('some label')', these promises will be fullfilled with reject.


once(label: TLabel, cb: TOnCb): Promitter;

Like 'EventEmitter.once(...)'.


on(label: TLabel, cb: TOnCb): Promitter;

Like 'EventEmitter.on_(...)'


wait<T = unknown>(label: TLabel): Promise;

Return Promise, that will be fullfilled with first promitter's 'label' emitting anywhere in your code after this moment.


emitAndWaitComplete<T = unknown>(label: TLabel, data?: unknown): Promise;

You emit like 'EventEmitter.emit(label, data)' but will return a Promise, that will be fullfilled with first listener completion.


rmListeners(label?: TLabel, cbs?: TOnCb[]): Promitter;

Without arguments - absolutelly clean your Promitter instance from listeners With label - remove all listeners for this label for your Promitter instance With label + callback's array - remove concrete listeners... So like with EventEmitter, if you want to delete listener in future - save its callback for this (don't pass it like anonimus function, but first - save in variable)

Usage example with Socket:

import Ws, { WebSocket } from 'ws';
import Promitter from '@nik-kita/promitter';

type MessageExampleType = {
    channel: 'channel1' | 'channel2',
    data: any,
}
let isPause = false;
const promitter = new Promitter<
    'open' | 'close' | 'error' | 'message' | 'channel1' | 'channel2' | 'pause' | 'continue' | 'job'
>().on('continue', () => {
        isPause = false;
    })
    .on('pause', () => {
        isPause = true;
    });

const ws = new Ws('ws://your-socket-connection')
    .on('open', () => {
        promitter.emit('open');
    })
    .on('close', () => {
        promitter.emit('close');
    })
    .on('error', () => {
        promitter.emit('error');
    })
    .on('message', (data: string) => {
        const message = JSON.parse(data) as MessageExampleType;

        if (isPause) return;

        promitter.emit(message.channel, message);
    });

setTimeout(() => promitter.emit('pause'), 4000);

(async () => {
    await promitter.wait('open');
    ws.send('subscribe.channel2');

    promitter.on('message', console.log);

    await promitter.wait('pause');

    console.log('FIRST PAUSE');

    await new Promise((resolve) => setTimeout(resolve, 4000));

    await promitter.emitAndWaitComplete('continue');

    console.log('CONTINUE');
})();

// Example little strange... but hope it will explain the possibilities and goals of this package