workadventure/front/src/Stores/CoWebsiteStore.ts

52 lines
1.7 KiB
TypeScript
Raw Normal View History

import { derived, writable } from "svelte/store";
2022-02-10 11:40:36 +01:00
import type { CoWebsite } from "../WebRtc/CoWebsite/CoWesbite";
2022-01-05 10:30:27 +01:00
function createCoWebsiteStore() {
const { subscribe, set, update } = writable(Array<CoWebsite>());
set(Array<CoWebsite>());
return {
subscribe,
add: (coWebsite: CoWebsite, position?: number) => {
2022-02-10 11:40:36 +01:00
coWebsite.getStateSubscriber().subscribe((value) => {
2022-01-05 10:30:27 +01:00
update((currentArray) => currentArray);
});
if (position || position === 0) {
update((currentArray) => {
if (position === 0) {
return [coWebsite, ...currentArray];
} else if (currentArray.length > position) {
const test = [...currentArray.splice(position, 0, coWebsite)];
return [...currentArray.splice(position, 0, coWebsite)];
}
return [...currentArray, coWebsite];
});
return;
}
update((currentArray) => [...currentArray, coWebsite]);
},
remove: (coWebsite: CoWebsite) => {
update((currentArray) => [
2022-02-10 11:40:36 +01:00
...currentArray.filter((currentCoWebsite) => currentCoWebsite.getId() !== coWebsite.getId()),
2022-01-05 10:30:27 +01:00
]);
},
empty: () => {
set(Array<CoWebsite>());
},
};
}
export const coWebsites = createCoWebsiteStore();
export const coWebsitesNotAsleep = derived([coWebsites], ([$coWebsites]) =>
2022-02-10 11:40:36 +01:00
$coWebsites.filter((coWebsite) => coWebsite.getState() !== "asleep")
2022-01-05 10:30:27 +01:00
);
export const mainCoWebsite = derived([coWebsites], ([$coWebsites]) =>
2022-02-10 11:40:36 +01:00
$coWebsites.find((coWebsite) => coWebsite.getState() !== "asleep")
);