2021-06-25 18:14:40 +02:00
|
|
|
import { readable, writable } from "svelte/store";
|
2021-09-03 15:16:21 +02:00
|
|
|
import type { VideoPeer } from "../WebRtc/VideoPeer";
|
|
|
|
import type { ScreenSharingPeer } from "../WebRtc/ScreenSharingPeer";
|
2021-05-18 16:38:56 +02:00
|
|
|
|
|
|
|
/**
|
2021-06-11 11:29:36 +02:00
|
|
|
* A store that contains the list of (video) peers we are connected to.
|
2021-05-18 16:38:56 +02:00
|
|
|
*/
|
|
|
|
function createPeerStore() {
|
2021-09-03 15:16:21 +02:00
|
|
|
const { subscribe, set, update } = writable(new Map<number, VideoPeer>());
|
2021-05-18 16:38:56 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
2021-09-03 15:16:21 +02:00
|
|
|
pushNewPeer(peer: VideoPeer) {
|
|
|
|
update((users) => {
|
|
|
|
users.set(peer.userId, peer);
|
|
|
|
return users;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removePeer(userId: number) {
|
|
|
|
update((users) => {
|
|
|
|
users.delete(userId);
|
|
|
|
return users;
|
2021-06-25 18:14:40 +02:00
|
|
|
});
|
|
|
|
},
|
2021-09-03 15:16:21 +02:00
|
|
|
cleanupStore() {
|
|
|
|
set(new Map<number, VideoPeer>());
|
|
|
|
},
|
2021-06-11 11:29:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A store that contains the list of screen sharing peers we are connected to.
|
|
|
|
*/
|
|
|
|
function createScreenSharingPeerStore() {
|
2021-09-03 15:16:21 +02:00
|
|
|
const { subscribe, set, update } = writable(new Map<number, ScreenSharingPeer>());
|
2021-06-11 11:29:36 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
2021-09-03 15:16:21 +02:00
|
|
|
pushNewPeer(peer: ScreenSharingPeer) {
|
|
|
|
update((users) => {
|
|
|
|
users.set(peer.userId, peer);
|
|
|
|
return users;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removePeer(userId: number) {
|
|
|
|
update((users) => {
|
|
|
|
users.delete(userId);
|
|
|
|
return users;
|
2021-06-25 18:14:40 +02:00
|
|
|
});
|
|
|
|
},
|
2021-09-03 15:16:21 +02:00
|
|
|
cleanupStore() {
|
|
|
|
set(new Map<number, ScreenSharingPeer>());
|
|
|
|
},
|
2021-05-18 16:38:56 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const peerStore = createPeerStore();
|
2021-06-11 11:29:36 +02:00
|
|
|
export const screenSharingPeerStore = createScreenSharingPeerStore();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A store that contains ScreenSharingPeer, ONLY if those ScreenSharingPeer are emitting a stream towards us!
|
|
|
|
*/
|
|
|
|
function createScreenSharingStreamStore() {
|
|
|
|
let peers = new Map<number, ScreenSharingPeer>();
|
|
|
|
|
|
|
|
return readable<Map<number, ScreenSharingPeer>>(peers, function start(set) {
|
2021-06-25 18:14:40 +02:00
|
|
|
let unsubscribes: (() => void)[] = [];
|
2021-06-11 11:29:36 +02:00
|
|
|
|
|
|
|
const unsubscribe = screenSharingPeerStore.subscribe((screenSharingPeers) => {
|
|
|
|
for (const unsubscribe of unsubscribes) {
|
|
|
|
unsubscribe();
|
|
|
|
}
|
|
|
|
unsubscribes = [];
|
|
|
|
|
|
|
|
peers = new Map<number, ScreenSharingPeer>();
|
|
|
|
|
|
|
|
screenSharingPeers.forEach((screenSharingPeer: ScreenSharingPeer, key: number) => {
|
|
|
|
if (screenSharingPeer.isReceivingScreenSharingStream()) {
|
|
|
|
peers.set(key, screenSharingPeer);
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
unsubscribes.push(
|
|
|
|
screenSharingPeer.streamStore.subscribe((stream) => {
|
|
|
|
if (stream) {
|
|
|
|
peers.set(key, screenSharingPeer);
|
|
|
|
} else {
|
|
|
|
peers.delete(key);
|
|
|
|
}
|
|
|
|
set(peers);
|
|
|
|
})
|
|
|
|
);
|
2021-06-11 11:29:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
set(peers);
|
|
|
|
});
|
|
|
|
|
|
|
|
return function stop() {
|
|
|
|
unsubscribe();
|
|
|
|
for (const unsubscribe of unsubscribes) {
|
|
|
|
unsubscribe();
|
|
|
|
}
|
|
|
|
};
|
2021-06-25 18:14:40 +02:00
|
|
|
});
|
2021-06-11 11:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const screenSharingStreamStore = createScreenSharingStreamStore();
|