Skip to content

Commit a6b9afb

Browse files
committed
wip
1 parent 718843d commit a6b9afb

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

src/Halogen/VDom/DOM/Types.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type VDomHydrator4 i j k l a w
4343
-- | Widget machines recursively reference the configured spec to potentially
4444
-- | enable recursive trees of Widgets.
4545
newtype VDomSpec a w = VDomSpec
46-
{ buildWidget VDomSpec a w Machine w DOM.Node -- `buildWidget` takes a circular reference to the `VDomSpec`
47-
, hydrateWidget VDomSpec a w DOM.Element Machine w DOM.Node
46+
{ buildWidget Machine w DOM.Node -- `buildWidget` takes a circular reference to the `VDomSpec`
47+
, hydrateWidget DOM.Element Machine w DOM.Node
4848

4949
, buildAttributes DOM.Element Machine a Unit
5050
, hydrateAttributes DOM.Element Machine a Unit

src/Halogen/VDom/DOM/Widget.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type WidgetState a w =
1515

1616
hydrateWidget a w. VDomHydrator w a w
1717
hydrateWidget = EFn.mkEffectFn5 \elem (VDomSpec spec) _hydrate build w → do
18-
res ← EFn.runEffectFn1 (spec.hydrateWidget (VDomSpec spec) elem) w
18+
res ← EFn.runEffectFn1 (spec.hydrateWidget elem) w
1919
let
2020
res' :: Step (VDom a w) DOM.Node
2121
res' = res # unStep \(Step n s k1 k2) →
@@ -24,7 +24,7 @@ hydrateWidget = EFn.mkEffectFn5 \elem (VDomSpec spec) _hydrate build w → do
2424

2525
buildWidget a w. VDomBuilder w a w
2626
buildWidget = EFn.mkEffectFn3 \(VDomSpec spec) build w → do
27-
res ← EFn.runEffectFn1 (spec.buildWidget (VDomSpec spec)) w
27+
res ← EFn.runEffectFn1 spec.buildWidget w
2828
let
2929
res' :: Step (VDom a w) DOM.Node
3030
res' = res # unStep \(Step n s k1 k2) →

test/TestVdom.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* global exports, require */
2+
"use strict";
3+
4+
// module Control.Monad.Fix
5+
6+
var message = "Control.Monad.Fix: Premature access to result of fixpoint computation."
7+
8+
function f(x) {
9+
console.log(x)
10+
return 1
11+
}
12+
13+
var myobj = { a: f(myobj), b: 2 }
14+
15+
// fixEffect :: forall eff a. ((Unit -> a) -> Eff eff a) -> Eff eff a
16+
exports.fixEffect = function(f) {
17+
return function() {
18+
var result = null;
19+
var ready = false;
20+
21+
result = f(function(u) {
22+
if (!ready) throw new Error(message);
23+
return result;
24+
})();
25+
26+
ready = true;
27+
return result;
28+
}
29+
}
30+
31+
// fixPure :: forall a. ((Unit -> a) -> a) -> a
32+
exports.fixPure = function(f) {
33+
return exports.fixEffect(function(a) { return function () { return f(a); }})();
34+
}

test/TestVdom.purs

+19-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Web.DOM.Document (Document) as DOM
1515
import Web.DOM.Element (Element) as DOM
1616
import Halogen.VDom.Machine (Machine)
1717
import Effect (Effect)
18+
import Control.Lazy as CL
19+
import Data.Lazy as DL
1820

1921
infixr 1 prop as :=
2022

@@ -43,13 +45,23 @@ thunk render val = VDom $ V.Widget $ Fn.runFn2 thunk1 render val
4345
myfn :: ((Void Effect Unit) -> DOM.Element -> Machine (Array (Prop Void)) Unit) DOM.Element Machine (Array (Prop Void)) Unit
4446
myfn buildProp element = buildProp (const (pure unit)) element
4547

48+
type As a = a -> a
49+
50+
foreign import fixPure :: forall a. ((Unit -> a) -> a) -> a
51+
4652
mkSpec
4753
DOM.Document
4854
V.VDomSpec (Array (Prop Void)) (Thunk VDom Void)
49-
mkSpec document = V.VDomSpec
50-
{ buildWidget: buildThunk (un VDom)
51-
, hydrateWidget: hydrateThunk (un VDom)
52-
, buildAttributes: buildProp (const (pure unit))
53-
, hydrateAttributes: hydrateProp (const (pure unit))
54-
, document
55-
}
55+
mkSpec document = DL.force (CL.fix go)
56+
where
57+
go :: As (DL.Lazy (V.VDomSpec (Array (Prop Void)) (Thunk VDom Void)))
58+
go lazySpec =
59+
let self = DL.force lazySpec
60+
in DL.defer \_ ->
61+
V.VDomSpec
62+
{ buildWidget: buildThunk (un VDom) self
63+
, hydrateWidget: hydrateThunk (un VDom) self
64+
, buildAttributes: buildProp (const (pure unit))
65+
, hydrateAttributes: hydrateProp (const (pure unit))
66+
, document
67+
}

0 commit comments

Comments
 (0)