angular-phoenix
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "angular-phoenix", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "MikaAK <mikakalathil@gmail.com>" |
@@ -1,5 +0,6 @@ | ||
"use strict"; | ||
'use strict'; | ||
angular.module("angular-phoenix", []).value("PhoenixBase", window.Phoenix).provider("Phoenix", function () { | ||
var urlBase = "/ws"; | ||
angular.module('angular-phoenix', []).value('PhoenixBase', window.Phoenix).provider('Phoenix', function () { | ||
var urlBase = '/ws', | ||
_autoJoinSocket = true; | ||
@@ -9,67 +10,114 @@ this.setUrl = function (url) { | ||
}; | ||
this.setAutoJoin = function (bool) { | ||
return _autoJoinSocket = bool; | ||
}; | ||
this.$get = ["$rootScope", "$window", "PhoenixBase", function ($rootScope, $window, PhoenixBase) { | ||
var createSocketUrl = function createSocketUrl(url) { | ||
var loc = $window.location, | ||
socketProtocol = loc.protocol === "http:" ? "ws:" : "wss:", | ||
baseUrl = url.startsWith("/") ? url.substr(1, url.length) : url; | ||
this.$get = ['$rootScope', '$window', 'PhoenixBase', function ($rootScope, $window, PhoenixBase) { | ||
var socket = new PhoenixBase.Socket(urlBase), | ||
channels = new Map(), | ||
helpers = { | ||
_extendChannel: function _extendChannel(channel) { | ||
var phoenixReplace = { | ||
on: function on(scope, event, callback) { | ||
var _this = this; | ||
return "" + socketProtocol + "//" + loc.host + "" + loc.pathname + "" + baseUrl; | ||
}; | ||
if (typeof scope === 'string') { | ||
callback = event; | ||
event = scope; | ||
scope = null; | ||
} | ||
var socketUrl = urlBase.search("ws") === 0 ? urlBase : createSocketUrl(urlBase), | ||
socket = new PhoenixBase.Socket(urlBase), | ||
channels = new Map(); | ||
this.bindings.push({ event: event, callback: (function (_callback) { | ||
function callback(_x) { | ||
return _callback.apply(this, arguments); | ||
} | ||
return { | ||
base: PhoenixBase, | ||
socket: socket, | ||
leave: function leave(name) { | ||
if (!channels.get(name)) { | ||
return; | ||
}socket.leave(name); | ||
channels.set(name, false); | ||
callback.toString = function () { | ||
return _callback.toString(); | ||
}; | ||
return callback; | ||
})(function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
callback.apply(undefined, args); | ||
$rootScope.$apply(); | ||
}) }); | ||
if (scope) scope.$on('$destroy', function () { | ||
return _this.bindings.splice(callback, 1); | ||
}); | ||
} | ||
}; | ||
return angular.extend(channel, phoenixReplace); | ||
}, | ||
join: function join(scope, name) { | ||
var message = arguments[2] === undefined ? {} : arguments[2]; | ||
joinChannel: function joinChannel(name, message) { | ||
var joinRes, | ||
promise, | ||
channel = channels.get(name); | ||
if (typeof scope === "string") { | ||
message = angular.isDefined(name) || {}; | ||
name = scope; | ||
scope = null; | ||
} | ||
joinRes = function (resolve, reject) { | ||
channel = socket.join(name, message); | ||
var on = function on(event, callback) { | ||
var _this = this; | ||
channel.receive = (function () { | ||
var _oldReceive = angular.copy(channel.receive); | ||
this.bindings.push({ event: event, callback: callback }); | ||
return function receive(status, callback) { | ||
if (typeof status === 'function') { | ||
callback = status; | ||
status = null; | ||
} | ||
if (scope) scope.$on("$destroy", function () { | ||
return _this.bindings.splice(callback, 1); | ||
if (!status) status = 'ok'; | ||
_oldReceive.call(this, status, function (chan) { | ||
return callback(helpers._extendChannel(chan)); | ||
}); | ||
}; | ||
})(); | ||
channels.set(name, { status: 'fetching', channel: channel }); | ||
channel.after(5000, reject).receive(function (chan) { | ||
return resolve(chan); | ||
}); | ||
}; | ||
var resolve = function (resolve) { | ||
var channel; | ||
promise = new Promise(joinRes); | ||
if (channel = channels.get(name)) if (!message) return resolve(angular.extend(channel, { on: on }));else socket.leave(name); | ||
promise.then(function () { | ||
channels.set(name, { status: 'connected', channel: channel, promise: promise }); | ||
}); | ||
socket.join(name, message, function (channel) { | ||
channels.set(name, channel); | ||
return angular.extend(channel, { promise: promise }); | ||
} | ||
}; | ||
resolve(angular.extend(channel, { | ||
on: on, | ||
trigger: function trigger(e, t) { | ||
this.bindings.filter(function (t) { | ||
return t.event === e; | ||
}).map(function (e) { | ||
return $rootScope.$digest(e.callback(t)); | ||
}); | ||
} | ||
})); | ||
}); | ||
}; | ||
if (_autoJoinSocket) socket.connect(); | ||
return new Promise(resolve); | ||
return { | ||
base: PhoenixBase, | ||
socket: socket, | ||
leave: function leave(name) { | ||
if (!channels.get(name)) { | ||
return; | ||
}socket.leave(name); | ||
channels.set(name, { status: 'disconnected' }); | ||
}, | ||
join: function join(name) { | ||
var message = arguments[1] === undefined ? {} : arguments[1]; | ||
var channel = channels.get(name), | ||
status = channel && channel.status; | ||
if (channel) if (status === 'fetching') { | ||
return channel.channel; | ||
} else if (status === 'connected') if (Object.keys(message).length) socket.leave(name);else { | ||
return channel.channel; | ||
}return helpers.joinChannel(name, message); | ||
} | ||
@@ -76,0 +124,0 @@ }; |
{ | ||
"name": "angular-phoenix", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Native bindings for phoenix in angular", | ||
@@ -5,0 +5,0 @@ "main": "dist/angular-phoenix.js", |
@@ -36,12 +36,13 @@ Angular Phoenix | ||
```javascript | ||
// $scope, eventName, message | ||
Phoenix.join($scope, 'name', {}) | ||
.then(chann => { | ||
Phoenix.join('name', {}) | ||
.receive(chann => { | ||
// Now our callbacks will get removed on scope destruction | ||
chann.on('message', handler) | ||
chann.on(scope, 'message', handler) | ||
chann.on('message', hander) | ||
}) | ||
Phoenix.join('name', {}) | ||
Phoenix.join('name', {}).promise | ||
.then(chann => { | ||
// We didn't pass scope these callbacks will always run | ||
// Now our callbacks will get removed on scope destruction | ||
chann.on(scope, 'message', handler) | ||
chann.on('message', hander) | ||
@@ -51,2 +52,5 @@ }) | ||
### Why add a promise? | ||
For things like UI-Router this allows you to join into a channel as a resolve property!! | ||
### Leaving a channel | ||
@@ -53,0 +57,0 @@ `Phoenix.leave('name')` |
@@ -6,18 +6,78 @@ 'use strict' | ||
.provider('Phoenix', function() { | ||
var urlBase = '/ws' | ||
var urlBase = '/ws', | ||
_autoJoinSocket = true | ||
this.setUrl = url => urlBase = url | ||
this.setUrl = url => urlBase = url | ||
this.setAutoJoin = bool => _autoJoinSocket = bool | ||
this.$get = ['$rootScope', '$window', 'PhoenixBase', ($rootScope, $window, PhoenixBase) => { | ||
var createSocketUrl = function(url) { | ||
var loc = $window.location, | ||
socketProtocol = loc.protocol === 'http:' ? 'ws:' : 'wss:', | ||
baseUrl = url.startsWith('/') ? url.substr(1, url.length) : url | ||
var socket = new PhoenixBase.Socket(urlBase), | ||
channels = new Map(), | ||
helpers = { | ||
_extendChannel(channel) { | ||
var phoenixReplace = { | ||
on(scope, event, callback) { | ||
if (typeof scope === 'string') { | ||
callback = event | ||
event = scope | ||
scope = null | ||
} | ||
return `${socketProtocol}//${loc.host}${loc.pathname}${baseUrl}` | ||
this.bindings.push({event, callback: (...args) => { | ||
callback(...args) | ||
$rootScope.$apply() | ||
}}) | ||
if (scope) | ||
scope.$on('$destroy', () => this.bindings.splice(callback, 1)) | ||
} | ||
} | ||
return angular.extend(channel, phoenixReplace) | ||
}, | ||
joinChannel(name, message) { | ||
var joinRes, | ||
promise, | ||
channel = channels.get(name) | ||
joinRes = (resolve, reject) => { | ||
channel = socket.join(name, message) | ||
channel.receive = (() => { | ||
var _oldReceive = angular.copy(channel.receive) | ||
return function receive(status, callback) { | ||
if (typeof status === 'function') { | ||
callback = status | ||
status = null | ||
} | ||
if (!status) | ||
status = 'ok' | ||
_oldReceive.call(this, status, chan => callback(helpers._extendChannel(chan))) | ||
} | ||
})(); | ||
channels.set(name, {status: 'fetching', channel}) | ||
channel | ||
.after(5000, reject) | ||
.receive((chan) => resolve(chan)) | ||
} | ||
promise = new Promise(joinRes) | ||
promise | ||
.then(() => { | ||
channels.set(name, {status: 'connected', channel, promise}) | ||
}) | ||
return angular.extend(channel, {promise}) | ||
} | ||
} | ||
var socketUrl = urlBase.search('ws') === 0 ? urlBase : createSocketUrl(urlBase), | ||
socket = new PhoenixBase.Socket(urlBase), | ||
channels = new Map() | ||
if (_autoJoinSocket) | ||
socket.connect() | ||
@@ -32,45 +92,19 @@ return { | ||
socket.leave(name) | ||
channels.set(name, false) | ||
channels.set(name, {status: 'disconnected'}) | ||
}, | ||
join(scope, name, message = {}) { | ||
if (typeof scope === 'string') { | ||
message = angular.isDefined(name) || {} | ||
name = scope | ||
scope = null | ||
} | ||
join(name, message = {}) { | ||
var channel = channels.get(name), | ||
status = channel && channel.status | ||
var on = function(event, callback) { | ||
this.bindings.push({event, callback}) | ||
if (scope) | ||
scope.$on('$destroy', () => this.bindings.splice(callback, 1)) | ||
} | ||
var resolve = (resolve) => { | ||
var channel | ||
if (channel = channels.get(name)) | ||
if (!message) | ||
return resolve(angular.extend(channel, {on})) | ||
if (channel) | ||
if (status === 'fetching') | ||
return channel.channel | ||
else if (status === 'connected') | ||
if (Object.keys(message).length) | ||
socket.leave(name) | ||
else | ||
socket.leave(name) | ||
return channel.channel | ||
socket.join(name, message, channel => { | ||
channels.set(name, channel) | ||
resolve(angular.extend(channel, { | ||
on, | ||
trigger(e, t) { | ||
this.bindings | ||
.filter(function(t){return t.event===e}) | ||
.map(function(e){return $rootScope.$digest(e.callback(t))}) | ||
} | ||
})) | ||
}) | ||
} | ||
return new Promise(resolve) | ||
return helpers.joinChannel(name, message) | ||
} | ||
@@ -77,0 +111,0 @@ } |
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
11573
212
62
1