Skip to content

Commit

Permalink
Merge pull request #10 from smucclaw/cleanup-swipl-wasm
Browse files Browse the repository at this point in the history
dynamic import swipl-wasm and guifier once at top-level
  • Loading branch information
joewatt95 authored Apr 4, 2024
2 parents 0549519 + 67b67de commit 53bdf56
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ jobs:
- name: Precompile SWI-Prolog files to qlf
run: pnpm build:swipl-public-qlf

- name: Build Clojurescript web editor app
run: pnpm build:webeditor
- name: Build web editor app
run: pnpm build:web-editor

- name: Setup Pages
uses: actions/configure-pages@v5
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
},
"scripts": {
"preinstall": "npx only-allow pnpm",
"start": "shadow-cljs watch webeditor",
"build:webeditor": "shadow-cljs release webeditor",
"start": "shadow-cljs watch web-editor",
"build:web-editor": "shadow-cljs release web-editor",
"build:browser-esm-lib": "shadow-cljs release browser-esm-lib",
"build:py-node-lib": "shadow-cljs release py-node-lib",
"build:node-lib": "shadow-cljs release node-lib",
"build:all": "shadow-cljs release webeditor && shadow-cljs release browser-esm-lib && shadow-cljs release node-lib && shadow-cljs release py-node-lib",
"build:all": "shadow-cljs release web-editor && shadow-cljs release browser-esm-lib && shadow-cljs release node-lib && shadow-cljs release py-node-lib",
"build:swipl-qlfs": "cd public/resources/swipl && swipl -t 'qcompile(prelude, [include(user)])' && cd ../../../src/l4_lp_py/swipl/prolog && swipl -t 'qcompile(prelude, [include(user)])' && cd ../../../../../../../",
"build:swipl-public-qlf": "cd public/resources/swipl && swipl -t 'qcompile(prelude, [include(user)])'",
"run:node-eg": "poetry run node public/node_example_usage.js",
Expand Down
8 changes: 4 additions & 4 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
{:deps true
:dev-http {8000 "public"}
:builds
{:webeditor
{:web-editor
{:target :esm
:output-dir "public/js"
:modules {:l4_webeditor {:init-fn l4-lp.webeditor.main/init!}}
:devtools {:before-load l4-lp.webeditor.main/stop!
:after-load l4-lp.webeditor.main/start!}
:modules {:l4_web_editor {:init-fn l4-lp.web-editor.main/init!}}
:devtools {:before-load l4-lp.web-editor.main/stop!
:after-load l4-lp.web-editor.main/start!}
:compiler-options {:output-feature-set :es2021
:externs ["datascript/externs.js"]}}

Expand Down
12 changes: 6 additions & 6 deletions src/l4_lp/swipl/js/wasm_query.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
[promesa.core :as prom]
[shadow.esm :refer [dynamic-import]]))

(def ^:private swipl-wasm-cdn-url
"https://SWI-Prolog.github.io/npm-swipl-wasm/3/7/5/dynamic-import.js")
(def ^:private swipl-constructor
(prom/let [cdn-url "https://SWI-Prolog.github.io/npm-swipl-wasm/3/7/5/dynamic-import.js"
mod (dynamic-import cdn-url)]
(jsi/get mod :SWIPL)))

(def ^:private prelude-qlf-url
"resources/swipl/prelude.qlf")
Expand All @@ -17,10 +19,8 @@
;; TODO: Document and clean up this function.
(defn query-and-trace! [program goal]
(prom/let
[^js swipl-mod (dynamic-import swipl-wasm-cdn-url)
swipl (-> swipl-mod
(jsi/get :SWIPL)
(new #js {:arguments #js ["-q"]}))
[Swipl swipl-constructor
swipl (Swipl. #js {:arguments #js ["-q"]})

stack-trace (transient [])

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(ns l4-lp.webeditor.codemirror-editor
(ns l4-lp.web-editor.codemirror-editor
(:require ["@codemirror/commands" :as cm-cmds]
["@codemirror/language" :as cm-lang]
["@codemirror/state" :as cm-state]
["@codemirror/view" :as cm-view]
["@nextjournal/clojure-mode" :as cm-clj]
["@uiw/codemirror-theme-solarized" :as cm-solarized]
[applied-science.js-interop :as jsi]
[l4-lp.webeditor.guifier :refer [query-and-trace-and-guifier!]]))
[l4-lp.web-editor.guifier :refer [query-and-trace-and-guifier!]]))

;; https://blog.jakubholy.net/2023/interactive-code-snippets-fulcro/
;; https://github.com/nextjournal/clojure-mode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
(ns l4-lp.webeditor.guifier
(ns l4-lp.web-editor.guifier
(:require [applied-science.js-interop :as jsi]
[l4-lp.swipl.js.wasm-query :as swipl-wasm-query]
[l4-lp.syntax.l4-to-prolog :as l4->prolog]
[promesa.core :as prom]
[shadow.esm :refer [dynamic-import]]))

(def ^:private guifier
{:cdn-url "https://cdn.jsdelivr.net/npm/guifier@1.0.24/dist/Guifier.js"
:element-id "guifier"
:constructor (atom nil)})

(defn init! []
(prom/let [guifier-mod (dynamic-import (:cdn-url guifier))
Guifier (jsi/get guifier-mod :default)]
(reset! (:constructor guifier) Guifier)))
{:element-id "guifier"
:constructor
(prom/let
[cdn-url "https://cdn.jsdelivr.net/npm/guifier@1.0.24/dist/Guifier.js"
mod (dynamic-import cdn-url)]
(jsi/get mod :default))})

(def ^:private query
(-> "query" l4->prolog/l4->prolog-str))
Expand All @@ -31,7 +29,7 @@

stack-trace (swipl-wasm-query/query-and-trace-js! program query)

Guifier @(:constructor guifier)]
Guifier (:constructor guifier)]
(Guifier. #js {:data stack-trace
:dataType "js"
:elementSelector (str "#" (:element-id guifier))
Expand Down
11 changes: 4 additions & 7 deletions src/l4_lp/webeditor/main.cljs → src/l4_lp/web_editor/main.cljs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(ns l4-lp.webeditor.main
(ns l4-lp.web-editor.main
(:require [applied-science.js-interop :as jsi]
[hoplon.core :as h]
[hoplon.goog]
[l4-lp.webeditor.codemirror-editor :refer [bind-editor!]]
[l4-lp.webeditor.guifier :as guifier]
[promesa.core :as prom]))
[l4-lp.web-editor.codemirror-editor :refer [bind-editor!]]))

(def ^:private initial-editor-text
";; Enter an L4 program here, and then press M-Enter to evaluate the query.
Expand Down Expand Up @@ -74,6 +72,5 @@ DECIDE b of 0 and _ OTHERWISE")

(defn init! []
(mount-components!)
(prom/let [_ (guifier/init!)]
(bind-editor! initial-editor-text)
(start!)))
(bind-editor! initial-editor-text)
(start!))

0 comments on commit 53bdf56

Please sign in to comment.