Skip to content

Commit

Permalink
[perf] extensions module
Browse files Browse the repository at this point in the history
  • Loading branch information
rasom committed Jun 13, 2019
1 parent 908f730 commit 7097de6
Show file tree
Hide file tree
Showing 27 changed files with 272 additions and 194 deletions.
110 changes: 85 additions & 25 deletions project.clj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/status_im/accounts/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require status-im.utils.db
status-im.network.module
status-im.ui.screens.bootnodes-settings.db
status-im.ui.screens.extensions.db
status-im.extensions.module
[cljs.spec.alpha :as spec]
[status-im.constants :as const]))

Expand Down
4 changes: 2 additions & 2 deletions src/status_im/browser/permissions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require [status-im.constants :as constants]
[status-im.data-store.dapp-permissions :as dapp-permissions]
[status-im.ethereum.core :as ethereum]
[status-im.extensions.registry :as extensions.registry]
[status-im.extensions.module :as extensions.module]
[status-im.i18n :as i18n]
[status-im.qr-scanner.core :as qr-scanner]
[status-im.ui.screens.navigation :as navigation]
Expand All @@ -28,7 +28,7 @@
(cond
(= permission constants/dapp-permission-install-extension)
(fx/merge cofx
(extensions.registry/load (:uri params) true)
(extensions.module/load (:uri params) true)
(send-response-to-bridge permission message-id true nil)
(process-next-permission dapp-name))

Expand Down
60 changes: 3 additions & 57 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
[status-im.contact.core :as contact]
[status-im.ethereum.subscriptions :as ethereum.subscriptions]
[status-im.ethereum.transactions.core :as ethereum.transactions]
[status-im.extensions.core :as extensions]
[status-im.extensions.registry :as extensions.registry]
[status-im.fleet.core :as fleet]
[status-im.group-chats.core :as group-chats]
[status-im.hardwallet.core :as hardwallet]
Expand Down Expand Up @@ -538,48 +536,6 @@
(fn [cofx [_ bootnode-id]]
(bootnodes/delete-bootnode cofx bootnode-id)))

;; extensions module

(handlers/register-handler-fx
:extensions.callback/qr-code-scanned
(fn [cofx [_ _ url]]
(extensions/set-extension-url-from-qr cofx url)))

(handlers/register-handler-fx
:extensions.ui/add-extension-pressed
(fn [cofx [_ extension-key]]
(extensions/edit cofx extension-key)))

(handlers/register-handler-fx
:extensions.ui/uninstall-extension-pressed
(fn [cofx [_ extension-key]]
(extensions.registry/uninstall cofx extension-key)))

(handlers/register-handler-fx
:extensions.ui/input-changed
(fn [cofx [_ input-key value]]
(extensions/set-input cofx input-key value)))

(handlers/register-handler-fx
:extensions.ui/activation-checkbox-pressed
(fn [cofx [_ extension-key active?]]
(extensions.registry/change-state cofx extension-key active?)))

(handlers/register-handler-fx
:extensions.ui/find-button-pressed
(fn [cofx [_ url]]
(extensions.registry/load cofx url false)))

(handlers/register-handler-fx
:extensions.ui/install-extension-button-pressed
(fn [cofx [_ url]]
(extensions.registry/install-from-message cofx url true)))

(handlers/register-handler-fx
:extensions.ui/install-button-pressed
(fn [cofx [_ url data modal?]]
(extensions.registry/install cofx url data modal?)))

;; logging module

(handlers/register-handler-fx
Expand Down Expand Up @@ -1514,19 +1470,9 @@
(browser.permissions/process-next-permission cofx dapp-name)))

(handlers/register-handler-fx
:browser.ui/open-in-status-option-selected
(fn [cofx [_ url]]
(browser/open-url cofx url)))

(handlers/register-handler-fx
:browser.ui/open-dapp-button-pressed
(fn [cofx [_ dapp-url]]
(browser/open-url cofx dapp-url)))

(handlers/register-handler-fx
:browser.ui/dapp-url-submitted
(fn [cofx [_ dapp-url]]
(browser/open-url cofx dapp-url)))
:browser.ui/open-url
(fn [cofx [_ url extension-params]]
(browser/open-url cofx (or (:url extension-params) url))))

(handlers/register-handler-fx
:browser.ui/open-modal-chat-button-pressed
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/extensions/capacities/components.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
:text-decoration-line :underline}
:on-press (case open-in
:device #(.openURL (react/linking) uri)
:status #(re-frame/dispatch [:browser.ui/open-in-status-option-selected uri])
:status #(re-frame/dispatch [:browser.ui/open-url uri])
#(re-frame/dispatch [:browser.ui/message-link-pressed uri]))}
(when style {:style style}))
(or text uri)])
Expand Down
10 changes: 2 additions & 8 deletions src/status_im/extensions/capacities/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[status-im.chat.commands.sending :as commands-sending]
[status-im.ui.screens.navigation :as navigation]
[status-im.ipfs.core :as ipfs]
[status-im.browser.core :as browser]
[status-im.utils.fx :as fx]
[status-im.accounts.update.core :as accounts.update]
status-im.extensions.capacities.ethereum
Expand Down Expand Up @@ -306,11 +305,6 @@
(fn [_ [_ _ m]]
{::arithmetic m}))

