Skip to content

Commit c9b8e72

Browse files
authored
Functors with lazy nodes (#50)
* Add functor mapping to lazy node creation * Add functor mapping to lazy node update
1 parent 2d2323e commit c9b8e72

File tree

6 files changed

+99
-13
lines changed

6 files changed

+99
-13
lines changed

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Flame/Renderer/Internal/Dom.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ function shallowCopy(origin) {
127127
nodeData: origin.nodeData,
128128
render: origin.render,
129129
arg: origin.arg,
130-
rendered: undefined
130+
rendered: undefined,
131+
messageMapper: origin.messageMapper
131132
};
132133
case managedNode:
133134
return {
@@ -162,6 +163,9 @@ F.prototype.createAllNodes = function (parent, html, referenceNode) {
162163
if (html.children !== undefined)
163164
this.createChildrenNodes(node, html.children);
164165
else if (html.rendered !== undefined) {
166+
if (html.messageMapper !== undefined)
167+
lazyMessageMap(html.messageMapper, html.rendered);
168+
165169
if (html.rendered.text !== undefined) {
166170
node.textContent = html.rendered.text;
167171
}
@@ -196,8 +200,13 @@ F.prototype.createChildrenNodes = function (parent, children) {
196200
else {
197201
if (c.children !== undefined)
198202
this.createChildrenNodes(node, c.children);
199-
if (c.rendered !== undefined && c.rendered.children !== undefined)
200-
this.createChildrenNodes(node, c.rendered.children);
203+
else if (c.rendered !== undefined) {
204+
if (c.messageMapper !== undefined)
205+
lazyMessageMap(c.messageMapper, c.rendered);
206+
207+
if (c.rendered.children !== undefined)
208+
this.createChildrenNodes(node, c.rendered.children);
209+
}
201210
}
202211

203212
parent.appendChild(node);
@@ -367,7 +376,6 @@ F.prototype.runHandlers = function (handlers, messageMapper, event) {
367376
maybeMessage = typeof h === "function" ? h(event)() : this.eventWrapper(h);
368377

369378
//handler can be just a message or a function that takes an event
370-
// a
371379
this.updater(messageMapper === undefined ? maybeMessage : messageMapper(maybeMessage))();
372380
}
373381
event.stopPropagation();
@@ -396,6 +404,9 @@ F.prototype.updateAllNodes = function (parent, currentHtml, updatedHtml) {
396404
if (updatedHtml.arg !== currentHtml.arg) {
397405
updatedHtml.rendered = updatedHtml.render(updatedHtml.arg);
398406

407+
if (updatedHtml.messageMapper !== undefined)
408+
lazyMessageMap(updatedHtml.messageMapper, updatedHtml.rendered);
409+
399410
this.updateAllNodes(parent, currentHtml.rendered, updatedHtml.rendered);
400411
}
401412
else
@@ -970,6 +981,15 @@ F.prototype.removeEvent = function (node, name) {
970981
this.applicationEvents.delete(name);
971982
}
972983
}
973-
984+
//functor mapping
985+
node[eventKey + eventPostfix] = undefined;
974986
node[eventKey] = undefined;
975987
};
988+
989+
function lazyMessageMap(mapper, html) {
990+
html.messageMapper = mapper;
991+
992+
if (html.children !== undefined && html.children.length > 0)
993+
for (let i = 0; i < html.children.length; ++i)
994+
lazyMessageMap(mapper, html.children[i]);
995+
}

src/Flame/Types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports.messageMapper = function (mapper) {
55
};
66

77
function addMessageMapper(html, mapper) {
8-
if (html.nodeData !== undefined && html.nodeData.events !== undefined)
8+
if (html.nodeType !== 1 && html.nodeType !== 4)
99
mapHtml(html, mapper);
1010

1111
if (html.children !== undefined && html.children.length > 0)

test/Basic/Functor.purs renamed to test/Functor/Basic.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Test.Basic.Functor where
1+
module Test.Functor.Basic where
22

33
import Prelude
44

test/Functor/Lazy.purs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Test.Functor.Lazy where
2+
3+
import Prelude
4+
import Effect
5+
import Flame
6+
import Flame.Html.Element
7+
import Data.Maybe
8+
import Flame.Html.Element as H
9+
import Flame.Html.Attribute as HA
10+
import Flame.Html.Event as E
11+
12+
data CounterMsg = Increment Int
13+
14+
type CounterModel = { count :: Int }
15+
16+
initCounter :: CounterModel
17+
initCounter = { count: 1 }
18+
19+
updateCounter :: CounterModel -> CounterMsg -> CounterModel
20+
updateCounter model (Increment val) = model { count = model.count + val }
21+
22+
counterView :: CounterModel -> Html CounterMsg
23+
counterView = lazy Nothing counterView_
24+
25+
counterView_ :: CounterModel -> Html CounterMsg
26+
counterView_ model = H.main "main" [
27+
H.button [ HA.id "add-button", E.onClick $ Increment 1000 ] [ H.text $ "Current Value: " <> show model.count ]
28+
]
29+
30+
data Msg = PageMsg PageMsg
31+
32+
data PageMsg = CounterMsg CounterMsg
33+
34+
type Model = { counter :: CounterModel }
35+
36+
init = { counter: initCounter } :> []
37+
38+
update model (PageMsg (CounterMsg msg)) = model { counter = updateCounter model.counter msg } :> []
39+
40+
view :: Model -> Html Msg
41+
view model = H.div_ [ PageMsg <$> CounterMsg <$> counterView model.counter ]
42+
43+
mount :: Effect Unit
44+
mount = mount_ (QuerySelector "#mount-point") {
45+
subscribe: []
46+
, init
47+
, update
48+
, view
49+
}

test/Main.purs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import Flame.Subscription.Unsafe.CustomEvent as FSUC
2929
import Partial.Unsafe (unsafePartial)
3030
import Test.Basic.EffectList as TBEL
3131
import Test.Basic.Effectful as TBE
32-
import Test.Basic.Functor as TBF
32+
import Test.Functor.Basic as TBF
33+
import Test.Functor.Lazy as TFL
3334
import Test.Basic.NoEffects as TBN
3435
import Test.Effectful.SlowEffects as TES
3536
import Test.ServerSideRendering.Effectful as TSE
@@ -740,7 +741,8 @@ main = TUM.runTest do
740741
TUA.equal "-2" currentDecrement3
741742
TUA.equal "2" currentLuckyNumber3
742743

743-
TU.test "functor" do
744+
TU.suite "functor" do
745+
TU.test "basic" do
744746
liftEffect do
745747
unsafeCreateEnviroment
746748
TBF.mount
@@ -762,6 +764,23 @@ main = TUM.runTest do
762764
current2 <- textContent "#text-output-1"
763765
TUA.equal "2" current2
764766

767+
TU.test "lazy" do
768+
liftEffect do
769+
unsafeCreateEnviroment
770+
TFL.mount
771+
childrenLength <- childrenNodeLength
772+
--div
773+
TUA.equal 1 childrenLength
774+
775+
dispatchEvent clickEvent "#add-button"
776+
initial <- textContent "#add-button"
777+
TUA.equal "Current Value: 1001" initial
778+
779+
dispatchEvent clickEvent "#add-button"
780+
dispatchEvent clickEvent "#add-button"
781+
current2 <- textContent "#add-button"
782+
TUA.equal "Current Value: 3001" current2
783+
765784
TU.suite "Effectful specific" do
766785
TU.test "slower effects" do
767786
liftEffect do

0 commit comments

Comments
 (0)