Skip to content

Commit

Permalink
feat: resolve navigation page on navigation event (#624)
Browse files Browse the repository at this point in the history
Allow passing in `resolveOnEvent`  (`navigatingTo` or `navigatedTo`) to `$navigateTo` options. When passed, an event listener will be attached to the page element, and whenever it fires - the Promise returned from `$navigateTo` will be resolved with the target `Page`. Useful for for triggering actions after a navigation is complete.
  • Loading branch information
farfromrefug committed Apr 11, 2020
1 parent 099941f commit 787d1a5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export type NativeScriptVueItemEventData<T> = ItemEventData & { item: T }

export interface NavigationEntryVue extends NavigationEntry {
props?: Record<string, any>,
frame?: any
frame?: any,
resolveOnEvent?: Page.navigatingToEvent | Page.navigatedToEvent
}

export type navigateTo = (
Expand Down
20 changes: 19 additions & 1 deletion platform/nativescript/plugins/navigator-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export default {

updateDevtools()

const resolveOnEvent = options.resolveOnEvent
// ensure we dont resolve twice event though this should never happen!
let resolved = false

const handler = args => {
if (args.isBackNavigation) {
page.off('navigatedFrom', handler)
Expand All @@ -97,6 +101,17 @@ export default {
}
page.on('navigatedFrom', handler)

if (resolveOnEvent) {
const resolveHandler = args => {
if (!resolved) {
resolved = true
resolve(page)
}
page.off(resolveOnEvent, resolveHandler)
}
page.on(resolveOnEvent, resolveHandler)
}

// ensure that the navEntryInstance vue instance is destroyed when the
// page is disposed (clearHistory: true for example)
const dispose = page.disposeNativeView
Expand All @@ -106,7 +121,10 @@ export default {
}

frame.navigate(Object.assign({}, options, { create: () => page }))
resolve(page)
if (!resolveOnEvent) {
resolved = true
resolve(page)
}
})
}
}
Expand Down

0 comments on commit 787d1a5

Please sign in to comment.