UNPKG

4.12 kBPlain TextView Raw
1import {Bootstrap, Comms} from 'cyclon.p2p';
2import {WebRTCComms} from './WebRTCComms';
3import {ShuffleStateFactory} from './ShuffleStateFactory';
4import {asyncExecService, consoleLogger, Logger, newInMemoryStorage} from 'cyclon.p2p-common';
5import {
6 DEFAULT_CHANNEL_STATE_TIMEOUT_MS,
7 DEFAULT_ICE_SERVERS,
8 DEFAULT_SIGNALLING_SERVER_RECONNECT_DELAY_MS,
9 DEFAULT_SIGNALLING_SERVERS,
10 HttpRequestService,
11 rtcBuilder
12} from 'cyclon.p2p-rtc-client';
13import {SignallingServerBootstrap} from './SignallingServerBootstrap';
14import {SignallingServerSpec} from 'cyclon.p2p-rtc-client/lib/SignallingServerSpec';
15import {DEFAULT_ROOMS_TO_JOIN} from './Defaults';
16
17interface BuilderResult {
18 comms: Comms,
19 bootstrap: Bootstrap
20}
21
22export class Builder {
23
24 private logger?: Logger;
25 private result?: BuilderResult;
26 private signallingServers?: SignallingServerSpec[];
27 private storage?: Storage;
28 private reconnectDelayInMillis?: number;
29 private channelStateTimeoutMillis?: number;
30 private iceServers?: RTCIceServer[];
31 private roomsToJoin?: string[];
32
33 withLogger(logger: Logger): Builder {
34 this.logger = logger;
35 return this;
36 }
37
38 withSignallingServers(signallingServerSpecs: SignallingServerSpec[]): Builder {
39 this.signallingServers = signallingServerSpecs;
40 return this;
41 }
42
43 withStorage(storage: Storage): Builder {
44 this.storage = storage;
45 return this;
46 }
47
48 withSignallingServerReconnectDelay(reconnectDelayInMillis: number): Builder {
49 this.reconnectDelayInMillis = reconnectDelayInMillis;
50 return this;
51 }
52
53 withChannelStateChangeTimeout(channelStateTimeoutMillis: number): Builder {
54 this.channelStateTimeoutMillis = channelStateTimeoutMillis;
55 return this;
56 }
57
58 withIceServers(iceServers: RTCIceServer[]): Builder {
59 this.iceServers = iceServers;
60 return this;
61 }
62
63 joiningRooms(roomsToJoin: string[]): Builder {
64 this.roomsToJoin = roomsToJoin;
65 return this;
66 }
67
68 build(): BuilderResult {
69 if (!this.result) {
70 this.result = this.doBuild();
71 }
72 return this.result;
73 }
74
75 private doBuild(): BuilderResult {
76 const rtcBuilderResult = rtcBuilder()
77 .withLogger(this.getLogger())
78 .withStorage(this.getStorage())
79 .withSignallingServers(this.getSignallingServers())
80 .withIceServers(this.getIceServers())
81 .withChannelStateChangeTimeout(this.getChannelStateTimeout())
82 .withSignallingServerReconnectDelay(this.getReconnectDelay())
83 .build();
84 const shuffleStateFactory = new ShuffleStateFactory(this.getLogger(), asyncExecService());
85 const roomsToJoin = this.getRoomsToJoin();
86 return {
87 comms: new WebRTCComms(rtcBuilderResult.rtc, shuffleStateFactory, this.getLogger(), roomsToJoin),
88 bootstrap: new SignallingServerBootstrap(rtcBuilderResult.signallingSocket, new HttpRequestService(), roomsToJoin)
89 }
90 }
91
92 private getRoomsToJoin() {
93 return this.roomsToJoin === undefined ? DEFAULT_ROOMS_TO_JOIN : this.roomsToJoin;
94 }
95
96 private getIceServers() {
97 return this.iceServers === undefined ? DEFAULT_ICE_SERVERS : this.iceServers;
98 }
99
100 private getChannelStateTimeout() {
101 return this.channelStateTimeoutMillis === undefined ? DEFAULT_CHANNEL_STATE_TIMEOUT_MS : this.channelStateTimeoutMillis;
102 }
103
104 private getReconnectDelay(): number {
105 return this.reconnectDelayInMillis === undefined ? DEFAULT_SIGNALLING_SERVER_RECONNECT_DELAY_MS : this.reconnectDelayInMillis;
106 }
107
108 private getLogger() {
109 return this.logger || consoleLogger();
110 }
111
112 private getStorage() {
113 if (!this.storage) {
114 this.storage = newInMemoryStorage();
115 }
116 return this.storage;
117 }
118
119 private getSignallingServers(): SignallingServerSpec[] {
120 return this.signallingServers === undefined ? JSON.parse(JSON.stringify(DEFAULT_SIGNALLING_SERVERS)) : this.signallingServers;
121 }
122}
\No newline at end of file