Skip to content

Commit

Permalink
5.2.0 (#64)
Browse files Browse the repository at this point in the history
Updated middleware signature to add an additional parameter to the handler that includes the calling message object
Changed build targets
Tweaks to documentation
  • Loading branch information
KrisSiegel authored May 19, 2017
1 parent e2a43e1 commit 051cf40
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 101 deletions.
17 changes: 4 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
language: node_js
sudo: false
node_js:
- "7.2"
- "6.9"
- "6.7"
- "6.4"
- "6.3.1"
- "6.2"
- "6.1"
- "7.8"
- "7.0"
- "6.10"
- "6.0"
- "5.12"
- "5.7"
- "5.1"
- "5.0"
- "4.6"
- "4.5"
- "4.2"
- "4.1"
- "4.8"
- "4.0"
- "0.12"
- "0.11"
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Changelog
This is a roll-up of all release notes in order of release

## [Release 5.2.0 - May 18, 2016](https://github.com/KrisSiegel/msngr.js/releases/tag/5.2.0)
Minor, non-breaking API additions

***What's new?***
- Updated middleware signature to add an additional parameter to the handler that includes the calling message object

***Misc changes***
- Changed build targets
- Tweaks to documentation

## [Release 5.1.0 - December 11, 2016](https://github.com/KrisSiegel/msngr.js/releases/tag/5.1.0)
Version 5.1.0 brings some minor improvements and changes

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The example above demonstrates that the handler will only fire when the specifie
A mechanism for middleware exists that creates a global or localized affect. This allows for payload transformation to occur prior to any messages and their payloads being delivered to handlers. A middleware needs to be registered via ```msngr.middleware(key, fn, force)``` where the ```key``` is the name of the middleware, the ```fn``` is the middleware itself and ```force``` is a boolean that specifies whether the middleware should execute on *all* messages or not (if ```false``` then ```msngr().use(key)``` needs to be called on a message to have it applied).

```javascript
msngr.middleware("uppercase", function (payload) {
msngr.middleware("uppercase", function (payload, message) {
if (msngr.is(payload).string) {
return payload.toUpperCase();
}
Expand Down Expand Up @@ -199,6 +199,6 @@ msngr.series([
```

#### Contact
For questions, news, and whatever else that doesn't fit in GitHub issues you can follow me [@BinaryIdiot](https://twitter.com/BinaryIdiot)
For questions, news, and whatever else that doesn't fit in GitHub issues you can follow me [@KrisSiegel](https://twitter.com/KrisSiegel)

Copyright © 2014-2016 Kris Siegel
Copyright © 2014-2017 Kris Siegel
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "msngr.js",
"main": "msngr.min.js",
"description": "msngr.js is an asynchronous messaging library, written in JavaScript, for node and browser use",
"version": "5.1.0",
"version": "5.2.0",
"homepage": "https://github.com/KrisSiegel/msngr.js",
"authors": [
"Kris Siegel"
Expand Down
115 changes: 76 additions & 39 deletions msngr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var msngr = msngr || (function () {
};

// Built version of msngr.js for programatic access; this is auto generated
external.version = "5.1.0";
external.version = "5.2.0";

// Takes a function, executes it passing in the external and internal interfaces
external.extend = function (fn) {
Expand Down Expand Up @@ -706,7 +706,7 @@ msngr.extend((function (external, internal) {
}
}
params.push(asyncFunc);
var syncResult = method.apply(ctx || this, params);
var syncResult = method.apply(ctx || undefined, params);
if (asyncFlag !== true) {
done.apply(ctx, [syncResult]);
}
Expand Down Expand Up @@ -960,6 +960,7 @@ msngr.extend((function (external, internal) {
payloadIndex.clear();
payloads = {};
payloadCount = 0;
internal.resetMiddlewares();
};

/*
Expand All @@ -983,40 +984,42 @@ msngr.extend((function (external, internal) {
return payload;
};

var explicitEmit = function (msgOrIds, middlewares, payload, callback) {
var settleMiddleware = function (uses, payload, message, callback) {
internal.executeMiddlewares(uses, payload, message, function (newPayload) {
callback.apply(undefined, [newPayload]);
});
};

// Execute middlewares if any
internal.executeMiddlewares(middlewares, payload, function (newPayload) {
var ids = (external.is(msgOrIds).array) ? msgOrIds : messageIndex.query(msgOrIds);
var explicitEmit = function (msgOrIds, payload, callback) {
var ids = (external.is(msgOrIds).array) ? msgOrIds : messageIndex.query(msgOrIds);

if (ids.length > 0) {
var methods = [];
var toDrop = [];
for (var i = 0; i < ids.length; ++i) {
var msg = (external.is(msgOrIds).object) ? external.copy(msgOrIds) : external.copy(messageIndex.query(ids[i]));
var obj = handlers[ids[i]];
methods.push({
method: obj.handler,
params: [payload, msg]
});

if (ids.length > 0) {
var methods = [];
var toDrop = [];
for (var i = 0; i < ids.length; ++i) {
var msg = (external.is(msgOrIds).object) ? external.copy(msgOrIds) : external.copy(messageIndex.query(ids[i]));
var obj = handlers[ids[i]];
methods.push({
method: obj.handler,
params: [newPayload, msg]
if (obj.once === true) {
toDrop.push({
msg: msg,
handler: obj.handler
});

if (obj.once === true) {
toDrop.push({
msg: msg,
handler: obj.handler
});
}
}
}

var execs = internal.executer(methods);

for (var i = 0; i < toDrop.length; ++i) {
external(toDrop[i].msg).drop(toDrop[i].handler);
}
var execs = internal.executer(methods);

execs.parallel(callback);
for (var i = 0; i < toDrop.length; ++i) {
external(toDrop[i].msg).drop(toDrop[i].handler);
}
});

execs.parallel(callback);
}
};

/*
Expand Down Expand Up @@ -1066,7 +1069,8 @@ msngr.extend((function (external, internal) {
var msgObj = {
use: function (middleware) {
if (!external.is(middleware).empty) {
uses.push(middleware.toLowerCase());
var normalizedKey = middleware.toLowerCase();
uses.indexOf(normalizedKey) === -1 && uses.push(middleware.toLowerCase());
}

return msgObj;
Expand All @@ -1078,7 +1082,14 @@ msngr.extend((function (external, internal) {
payload = undefined;
}

explicitEmit(msg, uses, payload, callback);
if (uses.length > 0 || internal.getForcedMiddlewareCount() > 0) {
settleMiddleware(uses, payload, msg, function (newPayload) {
explicitEmit(msg, newPayload, callback);
});
} else {
explicitEmit(msg, payload, callback);
}


return msgObj;
},
Expand Down Expand Up @@ -1122,7 +1133,13 @@ msngr.extend((function (external, internal) {

var payload = fetchPersistedPayload(msg);
if (payload !== undefined) {
explicitEmit([id], uses, payload, undefined);
if (uses.length > 0 || internal.getForcedMiddlewareCount() > 0) {
settleMiddleware(uses, payload, msg, function (newPayload) {
explicitEmit([id], newPayload, undefined);
});
} else {
explicitEmit([id], payload, undefined);
}
}

return msgObj;
Expand All @@ -1138,7 +1155,13 @@ msngr.extend((function (external, internal) {

var payload = fetchPersistedPayload(msg);
if (payload !== undefined) {
explicitEmit([id], uses, payload, undefined);
if (uses.length > 0 || internal.getForcedMiddlewareCount() > 0) {
settleMiddleware(uses, payload, msg, function (newPayload) {
explicitEmit([id], newPayload, undefined);
});
} else {
explicitEmit([id], payload, undefined);
}
}

return msgObj;
Expand Down Expand Up @@ -1227,24 +1250,38 @@ msngr.extend((function (external, internal) {
/*
Internal APIs
*/
internal.getMiddlewares = function (uses, payload) {
internal.getForcedMiddlewareCount = function () {
return forced.length;
};

internal.resetMiddlewares = function () {
middlewares = { };
forced = [];
};

internal.getMiddlewares = function (uses, payload, message) {
var results = [];
var keys = (uses || []).concat(forced);
var keys = (uses || []);
for (var i = 0; i < forced.length; ++i) {
if (keys.indexOf(forced[i]) === -1) {
keys.push(forced[i]);
}
}

for (var i = 0; i < keys.length; ++i) {
if (middlewares[keys[i]] !== undefined) {
results.push({
method: middlewares[keys[i]],
params: payload
params: [payload, message]
});
}
}

return results;
};

internal.executeMiddlewares = function (uses, payload, callback) {
var middles = internal.getMiddlewares(uses, payload);

internal.executeMiddlewares = function (uses, payload, message, callback) {
var middles = internal.getMiddlewares(uses, payload, message);
var execute = internal.executer(middles).series(function (result) {
return callback(internal.merge.apply(this, [payload].concat(result)));
});
Expand Down
2 changes: 1 addition & 1 deletion msngr.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "msngr",
"main": "msngr.js",
"description": "An asynchronous messaging library, written in JavaScript, for node and browser use",
"version": "5.1.0",
"version": "5.2.0",
"keywords": [
"message",
"messaging",
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var msngr = msngr || (function () {
};

// Built version of msngr.js for programatic access; this is auto generated
external.version = "5.1.0";
external.version = "5.2.0";

// Takes a function, executes it passing in the external and internal interfaces
external.extend = function (fn) {
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/executer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ msngr.extend((function (external, internal) {
}
}
params.push(asyncFunc);
var syncResult = method.apply(ctx || this, params);
var syncResult = method.apply(ctx || undefined, params);
if (asyncFlag !== true) {
done.apply(ctx, [syncResult]);
}
Expand Down
43 changes: 43 additions & 0 deletions src/messaging/message.aspec.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,47 @@ describe("./src/messaging/message.js", function() {
}, 250);
});

it("msngr().on() / msngr().once() - correctly handles executing and dropping bound method", function (done) {
expect(msngr.internal.handlerCount).to.equal(0);
var funcCalls1 = 0;
var funcCalls2 = 0;
var funcCalls3 = 0;
var func1 = function () {
funcCalls1++;
}.bind(this);

var func2 = function () {
funcCalls2++;
}.bind(this);

var func3 = function () {
funcCalls3++;
}.bind(this);

msngr("WhatUp", "YesThisIsDog").on(func1);
msngr("WhatUp", "YesThisIsDog").on(func2);
msngr("WhatUp", "YesThisIsDog").once(func3);

expect(msngr.internal.handlerCount).to.equal(3);

msngr("WhatUp", "YesThisIsDog").emit("test", function () {
expect(funcCalls1).to.equal(1);
expect(funcCalls2).to.equal(1);
expect(funcCalls3).to.equal(1);
expect(msngr.internal.handlerCount).to.equal(2);

msngr("WhatUp", "YesThisIsDog").drop(func1);
expect(msngr.internal.handlerCount).to.equal(1);

msngr("WhatUp", "YesThisIsDog").emit("test", function () {
expect(funcCalls1).to.equal(1);
expect(funcCalls2).to.equal(2);
expect(funcCalls3).to.equal(1);
expect(msngr.internal.handlerCount).to.equal(1);

done();
});
});
});

});
Loading

0 comments on commit 051cf40

Please sign in to comment.