Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

emits returns a function which will emit and parse the specified event.

License

NotificationsYou must be signed in to change notification settings

primus/emits

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version npmCICoverage Status

Installation

This module is compatible with browserify and node.js and is therefore releasedthrough npm:

npm install --save emits

Usage

In all examples we assume that you've assigned theemits function to theprototype of your class. This class should inherit from anEventEmitter classwhich uses theemit function to emit events. For example:

'use strict';varEventEmitter=require('events').EventEmitter,emits=require('emits');functionExample(){EventEmitter.call(this);}require('util').inherits(Example,EventEmitter);//// You can directly assign the function to the prototype if you wish or store it// in a variable and then assign it to the prototype. What pleases you more.//Example.prototype.emits=emits;// require('emits');//// Also initialize the example so we can use the assigned method.//varexample=newExample();

Now that we've set up our example code we can finally demonstrate the beauty ofthis functionality. To create a function that emitsdata we can simply do:

vardata=example.emits('data');

Every time you invoke thedata() function it will emit thedata event withall the arguments you supplied. If you want to "curry" some extra arguments youcan add those after the event name:

vardata=example.emits('data','foo');

Now when you calldata() thedata event will receivefoo as first argumentand the rest of the arguments would be the ones that you've supplied to thedata() function.

If you supply a function as the last argument we assume that this is an asyncargument parser. This allows you to modify the arguments, prevent the event frombeing fired or just clear all supplied arguments (except for the ones that arecurried in). The first argument of the function is always the callback function,all other arguments after that are the ones emitted with the event. The callbackfunction follows the usual error first pattern. When the callback is invokedwith an error it will emit anerror event on theEventEmitter instance. Inour case theexample instance:

vardata=example.emits('data',functionparser(next,arg){try{arg=JSON.parse(arg);}catch(e){returnnext(e);}next(undefined,arg);});

To modify the data you need to supply the change as second argument:

vardata=example.emits('data',functionparser(next,arg){next(undefined,'bar');});

In the example above we've transformed the incoming argument tobar. So whenyou calldata() it will emit adata event withbar as the second argument.If you call the callback withundefined as second argument we assume that nomodifications have been made and we emit all received arguments. If you want toclear all received arguments, call the callback withnull:

vardata=example.emits('data',functionparser(next,arg){next(undefined,null);});

Patterns

In Primus the most common pattern for this module is to proxy events from oneinstance to another:

eventemitter.on('data',example.emits('data'));

It is also very useful to re-format data. For example, in the case of WebSockets,if we don't want to referenceevt.data every time we need to access the data,we can parse the argument as following:

varws=newWebSocket('wss://example.org/path');ws.onmessage=example.emits('data',functionparser(next,evt){next(undefined,evt.data);});

In the example above we will now emit thedata event with a direct referencetoevt.data. The following final example shows how you can prevent eventsfrom being emitted.

varws=newWebSocket('wss://example.org/path');ws.onmessage=example.emits('data',functionparser(next,evt){vardata;try{data=JSON.parse(evt.data);}catch(e){returnnext(e);}if('object'!==typeofdata||Array.isArray(data))return;next(undefined,data);});

By not calling the callback we make sure that the event is not emitted. So thedata event will only be fired if we've received a valid JSON document from theserver and it's an object.

License

MIT

About

emits returns a function which will emit and parse the specified event.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp