Skip to content

Commit 2ccb0b8

Browse files
committed
Some small tweaks
1 parent f74d48a commit 2ccb0b8

File tree

4 files changed

+41
-54
lines changed

4 files changed

+41
-54
lines changed

src/Halogen/VDom/DOM.purs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import Control.Monad.Eff (Eff, foreachE)
1515

1616
import Data.Function.Uncurried as Fn
1717
import Data.Maybe (Maybe(..))
18-
import Data.Tuple (Tuple(..), fst, snd)
18+
import Data.Tuple (Tuple(..), fst)
1919

2020
import DOM (DOM)
2121
import DOM.Node.Types (Element, Node, Document, elementToNode) as DOM
2222

2323
import Halogen.VDom.Machine (Step(..), Machine)
2424
import Halogen.VDom.Machine as Machine
2525
import Halogen.VDom.Types (VDom(..), ElemSpec(..), ElemName, Namespace(..), runGraft)
26-
import Halogen.VDom.Util (forE, forInE, whenE, diffWithIxE, diffWithKeyAndIxE, strMapWithIxE, refEq)
26+
import Halogen.VDom.Util (forE, forInE, diffWithIxE, diffWithKeyAndIxE, strMapWithIxE, refEq)
2727

2828
data Quaple a b c d = Quaple a b c d
2929

@@ -65,9 +65,13 @@ buildText (VDomSpec spec) = render
6565
Grafted g →
6666
Fn.runFn2 patch node s1 (runGraft g)
6767
Text s2 → do
68-
Fn.runFn2 whenE (s1 /= s2) do
69-
Fn.runFn2 setTextContent s2 node
70-
pure (Step node (Fn.runFn2 patch node s2) done)
68+
let
69+
res = Step node (Fn.runFn2 patch node s2) done
70+
case s1 == s2 of
71+
true → effPure res
72+
_ → do
73+
Fn.runFn2 setTextContent s2 node
74+
pure res
7175
vdom →
7276
buildVDom (VDomSpec spec) vdom
7377

