feat(live-preview): exports ready function for reuse (#3600)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
export { handleMessage } from './handleMessage'
|
||||
export { mergeData } from './mergeData'
|
||||
export { ready } from './ready'
|
||||
export { subscribe } from './subscribe'
|
||||
export { unsubscribe } from './unsubscribe'
|
||||
|
||||
15
packages/live-preview/src/ready.ts
Normal file
15
packages/live-preview/src/ready.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export const ready = (args: { serverURL: string }): void => {
|
||||
const { serverURL } = args
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
// This subscription may have been from either an iframe `src` or `window.open()`
|
||||
// i.e. `window?.opener` || `window?.parent`
|
||||
window?.opener?.postMessage(
|
||||
JSON.stringify({
|
||||
popupReady: true,
|
||||
type: 'payload-live-preview',
|
||||
}),
|
||||
serverURL,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -15,17 +15,6 @@ export const subscribe = <T>(args: {
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('message', onMessage)
|
||||
|
||||
// This subscription may have been from either an iframe `src` or `window.open()`
|
||||
// i.e. `window?.opener` || `window?.parent`
|
||||
|
||||
window?.opener?.postMessage(
|
||||
JSON.stringify({
|
||||
popupReady: true,
|
||||
type: 'payload-live-preview',
|
||||
}),
|
||||
serverURL,
|
||||
)
|
||||
}
|
||||
|
||||
return onMessage
|
||||
|
||||
Reference in New Issue
Block a user