Giter Club home page Giter Club logo

chnl's Introduction

chnl

Actions Status npm version license

Implementation of event channels (aka pub/sub, dispatcher, emitter) inspired and compatible with Chrome extensions Events API.

Install

npm i chnl

Docs

https://vitalets.github.io/chnl

Usage

foo.js

import Channel from 'chnl';

// create channel
export const onData = new Channel();

// subscribe to channel
onData.addListener(data => console.log(data));

bar.js

import {onData} from './foo';

// dispatch event to channel
onData.dispatch({foo: 'bar'});

Adding/removing listeners in dispatching loop

Chnl makes a copy of the listeners before starting dispatching loop. So modifying listeners list (adding/removing) in dispatching loop will affect only the next dispatch:

const onData = new Channel();
const listener1 = () => console.log(1);
const listener2 = () => {
  console.log(2);
  onData.addListener(listener3);
};
const listener3 = () => console.log(3);
onData.addListener(listener1);
onData.addListener(listener2);

onData.dispatch();
// 1
// 2
onData.dispatch();
// 1
// 2
// 3

License

MIT @ Vitaliy Potapov

chnl's People

Contributors

stamoern avatar vitalets avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

nomanready procot

chnl's Issues

add ReactSubscription class to easily subscribe/unsubscribe react components in lyfecycle methods

Before:

class Button extends React.Component {
  constructor() {
    super();
    this.subscription = new Channel.Subscription([
      {channel: onNewData, listener: this.handleNewData.bind(this)}
    ]);
  }
  componentDidMount() {
    this.subscription.on();
  }
  componentWillUnmount() {
    this.subscription.off();
  }
}

After:

class Button extends React.Component {
  constructor() {
    super();
    new Channel.ReactSubscription(this, [
      {channel: onNewData, listener: this.handleNewData.bind(this)}
    ]);
  }
}

index.d.ts is not compatible for most cases

[!] (plugin commonjs--resolver) RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
node_modules/chnl/types/index.d.ts (1:19)
1: import RealChannel = require('./channel');

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.