diff options
author | Lia Lenckowski <lialenck@protonmail.com> | 2023-08-24 14:00:11 +0200 |
---|---|---|
committer | Lia Lenckowski <lialenck@protonmail.com> | 2023-08-24 14:00:11 +0200 |
commit | 1fedb7f1e73bc56002181775a546e9c2407a8e40 (patch) | |
tree | 2955ab53ef02a334935ad67c2c2bc9e46f53643f | |
parent | 33bc8bfb247f4fcbfc206bdeb9c0e118e961ba59 (diff) | |
download | fastbangs-1fedb7f1e73bc56002181775a546e9c2407a8e40.tar fastbangs-1fedb7f1e73bc56002181775a546e9c2407a8e40.tar.bz2 fastbangs-1fedb7f1e73bc56002181775a546e9c2407a8e40.tar.zst |
add toEncoding implementations (~15% encoding speedup)
-rw-r--r-- | src/BangState.hs | 2 | ||||
-rw-r--r-- | src/Data/Bang.hs | 6 | ||||
-rw-r--r-- | src/Main.hs | 5 |
3 files changed, 10 insertions, 3 deletions
diff --git a/src/BangState.hs b/src/BangState.hs index 328a57e..fc3afab 100644 --- a/src/BangState.hs +++ b/src/BangState.hs @@ -70,7 +70,7 @@ loadOwnBangs = eitherDecodeFileStrict "bangs.json" <|> return (Left "") >>= \cas initBangState :: IO BangState initBangState = do -- TODO error handling for ddg bang polling, as well as regular polling - ans <- (httpJSON "https://duckduckgo.com/bang.v260.js") :: IO (Response DDGBangs) + ans <- httpJSON "https://duckduckgo.com/bang.v260.js" :: IO (Response DDGBangs) s <- BangState <$> (loadOwnBangs >>= newTVarIO) <*> newTVarIO (toBangs $ getResponseBody ans) diff --git a/src/Data/Bang.hs b/src/Data/Bang.hs index d6c3bd3..42a5d06 100644 --- a/src/Data/Bang.hs +++ b/src/Data/Bang.hs @@ -30,7 +30,11 @@ instance ToJSON Bangs where $ (\(url, name) -> object ["url" .= url, "name" .= name]) <$> m - -- TODO toEncoding. semi important; makes startup/updates faster + -- this could be optimized by folding over 'm' to create a bytestring builder, + -- but I don't think the complexity/unsafety is worth the small performance gain + toEncoding (Bangs m) = toEncoding + $ (\(url, name) -> object ["url" .= url, "name" .= name]) + <$> m newtype DDGBangs = DDGBangs (M.Map Text (Text, Text)) deriving (Show) toBangs :: DDGBangs -> Bangs diff --git a/src/Main.hs b/src/Main.hs index 0f0146c..626983c 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -42,7 +42,10 @@ instance ToJSON PendingBang where Nothing -> [] Just e -> ["email" .= e] - -- TODO toEncoding. Not sure how much value that holds though + toEncoding (PendingBang b u dp em) = pairs $ case em of + Nothing -> withoutEmail + Just e -> withoutEmail <> "email" .= e + where withoutEmail = "bang" .= b <> "url" .= u <> "name" .= dp instance FromJSON PendingBang where parseJSON = withObject "PendingBang" $ \ob -> |