workadventure/front/src/Stores/LayoutStore.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";
export type DisplayableMedia = RemotePeer | ScreenSharingLocalMedia;
2021-06-15 17:30:28 +02:00
export const layoutModeStore = writable<LayoutMode>(LayoutMode.Presentation);
/**
* A store that contains the layout of the streams
*/
2021-06-15 17:30:28 +02:00
function createLayoutStore(): Readable<Map<string, DisplayableMedia>> {
return derived([
screenSharingStreamStore,
peerStore,
screenSharingLocalMedia,
], ([
$screenSharingStreamStore,
$peerStore,
$screenSharingLocalMedia,
], set) => {
const peers = new Map<string, DisplayableMedia>();
const addPeer = (peer: DisplayableMedia) => {
peers.set(peer.uniqueId, peer);
};
$screenSharingStreamStore.forEach(addPeer);
$peerStore.forEach(addPeer);
if ($screenSharingLocalMedia?.stream) {
addPeer($screenSharingLocalMedia);
}
set(peers);
});
}
export const layoutStore = createLayoutStore();