Skip to content

Commit bae0437

Browse files
committed
refactor: use EffectFn
1 parent c475110 commit bae0437

File tree

10 files changed

+93
-88
lines changed

10 files changed

+93
-88
lines changed

Diff for: src/React/Basic/DOM.js

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
import * as ReactDOM from 'react-dom';
2-
3-
export function renderThen(jsx) {
4-
return (node) => (callback) => () =>
5-
ReactDOM.render(jsx, node, callback);
6-
}
7-
8-
export function hydrateThen(jsx) {
9-
return (node) => (callback) => () =>
10-
ReactDOM.hydrate(jsx, node, callback);
11-
}
12-
13-
export function unmount(node) {
14-
return () => ReactDOM.unmountComponentAtNode(node);
15-
}
16-
17-
export function createPortal(jsx) {
18-
return (node) => ReactDOM.createPortal(jsx, node);
19-
}
20-
21-
export function flushSync(callback) {
22-
return () => ReactDOM.flushSync(callback);
23-
}
1+
import ReactDOM from "react-dom";
2+
3+
// eslint-disable-next-line react/no-deprecated
4+
export const renderThenFn = ReactDOM.render;
5+
// eslint-disable-next-line react/no-deprecated
6+
export const hydrateThenFn = ReactDOM.hydrate;
7+
// eslint-disable-next-line react/no-deprecated
8+
export const unmountComponentAtNode = ReactDOM.unmountComponentAtNode;
9+
export const createPortalFn = ReactDOM.createPortal;
10+
export const flushSyncFn = ReactDOM.flushSync;

Diff for: src/React/Basic/DOM.purs

+18-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ module React.Basic.DOM
1919

2020
import Prelude
2121

