-
Notifications
You must be signed in to change notification settings - Fork 754
/
socket.ts
71 lines (60 loc) · 1.65 KB
/
socket.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
import socket = require("socket.io-client");
import { Observable } from "rxjs/Observable";
import { BehaviorSubject } from "rxjs/BehaviorSubject";
import { of } from "rxjs/observable/of";
import { share } from "rxjs/operators/share";
/**
* Alias for socket.emit
* @param name
* @param data
*/
// export function emit(name, data) {
// if (io && io.emit) {
// // send relative path of where the event is sent
// data.url = window.location.pathname;
// io.emit(name, data);
// }
// }
//
// /**
// * Alias for socket.on
// * @param name
// * @param func
// */
// export function on(name, func) {
// io.on(name, func);
// }
export function initWindow() {
return of(window);
}
export function initDocument() {
return of(document);
}
export function initNavigator() {
return of(navigator);
}
export function initOptions() {
return new BehaviorSubject(window.___browserSync___.options);
}
export function initSocket() {
/**
* @type {{emit: emit, on: on}}
*/
const socketConfig = window.___browserSync___.socketConfig;
const socketUrl = window.___browserSync___.socketUrl;
const io = socket(socketUrl, socketConfig);
const onevent = io.onevent;
const socket$ = Observable.create(obs => {
io.onevent = function(packet) {
onevent.call(this, packet);
obs.next(packet.data);
};
}).pipe(share());
const io$ = new BehaviorSubject(io);
/**
* *****BACK-COMPAT*******
* Scripts that come after Browsersync may rely on the previous window.___browserSync___.socket
*/
window.___browserSync___.socket = io;
return { socket$, io$ };
}