Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Support * event type for mapping/handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone authored and fusion-bot[bot] committed Nov 14, 2017
1 parent 4b8ae44 commit 94e2f6d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ const eventsWithoutCtx = EventEmitter.of();
}
```

### * event type

`*` is a special event type which denotes all events. This allows you to add a mapper or handler to all events. For example:

```js
events.map('*', payload => {
//
});
```

---

### API
Expand Down
9 changes: 9 additions & 0 deletions docs/migrations/00009.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#### Changes made to event mappers/handlers on '*' event type

The following examples will run on every event type, where previously they
only ran on events with an explicit type '*'.

```js
events.on('*', (payload) => {});
events.map('*', (payload) => {});
```
88 changes: 85 additions & 3 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,92 @@ test('Base EventEmitter mappers', t => {
mapCount++;
return Object.assign(event, {a: true});
});
events.emit('test', {
b: true,
});
events.emit('test', {b: true});
t.equal(eventHandlerCount, 1, 'calls handler one time');
t.equal(mapCount, 1, 'calls mapper one time');
t.end();
});

test('Base EventEmitter * mappers', t => {
const events = new EventEmitter();
events.map('*', payload => {
return {...payload, a: true};
});
events.map('test', payload => {
return {...payload, b: true};
});
events.on('test', payload => {
t.deepLooseEqual(payload, {
a: true,
b: true,
c: true,
});
t.end();
});
events.emit('test', {c: true});
});

test('Base EventEmitter implicit * mappers', t => {
const events = new EventEmitter();
events.map(payload => {
return {...payload, a: true};
});
events.map('test', payload => {
return {...payload, b: true};
});
events.on('test', payload => {
t.deepLooseEqual(payload, {
a: true,
b: true,
c: true,
});
t.end();
});
events.emit('test', {c: true});
});

test('Base EventEmitter * handlers', t => {
const events = new EventEmitter();
let calledGlobal = false;
let calledNormal = false;
events.on(payload => {
t.deepLooseEqual(payload, {
c: true,
});
calledGlobal = true;
});

events.on('test', payload => {
t.deepLooseEqual(payload, {
c: true,
});
calledNormal = true;
});
events.emit('test', {c: true});
t.ok(calledGlobal);
t.ok(calledNormal);
t.end();
});

test('Base EventEmitter implicit * handlers', t => {
const events = new EventEmitter();
let calledGlobal = false;
let calledNormal = false;
events.on('*', payload => {
t.deepLooseEqual(payload, {
c: true,
});
calledGlobal = true;
});

events.on('test', payload => {
t.deepLooseEqual(payload, {
c: true,
});
calledNormal = true;
});
events.emit('test', {c: true});
t.ok(calledGlobal);
t.ok(calledNormal);
t.end();
});
13 changes: 8 additions & 5 deletions src/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

const globalEventType = '*';
export default class UniversalEmitter {
constructor() {
this.handlers = {};
this.mappers = {};
}
map(type, mapper) {
validateHandler(mapper);
map(...args) {
const {type, callback} = getArgs(args);
if (!this.mappers[type]) this.mappers[type] = [];
this.mappers[type].push(mapper);
this.mappers[type].push(callback);
}
on(...args) {
const {type, callback} = getArgs(args);
Expand All @@ -41,8 +42,10 @@ export default class UniversalEmitter {
if (index > -1) this.handlers[type].splice(index, 1);
}
emit(type, payload, ctx) {
const mappers = this.mappers[type] || [];
const handlers = this.handlers[type] || [];
const globalMappers = this.mappers[globalEventType] || [];
const globalHandlers = this.handlers[globalEventType] || [];
const mappers = (this.mappers[type] || []).concat(globalMappers);
const handlers = (this.handlers[type] || []).concat(globalHandlers);
const event = {
type,
payload: mappers.reduce((payload, mapper) => {
Expand Down

0 comments on commit 94e2f6d

Please sign in to comment.