Skip to content

Commit

Permalink
Cleanup logic added
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Sep 18, 2014
1 parent 6e66fc0 commit a0e7d5d
Show file tree
Hide file tree
Showing 5 changed files with 581 additions and 549 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -4,5 +4,6 @@ What DataScript-driven application looks like? Does DataScript really makes the

<img src="http://tonsky.me/blog/datascript-chat/datascript_chat.png" style="width: 620px; height: 420px;" />

Code walkthrough http://tonsky.me/blog/datascript-chat/
Live version http://tonsky.me/datascript-chat/
Code walkthrough http://tonsky.me/blog/datascript-chat/

Live version http://tonsky.me/datascript-chat/
1,093 changes: 549 additions & 544 deletions datascript-chat.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion index_dev.html
Expand Up @@ -16,7 +16,8 @@
<script type="text/javascript">goog.require("datascript_chat");</script>

<script>
datascript_chat.ui._STAR_debug_render_STAR_ = true;
datascript_chat.ui._STAR_debug_render_STAR_ = false;
datascript_chat.util._STAR_debug_q_STAR_ = false;
window.onload = function() { datascript_chat.start(); }
</script>

Expand Down
24 changes: 24 additions & 0 deletions src/datascript_chat.cljs
Expand Up @@ -12,6 +12,8 @@

(enable-console-print!)

(def ^:dynamic *room-msg-limit* 30)

;; DATABASE

;; schema is required only for :db.type/ref and :db.cardinality/many
Expand Down Expand Up @@ -139,3 +141,25 @@
]))


;; Clean-up: keeping last N messages per room
(go-loop-sub event-bus-pub :recv-msg [_ msg]
(let [db @conn
room-id (:message/room msg)
;; Last ?lim messages
keep-msgs (->> (u/q1 '[:find (max ?lim ?m)
:in $ ?room-id ?lim
:where [?m :message/room ?room-id]]
db
room-id
*room-msg-limit*)
set)
;; All other messages in same room
remove-msgs (u/q1s '[:find ?m
:in $ ?room-id ?remove-pred
:where [?m :message/room ?room-id]
[(?remove-pred ?m)]] ;; filter by custom predicate
db
room-id
#(not (contains? keep-msgs %)))]
(d/transact! conn
(map #(vector :db.fn/retractEntity %) remove-msgs))))
5 changes: 3 additions & 2 deletions src/datascript_chat/ui.cljs
Expand Up @@ -45,7 +45,7 @@
[:img.topic__avatar {:src (:user/avatar user)}]
[:.topic__title (:room/title room)
(when (pos? unread)
[:span.topic__unread (str unread)])]
[:span.topic__unread (str unread (when (>= unread 30) "+"))])]
[:.topic__msg (:message/text last-msg)]
[:.topic__ts
(:user/name user)
Expand Down Expand Up @@ -81,7 +81,8 @@

(r/defc message [msg]
(let [user (:message/author msg)]
[:.message {:class [(when (:message/unread msg) "message_unread")
[:.message {:key (:db/id msg)
:class [(when (:message/unread msg) "message_unread")
(when (:user/me user) "me")]}
(avatar user)
[:.message__name
Expand Down

0 comments on commit a0e7d5d

Please sign in to comment.