workadventure/front/src/Stores/ActionsMenuStore.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

import { writable } from "svelte/store";
2022-01-26 10:40:13 +01:00
export interface ActionsMenuData {
playerName: string;
2022-02-02 13:30:49 +01:00
actions: { actionName: string; callback: Function }[];
}
function createActionsMenuStore() {
2022-01-26 10:40:13 +01:00
const { subscribe, update, set } = writable<ActionsMenuData | undefined>(undefined);
return {
subscribe,
2022-01-26 10:40:13 +01:00
initialize: (playerName: string) => {
set({
playerName,
actions: [],
});
},
2022-01-26 10:40:13 +01:00
addAction: (actionName: string, callback: Function) => {
update((data) => {
data?.actions.push({ actionName, callback });
return data;
});
},
removeAction: (actionName: string) => {
update((data) => {
2022-02-02 13:30:49 +01:00
const actionIndex = data?.actions.findIndex((action) => action.actionName === actionName);
2022-01-26 10:40:13 +01:00
if (actionIndex !== undefined && actionIndex != -1) {
data?.actions.splice(actionIndex, 1);
}
return data;
});
},
/**
* Hides menu
*/
2022-01-26 10:40:13 +01:00
clear: () => {
set(undefined);
2022-02-02 13:30:49 +01:00
},
};
}
2022-01-27 16:50:25 +01:00
export const actionsMenuStore = createActionsMenuStore();