hark-store: rework caching

This commit is contained in:
Liam Fitzgerald 2020-11-16 12:22:42 +10:00
parent 8a5fdc0f97
commit ec580cf8c1
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
3 changed files with 51 additions and 27 deletions

View File

@ -25,7 +25,7 @@
:: albeit expensively
+$ cache
$: unread-count=@ud
graph-unreads=(map resource @ud)
by-index=(jug index:store @da)
~
==
::
@ -72,7 +72,7 @@
^- update:store
:- %more
^- (list update:store)
:- [%graph-unreads graph-unreads]
:- unreads
:+ [%set-dnd dnd]
[%count unread-count]
%+ weld
@ -85,6 +85,14 @@
(tap-nonempty:ha notifications)
(timebox-update |)
::
++ unreads
^- update:store
:- %unreads
^- (list [index:store @ud])
%+ turn
~(tap by by-index)
|=([=index:store =(set @da)] [index ~(wyt in set)])
::
++ timebox-update
|= archived=?
|= [time=@da =timebox:store]
@ -136,6 +144,7 @@
%archive (do-archive +.action)
%seen seen
%read (read +.action)
%read-index (read-index +.action)
%unread (unread +.action)
%set-dnd (set-dnd +.action)
==
@ -154,10 +163,22 @@
(~(put by timebox) index new)
:- (give:ha [/updates]~ %added last-seen index new)
%_ state
+ ?~(existing-notif (upd-unreads:ha index %.n) +.state)
+ ?~(existing-notif (upd-unreads:ha index last-seen %.n) +.state)
notifications (put:orm notifications last-seen new-timebox)
unread-count ?~(existing-notif +(unread-count) unread-count)
==
++ read-index
|= =index:store
^- (quip card _state)
=/ times=(list @da)
~(tap in (~(gut by by-index) index ~))
=| cards=(list card)
|-
?~ times
[cards state]
=* time i.times
=^ crds state
(read time index)
$(cards (weld cards crds), times t.times)
::
++ do-archive
|= [time=@da =index:store]
@ -170,7 +191,7 @@
(~(del by timebox) index)
:- (give:ha [/updates]~ %archive time index)
%_ state
+ ?.(read.notification (upd-unreads:ha index %.y) +.state)
+ ?.(read.notification (upd-unreads:ha index time %.y) +.state)
::
notifications
(put:orm notifications time new-timebox)
@ -187,7 +208,7 @@
^- (quip card _state)
:- (give:ha [/updates]~ %read time index)
%_ state
+ (upd-unreads:ha index %.y)
+ (upd-unreads:ha index time %.y)
unread-count (dec unread-count)
notifications (change-read-status:ha time index %.y)
==
@ -197,7 +218,7 @@
^- (quip card _state)
:- (give:ha [/updates]~ %unread time index)
%_ state
+ (upd-unreads:ha index %.n)
+ (upd-unreads:ha index time %.n)
unread-count +(unread-count)
notifications (change-read-status:ha time index %.n)
==
@ -302,18 +323,16 @@
[%give %fact paths [%hark-update !>(update)]]~
::
++ upd-unreads
|= [=index:store read=?]
|= [=index:store time=@da read=?]
^+ +.state
=/ f=$-(@ @)
%_ +.state
::
by-index
%. [index time]
?: read
dec
|=(a=@ +(a))
=. unread-count (f unread-count)
?. ?=(%graph -.index)
+.state
=/ curr-unread=@ud
(~(gut by graph-unreads) graph.index 0)
+.state(graph-unreads (~(put by graph-unreads) graph.index (f curr-unread)))
~(del ju by-index)
~(put ju by-index)
==
::
++ inflate-cache
|= state-0
@ -332,6 +351,6 @@
?: read.notification
inner(unreads t.unreads)
=. +.state
(upd-unreads index read.notification)
(upd-unreads index p.i.nots %.n)
inner(unreads t.unreads)
--

View File

@ -62,6 +62,7 @@
read+notif-ref
add+add
set-dnd+bo
read-index+index
==
--
::
@ -78,21 +79,24 @@
%timebox (timebox +.upd)
%set-dnd b+dnd.upd
%count (numb count.upd)
%graph-unreads (graph-unreads map.upd)
%unreads (unreads unreads.upd)
%more (more +.upd)
::
?(%archive %read %unread)
(notif-ref +.upd)
==
::
++ graph-unreads
|= =(map resource @ud)
++ unreads
|= l=(list [^index @ud])
^- json
:- %a
^- (list json)
%+ turn l
|= [idx=^index unread=@ud]
%- pairs
%+ turn
~(tap by map)
|= [rid=resource unread=@ud]
(enjs-path:resource rid)^(numb unread)
:~ unread+(numb unread)
index+(index idx)
==
::
++ added
|= [tim=@da idx=^index not=^notification]

View File

@ -32,6 +32,7 @@
$% [%add =index =notification]
[%archive time=@da index]
[%read time=@da index]
[%read-index index]
[%unread time=@da index]
[%set-dnd dnd=?]
[%seen ~]
@ -42,10 +43,10 @@
::
+$ update
$% action
[%more =(list update)]
[%more more=(list update)]
[%added time=@da =index =notification]
[%timebox time=@da archived=? =(list [index notification])]
[%count count=@ud]
[%graph-unreads =(map resource @ud)]
[%unreads unreads=(list [index @ud])]
==
--