GithubHelp home page GithubHelp logo

lucashenning / typed-rx-emitter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bcherny/typed-rx-emitter

0.0 3.0 0.0 247 KB

Typesafe RxJS-based EventEmitter

License: MIT License

CSS 4.82% HTML 3.64% TypeScript 81.68% JavaScript 9.85%

typed-rx-emitter's Introduction

typed-rx-emitter: Typesafe Rx-based event emitter

Build Status npm mit ts flow

Highlights

  • 100% typesafe:
    • Statically enforces that channels in .on() are defined
    • Statically enforces that channels in .emit() are defined
    • Statically enforces that emitters are called with the correct data given their Message name
    • Statically enforces that listeners are called with the correct data given their Message name
  • Supports all RxJS Observable methods
  • Supports RxJS versions 4, 5, and 6
  • Preforms dynamic analysis to detect and warn about cycles in emitters
  • 3.7kb gzipped & minified (using RxJS6), including RxJS

Installation (with RxJS v5.x or v6.x - recommended)

# Using Yarn:
yarn add typed-rx-emitter rxjs

# Using NPM:
npm install typed-rx-emitter rxjs --save

Installation (with RxJS v4.x)

# Using Yarn:
yarn add typed-rx-emitter@^0.3

# Using NPM:
npm install typed-rx-emitter@^0.3 --save

Usage

import { Emitter } from 'typed-rx-emitter'

// Enumerate messages
type Messages = {
  INCREMENT_COUNTER: number
  OPEN_MODAL: boolean
}

const emitter = new Emitter<Messages>()

// Listen on an event (basic)
emitter
  .on('OPEN_MODAL')
  .subscribe(_ => console.log(`Change modal visibility: ${_}`)) // _ is a boolean

// Listen on an event (advanced)
import { debounceTime, filter } from 'rxjs/operators'

emitter
  .on('INCREMENT_COUNTER')
  .pipe(
    filter(_ => _ > 3), // _ is a number
    debounceTime(100)
  )
  .subscribe(_ => console.log(`Counter incremented to ${_}`)) // _ is a number

// Listen on all events
emitter
  .all()
  .subscribe(() => console.log('Something changed'))

// Trigger an event - throws a compile time error unless id and value are set, and are of the right types
emitter.emit('OPEN_MODAL', true)

// Event is misspelled - throws a compile time error
emitter.emit('INCREMENT_CONTER')

See a complete browser usage example here.

Options

Emitter takes an optional options argument. options is an object, and each key in the object is optional:

Option Default Description
Error CyclicalDependencyError Custom Error subclass for cycle warning
isDevMode false Perform dynamic analysis to warn about cycles?

Tests

npm test

typed-rx-emitter's People

Contributors

bcherny avatar lucashenning avatar

Watchers

 avatar  avatar  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.