2022-01-05 10:27:40 +01:00
|
|
|
import { derived, get, Readable } from "svelte/store";
|
2021-06-25 18:14:40 +02:00
|
|
|
import { ScreenSharingLocalMedia, screenSharingLocalMedia } from "./ScreenSharingStore";
|
|
|
|
import { peerStore, screenSharingStreamStore } from "./PeerStore";
|
|
|
|
import type { RemotePeer } from "../WebRtc/SimplePeer";
|
2022-01-31 11:41:30 +01:00
|
|
|
import { highlightedEmbedScreen } from "./EmbedScreensStore";
|
2021-06-11 11:29:36 +02:00
|
|
|
|
2021-06-21 14:43:10 +02:00
|
|
|
export type Streamable = RemotePeer | ScreenSharingLocalMedia;
|
2021-06-11 11:29:36 +02:00
|
|
|
|
|
|
|
/**
|
2021-06-21 14:43:10 +02:00
|
|
|
* A store that contains everything that can produce a stream (so the peers + the local screen sharing stream)
|
2021-06-11 11:29:36 +02:00
|
|
|
*/
|
2021-06-21 14:43:10 +02:00
|
|
|
function createStreamableCollectionStore(): Readable<Map<string, Streamable>> {
|
2021-06-25 18:14:40 +02:00
|
|
|
return derived(
|
|
|
|
[screenSharingStreamStore, peerStore, screenSharingLocalMedia],
|
|
|
|
([$screenSharingStreamStore, $peerStore, $screenSharingLocalMedia], set) => {
|
|
|
|
const peers = new Map<string, Streamable>();
|
2021-06-11 11:29:36 +02:00
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
const addPeer = (peer: Streamable) => {
|
|
|
|
peers.set(peer.uniqueId, peer);
|
|
|
|
};
|
2021-06-11 11:29:36 +02:00
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
$screenSharingStreamStore.forEach(addPeer);
|
|
|
|
$peerStore.forEach(addPeer);
|
2021-06-11 11:29:36 +02:00
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
if ($screenSharingLocalMedia?.stream) {
|
|
|
|
addPeer($screenSharingLocalMedia);
|
|
|
|
}
|
2021-06-11 11:29:36 +02:00
|
|
|
|
2022-01-31 11:41:30 +01:00
|
|
|
const $highlightedEmbedScreen = get(highlightedEmbedScreen);
|
|
|
|
|
|
|
|
if ($highlightedEmbedScreen?.type === 'streamable' && !peers.has($highlightedEmbedScreen.embed.uniqueId)) {
|
|
|
|
highlightedEmbedScreen.removeHighlight();
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
set(peers);
|
2021-06-11 11:29:36 +02:00
|
|
|
}
|
2021-06-25 18:14:40 +02:00
|
|
|
);
|
2021-06-11 11:29:36 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 14:43:10 +02:00
|
|
|
export const streamableCollectionStore = createStreamableCollectionStore();
|