-
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
Conversation
@@ -115,21 +115,15 @@ export class Viewer { | |||
/** @private {number} */ | |||
this.paddingTop_ = 0; | |||
|
|||
/** @private {!Object<string, !Observable<!JSONType>>} */ | |||
this.messageObservables_ = []; |
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.
Is this supposed to be an object, or an array?
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.
Array it is~ Will fix this.
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.
Actually I want it to be a map of observables, the index is string. Which type should I annotate here?
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.
this.messageObservables_ = {};
?
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.
Use a prototype-less object:
import {map} from 'src/utils/object.js';
/** @private {!Object<string, !Observable<!JSONType>>} */
this.messageObservables_ = map();
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.
Thanks for the suggestion. Fixed.
@@ -339,7 +339,7 @@ export class AccessService { | |||
|
|||
/** @private */ | |||
listenToBroadcasts_() { | |||
this.viewer_.onBroadcast(message => { | |||
this.viewer_.onMessage('broadcast', message => { |
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.
Broadcast shouldn't change - it should stay as it was onBroadcast
. Broadcast is a special type of messaging, thus we want a special API for it.
Probably be the easiest to just revert this file and related other.
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.
Reverted.
@@ -136,7 +136,7 @@ export class Storage { | |||
|
|||
/** @private */ | |||
listenToBroadcasts_() { | |||
this.viewer_.onBroadcast(message => { | |||
this.viewer_.onMessage('broadcast', message => { |
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.
Ditto: revert
@@ -740,7 +740,7 @@ describe('AccessService authorization', () => { | |||
|
|||
it('should run authorization for broadcast events on same origin', () => { | |||
let broadcastHandler; | |||
sandbox.stub(service.viewer_, 'onBroadcast', handler => { | |||
sandbox.stub(service.viewer_, 'onMessage', (eventType, handler) => { |
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.
Ditto: revert
this.historyPoppedObservable_ = new Observable(); | ||
|
||
/** @private {!Observable<!JSONType>} */ | ||
this.broadcastObservable_ = new Observable(); |
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.
Keep.
* @param {function(ViewerHistoryPoppedEventDef)} handler | ||
* Adds a eventType listener for viewer events. | ||
* @param {string} eventType | ||
* @param {function(T)} handler |
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.
This will always be the ViewerMessage
. No need for a template.
@@ -787,7 +779,7 @@ export class Viewer { | |||
return Promise.resolve(); | |||
} | |||
if (eventType == 'broadcast') { | |||
this.broadcastObservable_.fire( |
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.
Keep
* @return {!UnlistenDef} | ||
*/ | ||
onBroadcast(handler) { | ||
return this.broadcastObservable_.add(handler); |
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.
Keep
@@ -909,7 +909,7 @@ describes.realWin('runtime multidoc', { | |||
const amp = win.AMP.attachShadowDoc(hostElement, importDoc, docUrl); | |||
const viewer = getServiceForDoc(ampdoc, 'viewer'); | |||
const broadcastReceived = sandbox.spy(); | |||
viewer.onBroadcast(broadcastReceived); | |||
viewer.onMessage('broadcast', broadcastReceived); |
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.
Revert
@@ -41,7 +41,7 @@ describe('Storage', () => { | |||
|
|||
viewerBroadcastHandler = undefined; | |||
viewer = { | |||
onBroadcast: handler => { | |||
onMessage: (eventType, handler) => { |
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.
Revert
@@ -500,7 +500,7 @@ describe('Viewer', () => { | |||
|
|||
it('should receive broadcast event', () => { | |||
let broadcastMessage = null; | |||
viewer.onBroadcast(message => { | |||
viewer.onMessage('broadcast', message => { |
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.
Revert
6b668a3
to
e5191c9
Compare
@jridgewell @dvoytenko PTAL |
* @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 comment
The reason will be displayed to describe this comment to others. Learn more.
functions are already !
by default. remove.
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.
done
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
It's already !JSONType
. Why cast it again?
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.
removed
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Here: this.paddingTop_ = Number(viewer.getParam('paddingTop') || 0)
. Should work, right?
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.
Yeah, thanks for the simpler solution
this.historyPoppedObservable_.fire({ | ||
newStackIndex: data['newStackIndex'], | ||
}); | ||
this.messageObservables_[eventType].fire( |
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.
Actually, thinking more about it. We can definitely easily add return type to our observable handler so this is already future-proof. So, agree with you, let's skip ViewerMessage
.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
According to old code, it's possible that paddingTop
is undefined.
I.e. see this old code snippet:
if (eventType == 'viewport') {
if (data['paddingTop'] !== undefined) {
this.paddingTop_ = data['paddingTop'];
this.viewportObservable_.fire(
/** @type {!JSONType} */ (data));
I don't know if it's a real situation here. If it is, we may want to add the following in this method:
const paddingTop = data['paddingTop'];
if (paddingTop == undefined) {
return;
}
Please check.
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.
yeah, fixed
@jridgewell PTAL |
* master: (310 commits) Update csa.md to remove non-required parameters (ampproject#6902) Add notes about requesting ads ATF and link to demo (ampproject#7037) Remove whitelist for lightbox scrollable validator (ampproject#7034) Delegate submit events until amp-form is loaded (ampproject#6929) Moves closure sha384 into a new extension amp-crypto-polyfill for lazy load (ampproject#7006) Refactor observables in viewer-impl into a map object (ampproject#7004) resizing of margins (ampproject#6824) Use URL replacer from embed for pixel (ampproject#7029) adds support for Gemius analytics (ampproject#6558) Avoid duplicating server-layout (ampproject#7021) Laterpay validator config (ampproject#6974) Validator rollup (ampproject#7023) skeleton for amp-tabs (ampproject#7003) Upgrade post-css and related packages to latest (ampproject#7020) handle unload (ampproject#7001) viewer-integr.js -> amp-viewer-integration (ampproject#6989) dev().info()->dev().fine() (ampproject#7017) Turned on experiment flag (ampproject#6774) Unlaunch ios-embed-wrapper for iOS8 to avoid scroll freezing issues (ampproject#7018) Add some A4A ad request parameters (ampproject#6643) ...
Brought up in #6679 (review)