laravel-echo
Advanced tools
Comparing version 0.0.1 to 0.0.2
323
dist/echo.js
@@ -6,177 +6,266 @@ 'use strict'; | ||
}); | ||
var PusherConnector = { | ||
/** | ||
* Create a fresh Pusher connection. | ||
*/ | ||
connect: function connect(pusherKey) { | ||
var customOptions = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
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; }; }(); | ||
var csrfToken = PusherConnector.csrfToken(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var pusher = new Pusher(pusherKey, PusherConnector.options(csrfToken, customOptions)); | ||
var PusherConnector = function () { | ||
function PusherConnector() { | ||
_classCallCheck(this, PusherConnector); | ||
} | ||
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: pusher.connection.socket_id | ||
})); | ||
}); | ||
_createClass(PusherConnector, null, [{ | ||
key: 'connect', | ||
return pusher; | ||
}, | ||
/** | ||
* Create a fresh Pusher connection. | ||
*/ | ||
value: function connect(pusherKey) { | ||
var customOptions = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var csrfToken = PusherConnector.csrfToken(); | ||
/** | ||
* Merge the custom Pusher options with the defaults. | ||
*/ | ||
options: function options(csrfToken, customOptions) { | ||
var options = { | ||
authEndpoint: '/broadcasting/auth', | ||
auth: { | ||
headers: { 'X-CSRF-Token': csrfToken } | ||
} | ||
}; | ||
var pusher = new Pusher(pusherKey, PusherConnector.options(csrfToken, customOptions)); | ||
for (var attrname in customOptions) { | ||
options[attrname] = customOptions[attrname]; | ||
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: pusher.connection.socket_id | ||
})); | ||
}); | ||
return pusher; | ||
} | ||
return options; | ||
}, | ||
/** | ||
* 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 } | ||
} | ||
}; | ||
/** | ||
* Extract the CSRF token from the page. | ||
*/ | ||
csrfToken: function csrfToken() { | ||
var selector = document.querySelector('meta[name="csrf-token"]'); | ||
for (var attrname in customOptions) { | ||
options[attrname] = customOptions[attrname]; | ||
} | ||
if (!selector) { | ||
console.error('Unable to locate CSRF token on page.'); | ||
} else { | ||
return selector.getAttribute('content'); | ||
return options; | ||
} | ||
} | ||
}; | ||
/** | ||
* 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; | ||
}(); | ||
/** | ||
* This class represents a basic Pusher channel. | ||
*/ | ||
function EchoChannel(channel) { | ||
var _this = this; | ||
this.channel = channel; | ||
var EchoChannel = function () { | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
function EchoChannel(channel) { | ||
_classCallCheck(this, EchoChannel); | ||
this.channel = channel; | ||
} | ||
/** | ||
* Listen for an event on the channel instance. | ||
*/ | ||
this.on = function (event, callback) { | ||
_this.channel.bind(event.replace(/\./g, '\\'), callback); | ||
return _this; | ||
}; | ||
} | ||
_createClass(EchoChannel, [{ | ||
key: 'on', | ||
value: function on(event, callback) { | ||
this.channel.bind(event.replace(/\./g, '\\'), callback); | ||
return this; | ||
} | ||
}]); | ||
return EchoChannel; | ||
}(); | ||
/** | ||
* This class represents a Pusher presence channel. | ||
*/ | ||
function EchoPresenceChannel(channel) { | ||
var _this2 = this; | ||
this.channel = channel; | ||
var EchoPresenceChannel = function () { | ||
/** | ||
* Register a callback to be called on successfully joining the channel. | ||
* Create a new class instance. | ||
*/ | ||
this.then = function (callback) { | ||
_this2.channel.bind('pusher:subscription_succeeded', function (members) { | ||
callback(members, _this2.channel); | ||
}); | ||
return _this2; | ||
}; | ||
function EchoPresenceChannel(channel) { | ||
_classCallCheck(this, EchoPresenceChannel); | ||
this.channel = channel; | ||
} | ||
/** | ||
* Listen for someone joining the channel. | ||
* Register a callback to be called on successfully joining the channel. | ||
*/ | ||
this.joining = function (callback) { | ||
_this2.channel.bind('pusher:member_added', function (member) { | ||
callback(member, _this2.channel.members, _this2.channel); | ||
}); | ||
return _this2; | ||
}; | ||
/** | ||
* Listen for someone leaving the channel. | ||
*/ | ||
this.leaving = function (callback) { | ||
_this2.channel.bind('pusher:member_removed', function (member) { | ||
callback(member, _this2.channel.members, _this2.channel); | ||
}); | ||
_createClass(EchoPresenceChannel, [{ | ||
key: 'then', | ||
value: function then(callback) { | ||
var _this = this; | ||
return _this2; | ||
}; | ||
this.channel.bind('pusher:subscription_succeeded', function (members) { | ||
callback(members, _this.channel); | ||
}); | ||
/** | ||
* Listen for an event on the channel instance. | ||
*/ | ||
this.on = function (event, callback) { | ||
_this2.channel.bind(event.replace(/\./g, '\\'), function (data) { | ||
callback(data, _this2.channel); | ||
}); | ||
return this; | ||
} | ||
return _this2; | ||
}; | ||
} | ||
/** | ||
* Listen for someone joining the channel. | ||
*/ | ||
}, { | ||
key: 'joining', | ||
value: function joining(callback) { | ||
var _this2 = this; | ||
this.channel.bind('pusher:member_added', function (member) { | ||
callback(member, _this2.channel.members, _this2.channel); | ||
}); | ||
return this; | ||
} | ||
/** | ||
* Listen for someone leaving the channel. | ||
*/ | ||
}, { | ||
key: 'leaving', | ||
value: function leaving(callback) { | ||
var _this3 = this; | ||
this.channel.bind('pusher:member_removed', function (member) { | ||
callback(member, _this3.channel.members, _this3.channel); | ||
}); | ||
return this; | ||
} | ||
/** | ||
* Listen for an event on the channel instance. | ||
*/ | ||
}, { | ||
key: 'on', | ||
value: function on(event, callback) { | ||
var _this4 = this; | ||
this.channel.bind(event.replace(/\./g, '\\'), function (data) { | ||
callback(data, _this4.channel); | ||
}); | ||
return this; | ||
} | ||
}]); | ||
return EchoPresenceChannel; | ||
}(); | ||
/** | ||
* This class is the primary API for interacting with Pusher. | ||
*/ | ||
function Echo(pusherKey) { | ||
var _this3 = this; | ||
var customOptions = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
this.channels = []; | ||
var Echo = function () { | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
this.pusher = PusherConnector.connect(pusherKey, customOptions); | ||
function Echo(pusherKey) { | ||
var customOptions = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
_classCallCheck(this, Echo); | ||
this.channels = []; | ||
this.pusher = PusherConnector.connect(pusherKey, customOptions); | ||
} | ||
/** | ||
* Listen for an event on a channel instance. | ||
*/ | ||
this.on = function (channel, event, callback) { | ||
return _this3.channel(channel).on(event, callback); | ||
}; | ||
/** | ||
* Get a channel instance by name. | ||
*/ | ||
this.channel = function (channel) { | ||
return new EchoChannel(_this3.createChannel(channel)); | ||
}; | ||
/** | ||
* Get a presence channel instance by name. | ||
*/ | ||
this.join = function (channel) { | ||
return new EchoPresenceChannel(_this3.createChannel(channel)); | ||
}; | ||
_createClass(Echo, [{ | ||
key: 'on', | ||
value: function on(channel, event, callback) { | ||
return this.channel(channel).on(event, callback); | ||
} | ||
/** | ||
* Create an subscribe to a fresh channel instance. | ||
*/ | ||
this.createChannel = function (channel) { | ||
if (!_this3.channels[channel]) { | ||
_this3.channels[channel] = _this3.pusher.subscribe(channel); | ||
/** | ||
* Get a channel instance by name. | ||
*/ | ||
}, { | ||
key: 'channel', | ||
value: function channel(_channel) { | ||
return new EchoChannel(this.createChannel(_channel)); | ||
} | ||
return _this3.channels[channel]; | ||
}; | ||
} | ||
/** | ||
* Get a presence channel instance by name. | ||
*/ | ||
}, { | ||
key: 'join', | ||
value: function join(channel) { | ||
return new EchoPresenceChannel(this.createChannel(channel)); | ||
} | ||
/** | ||
* Create an subscribe to a fresh channel instance. | ||
*/ | ||
}, { | ||
key: 'createChannel', | ||
value: function createChannel(channel) { | ||
if (!this.channels[channel]) { | ||
this.channels[channel] = this.pusher.subscribe(channel); | ||
} | ||
return this.channels[channel]; | ||
} | ||
}]); | ||
return Echo; | ||
}(); | ||
exports.default = Echo; |
{ | ||
"name": "laravel-echo", | ||
"version": "0.0.1", | ||
"description": "Laravel Echo library for beautiful Pusher integration", | ||
"main": "dist/echo.js", | ||
"scripts": { | ||
"compile": "babel --presets=es2015 -d dist/ src/", | ||
"prepublish": "npm run compile" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/laravel/echo" | ||
}, | ||
"keywords": [ | ||
"laravel", | ||
"pusher" | ||
], | ||
"author": { | ||
"name": "Taylor Otwell" | ||
}, | ||
"homepage": "https://github.com/laravel/echo", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"name": "laravel-echo", | ||
"version": "0.0.2", | ||
"description": "Laravel Echo library for beautiful Pusher integration", | ||
"main": "dist/echo.js", | ||
"scripts": { | ||
"compile": "babel --presets=es2015 -d dist/ src/", | ||
"prepublish": "npm run compile" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/laravel/echo" | ||
}, | ||
"keywords": [ | ||
"laravel", | ||
"pusher" | ||
], | ||
"author": { | ||
"name": "Taylor Otwell" | ||
}, | ||
"homepage": "https://github.com/laravel/echo", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel-cli": "^6.7.7", | ||
"babel-preset-es2015": "^6.6.0" | ||
} | ||
} | ||
} |
11652
342