angular-phoenix
Advanced tools
Comparing version 0.2.5 to 0.3.0
{ | ||
"name": "angular-phoenix", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "MikaAK <mikakalathil@gmail.com>" |
'use strict'; | ||
angular.module('angular-phoenix', []).factory('PhoenixBase', ['$rootScope', function ($rootScope) { | ||
var phoenix = angular.copy(window.Phoenix); | ||
phoenix.Channel.prototype.on = (function () { | ||
var _oldOn = angular.copy(phoenix.Channel.prototype.on); | ||
return function on(scope, event, callback) { | ||
var _this = this; | ||
var newCallback; | ||
if (typeof scope === 'string') { | ||
callback = event; | ||
event = scope; | ||
scope = null; | ||
} | ||
newCallback = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
callback.apply(undefined, args); | ||
$rootScope.$apply(); | ||
}; | ||
_oldOn.call(this, event, newCallback); | ||
if (scope) scope.$on('$destroy', function () { | ||
return _this.off(event); | ||
}); | ||
}; | ||
})(); | ||
phoenix.Channel.prototype.receive = (function () { | ||
var _oldReceive = angular.copy(phoenix.Channel.prototype.receive); | ||
return function receive(status, callback) { | ||
if (typeof status === 'function') { | ||
callback = status; | ||
status = null; | ||
} | ||
if (!status) status = 'ok'; | ||
return _oldReceive.call(this, status, callback); | ||
}; | ||
})(); | ||
phoenix.Channel.prototype.push = (function () { | ||
var _oldPush = angular.copy(phoenix.Channel.prototype.push); | ||
return function push() { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
var res = _oldPush.apply(this, args); | ||
res.receive = (function () { | ||
var oldRecieve = angular.copy(res.receive); | ||
return function receive(status, callback) { | ||
if (typeof status === 'function') { | ||
callback = status; | ||
status = null; | ||
} | ||
if (!status) status = 'ok'; | ||
return oldRecieve.call(this, status, callback); | ||
}; | ||
})(); | ||
return res; | ||
}; | ||
})(); | ||
return phoenix; | ||
}]).provider('Phoenix', function () { | ||
angular.module('angular-phoenix', []).value('PhoenixBase', Phoenix).provider('Phoenix', [function () { | ||
var urlBase = '/ws', | ||
@@ -93,82 +14,69 @@ _autoJoinSocket = true; | ||
this.$get = ['$q', 'PhoenixBase', function ($q, PhoenixBase) { | ||
this.$get = ['$q', '$rootScope', 'PhoenixBase', function ($q, $rootScope, PhoenixBase) { | ||
var socket = new PhoenixBase.Socket(urlBase), | ||
channels = new Map(), | ||
joinChannel = function joinChannel(name, message) { | ||
var joinRes, | ||
promise, | ||
channel = channels.get(name); | ||
runOnDestroy = function runOnDestroy(scope, cb) { | ||
return scope.$on('$destroy', cb); | ||
}; | ||
joinRes = function (resolve, reject) { | ||
channel = socket.join(name, message); | ||
PhoenixBase.Channel.prototype.on = (function () { | ||
var _oldOn = angular.copy(PhoenixBase.Channel.prototype.on); | ||
channels.set(name, { status: 'fetching', channel: channel }); | ||
return function on(scope, event, callback) { | ||
var _this = this; | ||
channel.after(5000, reject).receive(function (chan) { | ||
return resolve(chan); | ||
}); | ||
}; | ||
var newCallback; | ||
promise = new $q(joinRes); | ||
if (typeof scope === 'string') { | ||
callback = event; | ||
event = scope; | ||
scope = null; | ||
} | ||
promise.then(function () { | ||
channels.set(name, { status: 'connected', channel: channel, promise: promise }); | ||
}, function () { | ||
return console.warn('connection timed out...'); | ||
}); | ||
newCallback = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return angular.extend(channel, { promise: promise }); | ||
}; | ||
callback.apply(undefined, args); | ||
$rootScope.$apply(); | ||
}; | ||
if (_autoJoinSocket) socket.connect(); | ||
_oldOn.call(this, event, newCallback); | ||
PhoenixBase.Channel.prototype.leave = (function () { | ||
var _oldLeave = angular.copy(PhoenixBase.Channel.prototype.leave); | ||
return function leave() { | ||
channels.set(this.topic, { status: 'disconnected' }); | ||
return _oldLeave.call(this); | ||
if (scope) scope.$on('$destroy', function () { | ||
return _this.off(event); | ||
}); | ||
}; | ||
})(); | ||
return { | ||
base: PhoenixBase, | ||
socket: socket, | ||
leave: function leave(chan) { | ||
var channel = channels.get(chan.topic); | ||
if (!channel || channel.status === 'disconnected') { | ||
return; | ||
}channel.leave(); | ||
}, | ||
PhoenixBase.Channel.prototype.join = (function () { | ||
var _oldJoin = angular.copy(PhoenixBase.Channel.prototype.join); | ||
join: function join(scope, name) { | ||
var message = arguments[2] === undefined ? {} : arguments[2]; | ||
return function join(scope) { | ||
var _this2 = this; | ||
if (typeof scope === 'string') { | ||
message = name; | ||
name = scope; | ||
scope = null; | ||
} | ||
var res = _oldJoin.call(this); | ||
var resChannel, | ||
channel = channels.get(name), | ||
status = channel && channel.status; | ||
if (scope) runOnDestroy(scope, function () { | ||
_this2.leave(); | ||
}); | ||
if (channel) if (status === 'fetching') { | ||
return channel.channel; | ||
} else if (status === 'connected') if (Object.keys(message).length) socket.leave(name);else { | ||
return channel.channel; | ||
}resChannel = joinChannel(name, message); | ||
if (scope) resChannel.promise.then(function (chan) { | ||
scope.$on('$destroy', function () { | ||
return chan.leave(); | ||
this.promise = $q(function (resolve, reject) { | ||
res.after(5000, reject).receive('ok', function () { | ||
return resolve(_this2); | ||
}).receive('error', function () { | ||
return reject(_this2); | ||
}); | ||
}); | ||
return resChannel; | ||
} | ||
}; | ||
return res; | ||
}; | ||
})(); | ||
if (_autoJoinSocket) socket.connect(); | ||
socket.PhoenixBase = PhoenixBase; | ||
return socket; | ||
}]; | ||
}); | ||
}]); |
{ | ||
"name": "angular-phoenix", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"description": "Native bindings for phoenix in angular", | ||
@@ -5,0 +5,0 @@ "main": "dist/angular-phoenix.js", |
@@ -32,6 +32,8 @@ Angular Phoenix | ||
You can only join a channel once, if you provide a new message it will leave then rejoin the channel. | ||
Just like normal phoenix we call `Phoenix.join` however we also can take scope! | ||
Just like normal phoenix we call `chan.join` however we also can take scope! | ||
```javascript | ||
Phoenix.join('name', {}) | ||
var chan = Phoenix.chan('name', {}) | ||
chan.join() | ||
.receive(chann => { | ||
@@ -43,3 +45,3 @@ // Now our callbacks will get removed on scope destruction | ||
Phoenix.join('name', {}).promise | ||
chan.join().promise | ||
.then(chann => { | ||
@@ -71,3 +73,5 @@ // Now our callbacks will get removed on scope destruction | ||
// Alternatively with no resolve | ||
Phoenix.join('chatRoom') | ||
var chan = Phoenix.chan('chatRoom') | ||
chan.join() | ||
.after(5000, () => console.warn('it didn\'t work')) | ||
@@ -81,13 +85,4 @@ // This is the same as just passing in "ok" and a callback | ||
// the channel is left | ||
Phoenix.join(scope, 'chatRoom:lobby', user) | ||
chan.join(scope) | ||
} | ||
``` | ||
### Leaving a channel | ||
`Phoenix.leave('name')` | ||
### Accessing base phoenix or current socket | ||
```javascript | ||
Phoenix.base // Contains original Phoenix | ||
Phoenix.socket // Contains your current socket | ||
``` |
'use strict' | ||
angular.module('angular-phoenix', []) | ||
.factory('PhoenixBase', ['$rootScope', ($rootScope) => { | ||
var phoenix = angular.copy(window.Phoenix) | ||
phoenix.Channel.prototype.on = (() => { | ||
var _oldOn = angular.copy(phoenix.Channel.prototype.on) | ||
return function on(scope, event, callback) { | ||
var newCallback | ||
if (typeof scope === 'string') { | ||
callback = event | ||
event = scope | ||
scope = null | ||
} | ||
newCallback = (...args) => { | ||
callback(...args) | ||
$rootScope.$apply() | ||
} | ||
_oldOn.call(this, event, newCallback) | ||
if (scope) | ||
scope.$on('$destroy', () => this.off(event)) | ||
} | ||
})(); | ||
phoenix.Channel.prototype.receive = (() => { | ||
var _oldReceive = angular.copy(phoenix.Channel.prototype.receive) | ||
return function receive(status, callback) { | ||
if (typeof status === 'function') { | ||
callback = status | ||
status = null | ||
} | ||
if (!status) | ||
status = 'ok' | ||
return _oldReceive.call(this, status, callback) | ||
} | ||
})(); | ||
phoenix.Channel.prototype.push = (() => { | ||
var _oldPush = angular.copy(phoenix.Channel.prototype.push) | ||
return function push(...args) { | ||
var res = _oldPush.apply(this, args) | ||
res.receive = (() => { | ||
var oldRecieve = angular.copy(res.receive) | ||
return function receive(status, callback) { | ||
if (typeof status === 'function') { | ||
callback = status | ||
status = null | ||
} | ||
if (!status) | ||
status = 'ok' | ||
return oldRecieve.call(this, status, callback) | ||
} | ||
})(); | ||
return res | ||
} | ||
})(); | ||
return phoenix | ||
}]) | ||
.provider('Phoenix', function() { | ||
.value('PhoenixBase', Phoenix) | ||
.provider('Phoenix', [function() { | ||
var urlBase = '/ws', | ||
_autoJoinSocket = true | ||
this.setUrl = url => urlBase = url | ||
this.setAutoJoin = bool => _autoJoinSocket = bool | ||
this.setUrl = url => urlBase = url | ||
this.setAutoJoin = bool => _autoJoinSocket = bool | ||
this.$get = ['$q', 'PhoenixBase', ($q, PhoenixBase) => { | ||
var socket = new PhoenixBase.Socket(urlBase), | ||
channels = new Map(), | ||
joinChannel = (name, message) => { | ||
var joinRes, | ||
promise, | ||
channel = channels.get(name) | ||
this.$get = ['$q', '$rootScope', 'PhoenixBase', ($q, $rootScope, PhoenixBase) => { | ||
var socket = new PhoenixBase.Socket(urlBase), | ||
runOnDestroy = (scope, cb) => scope.$on('$destroy', cb) | ||
joinRes = (resolve, reject) => { | ||
channel = socket.join(name, message) | ||
PhoenixBase.Channel.prototype.on = (() => { | ||
var _oldOn = angular.copy(PhoenixBase.Channel.prototype.on) | ||
channels.set(name, {status: 'fetching', channel}) | ||
return function on(scope, event, callback) { | ||
var newCallback | ||
channel | ||
.after(5000, reject) | ||
.receive((chan) => resolve(chan)) | ||
} | ||
if (typeof scope === 'string') { | ||
callback = event | ||
event = scope | ||
scope = null | ||
} | ||
promise = new $q(joinRes) | ||
promise | ||
.then(() => { | ||
channels.set(name, {status: 'connected', channel, promise}) | ||
}, () => console.warn('connection timed out...')) | ||
return angular.extend(channel, {promise}) | ||
newCallback = (...args) => { | ||
callback(...args) | ||
$rootScope.$apply() | ||
} | ||
if (_autoJoinSocket) | ||
socket.connect() | ||
_oldOn.call(this, event, newCallback) | ||
PhoenixBase.Channel.prototype.leave = (() => { | ||
var _oldLeave = angular.copy(PhoenixBase.Channel.prototype.leave) | ||
return function leave() { | ||
channels.set(this.topic, {status: 'disconnected'}) | ||
return _oldLeave.call(this) | ||
if (scope) | ||
scope.$on('$destroy', () => this.off(event)) | ||
} | ||
})(); | ||
PhoenixBase.Channel.prototype.join = (() => { | ||
var _oldJoin = angular.copy(PhoenixBase.Channel.prototype.join) | ||
return { | ||
base: PhoenixBase, | ||
socket: socket, | ||
leave(chan) { | ||
var channel = channels.get(chan.topic) | ||
if (!channel || channel.status === 'disconnected') | ||
return | ||
return function join(scope) { | ||
var res = _oldJoin.call(this) | ||
channel.leave() | ||
}, | ||
if (scope) | ||
runOnDestroy(scope, () => { | ||
this.leave() | ||
}) | ||
join(scope, name, message = {}) { | ||
if (typeof scope === 'string') { | ||
message = name | ||
name = scope | ||
scope = null | ||
} | ||
this.promise = $q((resolve, reject) => { | ||
res | ||
.after(5000, reject) | ||
.receive('ok', () => resolve(this)) | ||
.receive('error', () => reject(this)) | ||
}) | ||
var resChannel, | ||
channel = channels.get(name), | ||
status = channel && channel.status | ||
return res | ||
} | ||
})(); | ||
if (channel) | ||
if (status === 'fetching') | ||
return channel.channel | ||
else if (status === 'connected') | ||
if (Object.keys(message).length) | ||
socket.leave(name) | ||
else | ||
return channel.channel | ||
if (_autoJoinSocket) | ||
socket.connect() | ||
resChannel = joinChannel(name, message) | ||
socket.PhoenixBase = PhoenixBase | ||
if (scope) | ||
resChannel.promise | ||
.then((chan) => { | ||
scope.$on('$destroy', () => chan.leave()) | ||
}) | ||
return resChannel | ||
} | ||
} | ||
return socket | ||
}] | ||
}) | ||
}]) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9197
138
85
1