22+
import Data.Function.Uncurried (Fn2, runFn2)
2223
import Effect (Effect)
24+
import Effect.Uncurried (EffectFn1, runEffectFn1, EffectFn3, runEffectFn3)
2325
import Prim.TypeError (class Warn, Text)
2426
import React.Basic (JSX)
2527
import React.Basic.DOM.Generated (Props_a, Props_abbr, Props_address, Props_area, Props_article, Props_aside, Props_audio, Props_b, Props_base, Props_bdi, Props_bdo, Props_blockquote, Props_body, Props_br, Props_button, Props_canvas, Props_caption, Props_cite, Props_code, Props_col, Props_colgroup, Props_data, Props_datalist, Props_dd, Props_del, Props_details, Props_dfn, Props_dialog, Props_div, Props_dl, Props_dt, Props_em, Props_embed, Props_fieldset, Props_figcaption, Props_figure, Props_footer, Props_form, Props_h1, Props_h2, Props_h3, Props_h4, Props_h5, Props_h6, Props_head, Props_header, Props_hgroup, Props_hr, Props_html, Props_i, Props_iframe, Props_img, Props_input, Props_ins, Props_kbd, Props_keygen, Props_label, Props_legend, Props_li, Props_link, Props_main, Props_map, Props_mark, Props_math, Props_menu, Props_menuitem, Props_meta, Props_meter, Props_nav, Props_noscript, Props_object, Props_ol, Props_optgroup, Props_option, Props_output, Props_p, Props_param, Props_picture, Props_pre, Props_progress, Props_q, Props_rb, Props_rp, Props_rt, Props_rtc, Props_ruby, Props_s, Props_samp, Props_script, Props_section, Props_select, Props_slot, Props_small, Props_source, Props_span, Props_strong, Props_style, Props_sub, Props_summary, Props_sup, Props_table, Props_tbody, Props_td, Props_template, Props_textarea, Props_tfoot, Props_th, Props_thead, Props_time, Props_title, Props_tr, Props_track, Props_u, Props_ul, Props_var, Props_video, Props_wbr, a, a', a_, abbr, abbr', abbr_, address, address', address_, area, area', article, article', article_, aside, aside', aside_, audio, audio', audio_, b, b', b_, base, base', bdi, bdi', bdi_, bdo, bdo', bdo_, blockquote, blockquote', blockquote_, body, body', body_, br, br', button, button', button_, canvas, canvas', canvas_, caption, caption', caption_, cite, cite', cite_, code, code', code_, col, col', colgroup, colgroup', colgroup_, data', data'', data_, datalist, datalist', datalist_, dd, dd', dd_, del, del', del_, details, details', details_, dfn, dfn', dfn_, dialog, dialog', dialog_, div, div', div_, dl, dl', dl_, dt, dt', dt_, em, em', em_, embed, embed', fieldset, fieldset', fieldset_, figcaption, figcaption', figcaption_, figure, figure', figure_, footer, footer', footer_, form, form', form_, h1, h1', h1_, h2, h2', h2_, h3, h3', h3_, h4, h4', h4_, h5, h5', h5_, h6, h6', h6_, head, head', head_, header, header', header_, hgroup, hgroup', hgroup_, hr, hr', html, html', html_, i, i', i_, iframe, iframe', iframe_, img, img', input, input', ins, ins', ins_, kbd, kbd', kbd_, keygen, keygen', keygen_, label, label', label_, legend, legend', legend_, li, li', li_, link, link', main, main', main_, map, map', map_, mark, mark', mark_, math, math', math_, menu, menu', menu_, menuitem, menuitem', menuitem_, meta, meta', meter, meter', meter_, nav, nav', nav_, noscript, noscript', noscript_, object, object', object_, ol, ol', ol_, optgroup, optgroup', optgroup_, option, option', option_, output, output', output_, p, p', p_, param, param', picture, picture', picture_, pre, pre', pre_, progress, progress', progress_, q, q', q_, rb, rb', rb_, rp, rp', rp_, rt, rt', rt_, rtc, rtc', rtc_, ruby, ruby', ruby_, s, s', s_, samp, samp', samp_, script, script', script_, section, section', section_, select, select', select_, slot, slot', slot_, small, small', small_, source, source', span, span', span_, strong, strong', strong_, style, style', style_, sub, sub', sub_, summary, summary', summary_, sup, sup', sup_, table, table', table_, tbody, tbody', tbody_, td, td', td_, template, template', template_, textarea, textarea', textarea_, tfoot, tfoot', tfoot_, th, th', th_, thead, thead', thead_, time, time', time_, title, title', title_, tr, tr', tr_, track, track', u, u', u_, ul, ul', ul_, var, var', var_, video, video', video_, wbr, wbr') as Generated
@@ -50,9 +52,9 @@ render'
5052
-> Element
5153
-> Effect Unit
5254
-> Effect Unit
53-
render' = renderThen
55+
render' = runEffectFn3 renderThenFn
5456

55-
foreign import renderThen :: JSX -> Element -> Effect Unit -> Effect Unit
57+
foreign import renderThenFn :: EffectFn3 JSX Element (Effect Unit) Unit
5658

5759
-- | Render or update/re-render a component tree into
5860
-- | a DOM element, attempting to reuse the existing
@@ -82,22 +84,28 @@ hydrate'
8284
-> Element
8385
-> Effect Unit
8486
-> Effect Unit
85-
hydrate' = hydrateThen
87+
hydrate' = runEffectFn3 hydrateThenFn
8688

87-
foreign import hydrateThen :: JSX -> Element -> Effect Unit -> Effect Unit
89+
foreign import hydrateThenFn :: EffectFn3 JSX Element (Effect Unit) Unit
8890

8991
-- | Attempt to unmount and clean up the React app
9092
-- | rendered into the given element. Returns `true`
9193
-- | if an app existed and was unmounted successfully.
9294
-- |
9395
-- | __*Note:* Relies on `ReactDOM.unmountComponentAtNode`__
9496
-- | __*Note:* `unmount` has been replaced with `Client.unmountRoot` in React 18
95-
foreign import unmount :: Element -> Effect Boolean
97+
unmount :: Element -> Effect Boolean
98+
unmount = runEffectFn1 unmountComponentAtNode
99+
100+
foreign import unmountComponentAtNode :: EffectFn1 Element Boolean
96101

97102
-- | Divert a render tree into a separate DOM node. The node's
98103
-- | content will be overwritten and managed by React, similar
99104
-- | to `render` and `hydrate`.
100-
foreign import createPortal :: JSX -> Element -> JSX
105+
createPortal :: JSX -> Element -> JSX
106+
createPortal = runFn2 createPortalFn
107+
108+
foreign import createPortalFn :: Fn2 JSX Element JSX
101109

102110
-- | Create a text node.
103111
text :: String -> JSX
@@ -112,4 +120,7 @@ text = unsafeCoerce
112120
-- | flushSync (setMessages (_ <> [msg]))
113121
-- | scrollToLastMessage
114122
-- | ```
115-
foreign import flushSync :: forall a. Effect a -> Effect a
123+
flushSync :: forall a. Effect a -> Effect a
124+
flushSync = runEffectFn1 flushSyncFn
125+
126+
foreign import flushSyncFn :: forall a. EffectFn1 (Effect a) a

Diff for: src/React/Basic/DOM/Client.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import * as ReactDOMClient from "react-dom/client";
1+
export {
2+
createRoot as createRootFn,
3+
hydrateRoot as hydrateRootFn
4+
} from "react-dom/client";
25

3-
export const createRoot = (container) => () =>
4-
ReactDOMClient.createRoot(container);
6+
export const renderRootFn = (root, children) => root.render(children);
57

6-
export const hydrateRoot = (container) => (initialChildren) => () =>
7-
ReactDOMClient.hydrateRoot(container, initialChildren);
8-
9-
export const renderRoot = (root) => (children) => () => root.render(children);
10-
11-
export const unmountRoot = (root) => () => root.unmount(root);
8+
export const unmountRootFn = (root) => root.unmount(root);

Diff for: src/React/Basic/DOM/Client.purs

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,32 @@ import Prelude
44
import React.Basic (JSX)
55
import Web.DOM (Element)
66
import Effect (Effect)
7+
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
78

89
foreign import data ReactRoot :: Type
910

1011
-- | Create a React root for the supplied container and return the root.
1112
-- | The root can be used to render a React element into the DOM with `render.`
1213
-- | Replaces `ReactDOM.render` when the `.render` method is called and enables Concurrent Mode.
13-
foreign import createRoot :: Element -> Effect ReactRoot
14+
createRoot :: Element -> Effect ReactRoot
15+
createRoot = runEffectFn1 createRootFn
16+
foreign import createRootFn :: EffectFn1 Element ReactRoot
1417

1518
-- | Same as `createRoot`, but is used to hydrate a container whose HTML contents were rendered by ReactDOMServer.
1619
-- | React will attempt to attach event listeners to the existing markup.
17-
foreign import hydrateRoot :: Element -> JSX -> Effect ReactRoot
20+
hydrateRoot :: Element -> JSX -> Effect ReactRoot
21+
hydrateRoot = runEffectFn2 hydrateRootFn
22+
23+
foreign import hydrateRootFn :: EffectFn2 Element JSX ReactRoot
1824

1925
-- | Render children in `ReactRoot`
20-
foreign import renderRoot :: ReactRoot -> JSX -> Effect Unit
26+
renderRoot :: ReactRoot -> JSX -> Effect Unit
27+
renderRoot = runEffectFn2 renderRootFn
28+
29+
foreign import renderRootFn :: EffectFn2 ReactRoot JSX Unit
2130

2231
-- | Unmount the react root
23-
foreign import unmountRoot :: ReactRoot -> Effect Unit
32+
unmountRoot :: ReactRoot -> Effect Unit
33+
unmountRoot = runEffectFn1 unmountRootFn
34+
35+
foreign import unmountRootFn :: EffectFn1 ReactRoot Unit

Diff for: src/React/Basic/DOM/Components/GlobalEvents.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
export var _passiveSupported = null;
2+
export let _passiveSupported = null;
33

44
function checkPassiveSupported() {
55
if (_passiveSupported == null) {
@@ -42,7 +42,7 @@ function down(target, eventType, handler, options) {
4242
);
4343
}
4444

45-
var GlobalEvent = function() {
45+
const GlobalEvent = function() {
4646
return this;
4747
};
4848

@@ -86,7 +86,7 @@ GlobalEvent.prototype.render = function() {
8686

8787
export { GlobalEvent as globalEvent_ };
8888

89-
export var unsafeWindowEventTarget = (function() {
89+
export const unsafeWindowEventTarget = (function() {
9090
if (typeof window === "undefined") {
9191
return undefined;
9292
} else {

Diff for: src/React/Basic/DOM/Components/Ref.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react";
22

33
export function mkRef(toMaybe) {
4-
var Ref = function(_props) {
4+
const Ref = function(_props) {
55
this.state = { node: null };
66
this.ref = React.createRef();
77
return this;
@@ -12,9 +12,9 @@ export function mkRef(toMaybe) {
1212
Ref.displayName = "Ref";
1313

1414
Ref.prototype.syncRef = function() {
15-
var selector = this.props.selector;
16-
var current = this.ref.current;
17-
var next =
15+
const selector = this.props.selector;
16+
const current = this.ref.current;
17+
const next =
1818
selector === null ? current : current && current.querySelector(selector);
1919
if (this.state.node !== next) {
2020
this.setState({ node: next });

Diff for: src/React/Basic/DOM/Events.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
3-
export const propagateThis = (f) => (t) => () => {
1+
export function propagateThis(f, t) {
42
return f.call(t);
5-
}
3+
}

Diff for: src/React/Basic/DOM/Events.purs

+4-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import Data.Maybe (Maybe)
5050
import Data.Nullable (toMaybe)
5151
import Effect (Effect)
5252
import Effect.Unsafe (unsafePerformEffect)
53+
import Effect.Uncurried (EffectFn2, runEffectFn2)
5354
import React.Basic.Events (EventFn, EventHandler, SyntheticEvent, handler, unsafeEventFn)
5455
import Unsafe.Coerce (unsafeCoerce)
5556
import Web.Event.Internal.Types (Event, EventTarget)
@@ -102,7 +103,7 @@ nativeEvent = unsafeEventFn \e -> (unsafeCoerce e).nativeEvent
102103

103104
preventDefault :: EventFn SyntheticEvent SyntheticEvent
104105
preventDefault = unsafeEventFn \e -> unsafePerformEffect do
105-
_ <- propagateThis (unsafeCoerce e).preventDefault e
106+
_ <- runEffectFn2 propagateThis (unsafeCoerce e).preventDefault e
106107
pure e
107108

108109
isDefaultPrevented :: EventFn SyntheticEvent Boolean
@@ -111,7 +112,7 @@ isDefaultPrevented = unsafeEventFn \e -> unsafePerformEffect do
111112

112113
stopPropagation :: EventFn SyntheticEvent SyntheticEvent
113114
stopPropagation = unsafeEventFn \e -> unsafePerformEffect do
114-
_ <- propagateThis (unsafeCoerce e).stopPropagation e
115+
_ <- runEffectFn2 propagateThis (unsafeCoerce e).stopPropagation e
115116
pure e
116117

117118
isPropagationStopped :: EventFn SyntheticEvent Boolean
@@ -208,4 +209,4 @@ clipboardData = unsafeEventFn \e -> toMaybe (unsafeCoerce e).clipboardData
208209
compositionData :: EventFn SyntheticEvent (Maybe String)
209210
compositionData = unsafeEventFn \e -> toMaybe (unsafeCoerce e).data
210211

211-
foreign import propagateThis :: forall f t a. f -> t -> Effect a
212+
foreign import propagateThis :: forall f t a. EffectFn2 f t a

Diff for: src/React/Basic/DOM/Internal.js

+24-27
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,28 @@ export function mergeStyles(styles) {
44
return Object.assign.apply(null, [{}].concat(styles));
55
}
66

7-
export function unsafeCreateDOMComponent_(createElement) {
8-
return (el) => {
9-
const flattenDataProp = (props, ref) => {
10-
var data = null;
11-
if (props._data != null) {
12-
data = { _data: undefined };
13-
Object.entries(props._data).forEach(function(entry) {
14-
data["data-" + entry[0]] = entry[1];
15-
});
16-
}
17-
var aria = null;
18-
if (props._aria != null) {
19-
aria = { _aria: undefined };
20-
Object.entries(props._aria).forEach(function(entry) {
21-
aria["aria-" + entry[0]] = entry[1];
22-
});
23-
}
24-
return Object.assign({ ref }, props, data, aria);
25-
};
26-
return () => {
27-
const c = React.forwardRef((props, ref) =>
28-
createElement(el)(flattenDataProp(props, ref))
29-
);
30-
c.displayName = el;
31-
return c;
32-
};
33-
};
7+
const flattenDataProp = (props, ref) => {
8+
let data = null;
9+
if (props._data != null) {
10+
data = { _data: undefined };
11+
Object.entries(props._data).forEach(function (entry) {
12+
data["data-" + entry[0]] = entry[1];
13+
});
14+
}
15+
let aria = null;
16+
if (props._aria != null) {
17+
aria = { _aria: undefined };
18+
Object.entries(props._aria).forEach(function (entry) {
19+
aria["aria-" + entry[0]] = entry[1];
20+
});
21+
}
22+
return Object.assign({ ref }, props, data, aria);
23+
};
24+
25+
export function unsafeCreateDOMComponent_(createElement, el) {
26+
const c = React.forwardRef((props, ref) =>
27+
createElement(el)(flattenDataProp(props, ref))
28+
);
29+
c.displayName = el;
30+
return c;
3431
}

Diff for: src/React/Basic/DOM/Internal.purs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module React.Basic.DOM.Internal where
22

33
import Prelude
44
import Effect (Effect)
5+
import Effect.Uncurried (runEffectFn2, EffectFn2)
56
import React.Basic (JSX, ReactComponent, element)
67
import React.Basic.Events (EventHandler)
78
import Unsafe.Coerce (unsafeCoerce)
@@ -71,10 +72,11 @@ type SharedSVGProps specific
7172
)
7273

7374
unsafeCreateDOMComponent :: forall props. String -> Effect (ReactComponent { | props })
74-
unsafeCreateDOMComponent = unsafeCreateDOMComponent_ element
75+
unsafeCreateDOMComponent = runEffectFn2 unsafeCreateDOMComponent_ element
7576

7677
foreign import unsafeCreateDOMComponent_ ::
7778
forall props.
78-
(ReactComponent props -> props -> JSX) ->
79-
String ->
80-
Effect (ReactComponent props)
79+
EffectFn2
80+
(ReactComponent props -> props -> JSX)
81+
String
82+
(ReactComponent props)

0 commit comments

Comments
 (0)