Git Product home page Git Product logo

consecute's Introduction

consecute

Handle any Javascript and Typescript function using pub/sub.

consecute Github licence CI

Special thanks to David Walsh (@darkwing) for the inspiration!

Installation

npm i consecute 

Getting started

Import the globally available instance

import cs from 'consecute'; // Globally available instance

Or instanciate your own instance

import { consecute } from 'consecute';
const cs = consecute();

Usage

Subscribe and publish simple usage

const sub = cs.subscribe('interesting-topic', yourFunction);

cs.publish('interesting-topic', your, arguments, here)
  .then((promiseSettledResults) => maybeDoSomethingIfYouLike());

sub.remove(); // deletes this specific subscription

cs.clear(); // clear all topics

Typescript safety

You can merge the EventMapBase declaration to specify your hook types.

interface EventMapBase {
  bingo: [string, number];
}

cs.subscribe('bingo', (one, two) => {
//                     ^^^^^^^^ These have the type `string`, `number`
});

You can also extend the EventMapBase for your separate instances.

interface MyEventMap extends EventMapBase {
  binga: number;
  bingo: { a: number, b: string };
  bingu: [string, number, boolean];
}

const customInstance = consecute<MyEventMap>();

customInstance.subscribe('binga', (one) => {
//                                 ^^^ This has the type `number`
});

customInstance.subscribe('bingo', (one) => {
//                                 ^^^ This has the type `{ a: number, b: string }`
});

customInstance.subscribe('bingu', (one, two, three) => {
//                                 ^^^^^^^^^^^^^^^ These have the type `string`, `number` and `boolean` respectively
});

In the case where you would want to send an array as a standalone argument (not spread), you would do this

interface MyEventMap extends EventMapBase {
  bingy: [Array<string>];
}

const customInstance = consecute<MyEventMap>();

customInstance.subscribe('bingy', (one) => {
//                                 ^^^ This has the type `Array<string>`
});

consecute's People

Contributors

mats852 avatar emeraldsanto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.