Skip to content

Commit

Permalink
Make showing recently read for user a bit more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Dec 16, 2022
1 parent 2e15b05 commit dd666a8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/calibre/srv/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ def basic_interface_data(ctx, rd):
}
ans['library_map'], ans['default_library_id'] = ctx.library_info(rd)
if ans['username']:
lrc = last_read_cache()
ans['recently_read_by_user'] = lrc.get_recently_read(ans['username'])
ans['recently_read_by_user'] = tuple(x for x in last_read_cache().get_recently_read(ans['username']) if x['library_id'] in ans['library_map'])
return ans


Expand Down
24 changes: 20 additions & 4 deletions src/pyj/book_list/home.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ add_extra_css(def():
return ans
)

recently_read_by_user = {'updated': False}


def update_recently_read_by_user(items):
recently_read_by_user.items = items
recently_read_by_user.updated = True


def show_cover(blob, name, mt, book):
img = document.getElementById(this)
Expand Down Expand Up @@ -117,10 +124,10 @@ def prepare_recent_container(container):
return cover_container


def show_recent_for_user(container_id, interface_data):
def show_recent_for_user(container_id):
container = document.getElementById(container_id)
images = prepare_recent_container(container)
for item in interface_data.recently_read_by_user[:3]:
for item in recently_read_by_user.items[:3]:
q = {'library_id': item.library_id}
if item.cfi:
q.bookpos = item.cfi
Expand Down Expand Up @@ -160,6 +167,15 @@ def show_recent_stage2(books):
start_sync(to_sync)


def show_recent_for_user_if_fetched():
container = this
if not recently_read_by_user.updated:
return conditional_timeout(container.id, 5, show_recent_for_user_if_fetched)
if recently_read_by_user.items and recently_read_by_user.items.length > 0:
return show_recent_for_user(container.id)
return show_recent.call(container)


def show_recent():
container = this
db = get_db()
Expand Down Expand Up @@ -265,8 +281,8 @@ def init(container_id):
recent = E.div(style='display:none', class_='recently-read')
recent_container_id = ensure_id(recent)
container.appendChild(recent)
if interface_data.username and interface_data.recently_read_by_user and interface_data.recently_read_by_user.length > 0:
show_recent_for_user(recent_container_id, interface_data)
if interface_data.username:
conditional_timeout(recent_container_id, 5, show_recent_for_user_if_fetched)
else:
conditional_timeout(recent_container_id, 5, show_recent)

Expand Down
3 changes: 3 additions & 0 deletions src/pyj/book_list/main.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ from popups import install_event_filters
from utils import safe_set_inner_html

from book_list.constants import book_list_container_id, read_book_container_id, INIT_ENDPOINT
from book_list.home import update_recently_read_by_user
from book_list.library_data import fetch_init_data, update_library_data, url_books_query
from book_list.theme import get_color, get_font_family, css_for_variables
from book_list.router import update_window_title, set_default_mode_handler, apply_url, set_mode_handler, on_pop_state
Expand Down Expand Up @@ -83,6 +84,7 @@ def init_ui():
def install_data_and_init_ui(raw_data):
data = JSON.parse(raw_data)
update_interface_data(data)
update_recently_read_by_user(data.recently_read_by_user)
update_library_data(data)
interface_data = get_interface_data()
sd = UserSessionData(interface_data.username, interface_data.user_session_data)
Expand Down Expand Up @@ -132,6 +134,7 @@ def do_update_interface_data():
if end_type is 'load':
data = JSON.parse(xhr.responseText)
update_interface_data(data)
update_recently_read_by_user(data.recently_read_by_user)
if data.translations?:
get_translations(data.translations)
install(data.translations)
Expand Down
1 change: 0 additions & 1 deletion src/pyj/session.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ default_interface_data = {
'custom_list_template': None,
'num_per_page': 50,
'lang_code_for_user_manual': '',
'recently_read_by_user': v'[]',
}

def get_interface_data():
Expand Down

0 comments on commit dd666a8

Please sign in to comment.