Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eventif

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventif - npm Package Compare versions

Comparing version 0.2.2 to 0.3.0

123

lib/index.js

@@ -5,74 +5,81 @@ "use strict";

class EventIf extends events {
on(ev, cond, handler) {
let condition, args, that = this;
// explore the handler and get its arguments
const createClass = function (parent) {
if (parent === null || parent === undefined || !(parent === events || parent.prototype instanceof events)) {
return null;
}
class cl extends parent {
on(ev, cond, handler) {
let condition, args, that = this;
// explore the handler and get its arguments
// the no condition use case
if (handler === undefined && typeof(cond) === "function") {
return super.on(ev,cond);
}
// the no condition use case
if (handler === undefined && typeof(cond) === "function") {
return super.on(ev,cond);
}
condition = cond;
args = introspector.argNames(handler);
condition = cond;
args = introspector.argNames(handler);
// an actual condition
return super.on(ev,function () {
let parms = {};
// construct the data set to match against
// loop through the array
for (let i in arguments) {
parms[i] = arguments[i];
if (args[i] !== null && args[i] !== undefined) {
parms[args[i]] = arguments[i];
// an actual condition
return super.on(ev,function () {
let parms = {};
// construct the data set to match against
// loop through the array
for (let i in arguments) {
parms[i] = arguments[i];
if (args[i] !== null && args[i] !== undefined) {
parms[args[i]] = arguments[i];
}
}
}
// if the condition matched, call it, else ignore
if (search.matchObject(parms,condition)) {
return handler.apply(that,arguments);
}
});
}
once(ev, cond, handler) {
let condition, args, that = this, oncehandler;
// explore the handler and get its arguments
// if the condition matched, call it, else ignore
if (search.matchObject(parms,condition)) {
return handler.apply(that,arguments);
}
});
}
once(ev, cond, handler) {
let condition, args, that = this, oncehandler;
// explore the handler and get its arguments
// the no condition use case
if (handler === undefined && typeof(cond) === "function") {
return super.once(ev,cond);
}
// the no condition use case
if (handler === undefined && typeof(cond) === "function") {
return super.once(ev,cond);
}
condition = cond;
args = introspector.argNames(handler);
condition = cond;
args = introspector.argNames(handler);
// an actual condition
oncehandler = function () {
let parms = {};
// construct the data set to match against
// loop through the array
for (let i in arguments) {
parms[i] = arguments[i];
if (args[i] !== null && args[i] !== undefined) {
parms[args[i]] = arguments[i];
// an actual condition
oncehandler = function () {
let parms = {};
// construct the data set to match against
// loop through the array
for (let i in arguments) {
parms[i] = arguments[i];
if (args[i] !== null && args[i] !== undefined) {
parms[args[i]] = arguments[i];
}
}
}
// if the condition matched, call it, else ignore
if (search.matchObject(parms,condition)) {
let ret = handler.apply(that,arguments);
// stop it from running again
that.removeListener(ev, oncehandler);
}
};
return super.on(ev,oncehandler);
// if the condition matched, call it, else ignore
if (search.matchObject(parms,condition)) {
let ret = handler.apply(that,arguments);
// stop it from running again
that.removeListener(ev, oncehandler);
}
};
return super.on(ev,oncehandler);
}
}
}
return cl;
};
const EventIf = createClass(events);
EventIf.y = function (target) {
const SubIf = createClass(target);
return SubIf;
};

@@ -85,4 +92,2 @@ // enable is just an alias for y

// next steps:
// 1- create the .on() and .once() functions that override the emitter
// 2- create the .y/.enable function
{
"name": "eventif",
"description": "Intelligent event emitter for nodejs - only handle events when they have 'the right stuff'",
"version": "0.2.2",
"version": "0.3.0",
"license": "MIT",

@@ -6,0 +6,0 @@ "url": "http://github.com/deitch/eventif",

@@ -89,6 +89,4 @@ # eventif

#### Enable an Existing Emitter
**NOTE: Enabling is not functional yet.**
If you already have a standard event emitter class which, somewhere down the line, inherits from `events`, you may not be able to change it to inherit from `eventif` instead. In that case, instead of creating a new event emitter, you *enable* the existing one to be eventif capable.
If you already have a standard event emitter which, somewhere down the line, inherits from `events`, you may not be able to change it to inherit from `eventif` instead. In that case, instead of creating a new event emitter, you *enable* the existing one to be eventif capable.
````javascript

@@ -104,8 +102,28 @@ const Eventif = require('eventif');

myEmitter = new MyEmitter();
Eventif.y(myEmitter);
Em = Eventif.y(MyEmitter);
// or an alias
Eventif.enable(myEmitter);
Em = Eventif.enable(MyEmitter);
myEmitter = new Em();
````
If es6 classes make you happier:
````javascript
const EventIf = require('eventif');
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
Em = Eventif.y(MyEmitter);
// or an alias
Em = Eventif.enable(MyEmitter);
myEmitter = new Em();
````
**NOTE:** You only can enable an existing event emitter class if it inherits from `EventEmitter`.
Now we have an eventif-capable emitter. Let's use it!

@@ -112,0 +130,0 @@

@@ -106,4 +106,4 @@ /*global describe,before,it, beforeEach */

class MyEmitter extends events {}
emitter = new MyEmitter();
EventIf.y(emitter);
emitter = EventIf.y(MyEmitter);
emitter = new emitter();
});

@@ -115,4 +115,4 @@ allTests();

class MyEmitter extends events {}
emitter = new MyEmitter();
EventIf.enable(emitter);
emitter = EventIf.enable(MyEmitter);
emitter = new emitter();
});

@@ -119,0 +119,0 @@ allTests();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc