Skip to content

Commit

Permalink
wakeup focus & deep-link routing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmarkclements committed Mar 6, 2024
1 parent de7975a commit bc7689e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/app-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ customElements.define('app-router', class AppRouter extends HTMLElement {
constructor () {
super()
this.routes = {}
this.page = '/'
}

unload () {
for (const element of Object.values(this.routes)) element?.unload && element.unload()
}

async load (pathname = '/', opts = {}) {
if (this.page === pathname) return
this.page = pathname
for (const [route, element] of Object.entries(this.routes)) {
if (pathname.startsWith(route)) {
const page = pathname.slice(route.length) || '/'
Expand Down Expand Up @@ -67,11 +70,16 @@ customElements.define('app-router', class AppRouter extends HTMLElement {
})

window.addEventListener('load', () => {
if (Pear.config.link.indexOf('pear://runtime/') === 0) {
this.load(Pear.config.link.slice(14)).catch(console.error)
if (Pear.config.linkData) {
this.load('/' + Pear.config.linkData).catch(console.error)
} else {
this.load('/')
}
Pear.wakeup(({ data }) => {
Pear.Window.self.focus().catch(console.error)
const page = '/' + (data || '')
this.load(page).catch(console.error)
})
})
}
})

0 comments on commit bc7689e

Please sign in to comment.