Last active
September 29, 2015 21:44
-
-
Save GlenDC/f9fde47e4991203b21cf to your computer and use it in GitHub Desktop.
Proposal for lazyif functionallity in elm-virtualdom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/Native/VirtualDom.js b/src/Native/VirtualDom.js | |
index efb9ce7..45ec52e 100644 | |
--- a/src/Native/VirtualDom.js | |
+++ b/src/Native/VirtualDom.js | |
@@ -1407,13 +1407,18 @@ Elm.Native.VirtualDom.make = function(elm) | |
return newNode; | |
} | |
+ function predicate(a, b) | |
+ { | |
+ return a === b; | |
+ } | |
+ | |
function lazyRef(fn, a) | |
{ | |
function thunk() | |
{ | |
return fn(a); | |
} | |
- return new Thunk(fn, [a], thunk); | |
+ return new Thunk(fn, [a], predicate, thunk); | |
} | |
function lazyRef2(fn, a, b) | |
@@ -1422,7 +1427,7 @@ Elm.Native.VirtualDom.make = function(elm) | |
{ | |
return A2(fn, a, b); | |
} | |
- return new Thunk(fn, [a,b], thunk); | |
+ return new Thunk(fn, [a,b], predicate, thunk); | |
} | |
function lazyRef3(fn, a, b, c) | |
@@ -1431,10 +1436,20 @@ Elm.Native.VirtualDom.make = function(elm) | |
{ | |
return A3(fn, a, b, c); | |
} | |
- return new Thunk(fn, [a,b,c], thunk); | |
+ return new Thunk(fn, [a,b,c], predicate, thunk); | |
+ } | |
+ | |
+ function lazyPredicateRef(fn, p, a) | |
+ { | |
+ function thunk() | |
+ { | |
+ return fn(a); | |
+ } | |
+ | |
+ return new Thunk(fn, [a], p, thunk) | |
} | |
- function Thunk(fn, args, thunk) | |
+ function Thunk(fn, args, predicate, thunk) | |
{ | |
/* public (used by VirtualDom.js) */ | |
this.vnode = null; | |
@@ -1444,6 +1459,7 @@ Elm.Native.VirtualDom.make = function(elm) | |
this.fn = fn; | |
this.args = args; | |
this.thunk = thunk; | |
+ this.predicate = predicate; | |
} | |
Thunk.prototype.type = "Thunk"; | |
@@ -1462,7 +1478,7 @@ Elm.Native.VirtualDom.make = function(elm) | |
for (var i = cargs.length; i--; ) | |
{ | |
- if (cargs[i] !== pargs[i]) | |
+ if (!previous.predicate(cargs[i], pargs[i])) | |
{ | |
return true; | |
} | |
@@ -1495,6 +1511,8 @@ Elm.Native.VirtualDom.make = function(elm) | |
lazy2: F3(lazyRef2), | |
lazy3: F4(lazyRef3), | |
+ lazyIf: F3(lazyPredicateRef), | |
+ | |
toElement: F3(toElement), | |
fromElement: fromElement, | |
diff --git a/src/VirtualDom.elm b/src/VirtualDom.elm | |
index e685fac..62a52ea 100644 | |
--- a/src/VirtualDom.elm | |
+++ b/src/VirtualDom.elm | |
@@ -5,6 +5,7 @@ module VirtualDom | |
, Property, property, attribute | |
, on, onWithOptions, Options, defaultOptions | |
, lazy, lazy2, lazy3 | |
+ , lazyIf, lazyIf2, lazyIf3 | |
) where | |
{-| API to the core diffing algorithm. Can serve as a foundation for libraries | |
@@ -221,3 +222,18 @@ lazy2 = | |
lazy3 : (a -> b -> c -> Node) -> a -> b -> c -> Node | |
lazy3 = | |
Native.VirtualDom.lazy3 | |
+ | |
+ | |
+lazyIf : (a -> Node) -> (a -> a -> Bool) -> a -> Node | |
+lazyIf = | |
+ Native.VirtualDom.lazyIf | |
+ | |
+ | |
+lazyIf2 : ((a, b) -> Node) -> ((a, b) -> (a, b) -> Bool) -> (a, b) -> Node | |
+lazyIf2 = | |
+ Native.VirtualDom.lazyIf2 | |
+ | |
+ | |
+lazyIf3 : ((a, b, c) -> Node) -> ((a, b, c) -> (a, b, c) -> Bool) -> (a, b, c) -> Node | |
+lazyIf3 = | |
+ Native.VirtualDom.lazyIf3 | |
diff --git a/src/wrapper.js b/src/wrapper.js | |
index 47de45c..886306b 100644 | |
--- a/src/wrapper.js | |
+++ b/src/wrapper.js | |
@@ -200,13 +200,18 @@ Elm.Native.VirtualDom.make = function(elm) | |
return newNode; | |
} | |
+ function predicate(a, b) | |
+ { | |
+ return a === b; | |
+ } | |
+ | |
function lazyRef(fn, a) | |
{ | |
function thunk() | |
{ | |
return fn(a); | |
} | |
- return new Thunk(fn, [a], thunk); | |
+ return new Thunk(fn, [a], predicate, thunk); | |
} | |
function lazyRef2(fn, a, b) | |
@@ -215,7 +220,7 @@ Elm.Native.VirtualDom.make = function(elm) | |
{ | |
return A2(fn, a, b); | |
} | |
- return new Thunk(fn, [a,b], thunk); | |
+ return new Thunk(fn, [a,b], predicate, thunk); | |
} | |
function lazyRef3(fn, a, b, c) | |
@@ -224,10 +229,20 @@ Elm.Native.VirtualDom.make = function(elm) | |
{ | |
return A3(fn, a, b, c); | |
} | |
- return new Thunk(fn, [a,b,c], thunk); | |
+ return new Thunk(fn, [a,b,c], predicate, thunk); | |
+ } | |
+ | |
+ function lazyPredicateRef(fn, p, a) | |
+ { | |
+ function thunk() | |
+ { | |
+ return fn(a); | |
+ } | |
+ | |
+ return new Thunk(fn, [a], p, thunk) | |
} | |
- function Thunk(fn, args, thunk) | |
+ function Thunk(fn, args, predicate, thunk) | |
{ | |
/* public (used by VirtualDom.js) */ | |
this.vnode = null; | |
@@ -237,6 +252,7 @@ Elm.Native.VirtualDom.make = function(elm) | |
this.fn = fn; | |
this.args = args; | |
this.thunk = thunk; | |
+ this.predicate = predicate; | |
} | |
Thunk.prototype.type = "Thunk"; | |
@@ -255,7 +271,7 @@ Elm.Native.VirtualDom.make = function(elm) | |
for (var i = cargs.length; i--; ) | |
{ | |
- if (cargs[i] !== pargs[i]) | |
+ if (!previous.predicate(cargs[i], pargs[i])) | |
{ | |
return true; | |
} | |
@@ -288,6 +304,8 @@ Elm.Native.VirtualDom.make = function(elm) | |
lazy2: F3(lazyRef2), | |
lazy3: F4(lazyRef3), | |
+ lazyIf: F3(lazyPredicateRef), | |
+ | |
toElement: F3(toElement), | |
fromElement: fromElement, | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment