Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit c6ea1f6

Browse files
feat(#773): use stencil/store for remote
1 parent 005e034 commit c6ea1f6

File tree

4 files changed

+64
-77
lines changed

4 files changed

+64
-77
lines changed

remote/src/app/modals/app-remote-connect/app-remote-connect.tsx

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
import {Component, Element, Listen, State, h} from '@stencil/core';
22

3-
import {Subscription} from 'rxjs';
3+
import remoteStore, {ActiveRoom} from '../../stores/remote.store';
44

55
// Services
6-
import {ActiveRoom, CommunicationService} from '../../services/communication/communication.service';
6+
import {CommunicationService} from '../../services/communication/communication.service';
77

88
@Component({
99
tag: 'app-remote-connect',
10-
styleUrl: 'app-remote-connect.scss'
10+
styleUrl: 'app-remote-connect.scss',
1111
})
1212
export class AppRemoteConnect {
1313
@Element() el: HTMLElement;
1414

15-
@State()
16-
private rooms: ActiveRoom[] = [];
17-
1815
@State()
1916
private readonly clientId: string;
2017

21-
private readonly subscription: Subscription;
22-
2318
private communicationService: CommunicationService;
2419

2520
constructor() {
2621
this.communicationService = CommunicationService.getInstance();
2722

28-
this.subscription = this.communicationService.watchRooms().subscribe(async (activeRooms: ActiveRoom[]) => {
29-
this.rooms = activeRooms;
30-
});
31-
3223
this.clientId = this.communicationService.clientId;
3324
}
3425

@@ -38,12 +29,6 @@ export class AppRemoteConnect {
3829
history.pushState({modal: true}, null);
3930
}
4031

41-
async componentDidUnload() {
42-
if (this.subscription) {
43-
this.subscription.unsubscribe();
44-
}
45-
}
46-
4732
@Listen('popstate', {target: 'window'})
4833
async handleHardwareBackbutton(_e: PopStateEvent) {
4934
await this.closeModal(false);
@@ -69,12 +54,12 @@ export class AppRemoteConnect {
6954
</ion-buttons>
7055
</app-header>,
7156

72-
<ion-content class="ion-padding">{this.renderContent()}</ion-content>
57+
<ion-content class="ion-padding">{this.renderContent()}</ion-content>,
7358
];
7459
}
7560

7661
private renderContent() {
77-
if (this.rooms && this.rooms.length > 0) {
62+
if (remoteStore.state.rooms && remoteStore.state.rooms.length > 0) {
7863
return (
7964
<ion-list>
8065
<ion-list-header class="ion-padding-bottom ion-padding-top">
@@ -92,7 +77,7 @@ export class AppRemoteConnect {
9277
}
9378

9479
private renderRooms() {
95-
return this.rooms.map((activeRoom: ActiveRoom) => {
80+
return remoteStore.state.rooms.map((activeRoom: ActiveRoom) => {
9681
return (
9782
<ion-item onClick={() => this.updateAndCloseModal(activeRoom.room)} disabled={activeRoom.connected}>
9883
<ion-label>{activeRoom.room}</ion-label>

remote/src/app/pages/app-remote/app-remote.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {Component, Element, Prop, State, h, JSX} from '@stencil/core';
22
import {alertController, modalController, OverlayEventDetail} from '@ionic/core';
33

4-
import {Subscription} from 'rxjs';
5-
64
import {isMobile} from '@deckdeckgo/utils';
75

6+
import {Subscription} from 'rxjs';
7+
88
import notesStores from '../../stores/notes.store';
9+
import remoteStore from '../../stores/remote.store';
910

1011
// Types
1112
import {
@@ -40,8 +41,8 @@ export class AppRemote {
4041
@Prop()
4142
room: string;
4243

43-
private subscriptionState: Subscription;
44-
private subscriptionEvent: Subscription;
44+
private stateDestroyListener;
45+
private eventDestroyListener;
4546

4647
@State() private connectionState: ConnectionState = ConnectionState.DISCONNECTED;
4748

@@ -81,7 +82,7 @@ export class AppRemote {
8182
}
8283

8384
async componentDidLoad() {
84-
this.subscriptionState = this.communicationService.watchState().subscribe(async (state: ConnectionState) => {
85+
this.stateDestroyListener = remoteStore.onChange('state', async (state: ConnectionState) => {
8586
this.connectionState = state;
8687

8788
if (state === ConnectionState.CONNECTED) {
@@ -92,7 +93,7 @@ export class AppRemote {
9293
}
9394
});
9495

95-
this.subscriptionEvent = this.communicationService.watchEvent().subscribe(async ($event: DeckdeckgoEvent) => {
96+
this.eventDestroyListener = remoteStore.onChange('$event', async ($event: DeckdeckgoEvent) => {
9697
if ($event.emitter === DeckdeckgoEventEmitter.DECK) {
9798
if ($event.type === DeckdeckgoEventType.SLIDES_ANSWER) {
9899
await this.initDeckAndSlides($event as DeckdeckgoEventDeck);
@@ -185,12 +186,12 @@ export class AppRemote {
185186
async componentDidUnload() {
186187
await this.disconnect();
187188

188-
if (this.subscriptionState) {
189-
this.subscriptionState.unsubscribe();
189+
if (this.stateDestroyListener) {
190+
this.stateDestroyListener();
190191
}
191192

192-
if (this.subscriptionEvent) {
193-
this.subscriptionEvent.unsubscribe();
193+
if (this.eventDestroyListener) {
194+
this.eventDestroyListener();
194195
}
195196

196197
if (this.acceleratorSubscription) {

remote/src/app/services/communication/communication.service.tsx

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as io from 'socket.io-client';
22

3-
import {BehaviorSubject, Observable, Subject} from 'rxjs';
3+
import remoteStore from '../../stores/remote.store';
44

55
// Types
66
import {
@@ -9,7 +9,7 @@ import {
99
DeckdeckgoEventNextPrevSlide,
1010
DeckdeckgoEventSlideAction,
1111
DeckdeckgoEventSlideTo,
12-
ConnectionState
12+
ConnectionState,
1313
} from '@deckdeckgo/types';
1414

1515
// Services
@@ -20,17 +20,11 @@ const configuration: RTCConfiguration = {
2020
{
2121
urls: 'turn:api.deckdeckgo.com:3478',
2222
username: 'user',
23-
credential: 'deckdeckgo'
24-
}
25-
]
23+
credential: 'deckdeckgo',
24+
},
25+
],
2626
};
2727

28-
export interface ActiveRoom {
29-
room: string;
30-
clients: number;
31-
connected: boolean;
32-
}
33-
3428
// @ts-ignore
3529
const PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.msRTCPeerConnection;
3630

@@ -51,10 +45,6 @@ export class CommunicationService {
5145
.toString()
5246
.replace(/\B(?=(\d{2})+(?!\d))/g, ' ');
5347

54-
private state: BehaviorSubject<ConnectionState> = new BehaviorSubject<ConnectionState>(ConnectionState.DISCONNECTED);
55-
private event: Subject<DeckdeckgoEvent> = new Subject<DeckdeckgoEvent>();
56-
private rooms: BehaviorSubject<ActiveRoom[]> = new BehaviorSubject<ActiveRoom[]>([]);
57-
5848
private constructor() {
5949
// Private constructor, singleton
6050
}
@@ -73,30 +63,30 @@ export class CommunicationService {
7363
return;
7464
}
7565

76-
this.state.next(ConnectionState.CONNECTING);
66+
remoteStore.state.state = ConnectionState.CONNECTING;
7767

7868
const url: string = EnvironmentConfigService.getInstance().get('signalingServerUrl');
7969

8070
this.socket = io.connect(url, {
8171
reconnectionAttempts: 5,
8272
transports: ['websocket', 'xhr-polling'],
83-
query: 'type=app'
73+
query: 'type=app',
8474
});
8575

8676
this.socket.on('connect', async () => {
8777
this.socket.emit('rooms');
8878
});
8979

9080
this.socket.on('active_rooms', async (data) => {
91-
this.rooms.next(data.rooms);
81+
remoteStore.state.rooms = [...data.rooms];
9282
});
9383

9484
this.socket.on('joined', async () => {
9585
this.startSignaling();
9686

9787
this.sendApp();
9888

99-
this.state.next(ConnectionState.CONNECTED_WITH_SIGNALING_SERVER);
89+
remoteStore.state.state = ConnectionState.CONNECTED_WITH_SIGNALING_SERVER;
10090
});
10191

10292
this.socket.on('deck_joined', async () => {
@@ -124,7 +114,7 @@ export class CommunicationService {
124114
}
125115
},
126116
(_err) => {
127-
this.state.next(ConnectionState.NOT_CONNECTED);
117+
remoteStore.state.state = ConnectionState.NOT_CONNECTED;
128118
}
129119
);
130120
} else {
@@ -134,23 +124,23 @@ export class CommunicationService {
134124
});
135125

136126
this.socket.on('connect_error', () => {
137-
this.state.next(ConnectionState.NOT_CONNECTED);
127+
remoteStore.state.state = ConnectionState.NOT_CONNECTED;
138128
});
139129

140130
this.socket.on('connect_timeout', () => {
141-
this.state.next(ConnectionState.NOT_CONNECTED);
131+
remoteStore.state.state = ConnectionState.NOT_CONNECTED;
142132
});
143133

144134
this.socket.on('error', () => {
145-
this.state.next(ConnectionState.NOT_CONNECTED);
135+
remoteStore.state.state = ConnectionState.NOT_CONNECTED;
146136
});
147137

148138
this.socket.on('reconnect_failed', () => {
149-
this.state.next(ConnectionState.NOT_CONNECTED);
139+
remoteStore.state.state = ConnectionState.NOT_CONNECTED;
150140
});
151141

152142
this.socket.on('reconnect_error', () => {
153-
this.state.next(ConnectionState.NOT_CONNECTED);
143+
remoteStore.state.state = ConnectionState.NOT_CONNECTED;
154144
});
155145

156146
resolve();
@@ -163,7 +153,7 @@ export class CommunicationService {
163153
}
164154

165155
this.socket.emit('join', {
166-
room: this.room
156+
room: this.room,
167157
});
168158
}
169159

@@ -182,13 +172,13 @@ export class CommunicationService {
182172

183173
if (this.socket) {
184174
this.socket.emit('leave', {
185-
room: this.room
175+
room: this.room,
186176
});
187177
this.socket.removeAllListeners();
188178
this.socket.disconnect();
189179
}
190180

191-
this.state.next(ConnectionState.DISCONNECTED);
181+
remoteStore.state.state = ConnectionState.DISCONNECTED;
192182

193183
resolve();
194184
});
@@ -200,18 +190,6 @@ export class CommunicationService {
200190
}
201191
}
202192

203-
watchState(): Observable<ConnectionState> {
204-
return this.state.asObservable();
205-
}
206-
207-
watchEvent(): Observable<DeckdeckgoEvent> {
208-
return this.event.asObservable();
209-
}
210-
211-
watchRooms(): Observable<ActiveRoom[]> {
212-
return this.rooms.asObservable();
213-
}
214-
215193
private startSignaling() {
216194
this.rtcPeerConn = new PeerConnection(configuration);
217195

@@ -223,7 +201,7 @@ export class CommunicationService {
223201
this.socket.emit('signal', {
224202
type: 'ice_candidate',
225203
message: JSON.stringify({candidate: evt.candidate}),
226-
room: this.room
204+
room: this.room,
227205
});
228206
}
229207
};
@@ -235,7 +213,7 @@ export class CommunicationService {
235213
this.socket.emit('signal', {
236214
type: 'app_here',
237215
room: this.room,
238-
message: this.clientId
216+
message: this.clientId,
239217
});
240218
}
241219

@@ -245,11 +223,11 @@ export class CommunicationService {
245223
this.socket.emit('signal', {
246224
type: 'sending_local_description',
247225
message: JSON.stringify({sdp: this.rtcPeerConn.localDescription}),
248-
room: this.room
226+
room: this.room,
249227
});
250228
},
251229
(_err) => {
252-
this.state.next(ConnectionState.NOT_CONNECTED);
230+
remoteStore.state.state = ConnectionState.NOT_CONNECTED;
253231
}
254232
);
255233
}
@@ -261,7 +239,7 @@ export class CommunicationService {
261239
};
262240

263241
private connectedWhenOpened = () => {
264-
this.state.next(ConnectionState.CONNECTED);
242+
remoteStore.state.state = ConnectionState.CONNECTED;
265243
};
266244

267245
private receiveDataChannelMessage = (event) => {
@@ -270,6 +248,6 @@ export class CommunicationService {
270248
}
271249

272250
const data: DeckdeckgoEvent = JSON.parse(event.data);
273-
this.event.next(data);
251+
remoteStore.state.$event = {...data};
274252
};
275253
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {createStore} from '@stencil/store';
2+
3+
import {ConnectionState, DeckdeckgoEvent} from '@deckdeckgo/types';
4+
5+
export interface ActiveRoom {
6+
room: string;
7+
clients: number;
8+
connected: boolean;
9+
}
10+
11+
interface RemoteStore {
12+
state: ConnectionState;
13+
$event: DeckdeckgoEvent | undefined;
14+
rooms: ActiveRoom[];
15+
}
16+
17+
const {state, onChange} = createStore({
18+
state: ConnectionState.DISCONNECTED,
19+
$event: undefined,
20+
rooms: [],
21+
} as RemoteStore);
22+
23+
export default {state, onChange};

0 commit comments

Comments
 (0)