(handlers/register-handler-fx
:extensions/open-url
(fn [cofx [_ _ {:keys [url]}]]
(browser/open-url cofx url)))

(handlers/register-handler-fx
:extensions/profile-settings-close
(fn [{:keys [db] :as cofx} [_ _ {:keys [on-close on-failure]}]]
Expand Down Expand Up @@ -408,7 +402,7 @@
:on-result :event}}
'browser/open-url
{:permissions [:read]
:data :extensions/open-url
:data :browser.ui/open-url
:arguments {:url :string}}
'camera/picture
{:permissions [:read]
Expand Down Expand Up @@ -750,4 +744,4 @@
{:permissions [:read]
:data :extensions/wallet-settings-close
:arguments {:on-close? :event
:on-failure? :event}}})
:on-failure? :event}}})
4 changes: 4 additions & 0 deletions src/status_im/extensions/constants.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns status-im.extensions.constants)

(def uri-prefix "https://get.status.im/extension/")
(def link-prefix "status-im://extension/")
21 changes: 4 additions & 17 deletions src/status_im/extensions/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
(:refer-clojure :exclude [list])
(:require [clojure.string :as string]
[pluto.core :as pluto]
[pluto.storages :as storages]
[re-frame.core :as re-frame]
[re-frame.registrar :as registrar]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.fx :as fx]
[status-im.extensions.capacities.components :as components]
[status-im.extensions.capacities.subs :as subs]
[status-im.extensions.capacities.events :as events]))
[status-im.extensions.capacities.events :as events]
[status-im.extensions.constants :as constants]))

(def capacities
{:components components/all
Expand Down Expand Up @@ -64,26 +64,13 @@
(parse (pluto/read (:content value)) id)
{:errors [{:type type :value value}]}))

(def uri-prefix "https://get.status.im/extension/")
(def link-prefix "status-im://extension/")

