From 0443dd32d9baf6315b1c7cd78ef538916642671a Mon Sep 17 00:00:00 2001 From: Kris Jenkins Date: Thu, 27 Jul 2017 17:42:08 +0100 Subject: [PATCH 1/2] Performance improvement - call resize only when necessary. A user of this library specifies the dimensions of the chart in their render call. That means that every time their render function is called, `eval SetDimensions ...` is invoked, which in turn calls echarts' `resize` function, which is a _relatively_ expensive call. This change ensures that resize is only triggered if the dimensions of the chart have actually changed. --- src/Halogen/ECharts.purs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Halogen/ECharts.purs b/src/Halogen/ECharts.purs index 48fdd48..7616715 100644 --- a/src/Halogen/ECharts.purs +++ b/src/Halogen/ECharts.purs @@ -18,7 +18,7 @@ import Control.Monad.Aff.Class (class MonadAff) import Data.Foldable (for_, traverse_) import Data.Foreign (Foreign) import Data.Int (toNumber) -import Data.Maybe (Maybe(..), maybe) +import Data.Maybe (Maybe(..), maybe, fromMaybe) import Data.Traversable (for) import Data.Tuple.Nested (type (/\), (/\)) @@ -141,12 +141,14 @@ eval (Clear next) = do for_ state.chart $ liftEff <<< EC.clear pure next eval (SetDimensions { width, height } next) = do - for_ width \w → do - H.modify _{ width = w } - for_ height \h → do - H.modify _{ height = h } - state ← H.get - for_ state.chart $ liftEff <<< EC.resize + state <- H.get + let newWidth = fromMaybe state.width width + newHeight = fromMaybe state.height height + + -- Only trigger a resize is the dimensions have actually changed. + when (newWidth /= state.width || newHeight /= state.height) + do H.modify _{ width = newWidth, height = newHeight } + for_ state.chart $ liftEff <<< EC.resize pure next eval (GetOptions continue) = do state ← H.get From 50350fc29e4f9013d1346964b9ba9e2bf6cd7380 Mon Sep 17 00:00:00 2001 From: Kris Jenkins Date: Thu, 27 Jul 2017 17:42:17 +0100 Subject: [PATCH 2/2] Changing SetDimensions to make width and height mandatory. --- src/Halogen/ECharts.purs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Halogen/ECharts.purs b/src/Halogen/ECharts.purs index 7616715..7282ec7 100644 --- a/src/Halogen/ECharts.purs +++ b/src/Halogen/ECharts.purs @@ -18,7 +18,7 @@ import Control.Monad.Aff.Class (class MonadAff) import Data.Foldable (for_, traverse_) import Data.Foreign (Foreign) import Data.Int (toNumber) -import Data.Maybe (Maybe(..), maybe, fromMaybe) +import Data.Maybe (Maybe(..), maybe) import Data.Traversable (for) import Data.Tuple.Nested (type (/\), (/\)) @@ -52,7 +52,7 @@ data EChartsQuery a | Reset (EM.DSL ETP.OptionI) a | Resize a | Clear a - | SetDimensions { width ∷ Maybe Int, height ∷ Maybe Int } a + | SetDimensions { width ∷ Int, height ∷ Int } a | GetOptions (Maybe Foreign → a) | GetWidth (Int → a) | GetHeight (Int → a) @@ -81,7 +81,7 @@ echarts ⇒ H.Component HH.HTML EChartsQuery (Dimensions /\ Unit) EChartsMessage g echarts theme = echarts' theme \(dim /\ _) → - Just $ H.action $ SetDimensions { width: Just dim.width, height: Just dim.height } + Just $ H.action $ SetDimensions dim echarts' ∷ ∀ eff g i @@ -142,12 +142,10 @@ eval (Clear next) = do pure next eval (SetDimensions { width, height } next) = do state <- H.get - let newWidth = fromMaybe state.width width - newHeight = fromMaybe state.height height -- Only trigger a resize is the dimensions have actually changed. - when (newWidth /= state.width || newHeight /= state.height) - do H.modify _{ width = newWidth, height = newHeight } + when (width /= state.width || height /= state.height) + do H.modify _{ width = width, height = height } for_ state.chart $ liftEff <<< EC.resize pure next eval (GetOptions continue) = do