Refactoring layoutManagerActionStore. It now supports an uuid field.

layoutManagerVisibilityStore is now a derived field from layoutManagerActionStore.
This commit is contained in:
David Négrier 2021-08-05 11:19:28 +02:00
parent 92fee33b64
commit 87e4367455
4 changed files with 42 additions and 53 deletions

View file

@ -1,15 +1,14 @@
import { writable } from "svelte/store";
import { derived, writable } from "svelte/store";
import type { UserInputManager } from "../Phaser/UserInput/UserInputManager";
export interface LayoutManagerAction {
type: string;
uuid: string;
type: "warning" | "message";
message: string | number | boolean | undefined;
callback: () => void;
userInputManager: UserInputManager | undefined;
}
export const layoutManagerVisibilityStore = writable(false);
function createLayoutManagerAction() {
const { subscribe, set, update } = writable<LayoutManagerAction[]>([]);
@ -18,26 +17,26 @@ function createLayoutManagerAction() {
addAction: (newAction: LayoutManagerAction): void => {
update((list: LayoutManagerAction[]) => {
let found = false;
for (const actions of list) {
if (actions.type === newAction.type && actions.message === newAction.message) {
for (const action of list) {
if (action.uuid === newAction.uuid) {
found = true;
}
}
if (!found) {
list.push(newAction);
newAction.userInputManager?.addSpaceEventListner(newAction.callback);
}
return list;
});
},
removeAction: (oldAction: LayoutManagerAction): void => {
removeAction: (uuid: string): void => {
update((list: LayoutManagerAction[]) => {
const index = list.findIndex(
(actions) => actions.type === oldAction.type && actions.message === oldAction.message
);
const index = list.findIndex((action) => action.uuid === uuid);
if (index !== -1) {
list[index].userInputManager?.removeSpaceEventListner(list[index].callback);
list.splice(index, 1);
}
@ -51,3 +50,7 @@ function createLayoutManagerAction() {
}
export const layoutManagerActionStore = createLayoutManagerAction();
export const layoutManagerVisibilityStore = derived(layoutManagerActionStore, ($layoutManagerActionStore) => {
return !!$layoutManagerActionStore.length;
});