multi-event
Advanced tools
Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "multi-event", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"homepage": "https://github.com/yousfiSaad/multi-event", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -21,3 +21,2 @@ // multi-event 1.0.0 | ||
type: 'mono', //molti/mono | ||
splited: [] | ||
@@ -63,2 +62,3 @@ }; | ||
}, | ||
matches: function matches(pattern) { | ||
@@ -76,3 +76,2 @@ return function (eName) { | ||
} | ||
return true; | ||
@@ -180,3 +179,5 @@ }; | ||
callBack.apply({ eventName: eventName }, args); | ||
callBack.apply({ | ||
eventName: eventName | ||
}, args); | ||
} | ||
@@ -183,0 +184,0 @@ } catch (err) { |
@@ -1,2 +0,2 @@ | ||
/*! multi-event 2015-09-04 */ | ||
/*! multi-event 2015-09-16 */ | ||
"use strict";function _toConsumableArray(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}return Array.from(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),helpers={eventType:function(a){var b={valid:!1,type:"mono",splited:[]};b.splited=a.split(".");var c=!0,d=!1,e=void 0;try{for(var f,g=b.splited[Symbol.iterator]();!(c=(f=g.next()).done);c=!0){var h=f.value,i=/^[a-z0-9]+$/i.test(h),j="*"===h;if(!i&&!j)return b;j&&(b.type="multi")}}catch(k){d=!0,e=k}finally{try{!c&&g["return"]&&g["return"]()}finally{if(d)throw e}}return b.valid=!0,b.name=a,b},matches:function(a){return function(b){var c=b.split(".");if(a.length!==c.length)return!1;for(var d in a)if("*"!==c[d]&&c[d]!==a[d])return!1;return!0}}},MultiEvent=function(){function a(){_classCallCheck(this,a),this._mapMono=new Map,this._mapMulti=new Map}return _createClass(a,[{key:"on",value:function(a,b){var c=helpers.eventType(a);if(!c.valid)throw"invalid name";if("function"!=typeof b)throw"you must give me a function";var d=void 0;d="multi"!==c.type?this._mapMono:this._mapMulti;var e=void 0;return d.has(a)?e=d.get(a):(e=new Set,d.set(a,e)),e.add(b),this}},{key:"emit",value:function(a){var b=helpers.eventType(a);if(!b.valid)throw"invalid event";if("multi"===b.type)throw"you can not emit multiple events, may be in the next version";var c=this._mapMono.has(a)?[].concat(_toConsumableArray(this._mapMono.get(a))):[],d=[].concat(_toConsumableArray(this._mapMulti.keys())),e=d.filter(helpers.matches(b.splited)),f=[],g=!0,h=!1,i=void 0;try{for(var j,k=e[Symbol.iterator]();!(g=(j=k.next()).done);g=!0){var l=j.value;Array.prototype.push.apply(f,[].concat(_toConsumableArray(this._mapMulti.get(l))))}}catch(m){h=!0,i=m}finally{try{!g&&k["return"]&&k["return"]()}finally{if(h)throw i}}var n=c.concat(f),o=!0,p=!1,q=void 0;try{for(var r=arguments.length,s=Array(r>1?r-1:0),t=1;r>t;t++)s[t-1]=arguments[t];for(var u,v=n[Symbol.iterator]();!(o=(u=v.next()).done);o=!0){var w=u.value;w.apply({eventName:a},s)}}catch(m){p=!0,q=m}finally{try{!o&&v["return"]&&v["return"]()}finally{if(p)throw q}}return this}}]),a}();exports["default"]=MultiEvent,module.exports=exports["default"]; |
@@ -0,0 +0,0 @@ module.exports = function(grunt) { |
@@ -0,0 +0,0 @@ The MIT License (MIT) |
require('babel/register'); | ||
require('./test2.js'); |
{ | ||
"name": "multi-event", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/multi-event.js", |
@@ -30,3 +30,6 @@ [![Build Status](https://travis-ci.org/yousfiSaad/multi-event.svg?branch=master)](https://travis-ci.org/yousfiSaad/multi-event) | ||
var myEvents = new MultiEvent(); | ||
var myEventsSubSet = new MultiEvent(); | ||
myEvents.pipe("event2.*", myEventsSubSet); | ||
// Implement listener | ||
@@ -44,3 +47,7 @@ var callBack1 = function (arg) { | ||
var pipeCallBack = function (arg) { | ||
console.log('pipeCallBack says : '+ arg + ' (from myEventsSubSet)'); | ||
}; | ||
// Register callBack event listener | ||
@@ -50,2 +57,4 @@ myEvents.on('event.subevent1', callBack1) | ||
.on('event.*', callBack); // this callBack is trigged to all 'event' sub-events | ||
myEventsSubSet.on("event2.eventViaPipe", pipeCallBack); | ||
@@ -60,2 +69,6 @@ myEvents.emit('event.subevent1', 'this string will be logged twice'); | ||
// callBack says : this string will be logged twice | ||
myEvents.emit('event2.eventViaPipe', 'this will be passed to the other emitter also'); | ||
// the following will be displayed on the console | ||
// pipeCallBack says : this will be passed to the other emitter also (from myEventsSubSet) | ||
``` | ||
@@ -73,1 +86,2 @@ | ||
- **Watch changes and run tests and buil** `npm run watch` | ||
@@ -1,44 +0,43 @@ | ||
// multi-event 1.0.0 | ||
// multi-event 1.0.3 | ||
// (c) 2015 Saad Yousfi <yousfi.saad@gmail.com> | ||
let helpers = { | ||
eventType: (eventName) => { | ||
let ret = { | ||
valid: false, | ||
type: 'mono' //molti/mono | ||
, | ||
splited: [] | ||
} | ||
ret.splited = eventName.split('.'); | ||
for (let part of ret.splited) { | ||
let ok = /^[a-z0-9]+$/i.test(part); | ||
let ast = part === '*'; | ||
if (!(ok || ast)) { | ||
return ret; | ||
eventType(eventName) { | ||
let ret = { | ||
valid: false, | ||
type: 'mono', //molti/mono | ||
splited: [] | ||
} | ||
if (ast) { | ||
ret.type = 'multi'; | ||
ret.splited = eventName.split('.'); | ||
for (let part of ret.splited) { | ||
let ok = /^[a-z0-9]+$/i.test(part); | ||
let ast = part === '*'; | ||
if (!(ok || ast)) { | ||
return ret; | ||
} | ||
if (ast) { | ||
ret.type = 'multi'; | ||
} | ||
} | ||
} | ||
ret.valid = true; | ||
ret.name = eventName; | ||
ret.valid = true; | ||
ret.name = eventName; | ||
return ret; | ||
}, | ||
matches: (pattern) => { | ||
return (eName) => { | ||
let patternCurrent = eName.split('.'); | ||
if (pattern.length !== patternCurrent.length) { | ||
return false; | ||
} else { | ||
for (let i in pattern) { | ||
if (patternCurrent[i] !== '*' && patternCurrent[i] !== pattern[i]) { | ||
return false; | ||
return ret; | ||
}, | ||
matches(pattern) { | ||
return function(eName) { | ||
let patternCurrent = eName.split('.'); | ||
if (pattern.length !== patternCurrent.length) { | ||
return false; | ||
} else { | ||
for (let i in pattern) { | ||
if (patternCurrent[i] !== '*' && patternCurrent[i] !== pattern[i]) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
}; | ||
} | ||
return true; | ||
}; | ||
} | ||
} | ||
@@ -81,3 +80,3 @@ | ||
emit(eventName, ...args){ | ||
emit(eventName, ...args) { | ||
let infos = helpers.eventType(eventName); | ||
@@ -98,4 +97,5 @@ if (!infos.valid) { | ||
// get multiCallBacks | ||
let multiCallBacks = []; | ||
for(let multiNamesMatch of multiNamesMatches){ | ||
for (let multiNamesMatch of multiNamesMatches) { | ||
Array.prototype.push.apply(multiCallBacks, [...this._mapMulti.get(multiNamesMatch)]); | ||
@@ -107,4 +107,6 @@ } | ||
//execute callBacks | ||
for(let callBack of callBacks){ | ||
callBack.apply({eventName}, args); | ||
for (let callBack of callBacks) { | ||
callBack.apply({ | ||
eventName | ||
}, args); | ||
} | ||
@@ -115,4 +117,9 @@ | ||
pipe(subEventPatternAsString, eventEmitter){ | ||
this.on(subEventPatternAsString, function (...args){ | ||
eventEmitter.emit(this.eventName, ...args); | ||
}); | ||
} | ||
} | ||
export default MultiEvent; |
33
test.js
@@ -66,6 +66,35 @@ var assert = require('assert'); | ||
}); | ||
describe('Pipe EventsSubSet', ()=>{ | ||
let mtOne = new EventEmitter(); | ||
let mtTwo = new EventEmitter(); | ||
mtOne.pipe('subSet.*', mtTwo); | ||
it('#on event in the sub set (basic test)', (done)=>{ | ||
mtTwo.on('subSet.pipeEventOk', (...arg)=>{ | ||
if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){ | ||
done(); | ||
} | ||
}); | ||
mtOne.emit('subSet.pipeEventOk', 'an argument', 'another'); | ||
}); | ||
it('#on event in the wrong sub set', (done)=>{ | ||
mtTwo.on('subSet.pipeEventOk2', (...arg)=>{ | ||
if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){ | ||
done(); | ||
} | ||
}); | ||
mtTwo.on('subSet2.pipeEventOk2', (...arg)=>{ | ||
if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){ | ||
done("error"); | ||
} | ||
}); | ||
mtOne.emit('subSet2.pipeEventOk2', 'an argument', 'another'); | ||
mtOne.emit('subSet.pipeEventOk2', 'an argument', 'another'); | ||
}); | ||
}); | ||
describe('MultiEventEmitter', () => { | ||
@@ -77,3 +106,3 @@ it('#on() and #emit() multi', (done) => { | ||
.on('a.*.c', function multi(args) { | ||
done(); | ||
done(); | ||
}); | ||
@@ -80,0 +109,0 @@ multiEventEmitter.emit('a.b.c'); |
@@ -0,0 +0,0 @@ import Eventy from './src/multi-event' |
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
24225
473
83