Code Monkey home page Code Monkey logo

node-event-handler's Introduction

node-event-handler

npm version build status coverage downloads js-standard-style

A generic Node.js style event handler.

Installation

$ npm install node-event-handler

Usage

const NodeEventHandler = require('node-event-handler')
const WebSocket = require('ws')

class MyWSController extends SomeOtherClass {
  constructor () {
    this.ws = new WebSocket('ws://localhost:8080')
    this.handler = new NodeEventHandler(this, this.ws)
  }

  // These methods handle the websocket events
  onmessage (data) {}
  onopen () {}
  onerror (error) {}
  onclose () {}
}

API

handler = new NodeEventHandler(ctx, [ee])

Create a new instance of NodeEventHandler passing in a context ctx (often this when created within a class) and optionally a Node.js style event emitter ee to attach listeners to on instantiation.

The ctx should be an object who's prototype contains event handler methods. Event handler methods must take the form of on${eventname} where eventname is the name of the event you want to listen on and handle (the name you would pass to ee.on(eventname)). In practice, you can pass a class instance as a ctx, or this when the instance owns the NodeEventHanlder instance.

handler.addEventListeners(ee)

Attach all event handler methods on ctx to the Node.js style event emitter ee.

handler.removeEventListeners(ee)

Remove all event handler event names on ctx from the Node.js style event emitter ee.

See also

This module is an API match to dom-event-handler which has a clever API, but requires the support of the handleEvent on the EventListener interface. Most Node.js modules have poor support for these interfaces because they are difficult to replicate outside of a browser.

When using dom-event-handler in a universal context, this can be used as a stand-in on the Node.js side of things.

License

MIT

node-event-handler's People

Contributors

bcomnes avatar

Stargazers

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