Commit c93cf9ea authored by Ricardo J. Mendez's avatar Ricardo J. Mendez

Refactoring data import, saving

- Refactoring getting and setting data into its own namespace
- :data-import no longer unnecessarily dispatches :data-set
- Unified :data-import and :data-import-done handlers

https://app.asana.com/0/50978096561390/61827811198606
parent fe99c225
(ns booklet.background
(:require [clojure.set :refer [difference]]
[cljs.core.async :refer [>! <!]]
[booklet.utils :refer [on-channel from-transit to-transit key-from-url]]
(:require [cljs.core.async :refer [>! <!]]
[booklet.data :as data]
[booklet.utils :refer [on-channel key-from-url]]
[khroma.alarms :as alarms]
[khroma.context-menus :as menus]
[khroma.idle :as idle]
[khroma.log :as console]
[khroma.storage :as storage]
[khroma.tabs :as tabs]
[khroma.runtime :as runtime]
[khroma.windows :as windows]
......@@ -99,65 +98,47 @@
::initialize
(fn [_]
(go
(dispatch [:data-import (:data (<! (storage/get))) true])
(dispatch [:data-import (<! (data/get))])
(dispatch [::window-focus {:windowId (:id (<! (windows/get-last-focused {:populate false})))}])
(dispatch [:idle-state-change {:newState (<! (idle/query-state 30))}]))
{:app-state {}
:hookup-done? false}))
;; :data-import currently gets dispatched from both booklet.core
;; and booklet.background, not entirely happy with that. Needs
;; further clean up
(register-handler
:data-import
(fn [app-state [_ transit-data]]
(let [new-data (from-transit transit-data)]
(console/trace "New data on import" new-data)
;; Create a new id if we don't have one
(when (empty? (:instance-id new-data))
(dispatch [:data-set :instance-id (.-uuid (random-uuid))]))
;; Dispatch instead of just doing an assoc so that it's also saved
(doseq [[key item] new-data]
(dispatch [:data-set key item]))
;; Once we've dispatched these, let's dispatch evaluate the state
(dispatch [:data-import-done])
;; We should only hook to the channels once.
(when (not (:hookup-done? app-state))
(hook-to-channels))
(-> app-state
(assoc-in [:ui-state :section] :time-track)
(assoc-in [:app-state :import] nil)
(assoc :hookup-done? true)
(assoc :data new-data))
)))
(register-handler
:data-import-done
(fn [app-state [_]]
(let [suspend-info (get-in app-state [:data :suspend-info])
(fn [app-state [_ new-data]]
(console/trace "New data on import" new-data)
;; Create a new id if we don't have one
(when (empty? (:instance-id new-data))
(dispatch [:data-set :instance-id (.-uuid (random-uuid))]))
;; Save the data we just imported
(data/set new-data)
;; We should only hook to the channels once.
(when (not (:hookup-done? app-state))
(hook-to-channels))
;; Process the suspend info
(let [suspend-info (:suspend-info new-data)
old-tab (:active-tab suspend-info)
current-tab (:active-tab app-state)
is-same? (and (= (:id old-tab) (:id current-tab))
(= (:url old-tab) (:url current-tab))
(= (:windowId old-tab) (:windowId current-tab)))]
(console/trace "From suspend:" is-same? suspend-info)
(if is-same?
(dispatch [:handle-activation old-tab (:start-time old-tab)])
(dispatch [:handle-deactivation old-tab (:time suspend-info)]))
(dispatch [:data-set :suspend-info] nil)
app-state
)
))
(dispatch [:handle-deactivation old-tab (:time suspend-info)])))
(-> app-state
(assoc-in [:ui-state :section] :time-track)
(assoc-in [:app-state :import] nil)
(assoc :hookup-done? true)
(assoc :data (assoc new-data :suspend-info nil)))))
(register-handler
:data-set
(fn [app-state [_ key item]]
(let [new-state (assoc-in app-state [:data key] item)
transit-data (to-transit (:data new-state))]
(storage/set {:data transit-data})
(let [new-state (assoc-in app-state [:data key] item)]
(data/set (:data new-state))
new-state)
))
......@@ -266,7 +247,6 @@
(let [{:keys [tabId windowId]} activeInfo
active-tab (:active-tab app-state)
replace? (= windowId (:windowId active-tab))]
; (console/trace ::tab-activated tabId windowId replace? active-tab)
(if replace?
(do
(dispatch [:handle-deactivation active-tab])
......@@ -279,7 +259,6 @@
(register-handler
::tab-updated
(fn [app-state [_ {:keys [tabId tab]}]]
; (console/trace ::tab-updated (:title tab) tabId tab (:active-tab app-state))
(let [active-tab (:active-tab app-state)
are-same? (= tabId (:id active-tab))]
(when (and are-same?
......@@ -318,7 +297,6 @@
:favIconUrl (:favIconUrl tab)
:timestamp (now))]
(console/trace time track? " milliseconds spent at " url-key tab)
; (console/trace " Previous " url-time)
(when track?
(dispatch [:data-set :url-times (assoc url-times url-key new-time)]))
app-state
......@@ -362,7 +340,6 @@
)
(defn ^:export main []
(menus/remove-all)
(menus/create {:id :on-relevance-show-data
......
(ns booklet.data
(:require [booklet.utils :refer [to-transit from-transit]]
[cljs.core.async :refer [<!]]
[khroma.storage :as storage])
(:require-macros [cljs.core.async.macros :refer [go]]))
(defn set
"Saves our data block on the extension's storage"
[data]
(storage/set {:data (to-transit data)}))
(defn get
"Returns a channel where we'll put the entire data block read from the
extension's storage"
[]
(go
(let [raw (:data (<! (storage/get)))]
(from-transit raw))))
......@@ -8,4 +8,4 @@
(fn [app-state [_ content]]
(console/log "Log event:" content)
app-state
))
\ No newline at end of file
))
(ns booklet.startpage
(:require [booklet.utils :refer [from-transit key-from-url time-display]]
(:require [booklet.data :as data]
[booklet.utils :refer [key-from-url time-display]]
[dommy.core :refer-macros [sel sel1] :as dommy]
[khroma.runtime :as runtime]
[khroma.log :as console]
[cljs.core.async :refer [>! <!]]
[khroma.storage :as storage])
[cljs.core.async :refer [>! <!]])
(:require-macros [cljs.core.async.macros :refer [go]]))
......@@ -32,7 +32,7 @@
(dommy/append! (create-node :span " " "rgb(80, 99, 152)"))
(dommy/append! (doto
(dommy/create-element :img)
(dommy/set-attr! :src "http://numergent.com/images/relevance/icon38.png")))
(dommy/set-attr! :src "http://numergent.com/images/relevance/icon38.png")))
(dommy/append! (create-node :span "[time viewed: " "rgb(80, 99, 152)"))
(dommy/append! (create-node :span (time-display time) "rgb(140, 101, 153)"))
(dommy/append! (create-node :span "]" "rgb(80, 99, 152)"))
......@@ -41,7 +41,7 @@
(defn do-transformations! []
(go
(let [data (from-transit (:data (<! (storage/get))))
(let [data (<! (data/get))
nodes (sel :.result_url_heading)
base (sel1 :.web_regular_results)]
(doseq [node nodes]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment