Skip to content

Commit d412c54

Browse files
committed
Merge pull request #8 from marklawlor/master
Updated .receive to acknowledge any .emit events with a callback
2 parents 206f964 + 8baf3db commit d412c54

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ angular.module('app')
161161

162162
## Changelog
163163

164+
### 1.1.0
165+
166+
* .receive will acknowledge any .emit events with a callback as the last parameter
167+
164168
### 1.0.0
165169
* Upgraded to work with latest angular.
166170
* Use 0.1.0 with older versions of angularjs.

angular-socket.io-mock.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ng.provider('socketFactory',function(){
1515

1616
// intercept 'once' calls and treat them as 'on'
1717
obj.once = obj.on;
18-
18+
1919
// intercept 'emit' calls from the client and record them to assert against in the test
2020
obj.emit = function(eventName){
2121
var args = Array.prototype.slice.call(arguments,1);
@@ -36,6 +36,18 @@ ng.provider('socketFactory',function(){
3636
});
3737
});
3838
};
39+
40+
if (this.emits[eventName]) {
41+
angular.forEach(this.emits[eventName], function(emit){
42+
var lastIndex = emit.length -1;
43+
if('function' === typeof emit[lastIndex] && !emit[lastIndex].acknowledged){
44+
$rootScope.$apply(function() {
45+
emit[lastIndex].acknowledged = true;
46+
emit[lastIndex].apply(this, args);
47+
});
48+
};
49+
});
50+
};
3951
};
4052

4153
return obj;

angular-socket.io-mock.spec.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@
22
'use strict';
33

44
describe('Angular Socket.io Mock',function(){
5+
var socketFactory;
56
beforeEach(module('btford.socket-io'))
6-
it('should be able to listen on an event',inject(function(socketFactory){
7+
beforeEach(inject(function(_socketFactory_){ socketFactory = _socketFactory_ }));
8+
it('should be able to listen on an event', function(){
79
expect(new socketFactory().on('test-event',function(){})).not.toBe(false)
8-
}))
9-
it('should be able to listen once event',inject(function(socketFactory){
10-
expect(socketFactory.once('test-event',function(){})).not.toBe(false)
11-
}))
12-
it('should be able to emit an event',inject(function(socketFactory){
10+
});
11+
it('should be able to listen once event', function(){
12+
expect(new socketFactory().once('test-event',function(){})).not.toBe(false)
13+
});
14+
it('should be able to emit an event', function(){
1315
expect(new socketFactory().emit('test-event',{})).not.toBe(false)
14-
}))
15-
it('should be able to receive an event',inject(function(socketFactory){
16+
});
17+
it('should be able to receive an event', function(){
1618
expect(new socketFactory().receive('test-event',{})).not.toBe(false)
17-
}))
19+
});
20+
it('should be able to acknowledge an emited event only once',function(done){
21+
var socket = new socketFactory();
22+
var timesCalled = 0;
23+
24+
socket.emit('test-event',{}, function(resp){
25+
expect(resp).not.toBe(false);
26+
expect(++timesCalled).toEqual(1);
27+
});
28+
29+
socket.receive('test-event', {});
30+
socket.receive('test-event', {});
31+
32+
setTimeout(function() { // Wait to see if the event was acknowledged twice
33+
done();
34+
}, 100);
35+
});
1836
})

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-socket.io-mock",
3-
"version": "0.1.1",
3+
"version": "1.1.1",
44
"dependencies": {
55
"angular": "latest",
66
"angular-socket-io": "latest"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-socket.io-mock",
33
"description": "Drop in Mock replacement for angular-socket-io",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"authors": [
66
{
77
"name": "Bryan Tong",

0 commit comments

Comments
 (0)