Skip to content

Commit d3f8cae

Browse files
committed
complete console and react output
1 parent 35c3982 commit d3f8cae

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

src/Playground.res

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,23 +1211,26 @@ let locMsgToCmError = (~kind: CodeMirror.Error.kind, locMsg: Api.LocMsg.t): Code
12111211
module RenderOutput = {
12121212
@react.component
12131213
let make = (~compilerState: CompilerManagerHook.state) => {
1214-
let code = switch compilerState {
1215-
| Ready(ready) =>
1216-
switch ready.result {
1217-
| Comp(Success(_)) => ControlPanel.codeFromResult(ready.result)->Some
1214+
React.useEffect(() => {
1215+
let code = switch compilerState {
1216+
| Ready(ready) =>
1217+
switch ready.result {
1218+
| Comp(Success(_)) => ControlPanel.codeFromResult(ready.result)->Some
1219+
| _ => None
1220+
}
12181221
| _ => None
12191222
}
1220-
| _ => None
1221-
}
12221223

1223-
let _valid = switch code {
1224-
| Some(code) =>
1225-
switch RenderOutputManager.renderOutput(code) {
1226-
| Ok(_) => true
1227-
| Error(_) => false
1224+
let _valid = switch code {
1225+
| Some(code) =>
1226+
switch RenderOutputManager.renderOutput(code) {
1227+
| Ok(_) => true
1228+
| Error(_) => false
1229+
}
1230+
| None => false
12281231
}
1229-
| None => false
1230-
}
1232+
None
1233+
}, [compilerState])
12311234

12321235
<div className={""}>
12331236
<iframe
@@ -1287,17 +1290,20 @@ module OutputPanel = {
12871290
let (logs, setLogs) = React.useState(_ => [])
12881291

12891292
React.useEffect(() => {
1290-
Webapi.Window.addEventListener("message", e => {
1293+
RescriptCore.Console.log2("logs", logs)
1294+
let cb = e => {
1295+
// RescriptCore.Console.log2("eventListener, logs", logs)
12911296
let data = e["data"]
12921297
let type_: string = data["type"]
12931298

12941299
if type_ === "log" {
12951300
let args: array<string> = data["args"]
12961301

1297-
setLogs(_ => logs->Belt.Array.concat([args]))
1302+
setLogs(previous => previous->Belt.Array.concat([args]))
12981303
}
1299-
})
1300-
None
1304+
}
1305+
Webapi.Window.addEventListener("message", cb)
1306+
Some(() => Webapi.Window.removeEventListener("message", cb))
13011307
}, [])
13021308

13031309
let cmCode = switch prevState.current {
@@ -1460,7 +1466,7 @@ module App = {
14601466
}
14611467
`
14621468

1463-
let since_10_1 = `@@jsxConfig({version: 4, mode: "classic"})
1469+
let _since_10_1 = `@@jsxConfig({version: 4, mode: "classic"})
14641470
14651471
module Button = {
14661472
@react.component
@@ -1484,6 +1490,10 @@ module App = {
14841490
}
14851491
}
14861492
1493+
`
1494+
let since_10_1 = `
1495+
Console.log(1)
1496+
Console.log(2)
14871497
`
14881498
}
14891499

src/bindings/Webapi.res

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module Element = {
2424
type contentWindow
2525
@get external contentWindow: Dom.element => option<contentWindow> = "contentWindow"
2626

27-
@send external postMessage: (contentWindow, string, string) => unit = "postMessage"
27+
@send
28+
external postMessage: (contentWindow, string, ~targetOrigin: string=?) => unit = "postMessage"
2829

2930
module Style = {
3031
@scope("style") @set external width: (Dom.element, string) => unit = "width"

src/common/RenderOutputManager.res

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module Frame = {
7474
switch frame {
7575
| Some(element) =>
7676
switch element->Element.contentWindow {
77-
| Some(win) => win->Element.postMessage(code, "*")
77+
| Some(win) => win->Element.postMessage(code, ~targetOrigin="*")
7878
| None => ()
7979
}
8080
| None => ()
@@ -90,6 +90,7 @@ let renderOutput = code => {
9090
Transpiler.run(transpiled)->Frame.sendOutput
9191
Ok()
9292
| false =>
93+
RescriptCore.Console.log("run without entry point")
9394
Frame.sendOutput(transpiled)
9495
Error()
9596
}

0 commit comments

Comments
 (0)