Skip to content

Commit 1aa1fd6

Browse files
committed
Merge pull request PatrickJS#23 from davej/patch-1
Use Angular's `bind` polyfill
2 parents a0ab9f0 + 918334f commit 1aa1fd6

File tree

1 file changed

+5
-27
lines changed

1 file changed

+5
-27
lines changed

src/angular-websocket.js

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,6 @@
2727
return -1;
2828
};
2929
}
30-
// ie8 wat
31-
if (!Function.prototype.bind) {
32-
Function.prototype.bind = function(oThis) {
33-
if (typeof this !== 'function') {
34-
// closest thing possible to the ECMAScript 5
35-
// internal IsCallable function
36-
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
37-
}
38-
39-
var aArgs = arraySlice.call(arguments, 1),
40-
fToBind = this,
41-
FNOP = function() {},
42-
fBound = function() {
43-
return fToBind.apply(this instanceof FNOP && oThis ? this : oThis, aArgs.concat(arraySlice.call(arguments)));
44-
};
45-
46-
FNOP.prototype = this.prototype;
47-
fBound.prototype = new FNOP();
48-
49-
return fBound;
50-
};
51-
}
5230

5331
// $WebSocketProvider.$inject = ['$rootScope', '$q', '$timeout', '$websocketBackend'];
5432
function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) {
@@ -127,10 +105,10 @@
127105
$WebSocket.prototype._connect = function _connect(force) {
128106
if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) {
129107
this.socket = $websocketBackend.create(this.url, this.protocols);
130-
this.socket.onmessage = this._onMessageHandler.bind(this);
131-
this.socket.onopen = this._onOpenHandler.bind(this);
132-
this.socket.onerror = this._onErrorHandler.bind(this);
133-
this.socket.onclose = this._onCloseHandler.bind(this);
108+
this.socket.onmessage = angular.bind(this, this._onMessageHandler);
109+
this.socket.onopen = angular.bind(this, this._onOpenHandler);
110+
this.socket.onerror = angular.bind(this, this._onErrorHandler);
111+
this.socket.onclose = angular.bind(this, this._onCloseHandler);
134112
}
135113
};
136114

@@ -293,7 +271,7 @@
293271
$WebSocket.prototype.reconnect = function reconnect() {
294272
this.close();
295273

296-
$timeout(this._connect.bind(this), this._getBackoffDelay(++this._reconnectAttempts));
274+
$timeout(angular.bind(this, this._connect), this._getBackoffDelay(++this._reconnectAttempts));
297275

298276
return this;
299277
};

0 commit comments

Comments
 (0)