Skip to content

Commit 9a85b83

Browse files
committed
fix: Navigation handler is not passed to new native view
1 parent 0d56424 commit 9a85b83

File tree

1 file changed

+15
-58
lines changed

1 file changed

+15
-58
lines changed

packages/svelte-hmr/runtime/svelte-native/proxy-adapter-native.js

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
5252
super(instance)
5353

5454
this.nativePageElement = null
55-
this.originalNativeView = null
56-
//this.navigatedFromHandler = null
57-
58-
//this.relayNativeNavigatedFrom = this.relayNativeNavigatedFrom.bind(this)
5955
}
6056

6157
dispose() {
@@ -70,31 +66,6 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
7066
}
7167
}
7268

73-
// svelte-native uses navigateFrom event + e.isBackNavigation to know
74-
// when to $destroy the component -- but we don't want our proxy instance
75-
// destroyed when we renavigate to the same page for navigation purposes!
76-
// interceptPageNavigation(pageElement) {
77-
// const originalNativeView = pageElement.nativeView
78-
// const { on } = originalNativeView
79-
// const ownOn = originalNativeView.hasOwnProperty('on')
80-
// // tricks svelte-native into giving us its handler
81-
// originalNativeView.on = function(type, handler) {
82-
// if (type === 'navigatedFrom') {
83-
// this.navigatedFromHandler = handler
84-
// if (ownOn) {
85-
// originalNativeView.on = on
86-
// } else {
87-
// delete originalNativeView.on
88-
// }
89-
// } else {
90-
// //some other handler wireup, we will just pass it on.
91-
// if (on) {
92-
// on(type, handler)
93-
// }
94-
// }
95-
// }
96-
// }
97-
9869
afterMount(target, anchor) {
9970
// nativePageElement needs to be updated each time (only for page
10071
// components, native component that are not pages follow normal flow)
@@ -118,8 +89,6 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
11889
target.firstChild.tagName == 'page'
11990
if (isNativePage) {
12091
const nativePageElement = target.firstChild
121-
// Commented this out as it breaks navigation events
122-
//this.interceptPageNavigation(nativePageElement)
12392
this.nativePageElement = nativePageElement
12493
} else {
12594
// try to protect against components changing from page to no-page
@@ -245,44 +214,32 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
245214
const {
246215
instance: { refreshComponent },
247216
} = this
248-
const { nativePageElement /*, relayNativeNavigatedFrom */ } = this
249-
const oldNativeView = nativePageElement.nativeView
217+
const { nativePageElement: oldNativePageElement } = this
218+
const oldNativeView = oldNativePageElement.nativeView
250219
// rerender
251220
const target = document.createElement('fragment')
221+
252222
// not using conservative for now, since there's nothing in place here to
253223
// leverage it (yet?) -- and it might be easier to miss breakages in native
254224
// only code paths
255225
refreshComponent(target, null)
226+
256227
// this.nativePageElement is updated in afterMount, triggered by proxy / hooks
257228
const newPageElement = this.nativePageElement
258-
// update event proxy
259-
//oldNativeView.off('navigatedFrom', relayNativeNavigatedFrom)
260-
//nativePageElement.nativeView.on('navigatedFrom', relayNativeNavigatedFrom)
229+
230+
// svelte-native uses navigateFrom event + e.isBackNavigation to know when to $destroy the component.
231+
// To keep that behaviour after refresh, we move event handler from old native view to the new one using
232+
// __navigationFromHandler property that svelte-native provides us with.
233+
const navigationFromHandler = oldNativeView.__navigationFromHandler;
234+
if (navigationFromHandler) {
235+
oldNativeView.off('navigatedFrom', navigationFromHandler)
236+
newPageElement.nativeView.on('navigatedFrom', navigationFromHandler)
237+
delete oldNativeView.__navigationFromHandler;
238+
}
239+
261240
return newPageElement
262241
}
263242

264-
// relayNativeNavigatedFrom({ isBackNavigation }) {
265-
// const { originalNativeView, navigatedFromHandler } = this
266-
// if (!isBackNavigation) {
267-
// return
268-
// }
269-
// if (originalNativeView) {
270-
// const { off } = originalNativeView
271-
// const ownOff = originalNativeView.hasOwnProperty('off')
272-
// originalNativeView.off = function() {
273-
// this.navigatedFromHandler = null
274-
// if (ownOff) {
275-
// originalNativeView.off = off
276-
// } else {
277-
// delete originalNativeView.off
278-
// }
279-
// }
280-
// }
281-
// if (navigatedFromHandler) {
282-
// return navigatedFromHandler.apply(this, arguments)
283-
// }
284-
// }
285-
286243
renderError(err /* , target, anchor */) {
287244
// TODO fallback on TNS error handler for now... at least our error
288245
// is more informative

0 commit comments

Comments
 (0)