-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathChannel.ts
148 lines (121 loc) · 3.14 KB
/
Channel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { EventFormatter } from 'laravel-echo/src/util';
import { Channel as BaseChannel } from 'laravel-echo/src/channel/channel';
import { PresenceChannel } from "laravel-echo/src/channel";
import { Websocket } from "./Websocket";
/**
* This class represents a Pusher channel.
*/
export class Channel extends BaseChannel implements PresenceChannel {
/**
* The Pusher client instance.
*/
socket: Websocket;
/**
* The name of the channel.
*/
name: string;
/**
* Channel options.
*/
options: object;
/**
* The event formatter.
*/
eventFormatter: EventFormatter;
/**
* Create a new class instance.
*/
constructor(socket: Websocket, name: string, options: object) {
super();
this.name = name;
this.socket = socket;
this.options = options;
this.eventFormatter = new EventFormatter(this.options["namespace"]);
this.subscribe();
}
/**
* Subscribe to a Pusher channel.
*/
subscribe(): any {
this.socket.subscribe(this)
}
/**
* Unsubscribe from a Pusher channel.
*/
unsubscribe(): void {
this.socket.unsubscribe(this);
}
/**
* Listen for an event on the channel instance.
*/
listen(event: string, callback: Function): Channel {
this.on(this.eventFormatter.format(event), callback);
return this;
}
/**
* Stop listening for an event on the channel instance.
*/
stopListening(event: string, callback?: Function): Channel {
this.socket.unbindEvent(this, event, callback)
return this;
}
/**
* Register a callback to be called anytime a subscription succeeds.
*/
subscribed(callback: Function): Channel {
this.on('subscription_succeeded', () => {
callback();
});
return this;
}
/**
* Register a callback to be called anytime a subscription error occurs.
*/
error(callback: Function): Channel {
this.on('error', (status) => {
callback(status);
});
return this;
}
/**
* Bind a channel to an event.
*/
on(event: string, callback: Function): Channel {
this.socket.bind(this, event, callback)
return this;
}
whisper(event: string, data: object): Channel {
let channel = this.name;
let formattedEvent = "client-" + event;
this.socket.send({
"event": formattedEvent,
data,
channel,
})
return this;
}
here(callback: Function): Channel {
this.on('subscription_succeeded', (data) => {
callback(data)
})
return this
}
/**
* Listen for someone joining the channel.
*/
joining(callback: Function): Channel {
this.on('member_added', (data) => {
callback(data)
})
return this
}
/**
* Listen for someone leaving the channel.
*/
leaving(callback: Function): Channel {
this.on('member_removed', (data) => {
callback(data)
})
return this
}
}