Skip to content

Commit

Permalink
wakeup focus & deep-link routing (#86)
Browse files Browse the repository at this point in the history
* wakeup focus & deep-link routing

* conventionalize

* should be wakeups

* fix
  • Loading branch information
davidmarkclements committed Mar 7, 2024
1 parent de7975a commit 7629756
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 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 = null
}

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,14 @@ 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)
} else {
this.load('/')
}
const page = '/' + (Pear.config.linkData || '')
console.log('load', page)
this.load(page).catch(console.error)
Pear.wakeups(({ data }) => {
Pear.Window.self.focus().catch(console.error)
const page = '/' + (data || '')
this.load(page).catch(console.error)
})
})
}
})

0 comments on commit 7629756

Please sign in to comment.