Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
srghma committed Jun 2, 2020
1 parent 718843d commit a6b9afb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Halogen/VDom/DOM/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ type VDomHydrator4 i j k l a w
-- | Widget machines recursively reference the configured spec to potentially
-- | enable recursive trees of Widgets.
newtype VDomSpec a w = VDomSpec
{ buildWidget VDomSpec a w Machine w DOM.Node -- `buildWidget` takes a circular reference to the `VDomSpec`
, hydrateWidget VDomSpec a w DOM.Element Machine w DOM.Node
{ buildWidget Machine w DOM.Node -- `buildWidget` takes a circular reference to the `VDomSpec`
, hydrateWidget DOM.Element Machine w DOM.Node

, buildAttributes DOM.Element Machine a Unit
, hydrateAttributes DOM.Element Machine a Unit
Expand Down
4 changes: 2 additions & 2 deletions src/Halogen/VDom/DOM/Widget.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type WidgetState a w =

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

buildWidget a w. VDomBuilder w a w
buildWidget = EFn.mkEffectFn3 \(VDomSpec spec) build w → do
res ← EFn.runEffectFn1 (spec.buildWidget (VDomSpec spec)) w
res ← EFn.runEffectFn1 spec.buildWidget w
let
res' :: Step (VDom a w) DOM.Node
res' = res # unStep \(Step n s k1 k2) →
Expand Down
34 changes: 34 additions & 0 deletions test/TestVdom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* global exports, require */
"use strict";

// module Control.Monad.Fix

var message = "Control.Monad.Fix: Premature access to result of fixpoint computation."

function f(x) {
console.log(x)
return 1
}

var myobj = { a: f(myobj), b: 2 }

// fixEffect :: forall eff a. ((Unit -> a) -> Eff eff a) -> Eff eff a
exports.fixEffect = function(f) {
return function() {
var result = null;
var ready = false;

result = f(function(u) {
if (!ready) throw new Error(message);
return result;
})();

ready = true;
return result;
}
}

// fixPure :: forall a. ((Unit -> a) -> a) -> a
exports.fixPure = function(f) {
return exports.fixEffect(function(a) { return function () { return f(a); }})();
}
26 changes: 19 additions & 7 deletions test/TestVdom.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Web.DOM.Document (Document) as DOM
import Web.DOM.Element (Element) as DOM
import Halogen.VDom.Machine (Machine)
import Effect (Effect)
import Control.Lazy as CL
import Data.Lazy as DL

infixr 1 prop as :=

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

type As a = a -> a

foreign import fixPure :: forall a. ((Unit -> a) -> a) -> a

mkSpec
DOM.Document
V.VDomSpec (Array (Prop Void)) (Thunk VDom Void)
mkSpec document = V.VDomSpec
{ buildWidget: buildThunk (un VDom)
, hydrateWidget: hydrateThunk (un VDom)
, buildAttributes: buildProp (const (pure unit))
, hydrateAttributes: hydrateProp (const (pure unit))
, document
}
mkSpec document = DL.force (CL.fix go)
where
go :: As (DL.Lazy (V.VDomSpec (Array (Prop Void)) (Thunk VDom Void)))
go lazySpec =
let self = DL.force lazySpec
in DL.defer \_ ->
V.VDomSpec
{ buildWidget: buildThunk (un VDom) self
, hydrateWidget: hydrateThunk (un VDom) self
, buildAttributes: buildProp (const (pure unit))
, hydrateAttributes: hydrateProp (const (pure unit))
, document
}

0 comments on commit a6b9afb

Please sign in to comment.