Skip to content

Commit

Permalink
fix: try to fix PWA prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Sep 14, 2024
1 parent 1d3a6c2 commit 7432404
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
11 changes: 5 additions & 6 deletions src/ic_panda_frontend/src/lib/ReloadPrompt.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import { useRegisterSW } from 'virtual:pwa-register/svelte'
const toastStore = getToastStore()
const minInterval = 20 * 1000
const maxInterval = 20 * 60 * 1000
const minInterval = 60 * 1000
const maxInterval = 60 * 60 * 1000
const { needRefresh, updateServiceWorker } = useRegisterSW({
const { offlineReady, needRefresh, updateServiceWorker } = useRegisterSW({
onRegistered(r) {
if (r) {
let i = 0
Expand All @@ -27,9 +27,8 @@
})
function close(res: { id: string; status: 'queued' | 'closed' }) {
if (res.status === 'closed') {
needRefresh.set(false)
}
offlineReady.set(false)
needRefresh.set(false)
}
$: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
let usernameInput = ''
let usernameErr = ''
let amount = 0n
let result: UserInfo | null = null
let existUsernames: string[] = []
function checkName() {
Expand Down Expand Up @@ -85,16 +84,17 @@
return
}
let user: UserInfo
if (!usernameInput) {
result = await myState.api.update_my_name(nameInput)
user = await myState.api.update_my_name(nameInput)
} else {
await tokenLedgerAPI.ensureAllowance(messageCanisterPrincipal, amount)
result = await myState.api.register_username(usernameInput, nameInput)
user = await myState.api.register_username(usernameInput, nameInput)
}
await myState.setCacheUserInfo(Date.now(), user)
await sleep(618)
await myState.refreshAllState()
await myState.refreshAllState(false)
const mk = await myState.masterKey()
if (!mk || !mk.isOpened() || myState.masterKeyKind() !== mk.kind) {
modalStore.close()
Expand Down
14 changes: 8 additions & 6 deletions src/ic_panda_frontend/src/lib/stores/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ export class MyMessageState {
this.setCacheUserInfo(Date.now(), $info)
set($info)
} else {
KVS.get<UserInfo>('Users', this.id).then((info) => {
if (info) {
set(info)
KVS.get<[number, UserInfo]>('Users', this.id).then((rt) => {
if (rt) {
set(rt[1])
}
})
}
Expand All @@ -177,9 +177,11 @@ export class MyMessageState {
if (force) {
await Promise.all([this.api.refreshMyInfo(), this.api.refreshState()])
}
if (this.principal.isAnonymous()) return

const now = Date.now()
this._myInfo = this.api.myInfo
if (!this._myInfo && !this.principal.isAnonymous()) {
if (!this._myInfo) {
this._myInfo = await this.getCacheUserInfo(now, this.principal)
}
if (this._myInfo) {
Expand Down Expand Up @@ -1319,7 +1321,7 @@ export class MyMessageState {
return rt
}

private async getCacheUserInfo(
async getCacheUserInfo(
now: number,
user: Principal | string
): Promise<UserInfo | null> {
Expand All @@ -1342,7 +1344,7 @@ export class MyMessageState {
return info && now - ts < usersCacheExp ? info : null
}

private async setCacheUserInfo(now: number, info: UserInfo) {
async setCacheUserInfo(now: number, info: UserInfo) {
await KVS.set<[number, UserInfo]>('Users', [now, info], info.id.toText())

if (info.username.length == 1) {
Expand Down

0 comments on commit 7432404

Please sign in to comment.