<commit_before>{-# LANGUAGE OverloadedStrings #-}
module System.Tianbar.Callbacks (
    callback,
    callbacks,
    Callbacks,
) where

import Data.Aeson

import GI.Gio.Objects.Cancellable

import GI.WebKit2.Objects.WebView

import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as E

newtype Callbacks = Callbacks WebView

callbacks :: WebView -> Callbacks
callbacks = Callbacks

callback :: (ToJSON i, ToJSON p) => Callbacks -> i -> p -> IO ()
callback (Callbacks wk) index param =
    webViewRunJavascript wk
        (T.pack $ callbackScript index param)
        noCancellable
        Nothing

callbackScript :: (ToJSON i, ToJSON p) => i -> p -> String
callbackScript index param =
    "window.tianbarEvents && " ++ eventStr ++ " && "
        ++ eventStr ++ ".fire.apply(" ++ eventStr ++ ", " ++ paramStr ++ ")"
    where eventStr = "window.tianbarEvents[" ++ indexStr ++ "]"
          indexStr = showJSON index
          paramStr = showJSON param

showJSON :: ToJSON a => a -> String
showJSON = TL.unpack . E.decodeUtf8 . encode . toJSON<commit_msg>Remove string conversions in Callbacks<commit_after>{-# LANGUAGE OverloadedStrings #-}
module System.Tianbar.Callbacks (
    callback,
    callbacks,
    Callbacks,
) where

import Data.Aeson
import Data.Monoid

import GI.Gio.Objects.Cancellable

import GI.WebKit2.Objects.WebView

import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as E

newtype Callbacks = Callbacks WebView

callbacks :: WebView -> Callbacks
callbacks = Callbacks

callback :: (ToJSON i, ToJSON p) => Callbacks -> i -> p -> IO ()
callback (Callbacks wk) index param =
    webViewRunJavascript wk
        (TL.toStrict $ callbackScript index param)
        noCancellable
        Nothing

callbackScript :: (ToJSON i, ToJSON p) => i -> p -> TL.Text
callbackScript index param =
    "window.tianbarEvents && " <> eventStr <> " && "
        <> eventStr <> ".fire.apply(" <> eventStr <> ", " <> paramStr <> ")"
    where eventStr = "window.tianbarEvents[" <> indexStr <> "]"
          indexStr = showJSON index
          paramStr = showJSON param

showJSON :: ToJSON a => a -> TL.Text
showJSON = E.decodeUtf8 . encode
{'fraction_non_alphanumeric': 0.11756756756756757, 'fraction_numerical': 0.0018018018018018018, 'mean_word_length': 3.2144212523719164, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '1341017', 'n_tokens_mistral': 703, 'n_tokens_neox': 677, 'n_words': 194}