I've followed along with the great Svelte tutorial but I'm having trouble understanding when I should use derived in my custom stores. In this example, I'm creating a game with 3 states:
PRE_GAMEIN_GAMEPOST_GAME
I want to return a boolean check for when I'm in one of those states, and I think it would be best on the custom store itself.
This is my current code:
import { writable, derived } from 'svelte/store';
export const gamestate = (() {
const { set, subscribe } = writable('PRE_GAME');
return {
subscribe,
set
};
})();
export const preGame = derived(gamestate, ($gamestate) => $gamestate === 'PRE_GAME');
export const inGame = derived(gamestate, ($gamestate) => $gamestate === 'IN_GAME');
export const postGame = derived(gamestate, ($gamestate) => $gamestate === 'POST_GAME');
Is it possible to move the derived methods onto the gamestate store like gamestate.preGame()? Does that make sense to do in Svelte? Then I can call $gameState and get whichever value, but also return a boolean check when I need an explicit value.
I expected to be able to do check the internal value without needing to derive its value. Maybe something like below, but it always returns false because gamestate is a writable object.
export const createStore = (() {
const { set, subscribe } = writable('PRE_GAME');
return {
subscribe,
set,
preGame: () => gamestate === 'PRE_GAME',
inGame: () => gamestate === 'IN_GAME',
postGame: () => gamestate === 'POST_GAME',
};
})();
What am I misunderstanding here?