Skip to content

Commit

Permalink
fix: dispatch from python.cljs
Browse files Browse the repository at this point in the history
  • Loading branch information
dviramontes committed Dec 29, 2017
1 parent 2509966 commit 8e62a6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
20 changes: 16 additions & 4 deletions src/klipse/klipse_editors.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns klipse.klipse-editors
(:require-macros
[gadjett.core :refer [dbg]]
[purnam.core :refer [!]]
[purnam.core :refer [! !>]]
[cljs.core.async.macros :refer [go go-loop]])
(:require
[goog.dom :as gdom]
Expand Down Expand Up @@ -51,7 +51,14 @@
(swap! state update-in [:eval-counter] inc)
(let [evaluation-chan (eval-fn (str preamble src-code) @state)
first-result (<! evaluation-chan)]
(setter first-result)
;; if first-result is String then continue as normal
;; otherwise dispatch custom event with payload
(if (string? first-result)
(setter first-result)
(when-let [klipse-dom-node (js/document.querySelector ".klipse-container")]
(let [event-payload (clj->js {:detail {:state first-result}})]
(!> klipse-dom-node.dispatchEvent
(js/CustomEvent. "klipse-snippet-evaled" event-payload)))))
(when loop-msec
(go-loop []
(let [[cmd c] (alts! [cmd-chan (timeout loop-msec)])]
Expand All @@ -65,7 +72,12 @@
(let [result (<! evaluation-chan)
results (str previous-results result)]
(when (some? result) ;exit if the channel is closed
(setter results)
(if (string? result)
(setter results)
(when-let [klipse-dom-node (js/document.querySelector ".klipse-container")]
(let [event-payload (clj->js {:detail {:state result}})]
(!> klipse-dom-node.dispatchEvent
(js/CustomEvent. "klipse-snippet-evaled" event-payload)))))
(recur results))))))
(catch :default e
(setter e))))))
Expand Down Expand Up @@ -102,7 +114,7 @@
(s/def ::codemirror-options map?)
(s/def ::editor-mode string?)

(s/fdef editor-options
(s/fdef editor-options
:args (s/cat :in-mode ::editor-mode
:out-mode ::editor-mode
:options-in ::codemirror-options
Expand Down
12 changes: 7 additions & 5 deletions src/klipse/lang/python.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
(init)
(let [c (chan)]
(!> js/Sk.configure #js {:output #(put! c %)
:read builtin-read })
(->
:read builtin-read})
(->
(!> js/Sk.misceval.asyncToPromise
(fn []
(put! c "Output:\n")
(put! c "Output\n")
(! js/Sk.TurtleGraphics.target container-id)
(!> js/Sk.importMainWithBody "<stdin>" false exp true)))
(.then (fn [mod]
(!> js/console.info "success to eval skulpt: "))
(put! c [:ok mod])
(!> js/console.info "success to eval skulpt"))
(fn [err]
(put! c (str "error: " err)))))
(put! c (str "error: " err))
(put! c [:err err]))))
c))

(def opts {:editor-in-mode "python"
Expand Down

0 comments on commit 8e62a6a

Please sign in to comment.