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(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18278
208
349
0