Skip to content
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

Merged
merged 4 commits into from
Jan 13, 2017

Conversation

muxin
Copy link
Contributor

@muxin muxin commented Jan 12, 2017

Brought up in #6679 (review)

@@ -115,21 +115,15 @@ export class Viewer {
/** @private {number} */
this.paddingTop_ = 0;

/** @private {!Object<string, !Observable<!JSONType>>} */
this.messageObservables_ = [];
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.messageObservables_ = {}; ?

Copy link
Contributor

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();

Copy link
Contributor Author

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 => {
Copy link
Contributor

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.

Copy link
Contributor Author

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 => {
Copy link
Contributor

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) => {
Copy link
Contributor

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();
Copy link
Contributor

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
Copy link
Contributor

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(
Copy link
Contributor

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);
Copy link
Contributor

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);
Copy link
Contributor

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) => {
Copy link
Contributor

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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert

@muxin
Copy link
Contributor Author

muxin commented Jan 13, 2017

@jridgewell @dvoytenko PTAL

* @param {function(ViewerHistoryPoppedEventDef)} handler
* Adds a eventType listener for viewer events.
* @param {string} eventType
* @param {!function(!JSONType)} handler
Copy link
Contributor

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.

Copy link
Contributor Author

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));
Copy link
Contributor

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?

Copy link
Contributor Author

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();
Copy link
Contributor

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?

Copy link
Contributor Author

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(
Copy link
Contributor

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'];
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, fixed

@muxin
Copy link
Contributor Author

muxin commented Jan 13, 2017

@jridgewell PTAL

@muxin muxin merged commit c882ba1 into ampproject:master Jan 13, 2017
@muxin muxin deleted the refactor-observables branch January 13, 2017 21:21
rpominov pushed a commit to yandex-pcode/amphtml that referenced this pull request Jan 20, 2017
* 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)
  ...
jridgewell pushed a commit to jridgewell/amphtml that referenced this pull request Jan 31, 2017
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants