Skip to content

Commit ddd9ed1

Browse files
committed
Let's try and get removal right
1 parent d11363f commit ddd9ed1

File tree

4 files changed

+35
-55
lines changed

4 files changed

+35
-55
lines changed

src/Halogen/VDom/DOM.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ exports.createElementNS = function (ns, name, doc) {
2424
};
2525
};
2626

27+
exports.tryRemoveChild = function (a, b) {
28+
return function () {
29+
try {
30+
b.removeChild(a);
31+
} catch (e) {
32+
}
33+
};
34+
};
35+
2736
exports.removeLastChild = function (a) {
2837
return function () {
2938
a.removeChild(a.lastChild);
@@ -41,9 +50,3 @@ exports.unsafeChildIx = function (i, d) {
4150
return d.childNodes.item(i);
4251
};
4352
};
44-
45-
exports.nodeLength = function (a) {
46-
return function () {
47-
return a.childNodes.length | 0;
48-
};
49-
};

src/Halogen/VDom/DOM.purs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module Halogen.VDom.DOM
1313
import Prelude
1414
import Control.Monad.Eff (Eff, foreachE)
1515

16-
import Data.Array as Array
1716
import Data.Function.Uncurried as Fn
1817
import Data.Maybe (Maybe(..))
1918
import Data.Tuple (Tuple(..), fst)
@@ -24,9 +23,7 @@ import DOM.Node.Types (Element, Node, Document, elementToNode) as DOM
2423
import Halogen.VDom.Machine (Step(..), Machine)
2524
import Halogen.VDom.Machine as Machine
2625
import Halogen.VDom.Types (VDom(..), ElemSpec(..), ElemName, Namespace(..), runGraft)
27-
import Halogen.VDom.Util (forE, forInE, replicateE, diffWithIxE, diffWithKeyAndIxE, strMapWithIxE, refEq)
28-
29-
data Quaple a b c d = Quaple a b c d
26+
import Halogen.VDom.Util (forE, forInE, diffWithIxE, diffWithKeyAndIxE, strMapWithIxE, refEq)
3027

3128
type VDomMachine eff a b = Machine (Eff eff) a b
3229

@@ -106,24 +103,23 @@ buildElem (VDomSpec spec) = render
106103
Fn.runFn4 patch node attrs es1 ch1 (runGraft g)
107104
Elem es2@(ElemSpec ns2 name2 as2) ch2 | Fn.runFn2 eqElemSpec es1 es2 → do
108105
let
109-
onThese = Fn.mkFn3 \ix (Step _ step halt) vdom → do
106+
onThese = Fn.mkFn3 \ix (Step n step halt) vdom → do
110107
res@Step n' m' h' ← step vdom
111-
n ← Fn.runFn2 unsafeChildIx ix node
112108
case Fn.runFn2 refEq n' n of
113109
true → pure res
114110
_ → do
115111
halt
112+
Fn.runFn2 tryRemoveChild n node
116113
Fn.runFn3 insertChildIx ix n' node
117114
pure res
118-
onThis = Fn.mkFn2 \ix (Step _ _ halt) → do
115+
onThis = Fn.mkFn2 \ix (Step n _ halt) → do
119116
halt
117+
Fn.runFn2 tryRemoveChild n node
120118
onThat = Fn.mkFn2 \ix vdom → do
121119
res@Step n m h ← buildVDom (VDomSpec spec) vdom
122120
Fn.runFn3 insertChildIx ix n node
123121
pure res
124122
steps ← Fn.runFn5 diffWithIxE ch1 ch2 onThese onThis onThat
125-
len ← nodeLength node
126-
Fn.runFn2 replicateE (len - Array.length ch2) (removeLastChild node)
127123
attrs' ← Machine.step attrs as2
128124
pure
129125
(Step node
@@ -149,9 +145,9 @@ buildKeyed (VDomSpec spec) = render
149145
let
150146
node = DOM.elementToNode el
151147
onChild = Fn.mkFn3 \k ix (Tuple _ vdom) → do
152-
Step n m h ← buildVDom (VDomSpec spec) vdom
148+
res@Step n m h ← buildVDom (VDomSpec spec) vdom
153149
Fn.runFn3 insertChildIx ix n node
154-
pure (Quaple k ix m h)
150+
pure (Tuple ix res)
155151
steps ← Fn.runFn3 strMapWithIxE ch1 fst onChild
156152
attrs ← spec.buildAttributes el as1
157153
pure
@@ -164,32 +160,29 @@ buildKeyed (VDomSpec spec) = render
164160
Fn.runFn4 patch node attrs es1 ch1 (runGraft g)
165161
Keyed es2@(ElemSpec ns2 name2 as2) ch2 | Fn.runFn2 eqElemSpec es1 es2 → do
166162
let
167-
onThese = Fn.mkFn4 \k ix (Quaple _ ix' step halt) (Tuple _ vdom) →
163+
onThese = Fn.mkFn4 \k ix' (Tuple ix (Step n step halt)) (Tuple _ vdom) →
168164
if ix == ix'
169165
then do
170-
Step n' m' h' ← step vdom
171-
n ← Fn.runFn2 unsafeChildIx ix node
172-
let
173-
res = Quaple k ix m' h'
174-
case Fn.runFn2 refEq n' n of
175-
true → pure res
166+
res@Step n' m' h' ← step vdom
167+
case Fn.runFn2 refEq n n' of
168+
true → pure (Tuple ix' res)
176169
_ → do
177170
halt
178-
Fn.runFn3 insertChildIx ix n' node
179-
pure res
171+
Fn.runFn2 tryRemoveChild n node
172+
Fn.runFn3 insertChildIx ix' n' node
173+
pure (Tuple ix' res)
180174
else do
181-
Step n' m' h' ← step vdom
182-
Fn.runFn3 insertChildIx ix n' node
183-
pure (Quaple k ix m' h')
184-
onThis = Fn.mkFn2 \k (Quaple _ _ _ halt) → do
175+
res@Step n' m' h' ← step vdom
176+
Fn.runFn3 insertChildIx ix' n' node
177+
pure (Tuple ix' res)
178+
onThis = Fn.mkFn2 \k (Tuple _ (Step n _ halt)) → do
185179
halt
180+
Fn.runFn2 tryRemoveChild n node
186181
onThat = Fn.mkFn3 \k ix (Tuple _ vdom) → do
187-
Step n' m' h' ← buildVDom (VDomSpec spec) vdom
182+
res@Step n' m' h' ← buildVDom (VDomSpec spec) vdom
188183
Fn.runFn3 insertChildIx ix n' node
189-
pure (Quaple k ix m' h')
184+
pure (Tuple ix res)
190185
steps ← Fn.runFn6 diffWithKeyAndIxE ch1 ch2 fst onThese onThis onThat
191-
len ← nodeLength node
192-
Fn.runFn2 replicateE (len - Array.length ch2) (removeLastChild node)
193186
attrs' ← Machine.step attrs as2
194187
pure
195188
(Step node
@@ -199,7 +192,7 @@ buildKeyed (VDomSpec spec) = render
199192
buildVDom (VDomSpec spec) vdom
200193

201194
done = Fn.mkFn2 \attrs steps → do
202-
Fn.runFn2 forInE steps (Fn.mkFn2 \_ (Quaple _ _ _ halt) → halt)
195+
Fn.runFn2 forInE steps (Fn.mkFn2 \_ (Tuple _ (Step _ _ halt)) → halt)
203196
Machine.halt attrs
204197

205198
buildWidget
@@ -261,6 +254,10 @@ foreign import createElementNS
261254
eff
262255
. Fn.Fn3 Namespace ElemName DOM.Document (Eff (dom DOM | eff) DOM.Element)
263256

257+
foreign import tryRemoveChild
258+
eff
259+
. Fn.Fn2 DOM.Node DOM.Node (Eff (dom DOM | eff) Unit)
260+
264261
foreign import removeLastChild
265262
eff
266263
. DOM.Node (Eff (dom DOM | eff) Unit)
@@ -272,7 +269,3 @@ foreign import insertChildIx
272269
foreign import unsafeChildIx
273270
eff
274271
. Fn.Fn2 Int DOM.Node (Eff (dom DOM | eff) DOM.Node)
275-
276-
foreign import nodeLength
277-
eff
278-
. DOM.Node (Eff (dom DOM | eff) Int)

src/Halogen/VDom/Util.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +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-
3123
exports.diffWithIxE = function (a1, a2, f1, f2, f3) {
3224
return function () {
3325
var a3 = [];

src/Halogen/VDom/Util.purs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Halogen.VDom.Util
22
( forE
33
, forInE
4-
, replicateE
54
, diffWithIxE
65
, diffWithKeyAndIxE
76
, strMapWithIxE
@@ -27,13 +26,6 @@ foreign import forInE
2726
(Fn.Fn2 String a (Eff eff Unit))
2827
(Eff eff Unit)
2928

30-
foreign import replicateE
31-
eff a
32-
. Fn.Fn2
33-
Int
34-
(Eff eff a)
35-
(Eff eff Unit)
36-
3729
foreign import diffWithIxE
3830
eff b c d
3931
. Fn.Fn5

0 commit comments

Comments
 (0)