@@ -83,14 +87,14 @@ buildElem (VDomSpec spec) = render
8387
where
8488
render es1@(ElemSpec ns1 name1 as1) ch1 = do
8589
el ← Fn.runFn3 createElem ns1 name1 spec.document
86-
attrs ← spec.buildAttributes el as1
8790
let
8891
node = DOM.elementToNode el
8992
onChild = Fn.mkFn2 \ix child → do
90-
Step n m h ← buildVDom (VDomSpec spec) child
93+
res@Step n m h ← buildVDom (VDomSpec spec) child
9194
Fn.runFn2 appendChild n node
92-
pure (Tuple m h)
95+
pure res
9396
steps ← Fn.runFn2 forE ch1 onChild
97+
attrs ← spec.buildAttributes el as1
9498
pure
9599
(Step node
96100
(Fn.runFn4 patch node attrs es1 steps)
@@ -100,23 +104,25 @@ buildElem (VDomSpec spec) = render
100104
Grafted g →
101105
Fn.runFn4 patch node attrs es1 ch1 (runGraft g)
102106
Elem es2@(ElemSpec ns2 name2 as2) ch2 | Fn.runFn2 eqElemSpec es1 es2 → do
103-
attrs' ← Machine.step attrs as2
104107
let
105-
onThese = Fn.mkFn3 \ix (Tuple step halt) vdom → do
106-
Step n' m' h' ← step vdom
108+
onThese = Fn.mkFn3 \ix (Step _ step halt) vdom → do
109+
res@Step n' m' h' ← step vdom
107110
n ← Fn.runFn2 unsafeChildIx ix node
108-
Fn.runFn2 whenE (not (Fn.runFn2 refEq n' n)) do
109-
halt
110-
Fn.runFn3 replaceChild n' n node
111-
pure (Tuple m' h')
112-
onThis = Fn.mkFn2 \ix (Tuple _ halt) → do
111+
case Fn.runFn2 refEq n' n of
112+
true → pure res
113+
_ → do
114+
halt
115+
Fn.runFn3 replaceChild n' n node
116+
pure res
117+
onThis = Fn.mkFn2 \ix (Step _ _ halt) → do
113118
halt
114119
removeLastChild node
115120
onThat = Fn.mkFn2 \ix vdom → do
116-
Step n m h ← buildVDom (VDomSpec spec) vdom
121+
res@Step n m h ← buildVDom (VDomSpec spec) vdom
117122
Fn.runFn2 appendChild n node
118-
pure (Tuple m h)
123+
pure res
119124
steps ← Fn.runFn5 diffWithIxE ch1 ch2 onThese onThis onThat
125+
attrs' ← Machine.step attrs as2
120126
pure
121127
(Step node
122128
(Fn.runFn4 patch node attrs' es2 steps)
@@ -125,7 +131,7 @@ buildElem (VDomSpec spec) = render
125131
buildVDom (VDomSpec spec) vdom
126132

127133
done = Fn.mkFn2 \attrs steps → do
128-
foreachE steps snd
134+
foreachE steps Machine.halt
129135
Machine.halt attrs
130136

131137
buildKeyed
@@ -162,10 +168,14 @@ buildKeyed (VDomSpec spec) = render
162168
then do
163169
Step n' m' h' ← step vdom
164170
n ← Fn.runFn2 unsafeChildIx ix node
165-
Fn.runFn2 whenE (not (Fn.runFn2 refEq n' n)) do
166-
halt
167-
Fn.runFn3 replaceChild n' n node
168-
pure (Quaple k ix m' h')
171+
let
172+
res = Quaple k ix m' h'
173+
case Fn.runFn2 refEq n' n of
174+
true → pure res
175+
_ → do
176+
halt
177+
Fn.runFn3 replaceChild n' n node
178+
pure res
169179
else do
170180
Step n' m' h' ← step vdom
171181
Fn.runFn3 unsafeInsertChildIx ix n' node
@@ -229,6 +239,9 @@ eqElemSpec = Fn.mkFn2 \a b →
229239
_, _ → false
230240
_, _ → false
231241

242+
effPure eff a. a Eff eff a
243+
effPure = pure
244+
232245
foreign import createTextNode
233246
eff
234247
. Fn.Fn2 String DOM.Document (Eff (dom DOM | eff) DOM.Node)

src/Halogen/VDom/DOM/Prop.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,20 @@ buildProp emit el = render
140140
_ →
141141
effPure v2
142142
Property _ val1, Property prop2 val2 →
143-
case Fn.runFn2 refEq val1 val2, prop2 == "value" of
143+
case Fn.runFn2 refEq val1 val2, prop2 of
144144
true, _ →
145145
effPure v2
146-
_, falsedo
147-
Fn.runFn3 setProperty prop2 val2 el
148-
pure v2
149-
_, _ → do
146+
_, "value"do
150147
elVal ← Fn.runFn2 getProperty "value" el
151148
case not (Fn.runFn2 refEq elVal val2) of
152149
truedo
153150
Fn.runFn3 setProperty prop2 val2 el
154151
pure v2
155152
_ →
156153
pure v2
154+
_, _ → do
155+
Fn.runFn3 setProperty prop2 val2 el
156+
pure v2
157157
Handler _ _, Handler (DOM.EventType ty) f → do
158158
let
159159
handler = Fn.runFn2 unsafeLookup ty prevEvents

src/Halogen/VDom/Util.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ exports.forInE = function (o, f) {
2020
};
2121
};
2222

23-
exports.replicateE = function (n, f) {
24-
return function () {
25-
for (var i = 0; i < n; i++) {
26-
f();
27-
}
28-
};
29-
};
30-
31-
exports.whenE = function (b, e) {
32-
return function () {
33-
if (b) {
34-
e();
35-
}
36-
};
37-
};
38-
3923
exports.diffWithIxE = function (a1, a2, f1, f2, f3) {
4024
return function () {
4125
var a3 = [];

src/Halogen/VDom/Util.purs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module Halogen.VDom.Util
22
( forE
33
, forInE
4-
, replicateE
5-
, whenE
64
, diffWithIxE
75
, diffWithKeyAndIxE
86
, strMapWithIxE
@@ -28,14 +26,6 @@ foreign import forInE
2826
(Fn.Fn2 String a (Eff eff Unit))
2927
(Eff eff Unit)
3028

31-
foreign import replicateE
32-
eff a
33-
. Fn.Fn2 Int (Eff eff a) (Eff eff Unit)
34-
35-
foreign import whenE
36-
eff a
37-
. Fn.Fn2 Boolean (Eff eff a) (Eff eff Unit)
38-
3929
foreign import diffWithIxE
4030
eff b c d
4131
. Fn.Fn5

0 commit comments

Comments
 (0)