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

Improve async compatibility of app store #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
40 changes: 27 additions & 13 deletions modules/firmware_apps/app_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import json
import os
import tarfile
import time
from tarfile import DIRTYPE, TarFile
from typing import Any, Callable

import app
import async_helpers
import wifi
import shutil
import machine
Expand Down Expand Up @@ -77,18 +79,6 @@ def get_index(self):
return
self.update_state("refreshing_index")

def background_update(self, delta):
if self.state == "refreshing_index":
try:
self.response = get(APP_STORE_LISTING_URL)
except Exception:
self.update_state("no_index")
return
self.update_state("index_received")
if self.to_install_app:
self.install_app(self.to_install_app)
self.to_install_app = None

def handle_index(self):
if not self.response:
print(self.response)
Expand Down Expand Up @@ -231,7 +221,17 @@ def error_screen(self, ctx, message):
ctx.gray(1).move_to(0, start_y + i * ctx.font_size).text(line)
ctx.restore()

def update(self, delta):
async def run(self, render_update):
last_time = time.ticks_ms()
await render_update()
while True:
cur_time = time.ticks_ms()
delta_ticks = time.ticks_diff(cur_time, last_time)
await self.main_loop(delta_ticks, render_update)
await render_update()
last_time = cur_time

async def main_loop(self, delta, render_update):
if self.state == "init":
if not wifi.status():
self.update_state("wifi_init")
Expand All @@ -255,6 +255,20 @@ def update(self, delta):
self.prepare_available_menu()
elif self.state == "installed_menu" and not self.installed_menu:
self.prepare_installed_menu()
elif self.state == "refreshing_index":
try:
self.response = await async_helpers.unblock(
get, render_update, APP_STORE_LISTING_URL
)
except Exception:
self.update_state("no_index")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would get changed in the next line wouldn't it

self.update_state("index_received")
Comment on lines +259 to +265
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, per Naomi's comment I think this ought to be like this?

Suggested change
try:
self.response = await async_helpers.unblock(
get, render_update, APP_STORE_LISTING_URL
)
except Exception:
self.update_state("no_index")
self.update_state("index_received")
try:
self.response = await async_helpers.unblock(
get, render_update, APP_STORE_LISTING_URL
)
self.update_state("index_received")
except Exception:
self.update_state("no_index")


if self.to_install_app:
await async_helpers.unblock(
self.install_app, render_update, self.to_install_app
)
self.to_install_app = None

if self.menu:
self.menu.update(delta)
Expand Down
Loading