Comparing version 1.1.5 to 1.1.6
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.9.0 | ||
// Generated by CoffeeScript 1.12.7 | ||
(function() { | ||
@@ -218,3 +218,7 @@ 'use strict'; | ||
listenerCount: function(emitter, type) { | ||
var data, result, reuslt; | ||
var data, result; | ||
if (typeof emitter === 'string') { | ||
type = emitter; | ||
emitter = this; | ||
} | ||
data = emitter._events; | ||
@@ -224,3 +228,3 @@ if (!(data && data[type])) { | ||
} else if (isFunction(data[type])) { | ||
reuslt = 1; | ||
result = 1; | ||
} else { | ||
@@ -227,0 +231,0 @@ result = data[type].length; |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.9.0 | ||
// Generated by CoffeeScript 1.12.7 | ||
(function() { | ||
@@ -3,0 +3,0 @@ "use strict"; |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.9.0 | ||
// Generated by CoffeeScript 1.12.7 | ||
(function() { | ||
@@ -43,2 +43,4 @@ 'use strict'; | ||
Eventable.prototype.listenerCount = methods.listenerCount; | ||
Eventable.prototype.emit = methods.emit; | ||
@@ -45,0 +47,0 @@ |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.9.0 | ||
// Generated by CoffeeScript 1.12.7 | ||
(function() { | ||
@@ -3,0 +3,0 @@ var EventEmitter, Eventable; |
{ | ||
"name": "events-ex", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "Browser-friendly enhanced events most compatible with standard node.js, it's powerful eventable ability.", | ||
@@ -43,2 +43,3 @@ "contributors": [ | ||
"devDependencies": { | ||
"coffee-script": "^1.12.7", | ||
"pre-commit": "~1.0.7", | ||
@@ -45,0 +46,0 @@ "tad": "~0.2.1" |
@@ -57,2 +57,4 @@ ### events-ex [![Build Status](https://img.shields.io/travis/snowyu/events-ex.js/master.png)](http://travis-ci.org/snowyu/events-ex.js) [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex) | ||
console.log 'event occur' | ||
# be care: @(this) is the event object. not the `my` instance. | ||
# the my instance is @target. | ||
@@ -84,4 +86,7 @@ my.emit 'event' | ||
```coffee | ||
## Coffee-script demo bubbling usage: | ||
EventEmitter = require('events-ex') | ||
inherits = require('inherits-ex') | ||
ABORT = -1 | ||
DONE = 0 | ||
@@ -98,4 +103,52 @@ class MyDb | ||
db = new MyDb | ||
db.on 'getting', (key)-> | ||
result = myGet(key); | ||
if result? | ||
# get the key succ | ||
this.result = | ||
state: DONE | ||
result: result | ||
else if result is null | ||
# abort default get key. | ||
this.result = state: ABORT; | ||
# this.stopped = true # it will skip other listeners if true | ||
``` | ||
```js | ||
// js demo bubbling usage: | ||
let EventEmitter = require('events-ex') | ||
let isObject = require('util-ex/lib/is/type/object') | ||
const ABORT = -1 | ||
const DONE = 0 | ||
class MyDb extends EventEmitter { | ||
get(key) { | ||
// Demo the event object bubbling usage: | ||
let result = this.emit('getting', key) | ||
if(isObject(result)) { | ||
if (result.state === ABORT) return | ||
if (result.state === DONE) return result.result | ||
} | ||
return _get(key) | ||
} | ||
} | ||
let db = new MyDb | ||
db.on('getting', function(key){ | ||
result = myGet(key); | ||
if (result != null) { | ||
// get the key succ | ||
this.result = { | ||
state: DONE, | ||
result: result, | ||
} | ||
} else if (result === null) { | ||
// abort default get key. | ||
this.result = {state: ABORT}; | ||
// this.stopped = true // it will skip other listeners if true | ||
} | ||
}) | ||
``` | ||
event-emitter usage: | ||
@@ -107,16 +160,16 @@ | ||
var emitter = ee({}), listener; | ||
var MyClass = function () { /* .. */ }; | ||
ee(MyClass.prototype); // All instances of MyClass will expose event-emitter interface | ||
var emitter = new MyClass(), listener; | ||
emitter.on('test', listener = function (args) { | ||
// …emitter logic | ||
// … react to 'test' event | ||
}); | ||
emitter.once('test', function (args) { | ||
// …invoked only once(!) | ||
//and can return result to emit. | ||
this.result = 18; | ||
// … react to first 'test' event (invoked only once!) | ||
}); | ||
//return the result is 18. | ||
var result = emitter.emit('test', arg1, arg2/*…args*/); // Two above listeners invoked | ||
emitter.emit('test', arg1, arg2/*…args*/); // Two above listeners invoked | ||
emitter.emit('test', arg1, arg2/*…args*/); // Only first listener invoked | ||
@@ -127,2 +180,3 @@ | ||
``` | ||
### API | ||
@@ -136,4 +190,6 @@ | ||
* `options` *(object)*: optional options | ||
* `include` *(array|string)*: only these emitter methods will be added to the class | ||
* `exclude` *(array|string)*: theses emitter methods would not be added to the class | ||
* `include` *(string[]|string)*: only these emitter methods will be added to the class | ||
* **NOTE:** static method should use the prefix '@' with name. | ||
* `exclude` *(string[]|string)*: theses emitter methods would not be added to the class | ||
* **NOTE:** static method should use the prefix '@' with name. | ||
* `methods` *(object)*: hooked methods to the class | ||
@@ -153,4 +209,4 @@ * key: the method name to hook. | ||
class MyClass | ||
# only 'on', 'off', 'emit' added to the class | ||
eventable MyClass, include: ['on', 'off', 'emit'] | ||
# only 'on', 'off', 'emit' and static methods 'listenerCount' added to the class | ||
eventable MyClass, include: ['on', 'off', 'emit', '@listenerCount'] | ||
@@ -157,0 +213,0 @@ # add the eventable ability to OtherClass and inject the exec method of OtherClass. |
@@ -15,3 +15,3 @@ 'use strict'; | ||
var My = function(){}; | ||
eventable(My, {include: ['on', 'off', 'listenerCount']}); | ||
eventable(My, {include: ['on', 'off', '@listenerCount']}); | ||
var keys = Object.keys(My); | ||
@@ -40,2 +40,3 @@ assert.deepEqual(keys, ['listenerCount']); | ||
'addListener', | ||
'listenerCount', | ||
'removeListener', | ||
@@ -55,2 +56,3 @@ 'removeAllListeners', | ||
'addListener', | ||
'listenerCount', | ||
'removeListener', | ||
@@ -72,2 +74,3 @@ 'removeAllListeners', | ||
'addListener', | ||
'listenerCount', | ||
'removeListener', | ||
@@ -74,0 +77,0 @@ 'removeAllListeners', |
@@ -36,4 +36,6 @@ 'use strict'; | ||
t.deepEqual(e1.listeners('foo'), [listener]); | ||
t.equal(e1.listenerCount('foo'), 1); | ||
e1.removeAllListeners('foo'); | ||
t.equal(e1.listenerCount('foo'), 0) | ||
t.deepEqual(e1.listeners('foo'), []); | ||
@@ -40,0 +42,0 @@ t.deepEqual(fooListeners, [listener]); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
95650
48
1880
275
3