Skip to content

WIP: prefetch query fields concurrently #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spec/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ feedTests =
ft <- toXMLFeed (_wordpress ctxt) wpfeed
ft `shouldBe` "<?xml version='1.0' ?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n <id>https://myurl.com/feed</id>\n <title type=\"text\">My Blog</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <entry>\n <id>https://myurl.com/2014/10/foo-bar/</id>\n <title type=\"html\">&lt;i&gt;Foo&lt;/i&gt; bar</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <published>2014-10-20T07:00:00Z</published>\n <summary type=\"html\">summary</summary>\n <content type=\"html\">This is the title: &lt;i&gt;Foo&lt;/i&gt; bar</content>\n <author>\n <name>Emma Goldman</name>\n </author>\n <link href=\"https://myurl.com/2014/10/foo-bar/\" title=\"&lt;i&gt;Foo&lt;/i&gt; bar\" />\n </entry>\n</feed>\n"

--larcenyFillTests :: SpecM () ()
larcenyFillTests :: Spec
larcenyFillTests = do
describe "<wpPosts>" $ do
Expand Down
34 changes: 33 additions & 1 deletion src/Web/Offset/Splices.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ wpPostByPermalinkFill extraFields getURI wpLens = maybeFillChildrenWith' $
return $ Just (postSubs wp extraFields post)
_ -> return Nothing


feedSubs :: [Field s] -> WPLens b s -> Object -> Substitutions s
feedSubs fields lens obj=
subs $ [("wpPost", wpPostFromObjectFill fields lens obj)]
Expand Down Expand Up @@ -300,6 +299,39 @@ prefetchSubs wp mkeys =
subs [ ("wpPosts", wpPostsPrefetch wp mkeys)
, ("wpPage", useAttrs (a"name") $ wpPagePrefetch mkeys) ]

postPrefetchSubs :: Wordpress b
-> MVar [WPKey]
-> [Field s]
-> Object
-> Substitutions s
postPrefetchSubs wp mkeys extraFields object =
subs (map (buildSplice object) (mergeFields postFields extraFields))
where -- run queries for Q fields
buildSplice o (Q n endpoint) =
(transformName n, customPrefetchFill wp mkeys (toEndpoint endpoint $ getText n o))
-- do nothing for other fields
buildSplice o (F n) = (transformName n, textFill "")
buildSplice o (P n _) = (transformName n, textFill "")
buildSplice o (PN n _) = (transformName n, textFill "")
buildSplice o (PM n _) = (transformName n, textFill "")
buildSplice o (N n _) = (transformName n, textFill "")
buildSplice o (C n _) = (transformName n, textFill "")
buildSplice o (CN n _ _) = (transformName n, textFill "")
buildSplice o (M n fs) = (transformName n, textFill "")

getText n o = case M.lookup n o of
Just (String t) -> t
Just (Number i) -> either (tshow :: Double -> Text)
(tshow :: Integer -> Text) (floatingOrInteger i)
_ -> ""

customPrefetchFill :: Wordpress b -> MVar [WPKey] -> Text -> Fill s
customPrefetchFill Wordpress{..} mKeys endpoint =
Fill $ \attrs (path, tpl) lib ->
do let key = EndpointKey endpoint
liftIO $ modifyMVar_ mKeys (\keys -> return $ key : keys)
return ""

wpPostsPrefetch :: Wordpress b
-> MVar [WPKey]
-> Fill s
Expand Down