partey_workadventure/front/src/Stores/StreamableCollectionStore.ts

41 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-01-05 10:27:40 +01:00
import { derived, get, Readable } from "svelte/store";
import { ScreenSharingLocalMedia, screenSharingLocalMedia } from "./ScreenSharingStore";
import { peerStore, screenSharingStreamStore } from "./PeerStore";
import type { RemotePeer } from "../WebRtc/SimplePeer";
import { highlightedEmbedScreen } from "./EmbedScreensStore";
2021-06-21 14:43:10 +02:00
export type Streamable = RemotePeer | ScreenSharingLocalMedia;
/**
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-21 14:43:10 +02:00
function createStreamableCollectionStore(): Readable<Map<string, Streamable>> {
return derived(
[screenSharingStreamStore, peerStore, screenSharingLocalMedia],
([$screenSharingStreamStore, $peerStore, $screenSharingLocalMedia], set) => {
const peers = new Map<string, Streamable>();
const addPeer = (peer: Streamable) => {
peers.set(peer.uniqueId, peer);
};
$screenSharingStreamStore.forEach(addPeer);
$peerStore.forEach(addPeer);
if ($screenSharingLocalMedia?.stream) {
addPeer($screenSharingLocalMedia);
}
const $highlightedEmbedScreen = get(highlightedEmbedScreen);
if ($highlightedEmbedScreen?.type === 'streamable' && !peers.has($highlightedEmbedScreen.embed.uniqueId)) {
highlightedEmbedScreen.removeHighlight();
}
set(peers);
}
);
}
2021-06-21 14:43:10 +02:00
export const streamableCollectionStore = createStreamableCollectionStore();