Skip to content

Implement "hidden" trigger for VisibilityV2 (IntersectionObserver based)#6634

Merged
lannka merged 6 commits intoampproject:masterfrom
lannka:visibility-v2-hidden
Dec 28, 2016
Merged

Implement "hidden" trigger for VisibilityV2 (IntersectionObserver based)#6634
lannka merged 6 commits intoampproject:masterfrom
lannka:visibility-v2-hidden

Conversation

@lannka
Copy link
Copy Markdown
Contributor

@lannka lannka commented Dec 13, 2016

For #5697

Copy link
Copy Markdown
Contributor

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

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

Everything looks fine to me, but this is something @avimehta should be reviewing.

}

/** @private */
onDocumentHidden_() {
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.

I don't believe the backwards iteration is necessary 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.

changed


const listeners = this.listeners_[resource.getId()];
for (let j = listeners.length - 1; j >= 0; j--) {
const listener = listeners[j];
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.

Can you explain what shouldBeVisible is?

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 indicates if it is a "visible" trigger or "hidden" trigger

// TODO: support "hidden" spec.
// Hidden trigger
if (!this.visibilityListenerRegistered_) {
this.viewer_.onVisibilityChanged(() => {
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.

Can we only register this when the first "hidden" visibility listener is registered?

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.

good point. done

if (this.updateCounters_(
lastChange.intersectionRatio * 100,
listener, shouldBeVisible)) {
listener, true)) {
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.

/* shouldBeVisible */ true.


const state = listener.state;
const lastChange = state[LAST_CHANGE_ENTRY];
const lastVisible = lastChange ? lastChange.intersectionRatio * 100 : 0;
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: Inline comment for false

@dvoytenko
Copy link
Copy Markdown
Contributor

@lannka Same here: looks good, with a couple non-essential comments. However, would be nice to confirm with @avimehta

this.updateCounters_(visible, listener, shouldBeVisible);

if (!shouldBeVisible) {
// For "hidden" trigger, only update state, don't trigger.
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.

The way I understand this, it looks like the hidden conditions will never trigger when the page is in background and the visibility conditions start meeting. Is that correct?

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.

hidden trigger is triggered on visibilityChanged event. See line 372

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.

Yes, saw that. I think I might be understanding this wrong. So just want to make sure that I understand it correctly. What happens for following situation:

"hidden", minContinuousTime = 5 seconds

  • Page is loaded, element is on screen for 2 seconds
  • Page goes in background
  • Does the trigger fire after 3 seconds or after the page comes to foreground 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.

in your example, since the spec is about "continuous" time. It will fire after page back to foregound for another 5 seconds.


// Update states and check if all conditions are satisfied
if (this.updateCounters_(visible, listener, shouldBeVisible)) {
const conditionsMet =
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 is being used only once. WHy move it into a variable?

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.

because we want this.updateCounters_ to run no matter of shouldBeVisible

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.

ooh. Missed that the if block was inside !shouldBeVisible. looks good.

});
});

it('"hidden" trigger should work with duration condition', () => {
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.

add a test for hidden trigger without duration conditions?

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.

Does it really make sense to have a hidden trigger without duration condition?

For example, what does it mean to have a spec like following?

          "hidden": {
            "on": "hidden",
            "visibilitySpec": {
              "selector": "#img-2",
              "visiblePercentageMin": 10
            },
          }

To me, it should probably be done with a "visible" trigger instead.

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.

Sorry I meant without any conditions at all. so something like this:

          "hidden": {
            "on": "hidden",
            "visibilitySpec": {
              "selector": "#img-2"
            },
          }

A test like that might exist but was not sure and hence brought it up.

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.

I'm not sure how exactly a spec like that should behave.

Does that mean "fires when page goes to background and the element has been visible"?
If so, I think a "visible" trigger should be used instead.

Or does that mean "fires when page goes to background and element is currently visible"? Why does people want to track that?

Copy link
Copy Markdown
Contributor Author

@lannka lannka Dec 27, 2016

Choose a reason for hiding this comment

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

In fact, I think the V1 code for hidden trigger does not take non-duration condition into consideration either.

I tested the V1 logic, your example spec behaves exactly the same as:

          "hidden": {
            "on": "hidden"
          }

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.

Interesting. I thought my example would mean: "fires when page goes to background and element is currently visible". Anyway, I think this is another edge case that we should document.

@lannka lannka merged commit 7ab6aaa into ampproject:master Dec 28, 2016
@lannka lannka deleted the visibility-v2-hidden branch December 28, 2016 00:00
torch2424 pushed a commit to torch2424/amphtml that referenced this pull request Jan 3, 2017
…ed) (ampproject#6634)

* Implement "hidden" trigger for VisibilityV2 (IntersectionObserver based visibility tracking).

* Give test cases better names.

* Address comments.

* Address comment.

* Fix tests.
jridgewell pushed a commit to jridgewell/amphtml that referenced this pull request Jan 31, 2017
…ed) (ampproject#6634)

* Implement "hidden" trigger for VisibilityV2 (IntersectionObserver based visibility tracking).

* Give test cases better names.

* Address comments.

* Address comment.

* Fix tests.
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.

4 participants