Skip to content

Commit 0d56424

Browse files
committed
feat(svelte-native): NS 8 compatibility
1 parent 02fa71c commit 0d56424

File tree

1 file changed

+60
-110
lines changed

1 file changed

+60
-110
lines changed

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

Lines changed: 60 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -47,65 +47,15 @@ const getNavTransition = ({ transition }) => {
4747
return transition ? { animated: true, transition } : { animated: false }
4848
}
4949

50-
// copied from TNS FrameBase.replacePage
51-
//
52-
// it is not public but there is a comment in there indicating it is for
53-
// HMR (probably their own core HMR though)
54-
//
55-
// NOTE this "worked" in TNS 5, but not anymore in TNS 6: updated version bellow
56-
//
57-
// eslint-disable-next-line no-unused-vars
58-
const replacePage_tns5 = (frame, newPageElement, hotOptions) => {
59-
const currentBackstackEntry = frame._currentEntry
60-
frame.navigationType = 2
61-
frame.performNavigation({
62-
isBackNavigation: false,
63-
entry: {
64-
resolvedPage: newPageElement.nativeView,
65-
//
66-
// entry: currentBackstackEntry.entry,
67-
entry: Object.assign(
68-
currentBackstackEntry.entry,
69-
getNavTransition(hotOptions)
70-
),
71-
navDepth: currentBackstackEntry.navDepth,
72-
fragmentTag: currentBackstackEntry.fragmentTag,
73-
frameId: currentBackstackEntry.frameId,
74-
},
75-
})
76-
}
77-
78-
// Updated for TNS v6
79-
//
80-
// https://github.com/NativeScript/NativeScript/blob/6.1.1/tns-core-modules/ui/frame/frame-common.ts#L656
81-
const replacePage = (frame, newPageElement) => {
82-
const currentBackstackEntry = frame._currentEntry
83-
const newPage = newPageElement.nativeView
84-
const newBackstackEntry = {
85-
entry: currentBackstackEntry.entry,
86-
resolvedPage: newPage,
87-
navDepth: currentBackstackEntry.navDepth,
88-
fragmentTag: currentBackstackEntry.fragmentTag,
89-
frameId: currentBackstackEntry.frameId,
90-
}
91-
const navigationContext = {
92-
entry: newBackstackEntry,
93-
isBackNavigation: false,
94-
navigationType: 2 /* NavigationType replace */,
95-
}
96-
frame._navigationQueue.push(navigationContext)
97-
frame._processNextNavigationEntry()
98-
}
99-
10050
export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
10151
constructor(instance) {
10252
super(instance)
10353

10454
this.nativePageElement = null
10555
this.originalNativeView = null
106-
this.navigatedFromHandler = null
56+
//this.navigatedFromHandler = null
10757

108-
this.relayNativeNavigatedFrom = this.relayNativeNavigatedFrom.bind(this)
58+
//this.relayNativeNavigatedFrom = this.relayNativeNavigatedFrom.bind(this)
10959
}
11060

11161
dispose() {
@@ -123,27 +73,27 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
12373
// svelte-native uses navigateFrom event + e.isBackNavigation to know
12474
// when to $destroy the component -- but we don't want our proxy instance
12575
// destroyed when we renavigate to the same page for navigation purposes!
126-
interceptPageNavigation(pageElement) {
127-
const originalNativeView = pageElement.nativeView
128-
const { on } = originalNativeView
129-
const ownOn = originalNativeView.hasOwnProperty('on')
130-
// tricks svelte-native into giving us its handler
131-
originalNativeView.on = function(type, handler) {
132-
if (type === 'navigatedFrom') {
133-
this.navigatedFromHandler = handler
134-
if (ownOn) {
135-
originalNativeView.on = on
136-
} else {
137-
delete originalNativeView.on
138-
}
139-
} else {
140-
//some other handler wireup, we will just pass it on.
141-
if (on) {
142-
on(type, handler)
143-
}
144-
}
145-
}
146-
}
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+
// }
14797

14898
afterMount(target, anchor) {
14999
// nativePageElement needs to be updated each time (only for page
@@ -168,7 +118,8 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
168118
target.firstChild.tagName == 'page'
169119
if (isNativePage) {
170120
const nativePageElement = target.firstChild
171-
this.interceptPageNavigation(nativePageElement)
121+
// Commented this out as it breaks navigation events
122+
//this.interceptPageNavigation(nativePageElement)
172123
this.nativePageElement = nativePageElement
173124
} else {
174125
// try to protect against components changing from page to no-page
@@ -216,6 +167,15 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
216167
throw new Error('Failed to create updated page')
217168
}
218169
const isFirstPage = !frame.canGoBack()
170+
const nativeView = newPageElement.nativeView
171+
const navigationEntry = Object.assign(
172+
{},
173+
{
174+
create: () => nativeView,
175+
clearHistory: true,
176+
},
177+
getNavTransition(hotOptions)
178+
);
219179

220180
if (isFirstPage) {
221181
// NOTE not so sure of bellow with the new NS6 method for replace
@@ -233,19 +193,9 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
233193
//
234194
// Fortunately, we can overwrite history in this case.
235195
//
236-
const nativeView = newPageElement.nativeView
237-
frame.navigate(
238-
Object.assign(
239-
{},
240-
{
241-
create: () => nativeView,
242-
clearHistory: true,
243-
},
244-
getNavTransition(hotOptions)
245-
)
246-
)
196+
frame.navigate(navigationEntry)
247197
} else {
248-
replacePage(frame, newPageElement, hotOptions)
198+
frame.replacePage(navigationEntry)
249199
}
250200
} else {
251201
const backEntry = frame.backStack.find(
@@ -295,7 +245,7 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
295245
const {
296246
instance: { refreshComponent },
297247
} = this
298-
const { nativePageElement, relayNativeNavigatedFrom } = this
248+
const { nativePageElement /*, relayNativeNavigatedFrom */ } = this
299249
const oldNativeView = nativePageElement.nativeView
300250
// rerender
301251
const target = document.createElement('fragment')
@@ -306,32 +256,32 @@ export const adapter = class ProxyAdapterNative extends ProxyAdapterDom {
306256
// this.nativePageElement is updated in afterMount, triggered by proxy / hooks
307257
const newPageElement = this.nativePageElement
308258
// update event proxy
309-
oldNativeView.off('navigatedFrom', relayNativeNavigatedFrom)
310-
nativePageElement.nativeView.on('navigatedFrom', relayNativeNavigatedFrom)
259+
//oldNativeView.off('navigatedFrom', relayNativeNavigatedFrom)
260+
//nativePageElement.nativeView.on('navigatedFrom', relayNativeNavigatedFrom)
311261
return newPageElement
312262
}
313263

314-
relayNativeNavigatedFrom({ isBackNavigation }) {
315-
const { originalNativeView, navigatedFromHandler } = this
316-
if (!isBackNavigation) {
317-
return
318-
}
319-
if (originalNativeView) {
320-
const { off } = originalNativeView
321-
const ownOff = originalNativeView.hasOwnProperty('off')
322-
originalNativeView.off = function() {
323-
this.navigatedFromHandler = null
324-
if (ownOff) {
325-
originalNativeView.off = off
326-
} else {
327-
delete originalNativeView.off
328-
}
329-
}
330-
}
331-
if (navigatedFromHandler) {
332-
return navigatedFromHandler.apply(this, arguments)
333-
}
334-
}
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+
// }
335285

336286
renderError(err /* , target, anchor */) {
337287
// TODO fallback on TNS error handler for now... at least our error

0 commit comments

Comments
 (0)