-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Listen to messages before iframe append #8981
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
43e7045
Listen to messages before iframe append and few unrelated cleanups fo…
aghassemi 05135ea
more cleanup
aghassemi d1a5b89
more cleanup
aghassemi e424c8c
more cleanup
aghassemi 8796623
more cleanup
aghassemi 1f4a2d0
more cleanup
aghassemi d33fef8
more cleanup
aghassemi 1e9196a
more cleanup
aghassemi cc51d8a
more cleanup
aghassemi f78de81
more cleanup
aghassemi a06bf7f
Resolve with load promise
aghassemi 5005d5e
lint
aghassemi e23c41d
merge then()s
aghassemi 9f91d93
lint
aghassemi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import { | |
installVideoManagerForDoc, | ||
} from '../../../src/service/video-manager-impl'; | ||
import {isObject} from '../../../src/types'; | ||
import {listen} from '../../../src/event-helper'; | ||
import {VideoEvents} from '../../../src/video-interface'; | ||
import {videoManagerForDoc} from '../../../src/services'; | ||
|
||
|
@@ -42,6 +43,9 @@ class AmpOoyalaPlayer extends AMP.BaseElement { | |
|
||
/** @private {?Function} */ | ||
this.playerReadyResolver_ = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be dropped. |
||
|
||
/** @private {?Function} */ | ||
this.unlistenMessage_ = null; | ||
} | ||
|
||
/** | ||
|
@@ -58,16 +62,6 @@ class AmpOoyalaPlayer extends AMP.BaseElement { | |
this.playerReadyResolver_ = resolve; | ||
}); | ||
|
||
const iframe = this.element.ownerDocument.createElement('iframe'); | ||
this.iframe_ = iframe; | ||
|
||
this.forwardEvents([VideoEvents.PLAY, VideoEvents.PAUSE], iframe); | ||
this.applyFillContent(iframe, true); | ||
this.element.appendChild(iframe); | ||
|
||
iframe.setAttribute('frameborder', '0'); | ||
iframe.setAttribute('allowfullscreen', 'true'); | ||
|
||
installVideoManagerForDoc(this.element); | ||
videoManagerForDoc(this.element).register(this); | ||
} | ||
|
@@ -100,16 +94,25 @@ class AmpOoyalaPlayer extends AMP.BaseElement { | |
|
||
src += '&ec=' + encodeURIComponent(embedCode) + | ||
'&pbid=' + encodeURIComponent(playerId); | ||
this.iframe_.src = src; | ||
|
||
window.addEventListener('message', | ||
event => this.handleOoyalaMessages_(event)); | ||
const iframe = this.element.ownerDocument.createElement('iframe'); | ||
this.applyFillContent(iframe, true); | ||
iframe.setAttribute('frameborder', '0'); | ||
iframe.setAttribute('allowfullscreen', 'true'); | ||
iframe.src = src; | ||
|
||
this.iframe_ = iframe; | ||
|
||
return this.loadPromise(this.iframe_) | ||
.then(() => { | ||
this.element.dispatchCustomEvent(VideoEvents.LOAD); | ||
this.playerReadyResolver_(this.iframe_); | ||
}); | ||
this.unlistenMessage_ = listen(this.win, 'message', event => { | ||
this.handleOoyalaMessages_(event); | ||
}); | ||
|
||
this.element.appendChild(this.iframe_); | ||
const loadPromise = this.loadPromise(this.iframe_).then(() => { | ||
this.element.dispatchCustomEvent(VideoEvents.LOAD); | ||
}); | ||
this.playerReadyResolver_(loadPromise); | ||
return loadPromise; | ||
} | ||
|
||
/** @override */ | ||
|
@@ -118,6 +121,15 @@ class AmpOoyalaPlayer extends AMP.BaseElement { | |
removeElement(this.iframe_); | ||
this.iframe_ = null; | ||
} | ||
|
||
if (this.unlistenMessage_) { | ||
this.unlistenMessage_(); | ||
} | ||
|
||
this.playerReadyPromise_ = new Promise(resolve => { | ||
this.playerReadyResolver_ = resolve; | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
|
@@ -158,34 +170,39 @@ class AmpOoyalaPlayer extends AMP.BaseElement { | |
} | ||
} | ||
|
||
/** | ||
* Sends a command to the player through postMessage. | ||
* @param {string} command | ||
* @private | ||
*/ | ||
sendCommand_(command) { | ||
this.playerReadyPromise_.then(() => { | ||
if (this.iframe_ && this.iframe_.contentWindow) { | ||
this.iframe_.contentWindow./*OK*/postMessage(command, '*'); | ||
} | ||
}); | ||
} | ||
|
||
// VideoInterface Implementation. See ../src/video-interface.VideoInterface | ||
|
||
/** @override */ | ||
play(unusedIsAutoplay) { | ||
this.playerReadyPromise_.then(() => { | ||
this.iframe_.contentWindow./*OK*/postMessage('play', '*'); | ||
}); | ||
this.sendCommand_('play'); | ||
} | ||
|
||
/** @override */ | ||
pause() { | ||
this.playerReadyPromise_.then(() => { | ||
this.iframe_.contentWindow./*OK*/postMessage('pause', '*'); | ||
}); | ||
this.sendCommand_('pause'); | ||
} | ||
|
||
/** @override */ | ||
mute() { | ||
this.playerReadyPromise_.then(() => { | ||
this.iframe_.contentWindow./*OK*/postMessage('mute', '*'); | ||
}); | ||
this.sendCommand_('mute'); | ||
} | ||
|
||
/** @override */ | ||
unmute() { | ||
this.playerReadyPromise_.then(() => { | ||
this.iframe_.contentWindow./*OK*/postMessage('unmute', '*'); | ||
}); | ||
this.sendCommand_('unmute'); | ||
} | ||
|
||
/** @override */ | ||
|
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 |
---|---|---|
|
@@ -188,7 +188,6 @@ class AmpYoutube extends AMP.BaseElement { | |
iframe.setAttribute('allowfullscreen', 'true'); | ||
iframe.src = src; | ||
this.applyFillContent(iframe); | ||
this.element.appendChild(iframe); | ||
|
||
this.iframe_ = iframe; | ||
|
||
|
@@ -198,16 +197,14 @@ class AmpYoutube extends AMP.BaseElement { | |
this.handleYoutubeMessages_.bind(this) | ||
); | ||
|
||
this.win.addEventListener( | ||
'message', event => this.handleYoutubeMessages_(event) | ||
); | ||
|
||
return this.loadPromise(iframe) | ||
this.element.appendChild(this.iframe_); | ||
const loadPromise = this.loadPromise(this.iframe_) | ||
.then(() => this.listenToFrame_()) | ||
.then(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Can we join this with the previous block? |
||
this.element.dispatchCustomEvent(VideoEvents.LOAD); | ||
this.playerReadyResolver_(this.iframe_); | ||
}); | ||
this.playerReadyResolver_(loadPromise); | ||
return loadPromise; | ||
} | ||
|
||
/** @override */ | ||
|
@@ -264,7 +261,7 @@ class AmpYoutube extends AMP.BaseElement { | |
* @param {string} command | ||
* @param {Array=} opt_args | ||
* @private | ||
* */ | ||
*/ | ||
sendCommand_(command, opt_args) { | ||
this.playerReadyPromise_.then(() => { | ||
if (this.iframe_ && this.iframe_.contentWindow) { | ||
|
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.
👏