<commit_before>-- Canvas.Hs, control javascript canvas with Haskell
-- Copyright (C) 2013, <NAME>, <NAME>, <NAME>, <NAME>,
-- <NAME>
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
-- 
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-- Lesser General Public License for more details.
-- 
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
-- USA

{- |
    The CanvasHS module allows haskell programmers to easily create graphical programs using event diven IO.    
    Using the installEventHandler function the user can register an event handler which will process incoming 
    events and with the current state in mind, will result in a new state and output, which could be graphical,
    actions (IO, timers and others) or both.
-}
module CanvasHs
(
    installEventHandler,
    shape,
    actions
)  where

import CanvasHs.Data
import CanvasHs.Server
import CanvasHs.Launch
import CanvasHs.Protocol
import CanvasHs.Shutdown as Shutdown (addEnd) 
    -- the serverthread will call shutdown when exiting. We use this to stop the timers

import Data.IORef (IORef, newIORef, atomicModifyIORef, readIORef)
import Control.Monad.Trans (liftIO, lift)
import System.IO (readFile, writeFile)
import System.Environment (getArgs)
import Data.Maybe (catMaybes)
import Control.Concurrent.Timer as Timer
import Control.Concurrent.Suspend (msDelay)
import Control.Applicative ((<$>))
import qualified Data.Map as Map
import qualified Data.ByteString.Lazy as BSL (readFile, writeFile)
import qualified Data.ByteString.UTF8 as BU
import Control.Concurrent.MVar
import qualified Control.Monad.State.Strict as S (runStateT, StateT, get, put)

import qualified Network.WebSockets as WS

-- | type of the user handler. It accepts a state and an 'Event' and produces a tuple of the new state and an 'Output'
type Callback a = (a -> Event -> (a, Output))

-- | Our internal state, holds the user state and a refrence to the user handler
data State a =  State   {extState :: a
                        ,callback :: Callback a
                        ,timers :: Map.Map String TimerIO
                        }

                      
                        
-- | Registers an event handler and starts CanvasHs. This will start the needed servers (weboscket and http) and will open a browser window.
installEventHandler :: 
        Callback userState -- ^ event handler on current state and incoming event, that produces a tuple of the new user state and ouput to process: (userState -> Event -> (userState, Output))
    ->  userState -- ^ start state<commit_msg>CanvasHs removed unneeded import<commit_after>-- Canvas.Hs, control javascript canvas with Haskell
-- Copyright (C) 2013, <NAME>, <NAME>, <NAME>, <NAME>,
-- <NAME>
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
-- 
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-- Lesser General Public License for more details.
-- 
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
-- USA

{- |
    The CanvasHS module allows haskell programmers to easily create graphical programs using event diven IO.    
    Using the installEventHandler function the user can register an event handler which will process incoming 
    events and with the current state in mind, will result in a new state and output, which could be graphical,
    actions (IO, timers and others) or both.
-}
module CanvasHs
(
    installEventHandler,
    shape,
    actions
)  where

import CanvasHs.Data
import CanvasHs.Server
import CanvasHs.Launch
import CanvasHs.Protocol
import CanvasHs.Shutdown as Shutdown (addEnd) 
    -- the serverthread will call shutdown when exiting. We use this to stop the timers

import Control.Monad.Trans (liftIO, lift)
import System.IO (readFile, writeFile)
import System.Environment (getArgs)
import Data.Maybe (catMaybes)
import Control.Concurrent.Timer as Timer
import Control.Concurrent.Suspend (msDelay)
import Control.Applicative ((<$>))
import qualified Data.Map as Map
import qualified Data.ByteString.Lazy as BSL (readFile, writeFile)
import qualified Data.ByteString.UTF8 as BU
import Control.Concurrent.MVar
import qualified Control.Monad.State.Strict as S (runStateT, StateT, get, put)

import qualified Network.WebSockets as WS

-- | type of the user handler. It accepts a state and an 'Event' and produces a tuple of the new state and an 'Output'
type Callback a = (a -> Event -> (a, Output))

-- | Our internal state, holds the user state and a refrence to the user handler
data State a =  State   {extState :: a
                        ,callback :: Callback a
                        ,timers :: Map.Map String TimerIO
                        }

                      
                        
-- | Registers an event handler and starts CanvasHs. This will start the needed servers (weboscket and http) and will open a browser window.
installEventHandler :: 
        Callback userState -- ^ event handler on current state and incoming event, that produces a tuple of the new user state and ouput to process: (userState -> Event -> (userState, Output))
    ->  userState -- ^ start state
{'fraction_non_alphanumeric': 0.07172097592926151, 'fraction_numerical': 0.005894874733911904, 'mean_word_length': 3.2387231089521165, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, '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': '635248', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1557, 'n_words': 782}