Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 2aa3f4b

Browse files
committed
Make built-in themes FFI-safe
1 parent fa3bc49 commit 2aa3f4b

File tree

3 files changed

+38
-50
lines changed

3 files changed

+38
-50
lines changed

src/ECharts/Chart.purs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ import Prelude
1515
import Control.Monad.Eff (Eff)
1616
import Control.Monad.Eff.Class (class MonadEff, liftEff)
1717
import Control.Monad.Eff.Exception (EXCEPTION)
18-
18+
import Data.Either (either)
1919
import Data.Foreign (Foreign, toForeign)
20-
2120
import DOM (DOM)
2221
import DOM.HTML.Types (HTMLElement)
23-
2422
import ECharts.Internal (undefinedValue)
2523
import ECharts.Monad (DSL, buildObj)
26-
import ECharts.Theme (Theme(..))
27-
import ECharts.Types.Phantom (OptionI)
24+
import ECharts.Theme (Theme, builtInThemeName)
2825
import ECharts.Types (Chart, ECHARTS)
26+
import ECharts.Types.Phantom (OptionI)
2927

3028
foreign import initImpl
3129
e. Foreign
@@ -45,8 +43,7 @@ initWithTheme
4543
Theme
4644
HTMLElement
4745
m Chart
48-
initWithTheme (ByName name) el = liftEff $ initImpl (toForeign name) el
49-
initWithTheme (FromObject theme) el = liftEff $ initImpl (toForeign theme) el
46+
initWithTheme theme el = liftEff $ initImpl (either (toForeign <<< builtInThemeName) toForeign theme) el
5047

5148
foreign import registerTheme
5249
e. String

src/ECharts/Theme.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
require("echarts/theme/dark")
2-
require("echarts/theme/infographic")
3-
require("echarts/theme/macarons")
4-
require("echarts/theme/roma")
5-
require("echarts/theme/shine")
6-
require("echarts/theme/vintage")
1+
"use strict";
72

8-
exports.forceExport = null
3+
exports._dark = { name: "dark", value: require("echarts/theme/dark") };
4+
exports._infographic = { name: "infographic", value: require("echarts/theme/infographic") };
5+
exports._macarons = { name: "macarons", value: require("echarts/theme/macarons") };
6+
exports._roma = { name: "roma", value: require("echarts/theme/roma") };
7+
exports._shine = { name: "shine", value: require("echarts/theme/shine") };
8+
exports._vintage = { name: "vintage", value: require("echarts/theme/vintage") };
9+
10+
exports.builtInThemeName = function (theme) {
11+
return theme.name;
12+
};

src/ECharts/Theme.purs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,45 @@
11
module ECharts.Theme
22
( Theme(..)
3+
, BuiltInTheme
4+
, builtInThemeName
35
, dark
46
, infographic
57
, macarons
68
, roma
79
, shine
810
, vintage
9-
, BuiltInTheme(..)
10-
, parseBuiltInTheme
11-
, builtInToTheme
1211
) where
1312

14-
import Prelude (($), (<>))
1513
import Data.Either (Either(..))
1614
import Data.Foreign (Foreign)
1715

18-
foreign import forceExport Foreign
16+
type Theme = Either BuiltInTheme Foreign
1917

20-
data Theme = ByName String | FromObject Foreign
21-
data BuiltInTheme = Infographic | Macarons | Roma | Shine | Vintage | Dark
18+
foreign import data BuiltInTheme :: Type
2219

23-
parseBuiltInTheme String Either String BuiltInTheme
24-
parseBuiltInTheme str = case str of
25-
"infographic"Right Infographic
26-
"macarons"Right Macarons
27-
"roma"Right Roma
28-
"shine"Right Shine
29-
"vintage"Right Vintage
30-
"dark"Right Dark
31-
_ → Left $ "`" <> str <> "` is not builtin theme"
20+
foreign import builtInThemeName :: BuiltInTheme -> String
3221

33-
builtInToTheme BuiltInTheme Theme
34-
builtInToTheme = case _ of
35-
InfographicByName "infographic"
36-
MacaronsByName "macarons"
37-
RomaByName "roma"
38-
ShineByName "shine"
39-
VintageByName "vintage"
40-
DarkByName "dark"
22+
dark :: Theme
23+
dark = Left _dark
4124

42-
dark Theme
43-
dark = builtInToTheme Dark
25+
infographic :: Theme
26+
infographic = Left _infographic
4427

45-
infographic Theme
46-
infographic = builtInToTheme Infographic
28+
macarons :: Theme
29+
macarons = Left _macarons
4730

48-
macarons Theme
49-
macarons = builtInToTheme Macarons
31+
roma :: Theme
32+
roma = Left _roma
5033

51-
roma Theme
52-
roma = builtInToTheme Roma
34+
shine :: Theme
35+
shine = Left _shine
5336

54-
shine Theme
55-
shine = builtInToTheme Shine
37+
vintage :: Theme
38+
vintage = Left _vintage
5639

57-
vintage Theme
58-
vintage = builtInToTheme Vintage
40+
foreign import _dark :: BuiltInTheme
41+
foreign import _infographic :: BuiltInTheme
42+
foreign import _macarons :: BuiltInTheme
43+
foreign import _roma :: BuiltInTheme
44+
foreign import _shine :: BuiltInTheme
45+
foreign import _vintage :: BuiltInTheme

0 commit comments

Comments
 (0)