Skip to content

Commit 5cf3975

Browse files
author
Michael Trotter
committed
Merge branch 'master' of github.com:lumihq/purescript-react-basic into api-exp-2
2 parents e8bf0dc + c8669be commit 5cf3975

12 files changed

Lines changed: 81 additions & 29 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/.psc*
77
/.purs*
88
/.psa*
9+
/.vscode/

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

bower.json

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
{
22
"name": "purescript-react-basic",
33
"license": "Apache-2.0",
4-
"ignore": [
5-
"**/.*",
6-
"node_modules",
7-
"bower_components",
8-
"output"
9-
],
4+
"ignore": ["**/.*", "node_modules", "bower_components", "output"],
105
"repository": {
116
"type": "git",
127
"url": "git://github.com/lumihq/purescript-react-basic.git"
138
},
149
"dependencies": {
10+
"purescript-aff": "^5.0.2",
11+
"purescript-console": "^4.1.0",
12+
"purescript-effect": "^2.0.0",
13+
"purescript-exceptions": "^4.0.0",
1514
"purescript-functions": "^4.0.0",
16-
"purescript-unsafe-coerce": "^4.0.0",
1715
"purescript-nullable": "^4.1.0",
18-
"purescript-typelevel-prelude": "^3.0.0",
1916
"purescript-record": "^1.0.0",
20-
"purescript-effect": "^2.0.0",
21-
"purescript-web-events": "^1.0.0",
17+
"purescript-typelevel-prelude": "^3.0.0",
18+
"purescript-unsafe-coerce": "^4.0.0",
2219
"purescript-web-dom": "^1.0.0",
23-
"purescript-exceptions": "^4.0.0"
20+
"purescript-web-events": "^1.0.0"
2421
},
2522
"devDependencies": {
2623
"purescript-web-html": "^1.0.0"

examples/component/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
all: node_modules
22
purs compile src/*.purs '../../src/**/*.purs' '../../bower_components/purescript-*/src/**/*.purs'
3-
purs bundle output/*/*.js > output/bundle.js
4-
echo 'PS.Main.main();' >> output/bundle.js
3+
purs bundle -m Main --main Main output/*/*.js > output/bundle.js
54
node_modules/.bin/browserify output/bundle.js -o html/index.js
65

76
node_modules:

examples/component/src/ToggleButton.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ToggleButton where
22

33
import Prelude
44

5+
import Effect.Console (log)
56
import React.Basic (Component, JSX, StateUpdate(..), createComponent, make)
67
import React.Basic.DOM as R
78
import React.Basic.Events as Events
@@ -20,7 +21,11 @@ render = make component
2021
}
2122

2223
, update = \self -> case _ of
23-
Toggle -> Update self.state { on = not self.state.on }
24+
Toggle ->
25+
UpdateAndSideEffects
26+
self.state { on = not self.state.on }
27+
\nextSelf -> do
28+
log $ "next state: " <> show nextSelf.state
2429

2530
, render = \self ->
2631
R.button

examples/controlled-input/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
all: node_modules
22
purs compile src/*.purs '../../src/**/*.purs' '../../bower_components/purescript-*/src/**/*.purs'
3-
purs bundle output/*/*.js > output/bundle.js
4-
echo 'PS.Main.main();' >> output/bundle.js
3+
purs bundle -m Main --main Main output/*/*.js > output/bundle.js
54
node_modules/.bin/browserify output/bundle.js -o html/index.js
65

76
node_modules:

examples/counter/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
all: node_modules
22
purs compile src/*.purs '../../src/**/*.purs' '../../bower_components/purescript-*/src/**/*.purs'
3-
purs bundle output/*/*.js > output/bundle.js
4-
echo 'PS.Main.main();' >> output/bundle.js
3+
purs bundle -m Main --main Main output/*/*.js > output/bundle.js
54
node_modules/.bin/browserify output/bundle.js -o html/index.js
65

76
node_modules:

examples/counter/src/Main.purs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import Web.HTML (window)
1212
import Web.HTML.HTMLDocument (toNonElementParentNode)
1313
import Web.HTML.Window (document)
1414

15-
-- dummy :: Component
16-
-- dummy = stateless { displayName: "dummy", render }
17-
-- where
18-
-- render _ =
19-
2015
main :: Effect Unit
2116
main = do
2217
container <- getElementById "container" =<< (map toNonElementParentNode $ document =<< window)

src/React/Basic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ exports.createComponent_ = function(noUpdate, buildStateUpdate, displayName) {
2525
{ $$state: updates.state },
2626
updates.effects !== null
2727
? function() {
28-
updates.effects(contextToSelf(this));
28+
updates.effects(contextToSelf(this))();
2929
}
3030
: undefined
3131
);
3232
} else if (updates.effects !== null) {
33-
updates.effects(self);
33+
updates.effects(self)();
3434
}
3535
};
3636
},

src/React/Basic.purs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1-
module React.Basic where
1+
module React.Basic
2+
( Component
3+
, StatelessComponent
4+
, ComponentSpec
5+
, JSX
6+
, Update
7+
, StateUpdate(..)
8+
, Self
9+
, LimitedSelf
10+
, ReactComponent
11+
, ReactComponentInstance
12+
, make
13+
, makeStateless
14+
, asyncEffects
15+
, createComponent
16+
, createStatelessComponent
17+
, empty
18+
, keyed
19+
, fragment
20+
, fragmentKeyed
21+
, element
22+
, elementKeyed
23+
) where
224

325
import Prelude
426

27+
import Data.Either (Either(..))
528
import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
629
import Data.Nullable (Nullable, notNull, null)
730
import Effect (Effect)
31+
import Effect.Aff (Aff, runAff_)
32+
import Effect.Console (error)
833
import Unsafe.Coerce (unsafeCoerce)
934

1035
-- | A virtual DOM element.
@@ -42,6 +67,23 @@ data StateUpdate props state action
4267
| SideEffects (Self props state action -> Effect Unit)
4368
| UpdateAndSideEffects state (Self props state action -> Effect Unit)
4469

70+
-- | Convenience function for sending an action asynchronously.
71+
-- |
72+
-- | Note: potential failure should be handled and converted to an
73+
-- | action, as the default error handler will simply log the
74+
-- | error to the console.
75+
asyncEffects
76+
:: forall props state action
77+
. (Self props state action -> Aff action)
78+
-> Self props state action
79+
-> Effect Unit
80+
asyncEffects work self = runAff_ handle (work self)
81+
where
82+
handle (Right action) = self.send action
83+
handle (Left err) = do
84+
error "An async action failed."
85+
error (unsafeCoerce err)
86+
4587
buildStateUpdate
4688
:: forall props state action
4789
. StateUpdate props state action
@@ -125,6 +167,10 @@ type Self props state action =
125167
, readProps :: Effect props
126168
, readState :: Effect state
127169
, send :: action -> Effect Unit
170+
171+
-- | Unsafe, but still frequently better than rewriting a
172+
-- | whold component in JS
173+
, instance_ :: ReactComponentInstance
128174
}
129175

130176
type LimitedSelf props state =

0 commit comments

Comments
 (0)