Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@
$ctr.remove()
}

function setupIFrame(data) {
function setupIFrame(data, loadCb, failCb) {
var $ctr = $("#column2");
var $iframe = $(
'<iframe sandbox="allow-scripts allow-forms" id="output-iframe" src="frame.html">'
);

$ctr.empty().append($iframe);

var tries = 0;

var sendSources = setInterval(function () {
// Stop after 10 seconds
if (tries >= 100) {
Expand All @@ -47,7 +46,9 @@
var iframe = $iframe.get(0).contentWindow;
if (iframe) {
iframe.postMessage(data, "*");
loadCb();
} else {
failCb();
console.warn("Frame is not available");
}
}, 100);
Expand Down
16 changes: 11 additions & 5 deletions client/src/Try/Container.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import Data.FoldableWithIndex (foldMapWithIndex)
import Data.Maybe (Maybe(..), fromMaybe, isNothing)
import Data.Symbol (SProxy(..))
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff (Aff, makeAff)
import Effect.Aff as Aff
import Effect.Class.Console (error)
import Effect.Uncurried (EffectFn1, runEffectFn1)
import Effect.Uncurried (EffectFn3, runEffectFn3)
import Foreign.Object (Object)
import Foreign.Object as Object
import Halogen as H
Expand Down Expand Up @@ -85,7 +86,9 @@ _editor = SProxy
loader :: Loader
loader = makeLoader Config.loaderUrl

foreign import setupIFrame :: EffectFn1 (Object JS) Unit
type LoadCb = Effect Unit
type FailCb = Effect Unit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these type aliases are needed, but 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with them in this case because it can be otherwise difficult to tell which is the 'load' callback and which is the 'fail' callback. In other projects I also use records to help, ie. { fail :: Effect Unit, load :: Effect Unit }.

foreign import setupIFrame :: EffectFn3 (Object JS) LoadCb FailCb Unit
foreign import teardownIFrame :: Effect Unit

component :: forall q i o. H.Component HH.HTML q i o Aff
Expand Down Expand Up @@ -182,7 +185,7 @@ component = H.mkComponent
pure unit

Right (Right res@(CompileSuccess { js, warnings })) -> do
{ settings } <- H.modify _ { compiled = Just (Right res) }
{ settings } <- H.get
if settings.showJs then
H.liftEffect teardownIFrame
else do
Expand All @@ -193,7 +196,10 @@ component = H.mkComponent
pure unit
for_ mbSources \sources -> do
let eventData = Object.insert "<file>" (JS js) sources
H.liftEffect $ runEffectFn1 setupIFrame eventData
H.liftAff $ makeAff \f -> do
runEffectFn3 setupIFrame eventData (f (Right unit)) (f (Left $ Aff.error "Could not load iframe"))
mempty
H.modify_ _ { compiled = Just (Right res) }

HandleEditor (Editor.TextChanged text) -> do
_ <- H.fork $ handleAction $ Cache text
Expand Down