Skip to content

Refactor observables in viewer-impl into a map object#7004

Merged
muxin merged 4 commits intoampproject:masterfrom
muxin:refactor-observables
Jan 13, 2017
Merged

Refactor observables in viewer-impl into a map object#7004
muxin merged 4 commits intoampproject:masterfrom
muxin:refactor-observables

Conversation

@muxin
Copy link
Copy Markdown
Contributor

@muxin muxin commented Jan 12, 2017

Brought up in #6679 (review)

this.paddingTop_ = 0;

/** @private {!Object<string, !Observable<!JSONType>>} */
this.messageObservables_ = [];
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

/** @private */
listenToBroadcasts_() {
this.viewer_.onBroadcast(message => {
this.viewer_.onMessage('broadcast', message => {
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted.

/** @private */
listenToBroadcasts_() {
this.viewer_.onBroadcast(message => {
this.viewer_.onMessage('broadcast', message => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto: revert

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

return Promise.resolve();
}
if (eventType == 'broadcast') {
this.broadcastObservable_.fire(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Keep

* @param {function(!JSONType)} handler
* @return {!UnlistenDef}
*/
onBroadcast(handler) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Keep

const viewer = getServiceForDoc(ampdoc, 'viewer');
const broadcastReceived = sandbox.spy();
viewer.onBroadcast(broadcastReceived);
viewer.onMessage('broadcast', broadcastReceived);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Revert

viewerBroadcastHandler = undefined;
viewer = {
onBroadcast: handler => {
onMessage: (eventType, handler) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Revert

it('should receive broadcast event', () => {
let broadcastMessage = null;
viewer.onBroadcast(message => {
viewer.onMessage('broadcast', message => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Revert

@muxin muxin force-pushed the refactor-observables branch from 6b668a3 to e5191c9 Compare January 13, 2017 00:01
@muxin
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

}
const observable = this.messageObservables_[eventType];
if (observable) {
observable.fire(/** @type {!JSONType} */ (data));
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed


/** @private {number} */
this.paddingTop_ = viewer.getPaddingTop();
this.paddingTop_ = viewer.getInitPaddingTop();
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

3 participants