git.iko.soy/rowtype-yoga/purescript-playwright
on main help sign in
Add affCall
commitec738bfa305ab769c396377504cf84e7b7c4e218 authorVladimir Kalnitsky <klntsky@gmail.com> dateSun 20 Sep 2020, 22:07 (6 years ago) parent3af9079
8 files changed+139−67
packages.dhall+38−3 src/Playwright.purs+41−30 src/Playwright/Data.purs−19 src/Playwright/Internal.js+37 src/Playwright/Internal.purs+14−1 src/Playwright/Keyboard.purs+6−12 src/Playwright/Options.purs+1 test/Main.purs+2−2
packages.dhall+38−3
@@ -1,8 +1,43 @@
11let upstream =
22 https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200911-2/packages.dhall sha256:872c06349ed9c8210be43982dc6466c2ca7c5c441129826bcb9bf3672938f16e
33
4−let overrides = {=}
4+let overrides =
5+ { untagged-union =
6+ { dependencies =
7+ [ "assert"
8+ , "console"
9+ , "effect"
10+ , "foreign"
11+ , "foreign-object"
12+ , "literals"
13+ , "maybe"
14+ , "newtype"
15+ , "proxy"
16+ , "psci-support"
17+ , "tuples"
18+ , "unsafe-coerce"
19+ ]
20+ , repo = "https://github.com/jvliwanag/purescript-untagged-union.git"
21+ , version = "edc091a1fde2cb6ac775865ee6e761b2fb99fc22"
22+ }
23+ }
524
6−let additions = {=}
25+let additions =
26+ { literals =
27+ { dependencies =
28+ [ "assert"
29+ , "effect"
30+ , "console"
31+ , "integers"
32+ , "numbers"
33+ , "partial"
34+ , "psci-support"
35+ , "unsafe-coerce"
36+ , "typelevel-prelude"
37+ ]
38+ , repo = "https://github.com/jvliwanag/purescript-literals.git"
39+ , version = "7b2ae20f77c67b7e419a92fdd0dc7a09b447b18e"
40+ }
41+ }
742
8−in upstream // overrides // additions
43+in upstream ⫽ overrides ⫽ additions
src/Playwright.purs+41−30
@@ -17,31 +17,31 @@ where
1717
1818import Prelude
1919import Effect (Effect)
20−import Control.Promise (toAffE)
20+-- import Control.Promise (toAffE)
2121import Effect.Aff (Aff)
2222import Untagged.Coercible (class Coercible, coerce)
23−import Untagged.Union (type (|+|))
23+import Untagged.Union (type (|+|), UndefinedOr)
2424import Node.Buffer (Buffer)
2525import Playwright.Data
2626import Playwright.Options
27−import Playwright.Internal (effCall, effProp)
27+import Playwright.Internal (effCall, effProp, affCall)
28+import Literals.Null (Null)
2829
2930launch
3031 :: forall o
3132 . Coercible o LaunchOptions
3233 => BrowserType -> o -> Aff Browser
3334launch bt opts =
34− toAffE $ effCall "launch" typeInfo bt (coerce opts :: LaunchOptions)
35+ affCall "launch" typeInfo bt (coerce opts :: LaunchOptions)
3536 where
36− typeInfo _ = launch :: BrowserType -> o -> Aff Browser
37+ typeInfo _ = launch
3738
3839close
3940 :: forall x
4041 . Coercible x (Browser |+| BrowserContext |+| Page)
4142 => x -> Aff Unit
4243close x =
43− toAffE $ effCall "close" typeInfo x
44− where typeInfo _ = close :: x -> Aff Unit
44+ affCall "close" (\_ -> close) x
4545
4646contexts :: Browser -> Effect (Array BrowserContext)
4747contexts =
@@ -61,7 +61,7 @@ newPage
6161 => Coercible o NewpageOptions
6262 => x -> o -> Aff Page
6363newPage sth opts =
64− toAffE $ effCall "newPage" typeInfo sth (coerce opts :: NewpageOptions)
64+ affCall "newPage" typeInfo sth opts
6565 where typeInfo _ = newPage :: x -> o -> Aff Page
6666
6767goForward
@@ -69,16 +69,16 @@ goForward
6969 . Coercible o GoOptions
7070 => Page -> o -> Aff (Null |+| Response)
7171goForward page opts =
72− toAffE $ effCall "goForward" typeInfo page (coerce opts :: GoOptions)
73− where typeInfo _ = goForward :: Page -> o -> Aff (Null |+| Response)
72+ affCall "goForward" typeInfo page (coerce opts :: GoOptions)
73+ where typeInfo _ = goForward
7474
7575goBack
7676 :: forall o
7777 . Coercible o GoOptions
7878 => Page -> o -> Aff (Null |+| Response)
7979goBack page opts =
80− toAffE $ effCall "goBack" typeInfo page (coerce opts :: GoOptions)
81− where typeInfo _ = goBack :: Page -> o -> Aff (Null |+| Response)
80+ affCall "goBack" typeInfo page (coerce opts :: GoOptions)
81+ where typeInfo _ = goBack
8282
8383goto
8484 :: forall x o
@@ -86,8 +86,23 @@ goto
8686 => Coercible o GotoOptions
8787 => x -> URL -> o -> Aff (Null |+| Response)
8888goto x url' opts =
89− toAffE $ effCall "goto" typeInfo x url' (coerce opts :: GotoOptions)
90− where typeInfo _ = goto :: x -> URL -> o -> Aff (Null |+| Response)
89+ affCall "goto" typeInfo x url' (coerce opts :: GotoOptions)
90+ where typeInfo _ = goto
91+
92+type Cookie =
93+ { name :: String
94+ , value :: String
95+ , url :: UndefinedOr String
96+ }
97+
98+addCookies
99+ :: BrowserContext
100+ -> Array Cookie
101+ -> Aff Unit
102+addCookies bc cookies =
103+ affCall "addCookies" typeInfo bc cookies
104+ where
105+ typeInfo _ = addCookies
91106
92107hover
93108 :: forall x o
@@ -95,7 +110,7 @@ hover
95110 => Coercible o HoverOptions
96111 => x -> o -> Aff Unit
97112hover sth opts =
98− toAffE $ effCall "hover" typeInfo sth (coerce opts :: HoverOptions)
113+ affCall "hover" typeInfo sth opts
99114 where typeInfo _ = hover :: x -> o -> Aff Unit
100115
101116innerHTML
@@ -104,12 +119,8 @@ innerHTML
104119 => Coercible o InnerHTMLOptions
105120 => x -> Selector -> o -> Aff String
106121innerHTML x selector o =
107− toAffE $ effCall "innerHTML" typeInfo x selector (coerce o :: InnerHTMLOptions)
122+ affCall "innerHTML" typeInfo x selector (coerce o :: InnerHTMLOptions)
108123 where typeInfo _ = innerHTML
109− :: x
110− -> Selector
111− -> o
112− -> Aff String
113124
114125innerText
115126 :: forall x o
@@ -120,7 +131,7 @@ innerText
120131 -> o
121132 -> Aff String
122133innerText x sel o =
123− toAffE $ effCall "innerText" typeInfo x sel o
134+ affCall "innerText" typeInfo x sel o
124135 where typeInfo _ = innerText
125136 :: x -> Selector -> o -> Aff String
126137
@@ -135,8 +146,8 @@ query
135146 :: forall x
136147 . Coercible x (ElementHandle |+| Page |+| Frame)
137148 => x -> Selector -> Aff ElementHandle
138−query x sel =
139− toAffE $ effCall "$" typeInfo x sel
149+query x =
150+ affCall "$" typeInfo x
140151 where typeInfo _ = query :: x -> Selector -> Aff ElementHandle
141152
142153-- | `sth.$$(selector)`
@@ -144,8 +155,8 @@ queryMany
144155 :: forall x
145156 . Coercible x (ElementHandle |+| Page |+| Frame)
146157 => x -> Selector -> Aff (Array ElementHandle)
147−queryMany sth sel =
148− toAffE $ effCall "$$" typeInfo sth sel
158+queryMany sth =
159+ affCall "$$" typeInfo sth
149160 where typeInfo _ = queryMany :: x -> Selector -> Aff (Array ElementHandle)
150161
151162screenshot
@@ -154,16 +165,16 @@ screenshot
154165 => Coercible o ScreenshotOptions
155166 => x -> o -> Aff Buffer
156167screenshot x o =
157− toAffE $ effCall "screenshot" typeInfo x (coerce o :: ScreenshotOptions)
158− where typeInfo _ = screenshot :: x -> o -> Aff Buffer
168+ affCall "screenshot" typeInfo x o
169+ where typeInfo _ = screenshot
159170
160171textContent
161172 :: forall x
162173 . Coercible x (Page |+| Frame |+| ElementHandle)
163174 => x -> Selector -> Aff (Null |+| String)
164−textContent x sel =
165− toAffE $ effCall "textContent" typeInfo x sel
166− where typeInfo _ = textContent :: x -> Selector -> Aff (Null |+| String)
175+textContent x =
176+ affCall "textContent" typeInfo x
177+ where typeInfo _ = textContent
167178
168179url
169180 :: forall x
src/Playwright/Data.purs−19
@@ -2,7 +2,6 @@ module Playwright.Data where
22
33import Prelude
44import Untagged.TypeCheck (class HasRuntimeType)
5−import Foreign (isNull)
65
76foreign import data BrowserType :: Type
87foreign import data Browser :: Type
@@ -53,21 +52,3 @@ foreign import alt :: Modifier
5352foreign import control :: Modifier
5453foreign import meta :: Modifier
5554foreign import shift :: Modifier
56−
57−-- | We need our own `null` to make it usable with `untagged-union`.
58−-- | TODO: push upstream?
59−foreign import data Null :: Type
60−
61−foreign import null :: Null
62−
63−instance eqNull :: Eq Null where
64− eq _ _ = true
65−
66−instance showNull :: Show Null where
67− show _ = "null"
68−
69−instance ordNull :: Ord Null where
70− compare _ _ = EQ
71−
72−instance hasRuntimeTypeNull :: HasRuntimeType Null where
73− hasRuntimeType _ = isNull
src/Playwright/Internal.js+37
@@ -34,12 +34,49 @@ function effectfulGetter (property, n) {
3434 };
3535}
3636
37+function affectfulGetter (property, argsCount, toAffE) {
38+ var args = [];
39+
40+ return function (object) {
41+ function effectRunner () {
42+ return object[property].apply(object, args);
43+ }
44+
45+ var affectRunner = toAffE(effectRunner);
46+
47+ function chooseNext () {
48+ return argsCount > 0 ? argsConsumer : affectRunner;
49+ }
50+
51+ function argsConsumer (arg) {
52+ if (argsCount == 0) {
53+ return affectRunner;
54+ } else {
55+ args.push(arg);
56+ argsCount--;
57+ return chooseNext();
58+ }
59+ }
60+
61+ return chooseNext();
62+ };
63+}
64+
65+
3766exports.unsafeEffCall = function (method) {
3867 return function (argsCount) {
3968 return effectfulGetter(method, argsCount);
4069 };
4170};
4271
72+exports.unsafeAffCall = function (toAffE) {
73+ return function (method) {
74+ return function (argsCount) {
75+ return affectfulGetter(method, argsCount, toAffE);
76+ };
77+ };
78+};
79+
4380exports.effProp = function (prop) {
4481 return function (object) {
4582 return function () {
src/Playwright/Internal.purs+14−1
@@ -3,12 +3,15 @@ module Playwright.Internal
33 , numberOfArgs
44 , effCall
55 , effProp
6+ , affCall
67 )
78 where
89
910import Type.Proxy
1011import Prelude
1112import Effect
13+import Effect.Aff
14+import Control.Promise
1215
1316class NumberOfArgs x where
1417 numberOfArgs :: Proxy x -> Int
@@ -57,7 +60,17 @@ foreign import unsafeEffCall
5760 -> Int
5861 -> r
5962
60−effCall :: forall f r. NumberOfArgs f => String -> (Unit -> f) -> r
63+foreign import unsafeAffCall
64+ :: forall r
65+ . (forall a. Effect (Promise a) -> Aff a)
66+ -> String
67+ -> Int
68+ -> r
69+
70+effCall :: forall f. NumberOfArgs f => String -> (Unit -> f) -> f
6171effCall p f = unsafeEffCall p (countArgs f)
6272
73+affCall :: forall f. NumberOfArgs f => String -> (Unit -> f) -> f
74+affCall prop f = unsafeAffCall toAffE prop (countArgs f)
75+
6376foreign import effProp :: forall obj r. String -> obj -> Effect r
src/Playwright/Keyboard.purs+6−12
@@ -1,7 +1,7 @@
11module Playwright.Keyboard where
22
33import Prelude
4−import Playwright.Internal (effCall)
4+import Playwright.Internal (effCall, affCall)
55import Playwright.Data (Keyboard)
66import Playwright.Options (KeyboardPressOptions)
77import Effect.Aff (Aff)
@@ -12,31 +12,25 @@ type Key = String
1212
1313down :: Keyboard -> Key -> Aff Unit
1414down kbd =
15− effCall "down" (\_ -> down) kbd >>> toAffE
15+ affCall "down" (\_ -> down) kbd
1616
1717insertText :: Keyboard -> String -> Aff Unit
1818insertText kbd =
19− effCall "insertText" (\_ -> insertText) kbd >>> toAffE
19+ affCall "insertText" (\_ -> insertText) kbd
2020
2121press
2222 :: forall o
2323 . Coercible o KeyboardPressOptions
2424 => Keyboard -> Key -> o -> Aff Unit
2525press kbd key opts =
26− toAffE $ effCall "press" typeInfo kbd key
27− (coerce opts :: KeyboardPressOptions)
28− where
29− typeInfo _ = press :: Keyboard -> Key -> o -> Aff Unit
26+ affCall "press" (\_ -> press) kbd key opts
3027
3128type'
3229 :: forall o
3330 . Coercible o KeyboardPressOptions
3431 => Keyboard -> String -> o -> Aff Unit
3532type' kbd text opts =
36− toAffE $ effCall "type" typeInfo kbd text
37− (coerce opts :: KeyboardPressOptions)
38− where
39− typeInfo _ = type' :: Keyboard -> String -> o -> Aff Unit
33+ affCall "type" (\_ -> type') kbd text opts
4034
4135up :: Keyboard -> Key -> Aff Unit
42−up kbd = effCall "up" (\_ -> up) kbd >>> toAffE
36+up kbd = affCall "up" (\_ -> up) kbd
src/Playwright/Options.purs+1
@@ -5,6 +5,7 @@ import Playwright.Data
55import Foreign.Object (Object)
66import Foreign (Foreign)
77import Untagged.Union (UndefinedOr, type (|+|))
8+import Literals.Null (Null)
89
910type Opt a = UndefinedOr a
1011
test/Main.purs+2−2
@@ -17,10 +17,10 @@ main :: Effect Unit
1717main = runTest do
1818 suite "browser" do
1919 test "launch" do
20− browser <- launch firefox {}
20+ browser <- launch chromium {}
2121 close browser
2222 test "textContent" do
23− browser <- launch firefox {}
23+ browser <- launch chromium {}
2424 page <- newPage browser {}
2525 void $ goto
2626 page