Skip to content

Commit bc2ad28

Browse files
author
dev
committed
Enhanced to optionally avoid causing a digest. Add changelog and build (minify) script.
1 parent 9e98e9f commit bc2ad28

6 files changed

Lines changed: 119 additions & 98 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Bower Component for using AngularJS with [Socket.IO](http://socket.io/),
44
based on [this](http://briantford.com/blog/angular-socket-io.html).
55

6+
Forked and enhanced by ionCube Ltd. Released as 1.0.0.
7+
Renamed ioncube-angular-socket-io to resolve name clash with original.
68

79
## Install
810

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "angular-socket-io",
3-
"version": "0.7.0",
2+
"name": "ioncube-angular-socket-io",
3+
"version": "1.0.0",
44
"main": "socket.js",
55
"dependencies": {
66
"angular": "^1.2.6"

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "angular-socket-io",
3-
"version": "0.7.0",
2+
"name": "ioncube-angular-socket-io",
3+
"version": "1.0.0",
44
"main": "socket.js",
55
"directories": {
66
"test": "test"
77
},
88
"scripts": {
99
"test": "./node_modules/.bin/karma start --browsers Firefox --single-run"
1010
},
11-
"author": "Brian Ford",
11+
"author": "ionCube Ltd, based on v0.7.0 Brian Ford",
1212
"license": "MIT",
1313
"devDependencies": {
1414
"angular": "^1.3.5",

socket.js

100644100755
Lines changed: 102 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,115 @@
11
/*
22
* @license
3-
* angular-socket-io v0.7.0
3+
* ioncube-angular-socket-io v1.0.0
4+
* Forked from angular-socket-io v0.7.0
5+
*
6+
* Changes (c) ionCube Ltd.
7+
* https://www.ioncube.com https:/ioncube24.com
8+
* License: MIT
9+
*
10+
* Original copyright
411
* (c) 2014 Brian Ford http://briantford.com
512
* License: MIT
613
*/
714

815
angular.module('btford.socket-io', []).
9-
provider('socketFactory', function () {
16+
provider('socketFactory', function () {
1017

1118
'use strict';
1219

1320
// when forwarding events, prefix the event name
14-
var defaultPrefix = 'socket:',
15-
ioSocket;
21+
var defaultPrefix = 'socket:';
1622

1723
// expose to provider
18-
this.$get = ['$rootScope', '$timeout', function ($rootScope, $timeout) {
19-
20-
var asyncAngularify = function (socket, callback) {
21-
return callback ? function () {
22-
var args = arguments;
23-
$timeout(function () {
24-
callback.apply(socket, args);
25-
}, 0);
26-
} : angular.noop;
27-
};
28-
29-
return function socketFactory (options) {
30-
options = options || {};
31-
var socket = options.ioSocket || io.connect();
32-
var prefix = options.prefix === undefined ? defaultPrefix : options.prefix ;
33-
var defaultScope = options.scope || $rootScope;
34-
35-
var addListener = function (eventName, callback) {
36-
socket.on(eventName, callback.__ng = asyncAngularify(socket, callback));
37-
};
38-
39-
var addOnceListener = function (eventName, callback) {
40-
socket.once(eventName, callback.__ng = asyncAngularify(socket, callback));
41-
};
42-
43-
var wrappedSocket = {
44-
on: addListener,
45-
addListener: addListener,
46-
once: addOnceListener,
47-
48-
emit: function (eventName, data, callback) {
49-
var lastIndex = arguments.length - 1;
50-
var callback = arguments[lastIndex];
51-
if(typeof callback == 'function') {
52-
callback = asyncAngularify(socket, callback);
53-
arguments[lastIndex] = callback;
54-
}
55-
return socket.emit.apply(socket, arguments);
56-
},
57-
58-
removeListener: function (ev, fn) {
59-
if (fn && fn.__ng) {
60-
arguments[1] = fn.__ng;
61-
}
62-
return socket.removeListener.apply(socket, arguments);
63-
},
64-
65-
removeAllListeners: function() {
66-
return socket.removeAllListeners.apply(socket, arguments);
67-
},
68-
69-
disconnect: function (close) {
70-
return socket.disconnect(close);
71-
},
72-
73-
connect: function() {
74-
return socket.connect();
75-
},
76-
77-
// when socket.on('someEvent', fn (data) { ... }),
78-
// call scope.$broadcast('someEvent', data)
79-
forward: function (events, scope) {
80-
if (events instanceof Array === false) {
81-
events = [events];
82-
}
83-
if (!scope) {
84-
scope = defaultScope;
85-
}
86-
events.forEach(function (eventName) {
87-
var prefixedEvent = prefix + eventName;
88-
var forwardBroadcast = asyncAngularify(socket, function () {
89-
Array.prototype.unshift.call(arguments, prefixedEvent);
90-
scope.$broadcast.apply(scope, arguments);
91-
});
92-
scope.$on('$destroy', function () {
93-
socket.removeListener(eventName, forwardBroadcast);
94-
});
95-
socket.on(eventName, forwardBroadcast);
96-
});
97-
}
98-
};
99-
100-
return wrappedSocket;
101-
};
102-
}];
103-
});
24+
this.$get = ['$rootScope', '$timeout',
25+
function ($rootScope, $timeout) {
26+
27+
var asyncAngularify = function (socket, callback, doDigest) {
28+
return callback ? function () {
29+
var args = arguments;
30+
$timeout(function () {
31+
callback.apply(socket, args);
32+
}, 0, (doDigest !== false));
33+
} : angular.noop;
34+
};
35+
36+
return function socketFactory(options) {
37+
options = options || {};
38+
var socket = options.ioSocket || io.connect();
39+
var prefix = options.prefix === undefined ? defaultPrefix : options.prefix;
40+
var defaultScope = options.scope || $rootScope;
41+
42+
var addListener = function (eventName, callback, doDigest) {
43+
socket.on(eventName, callback.__ng = asyncAngularify(socket, callback, doDigest));
44+
};
45+
46+
var addOnceListener = function (eventName, callback, doDigest) {
47+
socket.once(eventName, callback.__ng = asyncAngularify(socket, callback, doDigest));
48+
};
49+
50+
var wrappedSocket = {
51+
'on': addListener,
52+
53+
'addListener': addListener,
54+
55+
'once': addOnceListener,
56+
57+
emit: function (eventName, data, callback, doDigest) {
58+
var lastIndex = arguments.length - 1;
59+
var callback = arguments[lastIndex];
60+
61+
if (typeof callback == 'function') {
62+
callback = asyncAngularify(socket, callback, doDigest);
63+
arguments[lastIndex] = callback;
64+
}
65+
66+
return socket.emit.apply(socket, arguments);
67+
},
68+
69+
removeListener: function (ev, fn) {
70+
if (fn && fn.__ng) {
71+
arguments[1] = fn.__ng;
72+
}
73+
return socket.removeListener.apply(socket, arguments);
74+
},
75+
76+
removeAllListeners: function () {
77+
return socket.removeAllListeners.apply(socket, arguments);
78+
},
79+
80+
disconnect: function (close) {
81+
return socket.disconnect(close);
82+
},
83+
84+
connect: function () {
85+
return socket.connect();
86+
},
87+
88+
// when socket.on('someEvent', fn (data) { ... }),
89+
// call scope.$broadcast('someEvent', data)
90+
forward: function (events, scope, doDigest) {
91+
if (events instanceof Array === false) {
92+
events = [events];
93+
}
94+
if (!scope) {
95+
scope = defaultScope;
96+
}
97+
events.forEach(function (eventName) {
98+
var prefixedEvent = prefix + eventName;
99+
var forwardBroadcast = asyncAngularify(socket, function () {
100+
Array.prototype.unshift.call(arguments, prefixedEvent);
101+
scope.$broadcast.apply(scope, arguments);
102+
}, doDigest);
103+
scope.$on('$destroy', function () {
104+
socket.removeListener(eventName, forwardBroadcast);
105+
});
106+
socket.on(eventName, forwardBroadcast);
107+
});
108+
}
109+
};
110+
111+
return wrappedSocket;
112+
};
113+
}
114+
];
115+
});

socket.min.js

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

socket.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)