From 3e588ea74cf7f4f5f2c581b26ef3105cb64ea31d Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 2 Nov 2016 22:27:31 +0200 Subject: [PATCH] reloadable js resources for jail Former-commit-id: 958d900af7c2e40146d24e3f8219e29f43ee1326 --- .gitignore | 1 + env/dev/env/android/main.cljs | 12 ++- env/dev/env/android_test/main.cljs | 6 +- env/dev/user.clj | 41 ++++++--- figwheel-bridge.js | 22 +++-- project.clj | 89 ++++++++++--------- run-osx.sh | 2 +- src/cljsjs/react.cljs | 5 -- src/cljsjs/react/dom.cljs | 1 + src/cljsjs/react/dom/server.cljs | 1 + src/status_im/chat/handlers.cljs | 4 +- src/status_im/chat/views/request_message.cljs | 4 +- src/status_im/chat/views/response.cljs | 8 +- src/status_im/commands/handlers/loading.cljs | 18 ++-- .../components/refreshable_text/view.cljs | 6 +- src/status_im/components/status.cljs | 13 ++- src/status_im/utils/js_resources.cljs | 8 ++ 17 files changed, 144 insertions(+), 97 deletions(-) create mode 100644 src/cljsjs/react/dom.cljs create mode 100644 src/cljsjs/react/dom/server.cljs create mode 100644 src/status_im/utils/js_resources.cljs diff --git a/.gitignore b/.gitignore index 50b7cb023b..aa074c1dd8 100644 --- a/.gitignore +++ b/.gitignore @@ -59,6 +59,7 @@ figwheel_server.log # Lein # .lein-failures +.lein-repl-history ## Doo # diff --git a/env/dev/env/android/main.cljs b/env/dev/env/android/main.cljs index 231444df12..a26f34785c 100644 --- a/env/dev/env/android/main.cljs +++ b/env/dev/env/android/main.cljs @@ -1,4 +1,4 @@ - (ns ^:figwheel-no-load env.android.main +(ns ^:figwheel-no-load env.android.main (:require [reagent.core :as r] [status-im.android.core :as core] [figwheel.client :as figwheel :include-macros true])) @@ -8,10 +8,14 @@ (def cnt (r/atom 0)) (defn reloader [] @cnt [core/app-root]) (def root-el (r/as-element [reloader])) +(defn callback [] + (swap! cnt inc) + (status-im.components.status/init-jail) + (re-frame.core/dispatch [:load-commands!])) (figwheel/watch-and-reload - :websocket-url "ws://10.0.2.2:3449/figwheel-ws" - :heads-up-display false - :jsload-callback #(swap! cnt inc)) + :websocket-url "ws://10.0.3.2:3449/figwheel-ws" + :heads-up-display false + :jsload-callback callback) (core/init) diff --git a/env/dev/env/android_test/main.cljs b/env/dev/env/android_test/main.cljs index b3b074ae0c..8599609053 100644 --- a/env/dev/env/android_test/main.cljs +++ b/env/dev/env/android_test/main.cljs @@ -2,8 +2,8 @@ (:require [reagent.core :as r] [status-im.android.core :as core] [figwheel.client :as figwheel :include-macros true] - [status-im.test.handlers-stubs :refer [init-stubs]])) - + ;[status-im.test.handlers-stubs :refer [init-stubs]] + )) (enable-console-print!) (set! js/console.disableYellowBox true) @@ -18,4 +18,4 @@ :jsload-callback #(swap! cnt inc)) (core/init :test) -(init-stubs) +;(init-stubs) diff --git a/env/dev/user.clj b/env/dev/user.clj index 4c2ca8be74..6f5702afa6 100644 --- a/env/dev/user.clj +++ b/env/dev/user.clj @@ -1,5 +1,7 @@ (ns user - (:use [figwheel-sidecar.repl-api :as ra])) + (:use [figwheel-sidecar.repl-api :as ra]) + (:require [hawk.core :as hawk] + [clojure.string :as s])) ;; This namespace is loaded automatically by nREPL ;; read project.clj to get build configs @@ -10,17 +12,34 @@ (apply hash-map) :profiles)) -(def cljs-builds (get-in profiles [:dev :cljsbuild :builds])) +(def cljs-builds + (get-in profiles [:dev :cljsbuild :builds])) (defn start-figwheel - "Start figwheel for one or more builds" - [& build-ids] - (ra/start-figwheel! - {:build-ids build-ids - :all-builds cljs-builds}) - (ra/cljs-repl)) + "Start figwheel for one or more builds" + [build-ids] + (ra/start-figwheel! + {:figwheel-options {:nrepl-port 7888} + :build-ids build-ids + :all-builds cljs-builds})) + +(def start-cljs-repl ra/cljs-repl) (defn stop-figwheel - "Stops figwheel" - [] - (ra/stop-figwheel!)) \ No newline at end of file + "Stops figwheel" + [] + (ra/stop-figwheel!)) + +(hawk/watch! [{:paths ["resources"] + :handler (fn [ctx e] + (let [path "src/status_im/utils/js_resources.cljs" + js-resourced (slurp path)] + (spit path (str js-resourced " ;;")) + (spit path js-resourced)) + ctx)}]) + +(let [env-build-ids (System/getenv "BUILD_IDS") + build-ids (if env-build-ids + (map keyword (s/split env-build-ids #",")) + [:android])] + (start-figwheel build-ids)) diff --git a/figwheel-bridge.js b/figwheel-bridge.js index 6d44d98094..9e3163203a 100644 --- a/figwheel-bridge.js +++ b/figwheel-bridge.js @@ -164,11 +164,23 @@ function interceptRequire() { function compileWarningsToYellowBox() { var log = window.console.log; var compileWarningRx = /Figwheel: Compile/; + var compileExceptionRx = /Figwheel: Compile Exception/; + var errorInFileRx = /Error on file/; + var isBuffering = false; + var compileExceptionBuffer = ""; window.console.log = function (msg) { - if (compileWarningRx.test(msg)) { + log.apply(window.console, arguments); + if (compileExceptionRx.test(msg)) { // enter buffering mode to get all the messages for exception + isBuffering = true; + compileExceptionBuffer = msg + "\n"; + } else if (errorInFileRx.test(msg) && isBuffering) { // exit buffering mode and log buffered messages to YellowBox + isBuffering = false; + console.warn(compileExceptionBuffer + msg); + compileExceptionBuffer = ""; + } else if (isBuffering) { //log messages buffering mode + compileExceptionBuffer += msg + "\n"; + } else if (compileWarningRx.test(msg)) { console.warn(msg); - } else { - log.apply(window.console, arguments); } }; } @@ -208,7 +220,7 @@ function loadApp(platform, devHost, onLoadCb) { // seriously React packager? why. var googreq = goog.require; - googreq('figwheel.connect'); + googreq('figwheel.connect.' + platform); }); }); } @@ -278,4 +290,4 @@ self = { start: startApp }; -module.exports = self; \ No newline at end of file +module.exports = self; diff --git a/project.clj b/project.clj index a70b9fea77..f69a3d1341 100644 --- a/project.clj +++ b/project.clj @@ -5,14 +5,14 @@ :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.9.0-alpha13"] [org.clojure/clojurescript "1.9.229"] - [reagent "0.5.1" :exclusions [cljsjs/react]] + [reagent "0.6.0" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server]] [re-frame "0.7.0"] [natal-shell "0.3.0"] [com.andrewmcveigh/cljs-time "0.4.0"] [tailrecursion/cljs-priority-map "1.2.0"] [com.taoensso/timbre "4.7.4"]] :plugins [[lein-cljsbuild "1.1.4"] - [lein-figwheel "0.5.0-2"]] + [lein-figwheel "0.5.8"]] :clean-targets ["target/" "index.ios.js" "index.android.js"] :aliases {"prod-build" ^{:doc "Recompile code with prod profile."} ["do" "clean" @@ -20,51 +20,56 @@ ["with-profile" "prod" "cljsbuild" "once" "android"]]} :test-paths ["test/clj"] :figwheel {:nrepl-port 7888} - :profiles {:dev {:dependencies [[figwheel-sidecar "0.5.0-2"] + :profiles {:dev {:dependencies [[figwheel-sidecar "0.5.8"] [com.cemerick/piggieback "0.2.1"] - [io.appium/java-client "3.4.1"]] + [io.appium/java-client "3.4.1"] + [hawk "0.2.10"]] :plugins [[lein-doo "0.1.6"]] :source-paths ["src" "env/dev"] - :cljsbuild {:builds {:ios {:source-paths ["src" "env/dev"] - :figwheel true - :compiler {:output-to "target/ios/not-used.js" - :main "env.ios.main" - :output-dir "target/ios" - :optimizations :none}} - :android {:source-paths ["src" "env/dev"] - :figwheel true - :compiler {:output-to "target/android/not-used.js" - :main "env.android.main" - :output-dir "target/android" - :optimizations :none}} - :android-test {:source-paths ["src" "env/dev"] - :figwheel true - :compiler {:output-to "target/android/not-used.js" - :main "env.android-test.main" - :output-dir "target/android" - :optimizations :none}} - :test {:source-paths ["src" "test/cljs"] - :compiler - {:main status-im.test.runner - :output-to "target/test/test.js" - :optimizations :none - :target :nodejs}}}} - :repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}} + :cljsbuild {:builds [{:id :ios + :source-paths ["src" "env/dev"] + :figwheel true + :compiler {:output-to "target/ios/not-used.js" + :main "env.ios.main" + :output-dir "target/ios" + :optimizations :none}} + {:id :android + :source-paths ["src" "env/dev"] + :figwheel true + :compiler {:output-to "target/android/not-used.js" + :main "env.android.main" + :output-dir "target/android" + :optimizations :none}} + {:id :android-test + :source-paths ["src" "env/dev"] + :compiler {:output-to "target/android/not-used.js" + :main "env.android-test.main" + :output-dir "target/android-test" + :optimizations :none}} + {:id :test + :source-paths ["src" "test/cljs"] + :compiler + {:main status-im.test.runner + :output-to "target/test/test.js" + :optimizations :none + :target :nodejs}}]} + :repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl] + :timeout 120000}} :prod {:cljsbuild {:builds [{:id "ios" :source-paths ["src" "env/prod"] - :compiler {:output-to "index.ios.js" - :main "env.ios.main" - :output-dir "target/ios" - :static-fns true + :compiler {:output-to "index.ios.js" + :main "env.ios.main" + :output-dir "target/ios-prod" + :static-fns true :optimize-constants true - :optimizations :simple - :closure-defines {"goog.DEBUG" false}}} - {:id "android" + :optimizations :simple + :closure-defines {"goog.DEBUG" false}}} + {:id "android" :source-paths ["src" "env/prod"] - :compiler {:output-to "index.android.js" - :main "env.android.main" - :output-dir "target/android" - :static-fns true + :compiler {:output-to "index.android.js" + :main "env.android.main" + :output-dir "target/android-prod" + :static-fns true :optimize-constants true - :optimizations :simple - :closure-defines {"goog.DEBUG" false}}}]}}}) + :optimizations :simple + :closure-defines {"goog.DEBUG" false}}}]}}}) diff --git a/run-osx.sh b/run-osx.sh index 4097f2d4ea..4c76583232 100755 --- a/run-osx.sh +++ b/run-osx.sh @@ -57,7 +57,7 @@ fi lein deps && re-natal deps && re-natal use-android-device "${device_type}" && re-natal use-figwheel # open figwheel in new tab -tab "lein figwheel ${cljs_build}" +tab "BUILD_IDS=${cljs_build} lein repl" # open react-native package in new tab tab "react-native start" diff --git a/src/cljsjs/react.cljs b/src/cljsjs/react.cljs index db9e774237..5a52465be6 100644 --- a/src/cljsjs/react.cljs +++ b/src/cljsjs/react.cljs @@ -1,9 +1,4 @@ (ns cljsjs.react) (when (exists? js/window) - ;; cause there is no window.document object in the newest version - ;; of React Native, but chance.js requires it - (set! js/window.document #js {}) - - (set! js/window.React (js/require "react")) (set! js/ReactNative (js/require "react-native"))) diff --git a/src/cljsjs/react/dom.cljs b/src/cljsjs/react/dom.cljs new file mode 100644 index 0000000000..3c69f0090f --- /dev/null +++ b/src/cljsjs/react/dom.cljs @@ -0,0 +1 @@ +(ns cljsjs.react.dom) \ No newline at end of file diff --git a/src/cljsjs/react/dom/server.cljs b/src/cljsjs/react/dom/server.cljs new file mode 100644 index 0000000000..c7ffcf638f --- /dev/null +++ b/src/cljsjs/react/dom/server.cljs @@ -0,0 +1 @@ +(ns cljsjs.react.dom.server) \ No newline at end of file diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 6d9e484fd2..85e1334925 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -305,7 +305,9 @@ (let [chat-id (or id current-chat-id) messages (get-in db [:chats chat-id :messages]) db' (assoc db :current-chat-id chat-id) - commands-loaded? (get-in db [:chats chat-id :commands-loaded])] + commands-loaded? (if js/goog.DEBUG + false + (get-in db [:chats chat-id :commands-loaded]))] (when (= current-chat-id wallet-chat-id) (dispatch [:cancel-command])) (dispatch [:load-requests! chat-id]) diff --git a/src/status_im/chat/views/request_message.cljs b/src/status_im/chat/views/request_message.cljs index 4a639dc5cb..3bb6e65a09 100644 --- a/src/status_im/chat/views/request_message.cljs +++ b/src/status_im/chat/views/request_message.cljs @@ -53,12 +53,12 @@ :loop? loop?}] (r/create-class {:component-did-mount - (when-not @answered? #(request-button-animation-logic context)) + (if @answered? #(request-button-animation-logic context) (fn [])) :component-will-unmount #(reset! loop? false) :reagent-render (fn [message-id {command-icon :icon :as command} status-initialized?] - (if command + (when command [touchable-highlight {:on-press (when (and (not @answered?) status-initialized?) #(set-chat-command message-id command)) diff --git a/src/status_im/chat/views/response.cljs b/src/status_im/chat/views/response.cljs index 53190036d6..462c868689 100644 --- a/src/status_im/chat/views/response.cljs +++ b/src/status_im/chat/views/response.cljs @@ -1,7 +1,6 @@ (ns status-im.chat.views.response (:require-macros [reagent.ratom :refer [reaction]] - [status-im.utils.views :refer [defview]] - [status-im.utils.slurp :refer [slurp]]) + [status-im.utils.views :refer [defview]]) (:require [re-frame.core :refer [subscribe dispatch]] [reagent.core :as r] [status-im.components.react :refer [view @@ -25,7 +24,8 @@ [status-im.i18n :refer [label]] [status-im.utils.datetime :as dt] [taoensso.timbre :as log] - [status-im.utils.name :refer [shortened-name]])) + [status-im.utils.name :refer [shortened-name]] + [status-im.utils.js-resources :as js-res])) (defn drag-icon [] [view st/drag-container @@ -135,7 +135,7 @@ :source {:uri url} :render-error web-view-error :java-script-enabled true - :injected-java-script (slurp "resources/webview.js") + :injected-java-script js-res/webview-js :bounces false :on-navigation-state-change on-navigation-change}])) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 58362fb0ac..1f29bd8eed 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -1,5 +1,4 @@ (ns status-im.commands.handlers.loading - (:require-macros [status-im.utils.slurp :refer [slurp]]) (:require [re-frame.core :refer [path after dispatch subscribe trim-v debug]] [status-im.utils.handlers :as u] [status-im.utils.utils :refer [http-get show-popup]] @@ -10,13 +9,14 @@ [status-im.commands.utils :refer [reg-handler]] [status-im.constants :refer [console-chat-id wallet-chat-id]] [taoensso.timbre :as log] - [status-im.utils.homoglyph :as h])) + [status-im.utils.homoglyph :as h] + [status-im.utils.js-resources :as js-res])) (def commands-js "commands.js") (defn load-commands! - [_ [identity]] - (dispatch [::fetch-commands! identity]) + [{:keys [current-chat-id]} [identity]] + (dispatch [::fetch-commands! (or identity current-chat-id)]) ;; todo uncomment #_(if-let [{:keys [file]} (commands/get-by-chat-id identity)] (dispatch [::parse-commands! identity file]) @@ -28,13 +28,13 @@ ;-let [url (get-in db [:chats identity :dapp-url])] (cond (= console-chat-id identity) - (dispatch [::validate-hash identity (slurp "resources/console.js")]) + (dispatch [::validate-hash identity js-res/console-js]) (= wallet-chat-id identity) - (dispatch [::validate-hash identity (slurp "resources/wallet.js")]) + (dispatch [::validate-hash identity js-res/wallet-js]) :else - (dispatch [::validate-hash identity (slurp "resources/commands.js")]) + (dispatch [::validate-hash identity js-res/commands-js]) #_(http-get (s/join "/" [url commands-js]) #(dispatch [::validate-hash identity %]) @@ -89,8 +89,8 @@ (let [commands' (filter-forbidden-names id commands) responses' (filter-forbidden-names id responses)] (-> db - (update-in [id :commands] merge (mark-as :command commands')) - (update-in [id :responses] merge (mark-as :response responses')) + (assoc-in [id :commands] (mark-as :command commands')) + (assoc-in [id :responses] (mark-as :response responses')) (assoc-in [id :commands-loaded] true) (assoc-in [id :autorun] autorun)))) diff --git a/src/status_im/components/refreshable_text/view.cljs b/src/status_im/components/refreshable_text/view.cljs index 817b8c0b88..348b34e94d 100644 --- a/src/status_im/components/refreshable_text/view.cljs +++ b/src/status_im/components/refreshable_text/view.cljs @@ -1,6 +1,6 @@ (ns status-im.components.refreshable-text.view (:require [reagent.core :as r] - [reagent.impl.util :as ru] + [reagent.impl.component :as rc] [status-im.components.react :refer [view animated-view text]] @@ -39,7 +39,7 @@ :value value}) :component-will-update (fn [component props] - (let [{new-value :value} (ru/extract-props props) + (let [{new-value :value} (rc/extract-props props) {old-value :value} (r/props component)] (r/set-state component {:old-value old-value :value new-value}) @@ -64,4 +64,4 @@ :opacity new-value-opacity}} [text {:style text-style :font font} - value]]]))}))) \ No newline at end of file + value]]]))}))) diff --git a/src/status_im/components/status.cljs b/src/status_im/components/status.cljs index e6a44d9f01..565ff1448b 100644 --- a/src/status_im/components/status.cljs +++ b/src/status_im/components/status.cljs @@ -1,11 +1,12 @@ (ns status-im.components.status - (:require-macros [status-im.utils.slurp :refer [slurp]] - [cljs.core.async.macros :refer [go-loop go]]) + (:require-macros + [cljs.core.async.macros :refer [go-loop go]]) (:require [status-im.components.react :as r] [status-im.utils.types :as t] [re-frame.core :refer [dispatch]] [taoensso.timbre :as log] - [cljs.core.async :refer [