laravel-echo
Advanced tools
Comparing version 0.0.16 to 0.0.17
541
dist/echo.js
@@ -1,216 +0,141 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/** | ||
* Default event namespace. | ||
*/ | ||
var defaultNamespace = { | ||
value: 'App.Events' | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var EchoEventFormatter = function () { | ||
function EchoEventFormatter() { | ||
_classCallCheck(this, EchoEventFormatter); | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
_createClass(EchoEventFormatter, null, [{ | ||
key: 'format', | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
/** | ||
* Format the given event name. | ||
*/ | ||
value: function format(event) { | ||
if (event.charAt(0) != '\\') { | ||
event = defaultNamespace.value + '.' + event; | ||
} | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
return event.replace(/\./g, '\\'); | ||
} | ||
}]); | ||
return EchoEventFormatter; | ||
}(); | ||
var PusherConnector = function () { | ||
function PusherConnector() { | ||
_classCallCheck(this, PusherConnector); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
_createClass(PusherConnector, null, [{ | ||
key: 'connect', | ||
return target; | ||
}; | ||
/** | ||
* Create a fresh Pusher connection. | ||
*/ | ||
value: function connect(pusherKey) { | ||
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
var csrfToken = _ref.csrfToken; | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var customOptions = _objectWithoutProperties(_ref, ['csrfToken']); | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
var csrfToken = csrfToken || PusherConnector.csrfToken(); | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
var pusher = new Pusher(pusherKey, PusherConnector.options(csrfToken, customOptions)); | ||
var EventFormatter = function () { | ||
function EventFormatter() { | ||
classCallCheck(this, EventFormatter); | ||
pusher.connection.bind('connected', function () { | ||
var request = new XMLHttpRequest(); | ||
request.open('POST', '/broadcasting/socket', true); | ||
request.setRequestHeader('Content-Type', 'application/json'); | ||
request.setRequestHeader('X-CSRF-Token', csrfToken); | ||
request.send(JSON.stringify({ | ||
"socket_id": pusher.connection.socket_id | ||
})); | ||
}); | ||
this.defaultNamespace = 'App.Events'; | ||
} | ||
return pusher; | ||
createClass(EventFormatter, [{ | ||
key: 'format', | ||
value: function format(event) { | ||
if (event.charAt(0) != '\\' && event.charAt(0) != '.') { | ||
event = this.defaultNamespace + '.' + event; | ||
} else { | ||
event = event.substr(1); | ||
} | ||
return event.replace(/\./g, '\\'); | ||
} | ||
/** | ||
* Merge the custom Pusher options with the defaults. | ||
*/ | ||
}, { | ||
key: 'options', | ||
value: function options(csrfToken, customOptions) { | ||
var options = { | ||
authEndpoint: '/broadcasting/auth', | ||
auth: { | ||
headers: { 'X-CSRF-Token': csrfToken } | ||
} | ||
}; | ||
return _extends(options, customOptions); | ||
key: 'namespace', | ||
value: function namespace(value) { | ||
this.defaultNamespace = value; | ||
} | ||
/** | ||
* Extract the CSRF token from the page. | ||
*/ | ||
}, { | ||
key: 'csrfToken', | ||
value: function csrfToken() { | ||
var selector = document.querySelector('meta[name="csrf-token"]'); | ||
if (!selector) { | ||
console.error('Unable to locate CSRF token on page.'); | ||
} else { | ||
return selector.getAttribute('content'); | ||
} | ||
} | ||
}]); | ||
return PusherConnector; | ||
return EventFormatter; | ||
}(); | ||
/** | ||
* This class represents a basic Pusher channel. | ||
*/ | ||
var Channel = function () { | ||
function Channel(channel, connector) { | ||
classCallCheck(this, Channel); | ||
var EchoChannel = function () { | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
function EchoChannel(channel) { | ||
_classCallCheck(this, EchoChannel); | ||
this.channel = channel; | ||
this.connector = connector; | ||
this.eventFormatter = new EventFormatter(); | ||
if (this.connector.options.namespace) { | ||
this.eventFormatter.namespace(this.connector.options.namespace); | ||
} | ||
} | ||
/** | ||
* Listen for an event on the channel instance. | ||
*/ | ||
_createClass(EchoChannel, [{ | ||
createClass(Channel, [{ | ||
key: 'bind', | ||
value: function bind(event, callback) { | ||
var func = typeof this.channel.bind === 'function' ? 'bind' : 'on'; | ||
this.channel[func](event, callback); | ||
} | ||
}, { | ||
key: 'listen', | ||
value: function listen(event, callback) { | ||
this.channel.bind(EchoEventFormatter.format(event), callback); | ||
this.bind(this.eventFormatter.format(event), callback); | ||
return this; | ||
} | ||
}]); | ||
return EchoChannel; | ||
return Channel; | ||
}(); | ||
/** | ||
* This class represents a Pusher presence channel. | ||
*/ | ||
var PresenceChannel = function (_Channel) { | ||
inherits(PresenceChannel, _Channel); | ||
var EchoPresenceChannel = function () { | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
function EchoPresenceChannel(channel) { | ||
_classCallCheck(this, EchoPresenceChannel); | ||
this.channel = channel; | ||
function PresenceChannel() { | ||
classCallCheck(this, PresenceChannel); | ||
return possibleConstructorReturn(this, Object.getPrototypeOf(PresenceChannel).apply(this, arguments)); | ||
} | ||
/** | ||
* Register a callback to be called anytime the member list changes. | ||
*/ | ||
_createClass(EchoPresenceChannel, [{ | ||
createClass(PresenceChannel, [{ | ||
key: 'here', | ||
value: function here(callback) { | ||
var _this = this; | ||
this.then(callback); | ||
var addedOrRemovedCallback = function addedOrRemovedCallback(member) { | ||
var members = Object.keys(_this.channel.members.members).map(function (k) { | ||
return _this.channel.members.members[k]; | ||
}); | ||
callback(members, _this.channel); | ||
}; | ||
this.channel.bind('pusher:member_added', addedOrRemovedCallback); | ||
this.channel.bind('pusher:member_removed', addedOrRemovedCallback); | ||
return this; | ||
} | ||
/** | ||
* Register a callback to be called on successfully joining the channel. | ||
*/ | ||
}, { | ||
key: 'then', | ||
value: function then(callback) { | ||
var _this2 = this; | ||
this.channel.bind('pusher:subscription_succeeded', function (message) { | ||
var members = Object.keys(message.members).map(function (k) { | ||
return message.members[k]; | ||
}); | ||
this.bind(this.connector.updating, function (data) { | ||
var members = data; | ||
if (data.members) { | ||
members = Object.keys(data.members).map(function (k) { | ||
return data.members[k]; | ||
}); | ||
} | ||
callback(members, _this2.channel); | ||
}); | ||
return this; | ||
} | ||
/** | ||
* Listen for someone joining the channel. | ||
*/ | ||
}, { | ||
@@ -221,17 +146,8 @@ key: 'joining', | ||
this.channel.bind('pusher:member_added', function (member) { | ||
var members = Object.keys(_this3.channel.members.members).map(function (k) { | ||
return _this3.channel.members.members[k]; | ||
}); | ||
callback(member.info, members, _this3.channel); | ||
this.bind(this.connector.adding, function (member) { | ||
member = member.info || member; | ||
callback(member, _this3.channel); | ||
}); | ||
return this; | ||
} | ||
/** | ||
* Listen for someone leaving the channel. | ||
*/ | ||
}, { | ||
@@ -242,59 +158,178 @@ key: 'leaving', | ||
this.channel.bind('pusher:member_removed', function (member) { | ||
var members = Object.keys(_this4.channel.members.members).map(function (k) { | ||
return _this4.channel.members.members[k]; | ||
}); | ||
callback(member.info, members, _this4.channel); | ||
this.bind(this.connector.removing, function (member) { | ||
member = member.info || member; | ||
callback(member, _this4.channel); | ||
}); | ||
return this; | ||
} | ||
}]); | ||
return PresenceChannel; | ||
}(Channel); | ||
/** | ||
* Listen for an event on the channel instance. | ||
*/ | ||
var Connector = function () { | ||
function Connector(options) { | ||
classCallCheck(this, Connector); | ||
this._options = { | ||
auth: { | ||
headers: {} | ||
}, | ||
authEndpoint: '/broadcasting/auth', | ||
connector: 'pusher', | ||
csrfToken: null, | ||
host: 'http://localhost', | ||
pusherKey: null, | ||
namespace: null | ||
}; | ||
this.adding = 'member:added'; | ||
this.removing = 'member:removed'; | ||
this.updating = 'members:updated'; | ||
this.setOptions(options); | ||
this.connect(); | ||
} | ||
createClass(Connector, [{ | ||
key: 'setOptions', | ||
value: function setOptions(options) { | ||
this.options = _extends(this._options, options); | ||
if (this.csrfToken()) { | ||
this.options.auth.headers['X-CSRF-TOKEN'] = this.csrfToken(); | ||
} | ||
return options; | ||
} | ||
}, { | ||
key: 'listen', | ||
value: function listen(event, callback) { | ||
var _this5 = this; | ||
key: 'csrfToken', | ||
value: function csrfToken() { | ||
var selector = document.querySelector('meta[name="csrf-token"]'); | ||
if (this.options.csrfToken) { | ||
return this.options.csrfToken; | ||
} else if (selector) { | ||
return selector.getAttribute('content'); | ||
} | ||
return null; | ||
} | ||
}]); | ||
return Connector; | ||
}(); | ||
this.channel.bind(EchoEventFormatter.format(event), function (data) { | ||
callback(data, _this5.channel); | ||
}); | ||
var PusherConnector = function (_Connector) { | ||
inherits(PusherConnector, _Connector); | ||
return this; | ||
function PusherConnector() { | ||
var _Object$getPrototypeO; | ||
classCallCheck(this, PusherConnector); | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
}]); | ||
return EchoPresenceChannel; | ||
}(); | ||
var _this = possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(PusherConnector)).call.apply(_Object$getPrototypeO, [this].concat(args))); | ||
/** | ||
* This class is the primary API for interacting with Pusher. | ||
*/ | ||
_this.updating = 'pusher:subscription_succeeded'; | ||
_this.adding = 'pusher:member_added'; | ||
_this.removing = 'pusher:member_removed'; | ||
return _this; | ||
} | ||
createClass(PusherConnector, [{ | ||
key: 'connect', | ||
value: function connect() { | ||
var _this2 = this; | ||
var Echo = function () { | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
var pusher = new Pusher(this.options.pusher_key, this.options); | ||
pusher.connection.bind('connected', function () { | ||
var request = new XMLHttpRequest(); | ||
request.open('POST', '/broadcasting/socket', true); | ||
request.setRequestHeader('Content-Type', 'application/json'); | ||
request.setRequestHeader('X-CSRF-Token', _this2.csrfToken()); | ||
request.send(JSON.stringify({ | ||
"socket_id": pusher.connection.socket_id | ||
})); | ||
}); | ||
this.pusher = pusher; | ||
} | ||
}, { | ||
key: 'subscribe', | ||
value: function subscribe(channel) { | ||
return this.pusher.subscribe(channel); | ||
} | ||
}, { | ||
key: 'unsubscribe', | ||
value: function unsubscribe(channel) { | ||
this.pusher.unsubscribe(channel); | ||
} | ||
}, { | ||
key: 'socketId', | ||
value: function socketId() { | ||
return this.pusher.connection.socket_id; | ||
} | ||
}]); | ||
return PusherConnector; | ||
}(Connector); | ||
function Echo(pusherKey) { | ||
var customOptions = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var SocketIoConnector = function (_Connector) { | ||
inherits(SocketIoConnector, _Connector); | ||
_classCallCheck(this, Echo); | ||
function SocketIoConnector() { | ||
var _Object$getPrototypeO; | ||
this.channels = []; | ||
classCallCheck(this, SocketIoConnector); | ||
this.pusher = PusherConnector.connect(pusherKey, customOptions); | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var _this = possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(SocketIoConnector)).call.apply(_Object$getPrototypeO, [this].concat(args))); | ||
_this.adding = 'member:added'; | ||
_this.removing = 'member:removed'; | ||
_this.updating = 'members:updated'; | ||
return _this; | ||
} | ||
/** | ||
* Listen for an event on a channel instance. | ||
*/ | ||
createClass(SocketIoConnector, [{ | ||
key: 'connect', | ||
value: function connect() { | ||
this.socket = io(this.options.host); | ||
return this.socket; | ||
} | ||
}, { | ||
key: 'subscribe', | ||
value: function subscribe(channel) { | ||
return this.socket.emit('subscribe', { | ||
channel: channel, | ||
auth: this.options.auth || {} | ||
}); | ||
} | ||
}, { | ||
key: 'unsubscribe', | ||
value: function unsubscribe(channel) { | ||
this.socket.emit('unsubscribe', { | ||
channel: channel, | ||
auth: this.options.auth || {} | ||
}); | ||
} | ||
}, { | ||
key: 'socketId', | ||
value: function socketId() { | ||
return this.socket.id; | ||
} | ||
}]); | ||
return SocketIoConnector; | ||
}(Connector); | ||
var Echo = function () { | ||
function Echo(options) { | ||
classCallCheck(this, Echo); | ||
_createClass(Echo, [{ | ||
this.channels = []; | ||
if (options.connector == 'pusher') { | ||
this.connector = new PusherConnector(options); | ||
} else if (options.connector == 'socket.io') { | ||
this.connector = new SocketIoConnector(options); | ||
} | ||
} | ||
createClass(Echo, [{ | ||
key: 'listen', | ||
@@ -304,17 +339,7 @@ value: function listen(channel, event, callback) { | ||
} | ||
/** | ||
* Get a channel instance by name. | ||
*/ | ||
}, { | ||
key: 'channel', | ||
value: function channel(_channel) { | ||
return new EchoChannel(this.createChannel(_channel)); | ||
return new Channel(this.createChannel(_channel), this.connector); | ||
} | ||
/** | ||
* Get a private channel instance by name. | ||
*/ | ||
}, { | ||
@@ -325,17 +350,7 @@ key: 'private', | ||
} | ||
/** | ||
* Get a presence channel instance by name. | ||
*/ | ||
}, { | ||
key: 'join', | ||
value: function join(channel) { | ||
return new EchoPresenceChannel(this.createChannel('presence-' + channel)); | ||
return new PresenceChannel(this.createChannel('presence-' + channel), this.connector); | ||
} | ||
/** | ||
* Create an subscribe to a fresh channel instance. | ||
*/ | ||
}, { | ||
@@ -345,52 +360,30 @@ key: 'createChannel', | ||
if (!this.channels[channel]) { | ||
this.channels[channel] = this.pusher.subscribe(channel); | ||
this.channels[channel] = this.connector.subscribe(channel); | ||
} | ||
return this.channels[channel]; | ||
} | ||
/** | ||
* Leave the given channel. | ||
*/ | ||
}, { | ||
key: 'leave', | ||
value: function leave(channel) { | ||
var _this6 = this; | ||
var _this = this; | ||
var channels = [channel, 'private-' + channel, 'presence-' + channel]; | ||
channels.forEach(function (channelName) { | ||
if (_this6.channels[channelName]) { | ||
_this6.pusher.unsubscribe(channelName); | ||
_this6.channels.splice(channelName, 1); | ||
channels.forEach(function (channelName, index) { | ||
if (_this.channels[channelName]) { | ||
_this.connector.unsubscribe(channelName); | ||
delete _this.channels[channelName]; | ||
} | ||
}); | ||
} | ||
/** | ||
* Get the Socket ID for the connection. | ||
*/ | ||
}, { | ||
key: 'socketId', | ||
value: function socketId() { | ||
return this.pusher.connection.socket_id; | ||
return this.connector.socketId(); | ||
} | ||
/** | ||
* Set the default event namespace. | ||
*/ | ||
}, { | ||
key: 'namespace', | ||
value: function namespace(value) { | ||
defaultNamespace.value = value; | ||
} | ||
}]); | ||
return Echo; | ||
}(); | ||
exports.default = Echo; | ||
module.exports = Echo; | ||
export default Echo; |
{ | ||
"name": "laravel-echo", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Laravel Echo library for beautiful Pusher integration", | ||
"main": "dist/echo.js", | ||
"scripts": { | ||
"compile": "babel -d dist/ src/", | ||
"compile": "./node_modules/.bin/rollup -c", | ||
"prepublish": "npm run compile" | ||
@@ -16,3 +16,4 @@ }, | ||
"laravel", | ||
"pusher" | ||
"pusher", | ||
"socket.io" | ||
], | ||
@@ -25,7 +26,10 @@ "author": { | ||
"devDependencies": { | ||
"babel-cli": "^6.7.7", | ||
"babel-plugin-transform-object-assign": "^6.8.0", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-stage-2": "^6.5.0" | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"babel-preset-stage-2": "^6.5.0", | ||
"rollup": "^0.31.0", | ||
"rollup-plugin-babel": "^2.4.0", | ||
"rollup-plugin-typescript": "^0.7.5", | ||
"typings": "^1.0.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
117500
13
2632
7
1