2021-06-25 18:14:40 +02:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
import type { RemotePeer, SimplePeer } from "../WebRtc/SimplePeer";
|
|
|
|
import { VideoPeer } from "../WebRtc/VideoPeer";
|
|
|
|
import { ScreenSharingPeer } from "../WebRtc/ScreenSharingPeer";
|
|
|
|
import type { Streamable } from "./StreamableCollectionStore";
|
2021-06-15 14:45:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A store that contains the peer / media that has currently the "importance" focus.
|
|
|
|
*/
|
|
|
|
function createVideoFocusStore() {
|
2021-06-21 14:43:10 +02:00
|
|
|
const { subscribe, set, update } = writable<Streamable | null>(null);
|
2021-06-15 14:45:01 +02:00
|
|
|
|
2021-06-21 14:43:10 +02:00
|
|
|
let focusedMedia: Streamable | null = null;
|
2021-06-15 14:45:01 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
2021-06-21 14:43:10 +02:00
|
|
|
focus: (media: Streamable) => {
|
2021-06-15 14:45:01 +02:00
|
|
|
focusedMedia = media;
|
|
|
|
set(media);
|
|
|
|
},
|
|
|
|
removeFocus: () => {
|
|
|
|
focusedMedia = null;
|
|
|
|
set(null);
|
|
|
|
},
|
2021-06-21 14:43:10 +02:00
|
|
|
toggleFocus: (media: Streamable) => {
|
2021-06-15 14:45:01 +02:00
|
|
|
if (media !== focusedMedia) {
|
|
|
|
focusedMedia = media;
|
|
|
|
} else {
|
|
|
|
focusedMedia = null;
|
|
|
|
}
|
|
|
|
set(focusedMedia);
|
|
|
|
},
|
|
|
|
connectToSimplePeer: (simplePeer: SimplePeer) => {
|
|
|
|
simplePeer.registerPeerConnectionListener({
|
2021-06-25 18:14:40 +02:00
|
|
|
onConnect(peer: RemotePeer) {},
|
2021-06-15 14:45:01 +02:00
|
|
|
onDisconnect(userId: number) {
|
2021-06-25 18:14:40 +02:00
|
|
|
if (
|
|
|
|
(focusedMedia instanceof VideoPeer || focusedMedia instanceof ScreenSharingPeer) &&
|
|
|
|
focusedMedia.userId === userId
|
|
|
|
) {
|
2021-06-15 14:45:01 +02:00
|
|
|
set(null);
|
|
|
|
}
|
2021-06-25 18:14:40 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2021-06-15 14:45:01 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const videoFocusStore = createVideoFocusStore();
|