-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lazy loaded routes for samples #79
Merged
quentinderoubaix
merged 1 commit into
AmadeusITGroup:main
from
quentinderoubaix:46-sample-urls-should-only-import-required-widgets
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type {UnsubscribeFunction, UnsubscribeObject} from '@amadeus-it-group/tansu'; | ||
import {asReadable, writable} from '@amadeus-it-group/tansu'; | ||
|
||
/** | ||
* Creates an iframe handler, allowing to enable dynamic resizing and detect if the parent should display a spinner. | ||
* The returned spinner store includes a debounce for better user experience. | ||
* | ||
* In order to detect when the page is fully loaded, we listen to message events. | ||
* | ||
* @param defaultHeight the default height in pixels of the iframe | ||
* @param resize enable dynamic resizing | ||
* @param messageType the type of the message event we listen to | ||
* @param spinnerDebounce the debounce in milliseconds before the spinner store is set to true | ||
* @returns the handler and a spinner store | ||
*/ | ||
export function createIframeHandler(defaultHeight: number, resize = true, messageType = 'sampleload', spinnerDebounce = 300) { | ||
const _iframeLoaded$ = writable(true); | ||
const _showSpinner$ = writable(false); | ||
const _height$ = writable(0); | ||
|
||
let spinnerTimer: any; | ||
let resizeObserver: ResizeObserver | undefined; | ||
let heightSubscription: (UnsubscribeFunction & UnsubscribeObject) | undefined; | ||
|
||
const setupObserver = (iframe: HTMLIFrameElement) => { | ||
if (!resizeObserver) { | ||
resizeObserver = new ResizeObserver((entries) => { | ||
if (entries.length === 1) { | ||
if (_iframeLoaded$()) { | ||
_height$.set(Math.ceil(entries[0].contentRect.height || _height$())); | ||
} | ||
} | ||
}); | ||
} | ||
resizeObserver.disconnect(); | ||
const root = iframe.contentDocument?.getElementById('root'); | ||
if (root) { | ||
resizeObserver.observe(root); | ||
} | ||
}; | ||
const onLoad = (event: Event) => { | ||
if (event.target instanceof HTMLIFrameElement) { | ||
setupObserver(event.target); | ||
} | ||
}; | ||
|
||
return { | ||
showSpinner$: asReadable(_showSpinner$), | ||
handler: (iframe: HTMLIFrameElement, baseSrc: string) => { | ||
iframe.onload = onLoad; | ||
if (iframe.contentDocument?.getElementById('root')) { | ||
setupObserver(iframe); | ||
} | ||
if (resize) { | ||
heightSubscription = _height$.subscribe((height) => (iframe.height = (height || defaultHeight) + 'px')); | ||
} else { | ||
iframe.height = defaultHeight + 'px'; | ||
} | ||
|
||
const update = (baseSrc: string) => { | ||
if (!iframe.contentWindow?.location?.href?.startsWith(baseSrc)) { | ||
_iframeLoaded$.set(false); | ||
if (spinnerTimer) { | ||
clearTimeout(spinnerTimer); | ||
} | ||
spinnerTimer = setTimeout(() => { | ||
_showSpinner$.set(true); | ||
spinnerTimer = undefined; | ||
}, spinnerDebounce); | ||
} | ||
}; | ||
update(baseSrc); | ||
|
||
const sampleLoad = (e: Event) => { | ||
if (e instanceof MessageEvent && e.data.type === messageType && e.source === iframe.contentWindow) { | ||
if (spinnerTimer) { | ||
clearTimeout(spinnerTimer); | ||
spinnerTimer = undefined; | ||
} | ||
_iframeLoaded$.set(true); | ||
_showSpinner$.set(false); | ||
} | ||
}; | ||
window.addEventListener('message', sampleLoad, false); | ||
|
||
return { | ||
update, | ||
destroy: () => { | ||
window.removeEventListener('message', sampleLoad); | ||
resizeObserver?.disconnect(); | ||
heightSubscription?.unsubscribe(); | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, this should be extracted (as part of another PR) in a reactive utility in the core (similar to the intersection utility) that could also be used in the slider.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tracked in #291