-
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
Refactor observables in viewer-impl into a map object #7004
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
import {Observable} from '../observable'; | ||
import {findIndex} from '../utils/array'; | ||
import {map} from '../utils/object'; | ||
import {documentStateFor} from './document-state'; | ||
import {getServiceForDoc} from '../service'; | ||
import {dev} from '../log'; | ||
|
@@ -70,7 +71,6 @@ const TRUSTED_VIEWER_HOSTS = [ | |
/(^|\.)google\.(com?|[a-z]{2}|com?\.[a-z]{2}|cat)$/, | ||
]; | ||
|
||
|
||
/** | ||
* An AMP representation of the Viewer. This class doesn't do any work itself | ||
* but instead delegates everything to the actual viewer. This class and the | ||
|
@@ -113,20 +113,17 @@ export class Viewer { | |
this.prerenderSize_ = 1; | ||
|
||
/** @private {number} */ | ||
this.paddingTop_ = 0; | ||
this.initPaddingTop_ = 0; | ||
|
||
/** @private {!Object<string, !Observable<!JSONType>>} */ | ||
this.messageObservables_ = map(); | ||
|
||
/** @private {!Observable<boolean>} */ | ||
this.runtimeOnObservable_ = new Observable(); | ||
|
||
/** @private {!Observable} */ | ||
this.visibilityObservable_ = new Observable(); | ||
|
||
/** @private {!Observable<!JSONType>} */ | ||
this.viewportObservable_ = new Observable(); | ||
|
||
/** @private {!Observable<!ViewerHistoryPoppedEventDef>} */ | ||
this.historyPoppedObservable_ = new Observable(); | ||
|
||
/** @private {!Observable<!JSONType>} */ | ||
this.broadcastObservable_ = new Observable(); | ||
|
||
|
@@ -204,9 +201,9 @@ export class Viewer { | |
this.prerenderSize_; | ||
dev().fine(TAG_, '- prerenderSize:', this.prerenderSize_); | ||
|
||
this.paddingTop_ = parseInt(this.params_['paddingTop'], 10) || | ||
this.paddingTop_; | ||
dev().fine(TAG_, '- padding-top:', this.paddingTop_); | ||
this.initPaddingTop_ = parseInt(this.params_['paddingTop'], 10) || | ||
this.initPaddingTop_; | ||
dev().fine(TAG_, '- initial padding-top:', this.initPaddingTop_); | ||
|
||
/** | ||
* Whether the AMP document is embedded in a webview. | ||
|
@@ -600,8 +597,8 @@ export class Viewer { | |
* Returns the top padding requested by the viewer. | ||
* @return {number} | ||
*/ | ||
getPaddingTop() { | ||
return this.paddingTop_; | ||
getInitPaddingTop() { | ||
return this.initPaddingTop_; | ||
} | ||
|
||
/** | ||
|
@@ -704,21 +701,18 @@ export class Viewer { | |
} | ||
|
||
/** | ||
* Adds a "viewport" event listener for viewer events. | ||
* @param {function(!JSONType)} handler | ||
* @return {!UnlistenDef} | ||
*/ | ||
onViewportEvent(handler) { | ||
return this.viewportObservable_.add(handler); | ||
} | ||
|
||
/** | ||
* Adds a "history popped" event listener for viewer events. | ||
* @param {function(ViewerHistoryPoppedEventDef)} handler | ||
* Adds a eventType listener for viewer events. | ||
* @param {string} eventType | ||
* @param {!function(!JSONType)} handler | ||
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. functions are already 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. done |
||
* @return {!UnlistenDef} | ||
*/ | ||
onHistoryPoppedEvent(handler) { | ||
return this.historyPoppedObservable_.add(handler); | ||
onMessage(eventType, handler) { | ||
let observable = this.messageObservables_[eventType]; | ||
if (!observable) { | ||
observable = new Observable(); | ||
this.messageObservables_[eventType] = observable; | ||
} | ||
return observable.add(handler); | ||
} | ||
|
||
/** | ||
|
@@ -763,21 +757,6 @@ export class Viewer { | |
* @export | ||
*/ | ||
receiveMessage(eventType, data, unusedAwaitResponse) { | ||
if (eventType == 'viewport') { | ||
if (data['paddingTop'] !== undefined) { | ||
this.paddingTop_ = data['paddingTop']; | ||
this.viewportObservable_.fire( | ||
/** @type {!JSONType} */ (data)); | ||
return Promise.resolve(); | ||
} | ||
return undefined; | ||
} | ||
if (eventType == 'historyPopped') { | ||
this.historyPoppedObservable_.fire({ | ||
newStackIndex: data['newStackIndex'], | ||
}); | ||
return Promise.resolve(); | ||
} | ||
if (eventType == 'visibilitychange') { | ||
if (data['prerenderSize'] !== undefined) { | ||
this.prerenderSize_ = data['prerenderSize']; | ||
|
@@ -791,6 +770,11 @@ export class Viewer { | |
/** @type {!JSONType|undefined} */ (data)); | ||
return Promise.resolve(); | ||
} | ||
const observable = this.messageObservables_[eventType]; | ||
if (observable) { | ||
observable.fire(/** @type {!JSONType} */ (data)); | ||
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. It's already 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. removed |
||
return Promise.resolve(); | ||
} | ||
dev().fine(TAG_, 'unknown message:', eventType); | ||
return undefined; | ||
} | ||
|
@@ -991,14 +975,6 @@ function getChannelError(opt_reason) { | |
return new Error('No messaging channel: ' + opt_reason); | ||
} | ||
|
||
/** | ||
* @typedef {{ | ||
* newStackIndex: number | ||
* }} | ||
*/ | ||
export let ViewerHistoryPoppedEventDef; | ||
|
||
|
||
/** | ||
* Sets the viewer visibility state. This calls is restricted to runtime only. | ||
* @param {!VisibilityState} state | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,7 @@ export class Viewport { | |
this./*OK*/scrollLeft_ = null; | ||
|
||
/** @private {number} */ | ||
this.paddingTop_ = viewer.getPaddingTop(); | ||
this.paddingTop_ = viewer.getInitPaddingTop(); | ||
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. Here: 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. Yeah, thanks for the simpler solution |
||
|
||
/** @private {number} */ | ||
this.lastPaddingTop_ = 0; | ||
|
@@ -151,7 +151,7 @@ export class Viewport { | |
/** @private @const (function()) */ | ||
this.boundThrottledScroll_ = this.throttledScroll_.bind(this); | ||
|
||
this.viewer_.onViewportEvent(this.updateOnViewportEvent_.bind(this)); | ||
this.viewer_.onMessage('viewport', this.updateOnViewportEvent_.bind(this)); | ||
this.binding_.updatePaddingTop(this.paddingTop_); | ||
|
||
this.binding_.onScroll(this.scroll_.bind(this)); | ||
|
@@ -584,15 +584,15 @@ export class Viewport { | |
} | ||
|
||
/** | ||
* @param {!JSONType} event | ||
* @param {!JSONType} data | ||
* @private | ||
*/ | ||
updateOnViewportEvent_(event) { | ||
const paddingTop = event['paddingTop']; | ||
const duration = event['duration'] || 0; | ||
const curve = event['curve']; | ||
updateOnViewportEvent_(data) { | ||
const paddingTop = data['paddingTop']; | ||
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. According to old code, it's possible that
I don't know if it's a real situation here. If it is, we may want to add the following in this method:
Please check. 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. yeah, fixed |
||
const duration = data['duration'] || 0; | ||
const curve = data['curve']; | ||
/** @const {boolean} */ | ||
const transient = event['transient']; | ||
const transient = data['transient']; | ||
|
||
if (paddingTop != this.paddingTop_) { | ||
this.lastPaddingTop_ = this.paddingTop_; | ||
|
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.
We need a special object for these messages. Let's call it a
ViewerMessage
and put this class in this file. It will be similar to ActionInvocation class in https://github.com/ampproject/amphtml/blob/master/src/service/action-impl.js#L69E.g.
Thus, this declaration will become: