Shpadoinkle-console-0.0.1.1: Support for the native browser console

Safe HaskellNone
LanguageHaskell2010

Shpadoinkle.Console

Contents

Description

This module exposes the browser's native console logging and debugging features, including underutilized features such as time measurement, table displays, and assertions.

Synopsis

Classes

class LogJS (c :: Type -> Constraint) where Source #

LogJS is the base class for logging to the browser console. Browser consoles contain rich tooling for exploring JavaScript objects, DOM nodes, and much more. To take advantage of these native features, we need to choose how we are going to log. The LogJS class is intended to be used in conjunction with TypeApplications.

  data Person = Person { first :: String, last :: String, age :: Int } deriving (Generic, ToJSON)
  main = logJS @ToJSON "log" $ Person "bob" "saget" 45
  

is effectively equivalent to:

  console.log({first: "bob", last: "saget", age: 45})
  

in that the console will render with nice expand/collapse object exploration features.

Methods

logJS :: c a => Text -> a -> JSM () Source #

Instances
LogJS Show Source #

Logs against Show will be converted to a String before being sent to the console.

Instance details

Defined in Shpadoinkle.Console

Methods

logJS :: Show a => Text -> a -> JSM () Source #

LogJS ToJSON Source #

Logs against ToJSON will be encoded via Aeson then parsed using native JSON.parse before being sent to the console.

Instance details

Defined in Shpadoinkle.Console

Methods

logJS :: ToJSON a => Text -> a -> JSM () Source #

LogJS ToJSVal Source #

Logs against ToJSVal will be converted to a JSVal before being sent to the console.

Instance details

Defined in Shpadoinkle.Console

Methods

logJS :: ToJSVal a => Text -> a -> JSM () Source #

class Assert (c :: Type -> Constraint) where Source #

Assert is a class for assertion programming. It behaves the same as LogJS but calls console.assert instead of other console methods. This will only have an effect if the Bool provided to assert is False.

Methods

assert :: c a => Bool -> a -> JSM () Source #

Instances
Assert Show Source # 
Instance details

Defined in Shpadoinkle.Console

Methods

assert :: Show a => Bool -> a -> JSM () Source #

Assert ToJSON Source # 
Instance details

Defined in Shpadoinkle.Console

Methods

assert :: ToJSON a => Bool -> a -> JSM () Source #

Assert ToJSVal Source # 
Instance details

Defined in Shpadoinkle.Console

Methods

assert :: ToJSVal a => Bool -> a -> JSM () Source #

class LogJS c => Trapper c where Source #

Trapper is a class intended for continuous logging of your application and the catching of helpless animals. Usage is along the lines of trace where the effect of logging is implicit. To make this work in both GHC and GHCjs contexts, you do need to pass the JSContextRef in manually (askJSM re-exported here for convenience).

 main :: IO ()
 main = runJSorWarp 8080 $ do
   ctx <- askJSM
   simple runParDiff initial (view . trapper @ToJSON ctx) getBody
 

Minimal complete definition

Nothing

Methods

trapper :: c a => JSContextRef -> a -> a Source #

Instances
Trapper Show Source # 
Instance details

Defined in Shpadoinkle.Console

Methods

trapper :: Show a => JSContextRef -> a -> a Source #

Trapper ToJSON Source # 
Instance details

Defined in Shpadoinkle.Console

Methods

trapper :: ToJSON a => JSContextRef -> a -> a Source #

Trapper ToJSVal Source # 
Instance details

Defined in Shpadoinkle.Console

Methods

trapper :: ToJSVal a => JSContextRef -> a -> a Source #

askJSM :: MonadJSM m => m JSContextRef #

Gets the JavaScript context from the monad

Native methods

Log levels

log :: forall c a. LogJS c => c a => a -> JSM () Source #

Log to the console using console.log

debug :: forall c a. LogJS c => c a => a -> JSM () Source #

Log with the "debug" log level using console.debug

info :: forall c a. LogJS c => c a => a -> JSM () Source #

Log with the "info" log level using console.info

warn :: forall c a. LogJS c => c a => a -> JSM () Source #

Log with the "warn" log level using console.warn

Fancy display

table :: ToJSON a => [a] -> JSM () Source #

Log a list of JSON objects to the console where it will rendered as a table using console.table

Time Measurement

newtype TimeLabel Source #

A unique label for a timer. This is used to tie calls to console.time to console.timeEnd

Constructors

TimeLabel 

Fields

time :: TimeLabel -> JSM () Source #

Start a timer using console.time

timeEnd :: TimeLabel -> JSM () Source #

End a timer and print the milliseconds elapsed since it started using console.timeEnd