-
Notifications
You must be signed in to change notification settings - Fork 76
Bugfix: avoid unintentional polling #327
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
Open
ro0gr
wants to merge
3
commits into
html-next:main
Choose a base branch
from
ro0gr:fix-using-passive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,8 @@ module('Unit | Radar Utils | Scroll Handler'); | |
|
|
||
| test('We can add, trigger, and remove a scroll handler', (assert) => { | ||
| let scrollHandlers = new ScrollHandler(); | ||
| scrollHandlers.isUsingPassive = true; | ||
|
|
||
| let done = assert.async(2); | ||
| let scrollable = createScrollable(); | ||
| let handler = () => { | ||
|
|
@@ -44,31 +46,81 @@ test('We can add, trigger, and remove a scroll handler', (assert) => { | |
| // test adding a single handler | ||
| scrollHandlers.addScrollHandler(scrollable, handler); | ||
|
|
||
| assert.equal(scrollHandlers.length, 1, `We have one element to watch.`); | ||
| assert.strictEqual(scrollHandlers.length, 1, `We have one element to watch.`); | ||
| assert.false(scrollHandlers.isPolling, 'polling is inactive, using a passive handler'); | ||
|
|
||
| let scrollableIndex = scrollHandlers.elements.indexOf(scrollable); | ||
| assert.ok(scrollableIndex !== -1, `The scrollable was added to the watched elements list.`); | ||
| assert.true(scrollableIndex !== -1, `The scrollable was added to the watched elements list.`); | ||
| let cache = scrollHandlers.handlers[scrollableIndex]; | ||
| assert.ok(cache.handlers.length === 1); | ||
| assert.strictEqual(cache.handlers.length, 1); | ||
|
|
||
| // test triggering that handler | ||
| assert.equal(scrollable.scrollTop, 0, `The scrollable is initially unscrolled`); | ||
| assert.strictEqual(scrollable.scrollTop, 0, `The scrollable is initially unscrolled`); | ||
|
|
||
| afterNextScrollUpdate(() => { | ||
| scrollable.scrollTop = 10; | ||
| assert.equal(scrollable.scrollTop, 10, `We updated the scrollable's scroll position`); | ||
| assert.strictEqual(scrollable.scrollTop, 10, `We updated the scrollable's scroll position`); | ||
|
|
||
| afterNextScrollUpdate(() => { | ||
| // test removing that handler | ||
| scrollHandlers.removeScrollHandler(scrollable, handler); | ||
| let newScrollableIndex = scrollHandlers.elements.indexOf(scrollable); | ||
|
|
||
| assert.ok(cache.handlers.length === 0, `The handler was removed from the listener cache.`); | ||
| assert.ok(newScrollableIndex === -1, `Removing the last handler removed the element from the watched elements list.`); | ||
| assert.ok(scrollHandlers.handlers.indexOf(cache) === -1, `Removing the last handler removed the cache.`); | ||
| assert.strictEqual(cache.handlers.length, 0, `The handler was removed from the listener cache.`); | ||
| assert.strictEqual(newScrollableIndex, -1, `Removing the last handler removed the element from the watched elements list.`); | ||
| assert.strictEqual(scrollHandlers.handlers.indexOf(cache), -1, `Removing the last handler removed the cache.`); | ||
|
|
||
| assert.equal(scrollHandlers.length, 0, `We have no more elements to watch.`); | ||
| assert.equal(scrollHandlers.isPolling, false, `We are no longer polling the elements.`); | ||
| assert.strictEqual(scrollHandlers.length, 0, `We have no more elements to watch.`); | ||
| assert.false(scrollHandlers.isPolling, `polling is still inactive`); | ||
|
|
||
| destroyScrollable(scrollable); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test('Polling', (assert) => { | ||
| let scrollHandlers = new ScrollHandler(); | ||
| scrollHandlers.isUsingPassive = false; | ||
|
|
||
| let done = assert.async(2); | ||
| let scrollable = createScrollable(); | ||
| let handler = () => { | ||
| assert.ok('handler was triggered'); | ||
| done(); | ||
| }; | ||
|
|
||
| assert.strictEqual(scrollHandlers.length, 0, `We initially have no elements to watch.`); | ||
|
|
||
| // test adding a single handler | ||
| scrollHandlers.addScrollHandler(scrollable, handler); | ||
|
|
||
| assert.strictEqual(scrollHandlers.length, 1, `We have one element to watch.`); | ||
| assert.true(scrollHandlers.isPolling, 'polling is active'); | ||
|
|
||
| let scrollableIndex = scrollHandlers.elements.indexOf(scrollable); | ||
| assert.true(scrollableIndex !== -1, `The scrollable was added to the watched elements list.`); | ||
| let cache = scrollHandlers.handlers[scrollableIndex]; | ||
| assert.strictEqual(cache.handlers.length, 1); | ||
|
|
||
| // test triggering that handler | ||
| assert.strictEqual(scrollable.scrollTop, 0, `The scrollable is initially unscrolled`); | ||
|
|
||
| afterNextScrollUpdate(() => { | ||
| scrollable.scrollTop = 10; | ||
| assert.strictEqual(scrollable.scrollTop, 10, `We updated the scrollable's scroll position`); | ||
|
|
||
| afterNextScrollUpdate(() => { | ||
| // test removing that handler | ||
| scrollHandlers.removeScrollHandler(scrollable, handler); | ||
| let newScrollableIndex = scrollHandlers.elements.indexOf(scrollable); | ||
|
|
||
| assert.strictEqual(cache.handlers.length, 0, `The handler was removed from the listener cache.`); | ||
| assert.strictEqual(newScrollableIndex, -1, `Removing the last handler removed the element from the watched elements list.`); | ||
| assert.strictEqual(scrollHandlers.handlers.indexOf(cache), -1, `Removing the last handler removed the cache.`); | ||
|
|
||
| assert.strictEqual(scrollHandlers.length, 0, `We have no more elements to watch.`); | ||
| assert.false(scrollHandlers.isPolling, `We are no longer polling the elements.`); | ||
|
|
||
| destroyScrollable(scrollable); | ||
| done(); | ||
|
|
@@ -125,7 +177,6 @@ test('Adding/removing multiple handlers to an element works as expected', (asser | |
| assert.ok(scrollHandlers.handlers.indexOf(cache) === -1, `Removing the last handler removed the cache.`); | ||
|
|
||
| assert.equal(scrollHandlers.length, 0, `We have no more elements to watch.`); | ||
| assert.equal(scrollHandlers.isPolling, false, `We are no longer polling the elements.`); | ||
|
|
||
| destroyScrollable(scrollable); | ||
| done(); | ||
|
|
@@ -192,11 +243,68 @@ test('Multiple elements with handlers works as expected', (assert) => { | |
| assert.ok(scrollHandlers.handlers.indexOf(cache2) === -1, `Removing the last handler removed the cache.`); | ||
|
|
||
| assert.equal(scrollHandlers.length, 0, `We have no more elements to watch.`); | ||
| assert.equal(scrollHandlers.isPolling, false, `We are no longer polling the elements.`); | ||
|
|
||
| destroyScrollable(scrollable1); | ||
| destroyScrollable(scrollable2); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| test('multiple handlers with same scrollable', (assert) => { | ||
| let scrollHandlers = new ScrollHandler(); | ||
| let done = assert.async(3); | ||
| let scrollable = createScrollable(); | ||
| let handler1 = () => { | ||
| assert.ok('handler1 was triggered'); | ||
| done(); | ||
| }; | ||
| let handler2 = () => { | ||
| assert.ok('handler2 was triggered'); | ||
| done(); | ||
| }; | ||
|
|
||
| // test adding the handlers | ||
| assert.strictEqual(scrollHandlers.length, 0, `We initially have no elements to watch.`); | ||
| scrollHandlers.addScrollHandler(scrollable, handler1); | ||
| scrollHandlers.addScrollHandler(scrollable, handler2); | ||
|
|
||
| assert.strictEqual(scrollHandlers.length, 1, `We have one element to watch.`); | ||
|
|
||
| let scrollable1Index = scrollHandlers.elements.indexOf(scrollable); | ||
|
|
||
| assert.strictEqual(scrollable1Index, 0, `The scrollable was added to the watched elements list.`); | ||
|
|
||
| let cache1 = scrollHandlers.handlers[0]; | ||
|
|
||
| assert.true(cache1.handlers.length === 2, `We added the handler`); | ||
|
|
||
| // test triggering that handler | ||
| assert.strictEqual(scrollable.scrollTop, 0, `The scrollable is initially unscrolled`); | ||
|
|
||
| assert.false(scrollHandlers.isPolling, 'polling is inactive'); | ||
|
Author
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. this is the only assert which is fixed by this PR |
||
|
|
||
| afterNextScrollUpdate(() => { | ||
| scrollable.scrollTop = 10; | ||
| assert.equal(scrollable.scrollTop, 10, `We updated the scrollable's scroll position`); | ||
|
|
||
| afterNextScrollUpdate(() => { | ||
| // test removing that handler | ||
| scrollHandlers.removeScrollHandler(scrollable, handler1); | ||
| let newScrollableIndex = scrollHandlers.elements.indexOf(scrollable); | ||
| assert.strictEqual(newScrollableIndex, 0, `The element remains in the watched elements list.`); | ||
| assert.strictEqual(scrollHandlers.length, 1, `We have an element to watch.`); | ||
| assert.strictEqual(cache1.handlers.length, 1, `The first handler was removed from the listener cache.`); | ||
| assert.strictEqual(scrollHandlers.handlers.indexOf(cache1), 0, `The cache still exists.`); | ||
|
|
||
| scrollHandlers.removeScrollHandler(scrollable, handler2); | ||
| newScrollableIndex = scrollHandlers.elements.indexOf(scrollable); | ||
| assert.strictEqual(newScrollableIndex, -1, `Removing the last handler removed the element from the watched elements list.`); | ||
| assert.strictEqual(scrollHandlers.length, 0, `We have no more elements to watch.`); | ||
| assert.strictEqual(cache1.handlers.length, 0, `The last handler was removed from the listener cache.`); | ||
|
|
||
| destroyScrollable(scrollable); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.