Skip to content

Commit b4d01ee

Browse files
committed
Add UIRef
This commit adds an opaque `UIRef props state` type constructor which represents a reference to UI component. The following functions available for working with UI refs: * getSelf returns a refernce to a current React component * runUI is a handler which gets an UI reference and executes React effects with it (it also checks if current UI reference is still mounted and so valid) The motivation for this change is to add an ability to defer React for a some period of time (XHR, timer).
1 parent 4b66544 commit b4d01ee

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/React.purs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module React where
77
foreign import data ReadStateEff :: * -> !
88
foreign import data WriteStateEff :: * -> !
99
foreign import data UI :: *
10+
foreign import data UIRef :: * -> * -> *
1011
foreign import data EventHandler :: * -> *
1112

1213
foreign import noop0
@@ -99,6 +100,33 @@ module React where
99100
\ }"
100101
:: forall state eff. Eff (r :: ReadStateEff state | eff) state
101102

103+
foreign import getSelf
104+
" function getSelf() { \
105+
\ return __current; \
106+
\ }"
107+
:: forall eff props state.
108+
Eff (p :: ReadPropsEff props, r :: ReadStateEff state | eff) (UIRef props state)
109+
110+
foreign import runUI
111+
" function runUI(ref) { \
112+
\ return function(action) { \
113+
\ return function() { \
114+
\ if (ref.isMounted()) { \
115+
\ __current = ref; \
116+
\ try { \
117+
\ return action(); \
118+
\ } finally { \
119+
\ __current = null; \
120+
\ } \
121+
\ } \
122+
\ } \
123+
\ } \
124+
\ }"
125+
:: forall eff props state result.
126+
UIRef props state
127+
-> Eff (p :: ReadPropsEff props, r :: ReadStateEff state, w :: WriteStateEff state | eff) result
128+
-> Eff (eff) result
129+
102130
foreign import mkUI
103131
" var __current; \
104132
\ function mkUI(ss) { \

0 commit comments

Comments
 (0)