Skip to content

Commit 9a033cd

Browse files
committed
chore: build 1.0.8 release
1 parent f1c5b64 commit 9a033cd

11 files changed

+95
-68
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 1.0.8 (2015-1-29)
2+
3+
Features:
4+
- Allow .asyncApply() if useApplyAsync is true in config
5+
- Pass event to .notifyOpenCallbacks() callbacks
6+
- Tests for .bindToScope and .safeDigest
7+
8+
Bugfixes:
9+
- Bind ._connect() correctly for .reconnect()
10+
- Correct rootScopeFailover of $rootScope with bindToScope
11+
112
## 1.0.7 (2015-1-20)
213

314
Features:

angular-websocket-mock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
function $MockWebSocket(url, protocols) {
14-
this.protocols = protocols || 'Sec-WebSocket-Protocol';
14+
this.protocols = protocols;
1515
this.ssl = /(wss)/i.test(this.url);
1616

1717
}

angular-websocket.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) {
5454

5555
function $WebSocket(url, protocols, options) {
56-
// var bits = url.split('/');
57-
5856
if (!options && isObject(protocols) && !isArray(protocols)) {
5957
options = protocols;
6058
protocols = undefined;
@@ -73,7 +71,7 @@
7371
// TODO: refactor options to use isDefined
7472
this.scope = options && options.scope || $rootScope;
7573
this.rootScopeFailover = options && options.rootScopeFailover && true;
76-
// this.useApplyAsync = options && options.useApplyAsync || false;
74+
this.useApplyAsync = options && options.useApplyAsync || false;
7775
this._reconnectAttempts = options && options.reconnectAttempts || 0;
7876
this.initialTimeout = options && options.initialTimeout || 500; // 500ms
7977
this.maxTimeout = options && options.maxTimeout || 5 * 60 * 1000; // 5 minutes
@@ -113,22 +111,23 @@
113111
};
114112

115113
$WebSocket.prototype.bindToScope = function bindToScope(scope) {
114+
var self = this;
116115
if (scope) {
117116
this.scope = scope;
118117
if (this.rootScopeFailover) {
119118
this.scope.$on('$destroy', function() {
120-
this.scope = $rootScope;
119+
self.scope = $rootScope;
121120
});
122121
}
123122
}
124-
return this;
123+
return self;
125124
};
126125

127126
$WebSocket.prototype._connect = function _connect(force) {
128127
if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) {
129128
this.socket = $websocketBackend.create(this.url, this.protocols);
130-
this.socket.onopen = this._onOpenHandler.bind(this);
131129
this.socket.onmessage = this._onMessageHandler.bind(this);
130+
this.socket.onopen = this._onOpenHandler.bind(this);
132131
this.socket.onerror = this._onErrorHandler.bind(this);
133132
this.socket.onclose = this._onCloseHandler.bind(this);
134133
}
@@ -145,9 +144,9 @@
145144
}
146145
};
147146

148-
$WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks() {
147+
$WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks(event) {
149148
for (var i = 0; i < this.onOpenCallbacks.length; i++) {
150-
this.onOpenCallbacks[i].call(this);
149+
this.onOpenCallbacks[i].call(this, event);
151150
}
152151
};
153152

@@ -196,9 +195,9 @@
196195
return this;
197196
};
198197

199-
$WebSocket.prototype._onOpenHandler = function _onOpenHandler() {
198+
$WebSocket.prototype._onOpenHandler = function _onOpenHandler(event) {
200199
this._reconnectAttempts = 0;
201-
this.notifyOpenCallbacks();
200+
this.notifyOpenCallbacks(event);
202201
this.fireQueue();
203202
};
204203

@@ -215,26 +214,36 @@
215214

216215
$WebSocket.prototype._onMessageHandler = function _onMessageHandler(message) {
217216
var pattern;
218-
var socketInstance = this;
217+
var self = this;
219218
var currentCallback;
220-
for (var i = 0; i < socketInstance.onMessageCallbacks.length; i++) {
221-
currentCallback = socketInstance.onMessageCallbacks[i];
219+
for (var i = 0; i < self.onMessageCallbacks.length; i++) {
220+
currentCallback = self.onMessageCallbacks[i];
222221
pattern = currentCallback.pattern;
223222
if (pattern) {
224223
if (isString(pattern) && message.data === pattern) {
225-
currentCallback.fn.call(socketInstance, message);
226-
socketInstance.safeDigest(currentCallback.autoApply);
224+
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
227225
}
228226
else if (pattern instanceof RegExp && pattern.exec(message.data)) {
229-
currentCallback.fn.call(socketInstance, message);
230-
socketInstance.safeDigest(currentCallback.autoApply);
227+
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
231228
}
232229
}
233230
else {
234-
currentCallback.fn.call(socketInstance, message);
235-
socketInstance.safeDigest(currentCallback.autoApply);
231+
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
232+
}
233+
}
234+
235+
function applyAsyncOrDigest(callback, autoApply, args) {
236+
args = arraySlice.call(arguments, 2);
237+
if (self.useApplyAsync) {
238+
self.scope.$applyAsync(function() {
239+
callback.apply(self, args);
240+
});
241+
} else {
242+
callback.apply(self, args);
243+
self.safeDigest(autoApply);
236244
}
237245
}
246+
238247
};
239248

240249
$WebSocket.prototype.close = function close(force) {
@@ -246,18 +255,18 @@
246255

247256
$WebSocket.prototype.send = function send(data) {
248257
var deferred = $q.defer();
249-
var socketInstance = this;
258+
var self = this;
250259
var promise = cancelableify(deferred.promise);
251260

252-
if (socketInstance.readyState === socketInstance._readyStateConstants.RECONNECT_ABORTED) {
261+
if (self.readyState === self._readyStateConstants.RECONNECT_ABORTED) {
253262
deferred.reject('Socket connection has been closed');
254263
}
255264
else {
256-
socketInstance.sendQueue.push({
265+
self.sendQueue.push({
257266
message: data,
258267
deferred: deferred
259268
});
260-
socketInstance.fireQueue();
269+
self.fireQueue();
261270
}
262271

263272
// Credit goes to @btford
@@ -272,21 +281,20 @@
272281
}
273282

274283
function cancel(reason) {
275-
socketInstance.sendQueue.splice(socketInstance.sendQueue.indexOf(data), 1);
284+
self.sendQueue.splice(self.sendQueue.indexOf(data), 1);
276285
deferred.reject(reason);
277-
return socketInstance;
286+
return self;
278287
}
279288

280289
return promise;
281290
};
282291

283292
$WebSocket.prototype.reconnect = function reconnect() {
284-
var socketInstance = this;
285-
socketInstance.close();
293+
this.close();
286294

287-
$timeout(socketInstance._connect, socketInstance._getBackoffDelay(++socketInstance._reconnectAttempts));
295+
$timeout(this._connect.bind(this), this._getBackoffDelay(++this._reconnectAttempts));
288296

289-
return socketInstance;
297+
return this;
290298
};
291299
// Exponential Backoff Formula by Prof. Douglas Thain
292300
// http://dthain.blogspot.co.uk/2009/02/exponential-backoff-in-distributed.html
@@ -334,7 +342,7 @@
334342
};
335343
}
336344

337-
// $WebSocketBackendProvider.$inject = ['$window'];
345+
// $WebSocketBackendProvider.$inject = ['$window', '$log'];
338346
function $WebSocketBackendProvider($window, $log) {
339347
this.create = function create(url, protocols) {
340348
var match = /wss?:\/\//.exec(url);

angular-websocket.min.js

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)