Skip to content

Commit 718843d

Browse files
committed
revert(#b385b50)
1 parent 6764603 commit 718843d

File tree

10 files changed

+62
-114
lines changed

10 files changed

+62
-114
lines changed

GUIDE.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ render ∷ MyState → MyVDom
1414
1515
main = do
1616
-- Build the initial machine
17-
(machine1 :: VDomMachine a w) ← V.buildVDom myVDomSpec (render state1)
17+
machine1 ← V.buildVDom myVDomSpec (render state1)
1818
19-
-- `machine1` contains a new `DOM.Node` (output node) in it's state
20-
-- Attach that output node to the DOM
19+
-- Attach the output node to the DOM
2120
appendChildToBody (V.extract machine1)
2221
2322
-- Patch
24-
-- `V.step` patches previous `DOM.Node` (stored in `machine1`) by running effects
2523
machine2 ← V.step machine1 (render state2)
2624
machine3 ← V.step machine2 (render state3)
2725
...

src/Halogen/VDom/DOM.purs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ buildVDom ∷ ∀ a w. VDomSpec a w → VDomMachine a w
4444
buildVDom spec = build
4545
where
4646
build = EFn.mkEffectFn1 case _ of
47-
Text s → EFn.runEffectFn3 buildText spec build s -- build text machine
47+
Text s → EFn.runEffectFn3 buildText spec build s
4848
Elem namespace elemName a childrenVdoms → EFn.runEffectFn6 buildElem spec build namespace elemName a childrenVdoms
4949
Keyed namespace elemName a keyedChildrenVdoms → EFn.runEffectFn6 buildKeyed spec build namespace elemName a keyedChildrenVdoms
50-
Widget w → EFn.runEffectFn3 buildWidget spec build w -- machine that has full control of it's lifecycle
51-
Grafted g → EFn.runEffectFn1 build (runGraft g) -- optimization
50+
Widget w → EFn.runEffectFn3 buildWidget spec build w
51+
Grafted g → EFn.runEffectFn1 build (runGraft g)

src/Halogen/VDom/DOM/Elem.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ buildElem = EFn.mkEffectFn6 \(VDomSpec spec) build ns1 name1 as1 ch1 → do
8888
EFn.runEffectFn3 Util.insertChildIx ix (extract res) node
8989
pure res
9090
children ← EFn.runEffectFn2 Util.forE ch1 onChild
91-
attrs ← EFn.runEffectFn1 (spec.buildAttributes el) as1 -- build machine that takes attributes
91+
attrs ← EFn.runEffectFn1 (spec.buildAttributes el) as1
9292
let
9393
state =
9494
{ build

src/Halogen/VDom/DOM/Prop.purs

-15
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ hydrateProp emit el = renderProp
5252

5353
extraAttributeNames ← mkExtraAttributeNames el
5454

55-
-- for each prop in array:
56-
-- if prop is attr - dont set attr to element, store attr under "attr/XXX" key in a returned object
57-
-- if prop is property - dont set property to element, store property under "prop/XXX" key in a returned object
58-
-- if prop is handler for DOM.EventType - start listen and add listener to `events` mutable map, store handler under "handler/EVENTTYPE" in a returned object
59-
-- if prop is ref updater - store `emitterInputBuilder` in under a `ref` key in a returned object, call `emitter` on creation of all props (now) and on halt of all props (later)
6055
(props Object.Object (Prop a)) ← EFn.runEffectFn3 Util.strMapWithIxE ps1 propToStrKey (Fn.runFn4 hydrateApplyProp extraAttributeNames el emit events)
6156
let
6257
(state PropState a) =
@@ -75,19 +70,9 @@ buildProp
7570
. BuildPropFunction a
7671
buildProp emit el = renderProp
7772
where
78-
-- what it does - creates a machine, that contains state
79-
-- on next step - patches prop
80-
-- on halt - all ref watchers are notified that element is removed
81-
8273
renderProp EFn.EffectFn1 (Array (Prop a)) (Step (Array (Prop a)) Unit)
8374
renderProp = EFn.mkEffectFn1 \ps1 → do
8475
(events STObject' (EventListenerAndCurrentEmitterInputBuilder a)) ← Util.newMutMap
85-
86-
-- for each prop in array:
87-
-- if prop is attr - set attr to element, store attr under "attr/XXX" key in a returned object
88-
-- if prop is property - set property to element, store property under "prop/XXX" key in a returned object
89-
-- if prop is handler for DOM.EventType - start listen and add listener to `events` mutable map, store handler under "handler/EVENTTYPE" in a returned object
90-
-- if prop is ref updater - store `emitterInputBuilder` in under a `ref` key in a returned object, call `emitter` on creation of all props (now) and on halt of all props (later)
9176
(props Object.Object (Prop a)) ← EFn.runEffectFn3 Util.strMapWithIxE ps1 propToStrKey (Fn.runFn3 applyProp el emit events)
9277
let
9378
(state PropState a) =

src/Halogen/VDom/DOM/Prop/Types.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ type PropState a =
8888
}
8989

9090
type BuildPropFunction a
91-
= (a Effect Unit) -- emitter, for example the global broadcaster function for all elements in halogen component
91+
= (a Effect Unit) -- Emitter, for example the global broadcaster function for all elements in halogen component
9292
DOM.Element
93-
Machine (Array (Prop a)) Unit -- Machine takes array of properties for that element, outputs nothing
93+
Machine (Array (Prop a)) Unit

src/Halogen/VDom/DOM/Prop/Utils.purs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ unsafeGetProperty = Util.unsafeGetAny
2929

3030
removeProperty EFn.EffectFn2 String DOM.Element Unit
3131
removeProperty = EFn.mkEffectFn2 \key el →
32-
EFn.runEffectFn3 Util.hasAttribute (null Nullable Namespace) key el >>= if _ -- If attr exists on element
33-
then EFn.runEffectFn3 Util.removeAttribute (null Nullable Namespace) key el -- remove it using el.removeAttribute()
34-
else case typeOf (Fn.runFn2 Util.unsafeGetAny key el) of -- If it's property - set to following
32+
EFn.runEffectFn3 Util.hasAttribute (null Nullable Namespace) key el >>= if _
33+
then EFn.runEffectFn3 Util.removeAttribute (null Nullable Namespace) key el
34+
else case typeOf (Fn.runFn2 Util.unsafeGetAny key el) of
3535
"string"EFn.runEffectFn3 Util.unsafeSetAny key "" el
3636
_ → case key of
3737
"rowSpan"EFn.runEffectFn3 Util.unsafeSetAny key 1 el

src/Halogen/VDom/DOM/Types.purs

-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ import Web.DOM.Document (Document) as DOM
99
import Web.DOM.Element (Element) as DOM
1010
import Web.DOM.Node (Node) as DOM
1111

12-
-- A function, that takes `VDom a w` and builds a `DOM.Node`
1312
type VDomMachine a w = Machine (VDom a w) DOM.Node
1413

1514
type VDomStep a w = Step (VDom a w) DOM.Node
1615

1716
type VDomInit i a w = EFn.EffectFn1 i (VDomStep a w)
1817

19-
-- Equal to
20-
-- (VDomSpec a w) -> (VDOM a w -> Step (VDOM a w) DOM.Node) -> i -> Effect (Step (VDOM a w) DOM.Node)
2118
type VDomBuilder i a w = EFn.EffectFn3 (VDomSpec a w) (VDomMachine a w) i (VDomStep a w)
2219

2320
type VDomHydrator i a w
@@ -48,13 +45,7 @@ type VDomHydrator4 i j k l a w
4845
newtype VDomSpec a w = VDomSpec
4946
{ buildWidget VDomSpec a w Machine w DOM.Node -- `buildWidget` takes a circular reference to the `VDomSpec`
5047
, hydrateWidget VDomSpec a w DOM.Element Machine w DOM.Node
51-
-- example:
5248

53-
-- buildAttributes = buildProps handler
54-
-- https://github.com/purescript-halogen/purescript-halogen/blob/bb715fe5c06ba3048f4d8b377ec842cd8cf37833/src/Halogen/VDom/Driver.purs#L68-L71
55-
56-
-- what is handler
57-
-- https://github.com/purescript-halogen/purescript-halogen/blob/bb715fe5c06ba3048f4d8b377ec842cd8cf37833/src/Halogen/Aff/Driver.purs#L203
5849
, buildAttributes DOM.Element Machine a Unit
5950
, hydrateAttributes DOM.Element Machine a Unit
6051

src/Halogen/VDom/Machine.purs

-19
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,12 @@ import Prelude
1414
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
1515
import Unsafe.Coerce (unsafeCoerce)
1616

17-
{-
18-
19-
type Machine is equal to:
20-
21-
a -> Step a b
22-
a -> forall state . Step b state (state -> a -> Step a b) (state -> Unit)
23-
a -> forall state . Step b state (state -> Machine a b) (state -> Unit)
24-
25-
where
26-
27-
a is input
28-
b is output
29-
state is hidden state
30-
(state -> a -> Step a b) is a functon from state and input to the new Step
31-
(state -> Unit) is finalizer
32-
33-
-}
34-
3517
type Machine a b = EffectFn1 a (Step a b)
3618

3719
data Step' a b s = Step b s (EffectFn2 s a (Step a b)) (EffectFn1 s Unit)
3820

3921
foreign import data StepType Type Type
4022

41-
-- hides state type, makes it exsistential
4223
mkStep a b s. Step' a b s Step a b
4324
mkStep = unsafeCoerce
4425

src/Halogen/VDom/Util.js

+30-31
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,36 @@ exports.replicateE = function (n, f) {
4444
}
4545
};
4646

47-
exports.diffWithIxE = function (oldElems, newElems, onBothElements, onOldElement, onNewElement) {
48-
var outputs = [];
49-
var oldElemsLength = oldElems.length;
50-
var newElemsLength = newElems.length;
47+
exports.diffWithIxE = function (a1, a2, f1, f2, f3) {
48+
var a3 = [];
49+
var l1 = a1.length;
50+
var l2 = a2.length;
5151
var i = 0;
5252
while (1) {
53-
if (i < oldElemsLength) {
54-
if (i < newElemsLength) {
55-
outputs.push(onBothElements(i, oldElems[i], newElems[i]));
53+
if (i < l1) {
54+
if (i < l2) {
55+
a3.push(f1(i, a1[i], a2[i]));
5656
} else {
57-
onOldElement(i, oldElems[i]);
57+
f2(i, a1[i]);
5858
}
59-
} else if (i < newElemsLength) {
60-
outputs.push(onNewElement(i, newElems[i]));
59+
} else if (i < l2) {
60+
a3.push(f3(i, a2[i]));
6161
} else {
6262
break;
6363
}
6464
i++;
6565
}
66-
return outputs;
66+
return a3;
67+
};
68+
69+
exports.strMapWithIxE = function (as, fk, f) {
70+
var o = {};
71+
for (var i = 0; i < as.length; i++) {
72+
var a = as[i];
73+
var k = fk(a);
74+
o[k] = f(k, i, a);
75+
}
76+
return o;
6777
};
6878

6979
exports.diffWithKeyAndIxE = function (o1, as, fk, f1, f2, f3) {
@@ -86,16 +96,6 @@ exports.diffWithKeyAndIxE = function (o1, as, fk, f1, f2, f3) {
8696
return o2;
8797
};
8898

89-
exports.strMapWithIxE = function (children, propToStrKey, f) {
90-
var o = {};
91-
for (var i = 0; i < children.length; i++) {
92-
var child = children[i];
93-
var key = propToStrKey(child);
94-
o[key] = f(key, i, child);
95-
}
96-
return o;
97-
};
98-
9999
exports.refEq = function (a, b) {
100100
return a === b;
101101
};
@@ -116,22 +116,21 @@ exports.createElement = function (ns, name, doc) {
116116
}
117117
};
118118

119-
exports.insertChildIx = function (i, elem, parent) {
120-
var referenceNode = parent.childNodes.item(i) || null;
121-
if (referenceNode !== elem) {
122-
// insert before referenceNode, if referenceNode is null - inserted at the end
123-
parent.insertBefore(elem, referenceNode);
119+
exports.insertChildIx = function (i, a, b) {
120+
var n = b.childNodes.item(i) || null;
121+
if (n !== a) {
122+
b.insertBefore(a, n);
124123
}
125124
};
126125

127-
exports.removeChild = function (elem, parent) {
128-
if (parent && elem.parentNode === parent) {
129-
parent.removeChild(elem);
126+
exports.removeChild = function (a, b) {
127+
if (b && a.parentNode === b) {
128+
b.removeChild(a);
130129
}
131130
};
132131

133-
exports.parentNode = function (elem) {
134-
return elem.parentNode;
132+
exports.parentNode = function (a) {
133+
return a.parentNode;
135134
};
136135

137136
exports.setAttribute = function (ns, attr, val, el) {

src/Halogen/VDom/Util.purs

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Halogen.VDom.Util where
22

3-
import Prelude (Unit, unit, (<>), (==))
3+
import Prelude (Unit, (<>), (==))
44

55
import Data.Function.Uncurried as Fn
66
import Data.Nullable (Nullable)
@@ -75,33 +75,33 @@ foreign import replicateE
7575
Unit
7676

7777
foreign import diffWithIxE
78-
oldElem newElem output dismissed
78+
b c d
7979
. EFn.EffectFn5
80-
(Array oldElem) -- e.g. list of vdom elements
81-
(Array newElem) -- e.g. list of vdom elements
82-
(EFn.EffectFn3 Int oldElem newElem output) -- execute action when both elems are found in oldElems array and newElems array under the same index (usually used to remove old element from DOM and add new element to DOM)
83-
(EFn.EffectFn2 Int oldElem dismissed) -- execute action when only oldElem is found, there are no elems left in `Array newElem` (happens when array of old elements is bigger than array of new elements)
84-
(EFn.EffectFn2 Int newElem output) -- execute action when only newElem is found, there are no elems left in `Array oldElem` (happens when array of new elements is bigger than array of old elements)
85-
(Array output) -- e.g. list of dom elements
80+
(Array b)
81+
(Array c)
82+
(EFn.EffectFn3 Int b c d)
83+
(EFn.EffectFn2 Int b Unit)
84+
(EFn.EffectFn2 Int c d)
85+
(Array d)
8686

8787
foreign import diffWithKeyAndIxE
88-
oldElem newElemWithKey output dismissed
88+
a b c d
8989
. EFn.EffectFn6
90-
(Object.Object oldElem)
91-
(Array newElemWithKey)
92-
(newElemWithKey String)
93-
(EFn.EffectFn4 String Int oldElem newElemWithKey output)
94-
(EFn.EffectFn2 String oldElem dismissed)
95-
(EFn.EffectFn3 String Int newElemWithKey output)
96-
(Object.Object output)
90+
(Object.Object a)
91+
(Array b)
92+
(b String)
93+
(EFn.EffectFn4 String Int a b c)
94+
(EFn.EffectFn2 String a d)
95+
(EFn.EffectFn3 String Int b c)
96+
(Object.Object c)
9797

9898
foreign import strMapWithIxE
99-
child outputVal
99+
a b
100100
. EFn.EffectFn3
101-
(Array child) -- children
102-
(child String) -- propToStrKey
103-
(EFn.EffectFn3 String Int child outputVal) -- action, executed on each array element, (StrKey -> Index -> child -> outputVal)
104-
(Object.Object outputVal) -- key is StrKey, val type is outputVal
101+
(Array a)
102+
(a String)
103+
(EFn.EffectFn3 String Int a b)
104+
(Object.Object b)
105105

106106
foreign import refEq
107107
a b. Fn.Fn2 a b Boolean
@@ -115,9 +115,6 @@ foreign import setTextContent
115115
foreign import createElement
116116
EFn.EffectFn3 (Nullable Namespace) ElemName DOM.Document DOM.Element
117117

118-
-- Insert new child at index
119-
-- (if there is already an element on that index, that old element is moved below).
120-
-- If there are not enough elements - new child is moved at the end of the list.
121118
foreign import insertChildIx
122119
EFn.EffectFn3 Int DOM.Node DOM.Node Unit
123120

@@ -153,9 +150,6 @@ foreign import warnAny ∷ ∀ a . EFn.EffectFn2 String a Unit
153150

154151
foreign import logAny a . EFn.EffectFn2 String a Unit
155152

156-
undefined :: a . a
157-
undefined = unsafeCoerce unit
158-
159153
fullAttributeName Maybe Namespace ElemName String
160154
fullAttributeName maybeNamespace elemName =
161155
case maybeNamespace of

0 commit comments

Comments
 (0)