-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwave-presence-channel.ts
108 lines (82 loc) · 3.35 KB
/
wave-presence-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
import request, { beacon } from '../util/request';
import { PresenceChannel } from 'laravel-echo';
import WavePrivateChannel from './wave-private-channel';
import { authRequest } from '../channel-auth';
export default class WavePresenceChannel extends WavePrivateChannel implements PresenceChannel {
private joined = false;
private joinRequest: Promise<Response>;
private csrfToken: string;
private users: any[] = [];
private hereCallbacks: Function[] = [];
private unloadCallback: () => void = this.unsubscribeBeacon.bind(this);
constructor(connection, name, options) {
super(connection, name, options);
if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', this.unloadCallback);
}
}
public subscribe(): void {
super.subscribe();
this.joinRequest = authRequest(this.name, this.connection, { ...this.options, authEndpoint: this.options.endpoint + '/presence-channel-users' })
.then(async (response) => {
const data = await response.json();
this.csrfToken = data._token;
this.users = data.users;
return response;
}).then((response) => {
this.joined = true;
return response;
}).then((response) => {
this.hereCallbacks.forEach((callback) => callback(this.users));
this.hereCallbacks = [];
return response;
});
this.joinRequest.catch(error => {
this.errorCallbacks.forEach((callback) => callback(error));
})
}
public here(callback: Function): WavePresenceChannel {
if (this.joined) {
request(this.connection)
.get(this.options.endpoint + '/presence-channel-users', this.options, { channel_name: this.name })
.then(async (response) => callback(await response.json()))
.catch((error) => this.errorCallbacks.forEach((callback) => callback(error)));
return this;
}
this.hereCallbacks.push(() => callback(this.users));
return this;
}
/**
* Listen for someone joining the channel.
*/
public joining(callback: Function): WavePresenceChannel {
this.listen('.join', callback);
return this;
}
/**
* Listen for someone leaving the channel.
*/
public leaving(callback: Function): WavePresenceChannel {
this.listen('.leave', callback);
return this;
}
public unsubscribeBeacon(): void {
beacon(this.connection.getId(), this.csrfToken, this.options.endpoint + '/presence-channel-users', {
channel_name: this.name,
}, 'DELETE');
super.unsubscribe();
}
public unsubscribe() {
this.joinRequest.then(() => {
request(this.connection, true).delete(this.options.endpoint + '/presence-channel-users', this.options, { channel_name: this.name })
.then(() => {
super.unsubscribe();
if (typeof window !== 'undefined') {
window.removeEventListener('beforeunload', this.unloadCallback);
}
}).catch((error) => {
this.errorCallbacks.forEach((callback) => callback(error));
});
});
}
}