From 86b20b20ead6fb9fed3a6e9360c543aa99f2daa0 Mon Sep 17 00:00:00 2001 From: groundedSAGE Date: Wed, 19 May 2021 22:04:30 +0200 Subject: [PATCH] Support Datalog Console --- src/datascript/core.cljc | 5 ++++- .../integrations/datalog_console.cljc | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/datascript/integrations/datalog_console.cljc diff --git a/src/datascript/core.cljc b/src/datascript/core.cljc index 778e9fee..deb868bf 100644 --- a/src/datascript/core.cljc +++ b/src/datascript/core.cljc @@ -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] @@ -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 diff --git a/src/datascript/integrations/datalog_console.cljc b/src/datascript/integrations/datalog_console.cljc new file mode 100644 index 00000000..85387fb5 --- /dev/null +++ b/src/datascript/integrations/datalog_console.cljc @@ -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)))))))) \ No newline at end of file