Skip to content

Commit ab21fbe

Browse files
author
Chakwen Hong
committed
ScrollDirectionResolver add processing interval and remove VERSION properties
1 parent 33784c7 commit ab21fbe

6 files changed

Lines changed: 68 additions & 32 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swgrhck/scroll-monitor",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "This is a JavaScript plugin that monitors and responds to scroll event, and supports the usage via data attributes to separate html from script.",
55
"module": "dist/bundle/scroll-monitor.esm.js",
66
"browser": "dist/bundle/scroll-monitor.umd.js",

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ window.addEventListener('scroll.up.scroll-monitor', event => {
105105
})
106106
```
107107

108+
Since `scroll` events can fire at a high rate, so `ScrollDirectionResolver` have a time interval to recognize direction, which default to `50ms`. You can get or set interval by `resolver.inteval`, or set `interval` when creating resolver by `new ScrollDirectionResolver(inteval)`.
109+
108110
## Extend ScrollMonitor
109111

110112
The `ScrollMonitor` is flexible and pluggable, this means you can extend `ScrollMonitor` easily if present components can't satisfy your needs.

src/monitor.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/**
22
* ----------------------------------------------------------------------------------
3-
* ScrollMonitor (v0.1.0): monitor.js
3+
* ScrollMonitor: monitor.js
44
* Licensed under MIT (https://github.com/swgrhck/scroll-monitor/blob/master/LICENSE)
55
* ----------------------------------------------------------------------------------
66
*/
77

88
const Monitor = (() => {
99

10-
const VERSION = '0.1.0'
1110
const EVENT_NAMESPACE = 'scroll-monitor'
1211

1312
const Selectors = {
@@ -55,10 +54,6 @@ const Monitor = (() => {
5554

5655
// Static
5756

58-
static get VERSION() {
59-
return VERSION
60-
}
61-
6257
static get NAMESPACE() {
6358
return EVENT_NAMESPACE
6459
}

src/scroll-direction-resolver.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Monitor from './monitor'
22

33
/**
44
* ----------------------------------------------------------------------------------
5-
* ScrollMonitor (v0.1.0): scroll-up.js
5+
* ScrollMonitor: scroll-up.js
66
* Licensed under MIT (https://github.com/swgrhck/scroll-monitor/blob/master/LICENSE)
77
* ----------------------------------------------------------------------------------
88
*/
@@ -13,8 +13,6 @@ import Monitor from './monitor'
1313
*/
1414
const ScrollDirectionResolver = (() => {
1515

16-
const VERSION = '0.1.0'
17-
1816
const Selectors = {
1917
SCROLL_DIRECTION_MONITOR: '[data-monitor~="scroll-direction"]'
2018
}
@@ -41,10 +39,21 @@ const ScrollDirectionResolver = (() => {
4139
DOM_CONTENT_LOADED: 'DOMContentLoaded'
4240
}
4341

42+
const DEFAULT_INTERVAL = 50
43+
4444
class ScrollDirectionResolver {
4545

46-
static get VERSION() {
47-
return VERSION
46+
constructor(interval) {
47+
this._interval = interval !== undefined ? interval : DEFAULT_INTERVAL
48+
this._ticking = false
49+
}
50+
51+
get interval() {
52+
return this._interval
53+
}
54+
55+
set interval(value) {
56+
this._interval = value
4857
}
4958

5059
get eventTypes() {
@@ -86,18 +95,25 @@ const ScrollDirectionResolver = (() => {
8695

8796
resolve(lastMetric, crtMetric) {
8897
const events = []
89-
if (crtMetric.top < lastMetric.top) {
90-
events.push(new Event(Events.SCROLL_UP))
91-
}
92-
if (crtMetric.top > lastMetric.top) {
93-
events.push(new Event(Events.SCROLL_DOWN))
94-
}
95-
if (crtMetric.left < lastMetric.left) {
96-
events.push(new Event(Events.SCROLL_LEFT))
97-
}
98-
if (crtMetric.left > lastMetric.left) {
99-
events.push(new Event(Events.SCROLL_RIGHT))
98+
99+
if (!this._ticking) {
100+
this._ticking = true
101+
setTimeout(() => this._ticking = false, this._interval)
102+
103+
if (crtMetric.top < lastMetric.top) {
104+
events.push(new Event(Events.SCROLL_UP))
105+
}
106+
if (crtMetric.top > lastMetric.top) {
107+
events.push(new Event(Events.SCROLL_DOWN))
108+
}
109+
if (crtMetric.left < lastMetric.left) {
110+
events.push(new Event(Events.SCROLL_LEFT))
111+
}
112+
if (crtMetric.left > lastMetric.left) {
113+
events.push(new Event(Events.SCROLL_RIGHT))
114+
}
100115
}
116+
101117
return events
102118
}
103119
}

test/unit/monitor.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ describe('Monitor', function () {
1717
expect(Monitor).to.be.a('function')
1818
})
1919

20-
describe('static #VERSION', function () {
21-
it('should return as a string', function () {
22-
expect(Monitor).to.have.property('VERSION').that.is.a('string')
23-
})
24-
})
25-
2620
describe('static #NAMESPACE', function () {
2721
it('should return as a string', function () {
2822
expect(Monitor).to.have.property('NAMESPACE').that.is.a('string')

test/unit/scroll-direction-resolver.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ describe('ScrollDirectionResolver', function () {
55
expect(ScrollDirectionResolver).to.be.a('function')
66
})
77

8-
describe('static #VERSION', function () {
9-
it('should return version as string', function () {
10-
expect(ScrollDirectionResolver).to.have.property('VERSION').that.is.a('string')
8+
describe('#interval', function () {
9+
it('should return default interval when undefined', function () {
10+
expect(new ScrollDirectionResolver()).to.have.property('interval', 50)
11+
})
12+
13+
it('should return defined interval', function () {
14+
expect(new ScrollDirectionResolver(0)).to.have.property('interval', 0)
15+
})
16+
17+
it('should change interval', function () {
18+
var resolver = new ScrollDirectionResolver()
19+
resolver.interval = 100
20+
expect(resolver.interval).to.equal(100)
1121
})
1222
})
1323

@@ -73,10 +83,29 @@ describe('ScrollDirectionResolver', function () {
7383
expect(events[0]).to.be.an.instanceof(Event).that.has.property('type', 'scroll.up.scroll-monitor')
7484
expect(events[1]).to.be.an.instanceof(Event).that.has.property('type', 'scroll.right.scroll-monitor')
7585

86+
resolver = new ScrollDirectionResolver()
7687
events = resolver.resolve(crtMetric, lastMetric)
7788
expect(events).to.be.an.instanceof(Array).that.has.lengthOf(2)
7889
expect(events[0]).to.be.an.instanceof(Event).that.has.property('type', 'scroll.down.scroll-monitor')
7990
expect(events[1]).to.be.an.instanceof(Event).that.has.property('type', 'scroll.left.scroll-monitor')
8091
})
92+
93+
it('should not resolve events if subsequent invocations are within time interval', function () {
94+
var resolver = new ScrollDirectionResolver(1000)
95+
var m1 = {top: 100, left: 0}, m2 = {top: 0, left: 100}
96+
expect(resolver.resolve(m1, m2)).to.have.lengthOf.above(0)
97+
expect(resolver.resolve(m1, m2)).to.have.lengthOf(0)
98+
})
99+
100+
it('should resolve events if exceeds the time interval', function (done) {
101+
var resolver = new ScrollDirectionResolver()
102+
var m1 = {top: 100, left: 0}, m2 = {top: 0, left: 100}
103+
expect(resolver.resolve(m1, m2)).to.have.lengthOf.above(0)
104+
expect(resolver.resolve(m1, m2)).to.have.lengthOf(0)
105+
setTimeout(function () {
106+
expect(resolver.resolve(m1, m2)).to.have.lengthOf.above(0)
107+
done()
108+
}, 50)
109+
})
81110
})
82111
})

0 commit comments

Comments
 (0)