(defn valid-uri? [s]
(boolean
(when s
(let [s' (string/trim s)]
(or
(re-matches (re-pattern (str "^" uri-prefix "\\w+@.+")) s')
(re-matches (re-pattern (str "^" link-prefix "\\w+@.+")) s'))))))

(defn url->uri [s]
(when s
(-> s
(string/replace uri-prefix "")
(string/replace link-prefix ""))))

(defn load-from [url f]
(when-let [uri (url->uri url)]
(storages/fetch uri f)))
(re-matches (re-pattern (str "^" constants/uri-prefix "\\w+@.+")) s')
(re-matches (re-pattern (str "^" constants/link-prefix "\\w+@.+")) s'))))))

(fx/defn set-extension-url-from-qr
[cofx url]
Expand Down
44 changes: 44 additions & 0 deletions src/status_im/extensions/events.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns status-im.extensions.events
(:require [status-im.utils.handlers :as handlers]
[status-im.extensions.core :as extensions]
[status-im.extensions.registry :as extensions.registry]))

(handlers/register-handler-fx
:extensions.callback/qr-code-scanned
(fn [cofx [_ _ url]]
(extensions/set-extension-url-from-qr cofx url)))

(handlers/register-handler-fx
:extensions.ui/add-extension-pressed
(fn [cofx [_ extension-key]]
(extensions/edit cofx extension-key)))

(handlers/register-handler-fx
:extensions.ui/uninstall-extension-pressed
(fn [cofx [_ extension-key]]
(extensions.registry/uninstall cofx extension-key)))

(handlers/register-handler-fx
:extensions.ui/input-changed
(fn [cofx [_ input-key value]]
(extensions/set-input cofx input-key value)))

(handlers/register-handler-fx
:extensions.ui/activation-checkbox-pressed
(fn [cofx [_ extension-key active?]]
(extensions.registry/change-state cofx extension-key active?)))

(handlers/register-handler-fx
:extensions.ui/find-button-pressed
(fn [cofx [_ url]]
(extensions.registry/load cofx url false)))

(handlers/register-handler-fx
:extensions.ui/install-extension-button-pressed
(fn [cofx [_ url]]
(extensions.registry/install-from-message cofx url true)))

(handlers/register-handler-fx
:extensions.ui/install-button-pressed
(fn [cofx [_ url data modal?]]
(extensions.registry/install cofx url data modal?)))
78 changes: 78 additions & 0 deletions src/status_im/extensions/module.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
(ns status-im.extensions.module
(:require-macros [status-im.modules :as modules])
(:require status-im.extensions.ui.db
[pluto.storages :as storages]
[re-frame.core :as re-frame]
[status-im.utils.fx :as fx]
[clojure.string :as string]
[status-im.extensions.constants :as constants]))

(modules/defmodule extensions
{:load 'status-im.extensions.registry/load
:valid-uri? 'status-im.extensions.core/valid-uri?
:parse-extension 'status-im.extensions.core/parse-extension
:take-picture-view 'status-im.extensions.capacities.camera.views/take-picture
:screen-holder-view 'status-im.extensions.capacities.views/screen-holder
:extensions-settings-view 'status-im.extensions.ui.views/extensions-settings
:selection-modal-screen-view 'status-im.extensions.ui.views/selection-modal-screen
:edit-extension-view 'status-im.extensions.ui.add.views/edit-extension
:show-extension-view 'status-im.extensions.ui.add.views/show-extension
:show-extension-modal-view 'status-im.extensions.ui.add.views/show-extension-modal})

(defn load [& args]
(apply (get-symbol :load) args))

(defn valid-uri? [& args]
(apply (get-symbol :valid-uri?) args))

(defn parse-extension [& args]
(apply (get-symbol :parse-extension) args))

(defn take-picture-view []
[(get-symbol :take-picture-view)])

(defn screen-holder-view []
[(get-symbol :screen-holder-view)])

(defn extensions-settings-view []
[(get-symbol :extensions-settings-view)])

(defn selection-modal-screen-view []
[(get-symbol :selection-modal-screen-view)])

(defn edit-extension-view []
[(get-symbol :edit-extension-view)])

(defn show-extension-view []
[(get-symbol :show-extension-view)])

(defn show-extension-modal-view []
[(get-symbol :show-extension-modal-view)])

;; Initialization
;; Prevents loading of the rest of the module till it is necessary.

(defn url->uri [s]
(when s
(-> s
(string/replace constants/uri-prefix "")
(string/replace constants/link-prefix ""))))

(defn load-from [url f]
(when-let [uri (url->uri url)]
(storages/fetch uri f)))

(re-frame/reg-fx
:extensions/load
(fn [{:keys [extensions follow-up]}]
(doseq [{:keys [url active?]} extensions]
(load-from url #(re-frame/dispatch [follow-up url (parse-extension % url) active?])))))

(fx/defn initialize
[{{:account/keys [account] :as db} :db}]
(let [{:keys [extensions dev-mode?]} account
ext-vals (vals extensions)]
(when dev-mode?
{:db (assoc db :extensions/store (into {} (map (fn [{:keys [id data]}] {id data}) ext-vals)))
:extensions/load {:extensions ext-vals
:follow-up :extensions/add-to-registry}})))
9 changes: 0 additions & 9 deletions src/status_im/extensions/registry.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,6 @@
(re-frame/dispatch [:accounts.ui/dev-mode-switched true])
(re-frame/dispatch [:extensions.ui/install-extension-button-pressed url]))}}))

(fx/defn initialize
[{{:account/keys [account] :as db} :db}]
(let [{:keys [extensions dev-mode?]} account
ext-vals (vals extensions)]
(when dev-mode?
{:db (assoc db :extensions/store (into {} (map (fn [{:keys [id data]}] {id data}) ext-vals)))
:extensions/load {:extensions ext-vals
:follow-up :extensions/add-to-registry}})))

(defn existing-hooks-for
[type cofx extension-data]
(let [added-hooks (reduce (fn [acc [_ {:keys [hooks]}]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
(ns status-im.ui.screens.extensions.add.events
(:require [re-frame.core :as re-frame]
[status-im.extensions.core :as extensions]
[status-im.extensions.registry :as extensions.registry]
[status-im.ui.screens.navigation :as navigation]
(ns status-im.extensions.ui.add.events
(:require [status-im.extensions.registry :as extensions.registry]
[status-im.utils.handlers :as handlers]
[status-im.utils.fx :as fx]
[status-im.i18n :as i18n]))

(re-frame/reg-fx
:extensions/load
(fn [{:keys [extensions follow-up]}]
(doseq [{:keys [url active?]} extensions]
(extensions/load-from url #(re-frame/dispatch [follow-up url (extensions/parse-extension % url) active?])))))
[status-im.utils.fx :as fx]))

(handlers/register-handler-fx
:extensions/stage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns status-im.ui.screens.extensions.add.styles
(ns status-im.extensions.ui.add.styles
(:require-macros [status-im.utils.styles :refer [defstyle]])
(:require [status-im.ui.components.styles :as styles]
[status-im.ui.components.colors :as colors]))
Expand Down
Loading

0 comments on commit 7097de6

Please sign in to comment.