<commit_before>module Storage where

import Control.Monad.Eff
import DOM
import Data.Maybe

import Types

-- | Retrieve the current game state from local storage (FFI, needs 'Just' and 'Nothing' as input)
foreign import helpLoadGameState """
    function helpLoadGameState(just) {
        return function(nothing) {
            return function() {
                var data = localStorage.getItem('gameState');
                if (!data) {
                    return nothing;
                }
                return just(JSON.parse(data));
            };
        };
    } """ :: forall a eff. (a -> Maybe a) -> (Maybe a) -> Eff (dom :: DOM | eff) (Maybe GameState)

-- | Retrieve game state from local storage
loadGameState :: forall eff. Eff (dom :: DOM | eff) (Maybe GameState)
loadGameState = helpLoadGameState Just Nothing

-- | Store a game state in local storage
foreign import saveGameState """
    function saveGameState(gs) {
        return function() {
            localStorage.setItem('gameState', JSON.stringify(gs));
            return {};
        };
    } """ :: forall eff. GameState -> Eff (dom :: DOM | eff) Unit<commit_msg>localStorage is not a part of the DOM, use a separate Storage effect type<commit_after>module Storage where

import Control.Monad.Eff
import Data.Maybe

import Types

foreign import data Storage :: !

-- | Retrieve the current game state from local storage (FFI, needs 'Just' and 'Nothing' as input)
foreign import helpLoadGameState """
    function helpLoadGameState(just) {
        return function(nothing) {
            return function() {
                var data = localStorage.getItem('gameState');
                if (!data) {
                    return nothing;
                }
                return just(JSON.parse(data));
            };
        };
    } """ :: forall a eff. (a -> Maybe a) -> (Maybe a) -> Eff (storage :: Storage | eff) (Maybe GameState)

-- | Retrieve game state from local storage
loadGameState :: forall eff. Eff (storage :: Storage | eff) (Maybe GameState)
loadGameState = helpLoadGameState Just Nothing

-- | Store a game state in local storage
foreign import saveGameState """
    function saveGameState(gs) {
        return function() {
            localStorage.setItem('gameState', JSON.stringify(gs));
            return {};
        };
    } """ :: forall eff. GameState -> Eff (storage :: Storage | eff) Unit
{'fraction_non_alphanumeric': 0.11050648807032232, 'fraction_numerical': 0.0, 'mean_word_length': 2.3804809052333806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5127684', 'n_tokens_mistral': 632, 'n_tokens_neox': 616, 'n_words': 218}