Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datalog-console): respond to datalog-console messages in browser #395

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:refer-clojure :exclude [filter])
(:require
[#?(:cljs cljs.reader :clj clojure.edn) :as edn]
[datascript.integrations.datalog-console :as datalog-console]
[datascript.db :as db #?@(:cljs [:refer [FilteredDB]])]
#?(:clj [datascript.pprint])
[datascript.pull-api :as dp]
Expand Down Expand Up @@ -416,7 +417,9 @@
(defn conn-from-db
"Creates a mutable reference to a given immutable database. See [[create-conn]]."
[db]
(atom db :meta { :listeners (atom {}) }))
(let [conn (atom db :meta {:listeners (atom {})})]
(datalog-console/init! {:conn conn})
conn))


(defn conn-from-datoms
Expand Down
20 changes: 20 additions & 0 deletions src/datascript/integrations/datalog_console.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns datascript.integrations.datalog-console
#?(:cljs (:require [goog.object :as gobj]
[goog.labs.userAgent.browser :as user-agent]
[cljs.reader])))


(defn init!
[{:keys [conn]}]
#?(:cljs (when (seq (user-agent/getVersion))
(js/document.documentElement.setAttribute "__datalog-console-remote-installed__" true)
(.addEventListener js/window "message"
(fn [event]
(when-let [devtool-message (gobj/getValueByKeys event "data" ":datalog-console.client/devtool-message")]
(let [msg-type (:type (cljs.reader/read-string devtool-message))]
(case msg-type

:datalog-console.client/request-whole-database-as-string
(.postMessage js/window #js {":datalog-console.remote/remote-message" (pr-str @conn)} "*")

nil))))))))