Skip to content

Commit

Permalink
General maintenance, code styling, and add stopListening() for socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaverdure committed Nov 30, 2018
1 parent c829764 commit 1d90d7d
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 354 deletions.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"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": {
Expand All @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
27 changes: 9 additions & 18 deletions src/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 0 additions & 16 deletions src/channel/null-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,34 @@ import { Channel } from './channel';
export class NullChannel extends Channel {
/**
* Subscribe to a channel.
*
* @param {string} channel
* @return {object}
*/
subscribe(): any {
//
}

/**
* Unsubscribe from a channel.
*
* @return {void}
*/
unsubscribe(): void {
//
}

/**
* Listen for an event on the channel instance.
*
* @param {string} event
* @param {Function} callback
* @return {NullChannel}
*/
listen(event: string, callback: Function): NullChannel {
return this;
}

/**
* Stop listening for an event on the channel instance.
*
* @param {string} event
* @return {NullChannel}
*/
stopListening(event: string): NullChannel {
return this;
}

/**
* Bind a channel to an event.
*
* @param {string} event
* @param {Function} callback
* @return {NullChannel}
*/
on(event: string, callback: Function): NullChannel {
return this;
Expand Down
20 changes: 4 additions & 16 deletions src/channel/null-presence-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 1 addition & 4 deletions src/channel/null-private-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 0 additions & 9 deletions src/channel/presence-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
31 changes: 0 additions & 31 deletions src/channel/pusher-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,59 @@ 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();

this.name = name;
this.pusher = pusher;
this.options = options;

this.eventFormatter = new EventFormatter(this.options.namespace);

this.subscribe();
}

/**
* Subscribe to a Pusher channel.
*
* @param {string} channel
* @return {object}
*/
subscribe(): any {
this.subscription = this.pusher.subscribe(this.name);
}

/**
* Unsubscribe from a Pusher channel.
*
* @return {void}
*/
unsubscribe(): void {
this.pusher.unsubscribe(this.name);
}

/**
* 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);
Expand All @@ -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));
Expand All @@ -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);
Expand Down
23 changes: 6 additions & 17 deletions src/channel/pusher-presence-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions src/channel/pusher-private-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 1d90d7d

Please sign in to comment.