diff --git a/package.json b/package.json index d5046521..6e6bf7e5 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,13 @@ { "name": "laravel-echo", - "version": "1.4.0", + "version": "1.5.0", "description": "Laravel Echo library for beautiful Pusher and Socket.IO integration", "main": "dist/echo.js", "scripts": { + "build": "npm run compile && npm run declarations", "compile": "./node_modules/.bin/rollup -c", - "prepublish": "npm run compile", + "declarations": "./node_modules/.bin/tsc --emitDeclarationOnly", + "prepublish": "npm run build", "test": "jest" }, "repository": { @@ -32,10 +34,12 @@ "babel-preset-stage-2": "^6.5.0", "jest": "^22.1.0", "pusher-js": "^3.2.1", + "rollup": "^0.67.3", "rollup-plugin-babel": "^2.4.0", - "rollup-plugin-typescript": "^0.7.5", - "rollup": "^0.31.0", - "ts-jest": "^22.0.0" + "rollup-plugin-typescript": "^1.0.0", + "ts-jest": "^22.0.0", + "tslib": "^1.9.3", + "typescript": "^3.2.1" }, "jest": { "transform": { diff --git a/rollup.config.js b/rollup.config.js index 8de872df..3f7dcb87 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,8 +2,8 @@ import typescript from 'rollup-plugin-typescript'; import babel from 'rollup-plugin-babel'; export default { - entry: './src/echo.ts', - dest: './dist/echo.js', + input: './src/echo.ts', + output: [{ file: './dist/echo.js', format: 'esm' }], plugins: [ typescript(), babel({ diff --git a/src/channel/channel.ts b/src/channel/channel.ts index fae078c3..7af3144d 100644 --- a/src/channel/channel.ts +++ b/src/channel/channel.ts @@ -4,39 +4,30 @@ export abstract class Channel { /** * The Echo options. - * - * @type {any} */ options: any; /** * Listen for an event on the channel instance. - * - * @param {string} event - * @param {Function} callback - * @return {Channel} */ abstract listen(event: string, callback: Function): Channel; + /** + * Listen for a whisper event on the channel instance. + */ + listenForWhisper(event: string, callback: Function): Channel { + return this.listen('.client-' + event, callback); + } + /** * Listen for an event on the channel instance. - * - * @param {string} event - * @param {Function} callback - * @return {Channel} */ notification(callback: Function): Channel { return this.listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', callback); } /** - * Listen for a whisper event on the channel instance. - * - * @param {string} event - * @param {Function} callback - * @return {Channel} + * Stop listening to an event on the channel instance. */ - listenForWhisper(event: string, callback: Function): Channel { - return this.listen('.client-' + event, callback); - } + abstract stopListening(event: string): Channel; } diff --git a/src/channel/null-channel.ts b/src/channel/null-channel.ts index cd88867a..e79a1358 100644 --- a/src/channel/null-channel.ts +++ b/src/channel/null-channel.ts @@ -7,9 +7,6 @@ import { Channel } from './channel'; export class NullChannel extends Channel { /** * Subscribe to a channel. - * - * @param {string} channel - * @return {object} */ subscribe(): any { // @@ -17,8 +14,6 @@ export class NullChannel extends Channel { /** * Unsubscribe from a channel. - * - * @return {void} */ unsubscribe(): void { // @@ -26,10 +21,6 @@ export class NullChannel extends Channel { /** * Listen for an event on the channel instance. - * - * @param {string} event - * @param {Function} callback - * @return {NullChannel} */ listen(event: string, callback: Function): NullChannel { return this; @@ -37,9 +28,6 @@ export class NullChannel extends Channel { /** * Stop listening for an event on the channel instance. - * - * @param {string} event - * @return {NullChannel} */ stopListening(event: string): NullChannel { return this; @@ -47,10 +35,6 @@ export class NullChannel extends Channel { /** * Bind a channel to an event. - * - * @param {string} event - * @param {Function} callback - * @return {NullChannel} */ on(event: string, callback: Function): NullChannel { return this; diff --git a/src/channel/null-presence-channel.ts b/src/channel/null-presence-channel.ts index 095f01f4..cc4bea38 100644 --- a/src/channel/null-presence-channel.ts +++ b/src/channel/null-presence-channel.ts @@ -7,41 +7,29 @@ import { PresenceChannel } from './presence-channel'; export class NullPresenceChannel extends NullChannel implements PresenceChannel { /** * Register a callback to be called anytime the member list changes. - * - * @param {Function} callback - * @return {object} this */ - here(callback): NullPresenceChannel { + here(callback: Function): NullPresenceChannel { return this; } /** * Listen for someone joining the channel. - * - * @param {Function} callback - * @return {NullPresenceChannel} */ - joining(callback): NullPresenceChannel { + joining(callback: Function): NullPresenceChannel { return this; } /** * Listen for someone leaving the channel. - * - * @param {Function} callback - * @return {NullPresenceChannel} */ - leaving(callback): NullPresenceChannel { + leaving(callback: Function): NullPresenceChannel { return this; } /** * Trigger client event on the channel. - * - * @param {Function} callback - * @return {NullPresenceChannel} */ - whisper(eventName, data): NullPresenceChannel { + whisper(eventName: string, data: any): NullPresenceChannel { return this; } } diff --git a/src/channel/null-private-channel.ts b/src/channel/null-private-channel.ts index ac6b61c5..b3a78327 100644 --- a/src/channel/null-private-channel.ts +++ b/src/channel/null-private-channel.ts @@ -6,11 +6,8 @@ import { NullChannel } from './null-channel'; export class NullPrivateChannel extends NullChannel { /** * Trigger client event on the channel. - * - * @param {Function} callback - * @return {NullPrivateChannel} */ - whisper(eventName, data): NullPrivateChannel { + whisper(eventName: string, data: any): NullPrivateChannel { return this; } } diff --git a/src/channel/presence-channel.ts b/src/channel/presence-channel.ts index 86f12ec4..3989f34f 100644 --- a/src/channel/presence-channel.ts +++ b/src/channel/presence-channel.ts @@ -4,25 +4,16 @@ export interface PresenceChannel { /** * Register a callback to be called anytime the member list changes. - * - * @param {Function} callback - * @return {object} PresenceChannel */ here(callback: Function): PresenceChannel; /** * Listen for someone joining the channel. - * - * @param {Function} callback - * @return {PresenceChannel} */ joining(callback: Function): PresenceChannel; /** * Listen for someone leaving the channel. - * - * @param {Function} callback - * @return {PresenceChannel} */ leaving(callback: Function): PresenceChannel; } diff --git a/src/channel/pusher-channel.ts b/src/channel/pusher-channel.ts index 1390e2c2..dbd38c8b 100644 --- a/src/channel/pusher-channel.ts +++ b/src/channel/pusher-channel.ts @@ -7,45 +7,31 @@ import { Channel } from './channel'; export class PusherChannel extends Channel { /** * The Pusher client instance. - * - * @type {any} */ pusher: any; /** * The name of the channel. - * - * @type {object} */ name: any; /** * Channel options. - * - * @type {any} */ options: any; /** * The event formatter. - * - * @type {EventFormatter} */ eventFormatter: EventFormatter; /** * The subsciption of the channel. - * - * @type {any} */ subscription: any; /** * Create a new class instance. - * - * @param {any} pusher - * @param {object} name - * @param {any} options */ constructor(pusher: any, name: any, options: any) { super(); @@ -53,7 +39,6 @@ export class PusherChannel extends Channel { this.name = name; this.pusher = pusher; this.options = options; - this.eventFormatter = new EventFormatter(this.options.namespace); this.subscribe(); @@ -61,9 +46,6 @@ export class PusherChannel extends Channel { /** * Subscribe to a Pusher channel. - * - * @param {string} channel - * @return {object} */ subscribe(): any { this.subscription = this.pusher.subscribe(this.name); @@ -71,8 +53,6 @@ export class PusherChannel extends Channel { /** * Unsubscribe from a Pusher channel. - * - * @return {void} */ unsubscribe(): void { this.pusher.unsubscribe(this.name); @@ -80,10 +60,6 @@ export class PusherChannel extends Channel { /** * Listen for an event on the channel instance. - * - * @param {string} event - * @param {Function} callback - * @return {PusherChannel} */ listen(event: string, callback: Function): PusherChannel { this.on(this.eventFormatter.format(event), callback); @@ -93,9 +69,6 @@ export class PusherChannel extends Channel { /** * Stop listening for an event on the channel instance. - * - * @param {string} event - * @return {PusherChannel} */ stopListening(event: string): PusherChannel { this.subscription.unbind(this.eventFormatter.format(event)); @@ -105,10 +78,6 @@ export class PusherChannel extends Channel { /** * Bind a channel to an event. - * - * @param {string} event - * @param {Function} callback - * @return {void} */ on(event: string, callback: Function): PusherChannel { this.subscription.bind(event, callback); diff --git a/src/channel/pusher-presence-channel.ts b/src/channel/pusher-presence-channel.ts index 08c68c83..425bfbac 100644 --- a/src/channel/pusher-presence-channel.ts +++ b/src/channel/pusher-presence-channel.ts @@ -7,11 +7,8 @@ import { PresenceChannel } from './presence-channel'; export class PusherPresenceChannel extends PusherChannel implements PresenceChannel { /** * Register a callback to be called anytime the member list changes. - * - * @param {Function} callback - * @return {object} this */ - here(callback): PusherPresenceChannel { + here(callback: Function): PusherPresenceChannel { this.on('pusher:subscription_succeeded', (data) => { callback(Object.keys(data.members).map(k => data.members[k])); }); @@ -21,11 +18,8 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan /** * Listen for someone joining the channel. - * - * @param {Function} callback - * @return {PusherPresenceChannel} */ - joining(callback): PusherPresenceChannel { + joining(callback: Function): PusherPresenceChannel { this.on('pusher:member_added', (member) => { callback(member.info); }); @@ -35,11 +29,8 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan /** * Listen for someone leaving the channel. - * - * @param {Function} callback - * @return {PusherPresenceChannel} */ - leaving(callback): PusherPresenceChannel { + leaving(callback: Function): PusherPresenceChannel { this.on('pusher:member_removed', (member) => { callback(member.info); }); @@ -49,12 +40,10 @@ export class PusherPresenceChannel extends PusherChannel implements PresenceChan /** * Trigger client event on the channel. - * - * @param {Function} callback - * @return {PusherPresenceChannel} */ - whisper(eventName, data): PusherPresenceChannel { - this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data); + whisper(eventName: string, data: any): PusherPresenceChannel { + this.pusher.channels.channels[this.name] + .trigger(`client-${eventName}`, data); return this; } diff --git a/src/channel/pusher-private-channel.ts b/src/channel/pusher-private-channel.ts index b99f892d..6c42f2a0 100644 --- a/src/channel/pusher-private-channel.ts +++ b/src/channel/pusher-private-channel.ts @@ -6,12 +6,10 @@ import { PusherChannel } from './pusher-channel'; export class PusherPrivateChannel extends PusherChannel { /** * Trigger client event on the channel. - * - * @param {Function} callback - * @return {PusherPrivateChannel} */ - whisper(eventName, data): PusherPrivateChannel { - this.pusher.channels.channels[this.name].trigger(`client-${eventName}`, data); + whisper(eventName: string, data: any): PusherPrivateChannel { + this.pusher.channels.channels[this.name] + .trigger(`client-${eventName}`, data); return this; } diff --git a/src/channel/socketio-channel.ts b/src/channel/socketio-channel.ts index a8ff9a63..ef72a18c 100644 --- a/src/channel/socketio-channel.ts +++ b/src/channel/socketio-channel.ts @@ -7,45 +7,31 @@ import { Channel } from './channel'; export class SocketIoChannel extends Channel { /** * The Socket.io client instance. - * - * @type {any} */ socket: any; /** * The name of the channel. - * - * @type {object} */ name: any; /** * Channel options. - * - * @type {any} */ options: any; /** * The event formatter. - * - * @type {EventFormatter} */ eventFormatter: EventFormatter; /** * The event callbacks applied to the channel. - * - * @type {any} */ events: any = {}; /** * Create a new class instance. - * - * @param {any} socket - * @param {string} name - * @param {any} options */ constructor(socket: any, name: string, options: any) { super(); @@ -56,16 +42,13 @@ export class SocketIoChannel extends Channel { this.eventFormatter = new EventFormatter(this.options.namespace); this.subscribe(); - this.configureReconnector(); } /** * Subscribe to a Socket.io channel. - * - * @return {object} */ - subscribe(): any { + subscribe(): void { this.socket.emit('subscribe', { channel: this.name, auth: this.options.auth || {} @@ -74,8 +57,6 @@ export class SocketIoChannel extends Channel { /** * Unsubscribe from channel and ubind event callbacks. - * - * @return {void} */ unsubscribe(): void { this.unbind(); @@ -88,10 +69,6 @@ export class SocketIoChannel extends Channel { /** * Listen for an event on the channel instance. - * - * @param {string} event - * @param {Function} callback - * @return {SocketIoChannel} */ listen(event: string, callback: Function): SocketIoChannel { this.on(this.eventFormatter.format(event), callback); @@ -99,11 +76,19 @@ export class SocketIoChannel extends Channel { return this; } + /** + * Stop listening for an event on the channel instance. + */ + stopListening(event: string): SocketIoChannel { + const name = this.eventFormatter.format(event); + this.socket.removeListener(name); + delete this.events[name]; + + return this; + } + /** * Bind the channel's socket to an event and store the callback. - * - * @param {string} event - * @param {Function} callback */ on(event: string, callback: Function): void { let listener = (channel, data) => { @@ -120,32 +105,24 @@ export class SocketIoChannel extends Channel { * Attach a 'reconnect' listener and bind the event. */ configureReconnector(): void { - let listener = () => { + const listener = () => { this.subscribe(); }; this.socket.on('reconnect', listener); - this.bind('reconnect', listener); } /** * Bind the channel's socket to an event and store the callback. - * - * @param {string} event - * @param {Function} callback - * @return {void} */ bind(event: string, callback: Function): void { this.events[event] = this.events[event] || []; - this.events[event].push(callback); } /** * Unbind the channel's socket from all stored event callbacks. - * - * @return {void} */ unbind(): void { Object.keys(this.events).forEach(event => { diff --git a/src/channel/socketio-presence-channel.ts b/src/channel/socketio-presence-channel.ts index 024ef8b5..cf683ff2 100644 --- a/src/channel/socketio-presence-channel.ts +++ b/src/channel/socketio-presence-channel.ts @@ -1,4 +1,5 @@ -import { PresenceChannel, SocketIoPrivateChannel } from './'; +import { PresenceChannel } from './presence-channel'; +import { SocketIoPrivateChannel } from './socketio-private-channel'; /** * This class represents a Socket.io presence channel. @@ -6,12 +7,9 @@ import { PresenceChannel, SocketIoPrivateChannel } from './'; export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements PresenceChannel { /** * Register a callback to be called anytime the member list changes. - * - * @param {Function} callback - * @return {object} SocketIoPresenceChannel */ here(callback: Function): SocketIoPresenceChannel { - this.on('presence:subscribed', (members) => { + this.on('presence:subscribed', (members: any[]) => { callback(members.map(m => m.user_info)); }); @@ -20,9 +18,6 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P /** * Listen for someone joining the channel. - * - * @param {Function} callback - * @return {SocketIoPresenceChannel} */ joining(callback: Function): SocketIoPresenceChannel { this.on('presence:joining', (member) => callback(member.user_info)); @@ -32,9 +27,6 @@ export class SocketIoPresenceChannel extends SocketIoPrivateChannel implements P /** * Listen for someone leaving the channel. - * - * @param {Function} callback - * @return {SocketIoPresenceChannel} */ leaving(callback: Function): SocketIoPresenceChannel { this.on('presence:leaving', (member) => callback(member.user_info)); diff --git a/src/channel/socketio-private-channel.ts b/src/channel/socketio-private-channel.ts index 4cbdd498..8ffa9745 100644 --- a/src/channel/socketio-private-channel.ts +++ b/src/channel/socketio-private-channel.ts @@ -1,4 +1,4 @@ -import { SocketIoChannel } from './'; +import { SocketIoChannel } from './socketio-channel'; /** * This class represents a Socket.io presence channel. @@ -6,12 +6,8 @@ import { SocketIoChannel } from './'; export class SocketIoPrivateChannel extends SocketIoChannel { /** * Trigger client event on the channel. - * - * @param {string} eventName - * @param {object} data - * @return {PusherPrivateChannel} */ - whisper(eventName, data) { + whisper(eventName: string, data: any): SocketIoChannel { this.socket.emit('client event', { channel: this.name, event: `client-${eventName}`, diff --git a/src/connector/connector.ts b/src/connector/connector.ts index 3e6285c3..1779acb4 100644 --- a/src/connector/connector.ts +++ b/src/connector/connector.ts @@ -4,8 +4,6 @@ export abstract class Connector { /** * Default connector options. - * - * @type {Object} */ private _defaultOptions: any = { auth: { @@ -21,27 +19,19 @@ export abstract class Connector { /** * Connector options. - * - * @type {object} */ options: any; /** * Create a new class instance. - * - * @params {any} options */ constructor(options: any) { this.setOptions(options); - this.connect(); } /** * Merge the custom options with the defaults. - * - * @param {any} options - * @return {any} */ protected setOptions(options: any): any { this.options = Object.assign(this._defaultOptions, options); @@ -55,8 +45,6 @@ export abstract class Connector { /** * Extract the CSRF token from the page. - * - * @return {string} */ protected csrfToken(): string { let selector; @@ -74,54 +62,36 @@ export abstract class Connector { /** * Create a fresh connection. - * - * @retrn void */ abstract connect(): void; /** * Get a channel instance by name. - * - * @param {string} channel - * @return {PusherChannel} */ abstract channel(channel: string): Channel; /** * Get a private channel instance by name. - * - * @param {string} channel - * @return {Channel} */ abstract privateChannel(channel: string): Channel; /** * Get a presence channel instance by name. - * - * @param {string} channel - * @return {PresenceChannel} */ abstract presenceChannel(channel: string): PresenceChannel; /** * Leave the given channel. - * - * @param {string} channel - * @return {void} */ abstract leave(channel: string): void; /** * Get the socket_id of the connection. - * - * @return {string} */ abstract socketId(): string; /** * Disconnect from the Echo server. - * - * @return void */ abstract disconnect(): void; } diff --git a/src/connector/null-connector.ts b/src/connector/null-connector.ts index cb680f58..c3a96bf1 100644 --- a/src/connector/null-connector.ts +++ b/src/connector/null-connector.ts @@ -1,4 +1,4 @@ -import { Connector} from './connector'; +import { Connector } from './connector'; import { NullChannel, NullPrivateChannel, NullPresenceChannel, PresenceChannel } from './../channel'; @@ -9,15 +9,11 @@ import { export class NullConnector extends Connector { /** * All of the subscribed channel names. - * - * @type {array} */ channels: any = {}; /** * Create a fresh connection. - * - * @return void */ connect(): void { // @@ -25,11 +21,6 @@ export class NullConnector extends Connector { /** * Listen for an event on a channel instance. - * - * @param {string} name - * @param {event} string - * @param {Function} callback - * @return {NullChannel} */ listen(name: string, event: string, callback: Function): NullChannel { return new NullChannel; @@ -37,9 +28,6 @@ export class NullConnector extends Connector { /** * Get a channel instance by name. - * - * @param {string} name - * @return {NullChannel} */ channel(name: string): NullChannel { return new NullChannel; @@ -47,9 +35,6 @@ export class NullConnector extends Connector { /** * Get a private channel instance by name. - * - * @param {string} name - * @return {NullPrivateChannel} */ privateChannel(name: string): NullPrivateChannel { return new NullPrivateChannel; @@ -57,9 +42,6 @@ export class NullConnector extends Connector { /** * Get a presence channel instance by name. - * - * @param {string} name - * @return {PresenceChannel} */ presenceChannel(name: string): PresenceChannel { return new NullPresenceChannel; @@ -67,8 +49,6 @@ export class NullConnector extends Connector { /** * Leave the given channel. - * - * @param {string} channel */ leave(name: string) { // @@ -76,8 +56,6 @@ export class NullConnector extends Connector { /** * Get the socket ID for the connection. - * - * @return {string} */ socketId(): string { return 'fake-socket-id'; @@ -85,8 +63,6 @@ export class NullConnector extends Connector { /** * Disconnect the connection. - * - * @return void */ disconnect(): void { // diff --git a/src/connector/pusher-connector.ts b/src/connector/pusher-connector.ts index 296b8fb7..9ec16635 100644 --- a/src/connector/pusher-connector.ts +++ b/src/connector/pusher-connector.ts @@ -1,4 +1,4 @@ -import { Connector} from './connector'; +import { Connector } from './connector'; import { PusherChannel, PusherPrivateChannel, PusherPresenceChannel, PresenceChannel } from './../channel'; @@ -9,22 +9,16 @@ import { export class PusherConnector extends Connector { /** * The Pusher instance. - * - * @type {object} */ pusher: any; /** * All of the subscribed channel names. - * - * @type {array} */ channels: any = {}; /** * Create a fresh Pusher connection. - * - * @return void */ connect(): void { if (typeof this.options.client !== 'undefined') { @@ -36,11 +30,6 @@ export class PusherConnector extends Connector { /** * Listen for an event on a channel instance. - * - * @param {string} name - * @param {event} string - * @param {Function} callback - * @return {PusherChannel} */ listen(name: string, event: string, callback: Function): PusherChannel { return this.channel(name).listen(event, callback); @@ -48,9 +37,6 @@ export class PusherConnector extends Connector { /** * Get a channel instance by name. - * - * @param {string} name - * @return {PusherChannel} */ channel(name: string): PusherChannel { if (!this.channels[name]) { @@ -66,9 +52,6 @@ export class PusherConnector extends Connector { /** * Get a private channel instance by name. - * - * @param {string} name - * @return {PusherPrivateChannel} */ privateChannel(name: string): PusherChannel { if (!this.channels['private-' + name]) { @@ -84,9 +67,6 @@ export class PusherConnector extends Connector { /** * Get a presence channel instance by name. - * - * @param {string} name - * @return {PresenceChannel} */ presenceChannel(name: string): PresenceChannel { if (!this.channels['presence-' + name]) { @@ -102,8 +82,6 @@ export class PusherConnector extends Connector { /** * Leave the given channel. - * - * @param {string} channel */ leave(name: string) { let channels = [name, 'private-' + name, 'presence-' + name]; @@ -119,8 +97,6 @@ export class PusherConnector extends Connector { /** * Get the socket ID for the connection. - * - * @return {string} */ socketId(): string { return this.pusher.connection.socket_id; @@ -128,8 +104,6 @@ export class PusherConnector extends Connector { /** * Disconnect Pusher connection. - * - * @return void */ disconnect(): void { this.pusher.disconnect(); diff --git a/src/connector/socketio-connector.ts b/src/connector/socketio-connector.ts index 4d712a7a..1325d21e 100644 --- a/src/connector/socketio-connector.ts +++ b/src/connector/socketio-connector.ts @@ -7,22 +7,16 @@ import { SocketIoChannel, SocketIoPrivateChannel, SocketIoPresenceChannel } from export class SocketIoConnector extends Connector { /** * The Socket.io connection instance. - * - * @type {object} */ socket: any; /** * All of the subscribed channel names. - * - * @type {any} */ channels: any = {}; /** * Create a fresh Socket.io connection. - * - * @return void */ connect(): void { let io = this.getSocketIO(); @@ -34,8 +28,6 @@ export class SocketIoConnector extends Connector { /** * Get socket.io module from global scope or options. - * - * @type {object} */ getSocketIO(): any { if (typeof io !== 'undefined') { @@ -51,11 +43,6 @@ export class SocketIoConnector extends Connector { /** * Listen for an event on a channel instance. - * - * @param {string} name - * @param {string} event - * @param {Function} callback - * @return {SocketIoChannel} */ listen(name: string, event: string, callback: Function): SocketIoChannel { return this.channel(name).listen(event, callback); @@ -63,9 +50,6 @@ export class SocketIoConnector extends Connector { /** * Get a channel instance by name. - * - * @param {string} name - * @return {SocketIoChannel} */ channel(name: string): SocketIoChannel { if (!this.channels[name]) { @@ -81,9 +65,6 @@ export class SocketIoConnector extends Connector { /** * Get a private channel instance by name. - * - * @param {string} name - * @return {SocketIoChannel} */ privateChannel(name: string): SocketIoPrivateChannel { if (!this.channels['private-' + name]) { @@ -99,9 +80,6 @@ export class SocketIoConnector extends Connector { /** * Get a presence channel instance by name. - * - * @param {string} name - * @return {SocketIoPresenceChannel} */ presenceChannel(name: string): SocketIoPresenceChannel { if (!this.channels['presence-' + name]) { @@ -117,9 +95,6 @@ export class SocketIoConnector extends Connector { /** * Leave the given channel. - * - * @param {string} name - * @return {void} */ leave(name: string): void { let channels = [name, 'private-' + name, 'presence-' + name]; @@ -135,8 +110,6 @@ export class SocketIoConnector extends Connector { /** * Get the socket ID for the connection. - * - * @return {string} */ socketId(): string { return this.socket.id; @@ -144,8 +117,6 @@ export class SocketIoConnector extends Connector { /** * Disconnect Socketio connection. - * - * @return void */ disconnect(): void { this.socket.disconnect(); diff --git a/src/echo.ts b/src/echo.ts index 3e5850c8..6777ea0f 100644 --- a/src/echo.ts +++ b/src/echo.ts @@ -1,46 +1,40 @@ -import { EventFormatter } from './util'; import { Channel, PresenceChannel } from './channel' import { PusherConnector, SocketIoConnector, NullConnector } from './connector'; /** * This class is the primary API for interacting with broadcasting. */ -class Echo { - +export default class Echo { /** * The broadcasting connector. - * - * @type {object} */ connector: any; /** * The Echo options. - * - * @type {array} */ options: any; /** * Create a new class instance. - * - * @param {object} options */ constructor(options: any) { this.options = options; + this.connect(); + this.registerInterceptors(); + } - if (typeof Vue === 'function' && Vue.http) { - this.registerVueRequestInterceptor(); - } - - if (typeof axios === 'function') { - this.registerAxiosRequestInterceptor(); - } - - if (typeof jQuery === 'function') { - this.registerjQueryAjaxSetup(); - } + /** + * Get a channel instance by name. + */ + channel(channel: string): Channel { + return this.connector.channel(channel); + } + /** + * Create a new connection. + */ + connect(): void { if (this.options.broadcaster == 'pusher') { this.connector = new PusherConnector(this.options); } else if (this.options.broadcaster == 'socket.io') { @@ -51,109 +45,103 @@ class Echo { } /** - * Register a Vue HTTP interceptor to add the X-Socket-ID header. + * Disconnect from the Echo server. */ - registerVueRequestInterceptor() { - Vue.http.interceptors.push((request, next) => { - if (this.socketId()) { - request.headers.set('X-Socket-ID', this.socketId()); - } - - next(); - }); + disconnect(): void { + this.connector.disconnect(); } /** - * Register an Axios HTTP interceptor to add the X-Socket-ID header. + * Get a presence channel instance by name. */ - registerAxiosRequestInterceptor() { - axios.interceptors.request.use((config) => { - if (this.socketId()) { - config.headers['X-Socket-Id'] = this.socketId(); - } - - return config; - }); + join(channel: string): PresenceChannel { + return this.connector.presenceChannel(channel); } /** - * Register jQuery AjaxSetup to add the X-Socket-ID header. + * Leave the given channel. */ - registerjQueryAjaxSetup() { - if (typeof jQuery.ajax != 'undefined') { - jQuery.ajaxSetup({ - beforeSend: (xhr) => { - if (this.socketId()) { - xhr.setRequestHeader('X-Socket-Id', this.socketId()); - } - } - }); - } + leave(channel: string): void { + this.connector.leave(channel); } /** * Listen for an event on a channel instance. */ - listen(channel: string, event: string, callback: Function) { + listen(channel: string, event: string, callback: Function): Channel { return this.connector.listen(channel, event, callback); } /** - * Get a channel instance by name. - * - * @param {string} channel - * @return {object} + * Get a private channel instance by name. */ - channel(channel: string): Channel { - return this.connector.channel(channel); + private(channel: string): Channel { + return this.connector.privateChannel(channel); } /** - * Get a private channel instance by name. - * - * @param {string} channel - * @return {object} + * Get the Socket ID for the connection. */ - private(channel: string): Channel { - return this.connector.privateChannel(channel); + socketId(): string { + return this.connector.socketId(); } /** - * Get a presence channel instance by name. - * - * @param {string} channel - * @return {object} + * Register 3rd party request interceptiors. These are used to automatically + * send a connections socket id to a Laravel app with a X-Socket-Id header. */ - join(channel: string): PresenceChannel { - return this.connector.presenceChannel(channel); + registerInterceptors(): void { + if (typeof Vue === 'function' && Vue.http) { + this.registerVueRequestInterceptor(); + } + + if (typeof axios === 'function') { + this.registerAxiosRequestInterceptor(); + } + + if (typeof jQuery === 'function') { + this.registerjQueryAjaxSetup(); + } } /** - * Leave the given channel. - * - * @param {string} channel + * Register a Vue HTTP interceptor to add the X-Socket-ID header. */ - leave(channel: string) { - this.connector.leave(channel); + registerVueRequestInterceptor(): void { + Vue.http.interceptors.push((request, next) => { + if (this.socketId()) { + request.headers.set('X-Socket-ID', this.socketId()); + } + + next(); + }); } /** - * Get the Socket ID for the connection. - * - * @return {string} + * Register an Axios HTTP interceptor to add the X-Socket-ID header. */ - socketId(): string { - return this.connector.socketId(); + registerAxiosRequestInterceptor(): any { + axios.interceptors.request.use((config) => { + if (this.socketId()) { + config.headers['X-Socket-Id'] = this.socketId(); + } + + return config; + }); } /** - * Disconnect from the Echo server. - * - * @return void + * Register jQuery AjaxSetup to add the X-Socket-ID header. */ - disconnect(): void { - this.connector.disconnect(); + registerjQueryAjaxSetup(): void { + if (typeof jQuery.ajax != 'undefined') { + jQuery.ajaxSetup({ + beforeSend: (xhr) => { + if (this.socketId()) { + xhr.setRequestHeader('X-Socket-Id', this.socketId()); + } + } + }); + } } } - -module.exports = Echo; diff --git a/src/util/event-formatter.ts b/src/util/event-formatter.ts index 49eaff77..b5ec09bf 100644 --- a/src/util/event-formatter.ts +++ b/src/util/event-formatter.ts @@ -2,18 +2,13 @@ * Event name formatter */ export class EventFormatter { - /** * Event namespace. - * - * @type {string} */ namespace: string | boolean; /** * Create a new class instance. - * - * @params {string | boolean} namespace */ constructor(namespace: string | boolean) { this.setNamespace(namespace); @@ -21,9 +16,6 @@ export class EventFormatter { /** * Format the given event name. - * - * @param {string} event - * @return {string} */ format(event: string): string { if (event.charAt(0) === '.' || event.charAt(0) === '\\') { @@ -37,9 +29,6 @@ export class EventFormatter { /** * Set the event namespace. - * - * @param {string} value - * @return {void} */ setNamespace(value: string | boolean): void { this.namespace = value; diff --git a/tsconfig.json b/tsconfig.json index b38f7d95..c7c6c974 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,33 +1,22 @@ { "compilerOptions": { + "declaration": true, "module": "es6", - "target": "es6", "moduleResolution": "node", - "removeComments": true, + "outDir": "./dist", + "sourceMap": false, + "target": "es6", "isolatedModules": false, - "experimentalDecorators": true, - "emitDecoratorMetadata": true, - "declaration": false, - "noImplicitAny": false, - "noImplicitUseStrict": false, - "noLib": false, - "preserveConstEnums": true, - "suppressImplicitAnyIndexErrors": true, - "pretty": true, - "outDir": "build" + "typeRoots": [ + "node_modules/@types" + ], + "lib": [ + "es2017", + "dom" + ] }, - "exclude": [ - "node_modules/*", - "typings/browser", - "typings/browser.d.ts" - ], - "files": [ - "typings/index.d.ts", - "src/echo.ts" - ], - "compileOnSave": false, - "buildOnSave": false, - "atom": { - "rewriteTsconfig": false - } + "include": [ + "./typings/*.ts", + "./src/*.ts" + ] }