2021-05-18 16:38:56 +02:00
|
|
|
import { derived, writable, Writable } from "svelte/store";
|
|
|
|
import type {UserSimplePeerInterface} from "../WebRtc/SimplePeer";
|
|
|
|
import type {SimplePeer} from "../WebRtc/SimplePeer";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A store that contains the camera state requested by the user (on or off).
|
|
|
|
*/
|
|
|
|
function createPeerStore() {
|
|
|
|
let users = new Map<number, UserSimplePeerInterface>();
|
|
|
|
|
|
|
|
const { subscribe, set, update } = writable(users);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
connectToSimplePeer: (simplePeer: SimplePeer) => {
|
|
|
|
users = new Map<number, UserSimplePeerInterface>();
|
|
|
|
set(users);
|
|
|
|
simplePeer.registerPeerConnectionListener({
|
|
|
|
onConnect(user: UserSimplePeerInterface) {
|
2021-05-19 11:17:43 +02:00
|
|
|
update(users => {
|
|
|
|
users.set(user.userId, user);
|
|
|
|
return users;
|
|
|
|
});
|
2021-05-18 16:38:56 +02:00
|
|
|
},
|
|
|
|
onDisconnect(userId: number) {
|
2021-05-19 11:17:43 +02:00
|
|
|
update(users => {
|
|
|
|
users.delete(userId);
|
|
|
|
return users;
|
|
|
|
});
|
2021-05-18 16:38:56 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const peerStore = createPeerStore();
|