Use dynamic import for wasm module

This commit is contained in:
Belén Albeza 2024-09-13 15:14:10 +02:00
parent cc7ed497e8
commit 884ceb052b
9 changed files with 54 additions and 44 deletions

View file

@ -74,6 +74,7 @@
[app.main.repo :as rp]
[app.main.streams :as ms]
[app.main.worker :as uw]
[app.renderer-v2 :as renderer]
[app.util.dom :as dom]
[app.util.globals :as ug]
[app.util.http :as http]
@ -81,7 +82,6 @@
[app.util.router :as rt]
[app.util.storage :refer [storage]]
[app.util.timers :as tm]
[app.util.wasm :as wasm]
[app.util.webapi :as wapi]
[beicon.v2.core :as rx]
[cljs.spec.alpha :as s]
@ -350,10 +350,12 @@
(rx/merge
(rx/of (ntf/hide)
(features/initialize)
(when (contains? cf/flags :renderer-v2) (wasm/init))
(dcm/retrieve-comment-threads file-id)
(fetch-bundle project-id file-id))
(when (contains? cf/flags :renderer-v2)
(rx/of (renderer/init)))
(->> stream
(rx/filter dch/commit?)
(rx/map deref)

View file

@ -8,6 +8,7 @@
(:require-macros [app.main.style :as stl])
(:require
[app.common.data.macros :as dm]
[app.config :as cf]
[app.main.data.modal :as modal]
[app.main.data.notifications :as ntf]
[app.main.data.persistence :as dps]
@ -31,6 +32,7 @@
[app.main.ui.workspace.sidebar.collapsable-button :refer [collapsed-button]]
[app.main.ui.workspace.sidebar.history :refer [history-toolbox]]
[app.main.ui.workspace.viewport :refer [viewport]]
[app.renderer-v2 :as renderer]
[app.util.debug :as dbg]
[app.util.dom :as dom]
[app.util.globals :as globals]
@ -198,6 +200,10 @@
(ntf/hide)
(dw/finalize-file project-id file-id))))
(mf/with-effect [file-ready?]
(when (and file-ready? (contains? cf/flags :renderer-v2))
(renderer/print-msg "hello from wasm fn!")))
[:& (mf/provider ctx/current-file-id) {:value file-id}
[:& (mf/provider ctx/current-project-id) {:value project-id}
[:& (mf/provider ctx/current-team-id) {:value team-id}
@ -208,7 +214,6 @@
:style {:background-color background-color
:touch-action "none"}}
[:& context-menu]
(if ^boolean file-ready?
[:& workspace-page {:page-id page-id
:file file

View file

@ -0,0 +1,38 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.renderer-v2
(:require
[app.config :as cf]
[beicon.v2.core :as rx]
[potok.v2.core :as ptk]))
(defonce internal-module #js {})
(defn on-module-loaded
[module']
(let [init-fn (.-default ^js module')]
(->> (rx/from (init-fn))
(rx/map (constantly module')))))
(defn- on-module-initialized
[module]
(set! internal-module module))
(defn print-msg [msg]
(let [print-fn (.-print internal-module)]
(print-fn msg)))
(defn init
[]
(ptk/reify ::init
ptk/WatchEvent
(watch [_ _ _]
(let [module-uri (assoc cf/public-uri :path "/js/renderer/renderer.js")]
(->> (rx/from (js/dynamicImport (str module-uri)))
(rx/mapcat on-module-loaded)
(rx/tap on-module-initialized)
(rx/ignore))))))

View file

@ -1,23 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.util.wasm
(:require
["./renderer/renderer" :as renderer]
["./renderer/renderer.js$default" :as renderer-init]
[beicon.v2.core :as rx]
[potok.v2.core :as ptk]))
(defn foo [] (renderer/print "Lorem ipsum"))
(defn init
[]
(ptk/reify ::init
ptk/WatchEvent
(watch [_ _ _] ;; TODO: mirar la docu de potok
(->> (rx/from (renderer-init))
(rx/tap foo)
(rx/ignore)))))