partey_workadventure/front/src/Stores/StreamableCollectionStore.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-06-15 17:30:28 +02:00
import {derived, get, Readable, writable} from "svelte/store";
import {ScreenSharingLocalMedia, screenSharingLocalMedia} from "./ScreenSharingStore";
import { peerStore, screenSharingStreamStore} from "./PeerStore";
import type {RemotePeer} from "../WebRtc/SimplePeer";
2021-06-15 17:30:28 +02:00
import {LayoutMode} from "../WebRtc/LayoutManager";
2021-06-21 14:43:10 +02:00
export type Streamable = RemotePeer | ScreenSharingLocalMedia;
2021-06-15 17:30:28 +02:00
export const layoutModeStore = writable<LayoutMode>(LayoutMode.Presentation);
/**
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) => {
2021-06-21 14:43:10 +02:00
const peers = new Map<string, Streamable>();
2021-06-21 14:43:10 +02:00
const addPeer = (peer: Streamable) => {
peers.set(peer.uniqueId, peer);
};
$screenSharingStreamStore.forEach(addPeer);
$peerStore.forEach(addPeer);
if ($screenSharingLocalMedia?.stream) {
addPeer($screenSharingLocalMedia);
}
set(peers);
});
}
2021-06-21 14:43:10 +02:00
export const streamableCollectionStore = createStreamableCollectionStore();