Skip to content

Commit

Permalink
Merge pull request #3 from pdroll/update-covjs
Browse files Browse the repository at this point in the history
Update covjs dependency
  • Loading branch information
pdroll authored Feb 9, 2021
2 parents 925529b + 69af0d3 commit 50ae21f
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 72 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ winEvents.off('scroll.down', firstListener);
// the second listener will continue to work.
```

### `winEvents.off(eventName, functionReference)`

You can also unsubscribe a listener from an event by passing in the same function
that passed into the call to `on` or `once`

```javascript
var myCallback = function(scrollData) {
console.log('We are scrolling down the page');
}

var mySecondCallback = function(scrollData) {
console.log('A second listener for scrolling down');
}

winEvents.on('scroll.down', myCallback);
winEvents.on('scroll.down', mySecondCallback);

// Unsubscribe just the first Listener
winEvents.off('scroll.down', myCallback);

// myCallback no longer fire
// when the window is scrolled down, but
// mySecondCallback will continue to work.
```

### `winEvents.getState()`

Immediately get current size, scroll position, and visibility of the window. Returns an object with the following properties:
Expand Down
6 changes: 5 additions & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ global.window.resizeTo = (width, height) => {
global.window.dispatchEvent(resizeEvent)
}


global.beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
Expand All @@ -39,3 +38,8 @@ global.beforeEach(() => {
writable: true, value: 'loading'
})
})


global.afterEach(() => {
jest.runAllTimers();
})
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "windowevents",
"version": "1.1.1",
"version": "1.2.0",
"description": "Simple wrapper around common window events",
"main": "windowevents.min.js",
"license": "MIT",
Expand All @@ -13,8 +13,8 @@
"lint": "standard src test",
"start": "rollup -c -w",
"build": "rollup -c",
"test": "jest test --coverage --verbose",
"test:watch": "jest test --watch"
"test": "jest --coverage --verbose",
"test:watch": "jest --watch"
},
"browserslist": [
"> 0.5%",
Expand All @@ -23,7 +23,7 @@
"IE 10"
],
"dependencies": {
"covjs": "^1.2.0",
"covjs": "^2.0.1",
"throttle-debounce": "^1.0.1"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Pete Droll <[email protected]>
* @license MIT
*/
import publisher from 'covjs'
import { Covenant } from 'covjs'
import debounce from 'throttle-debounce/debounce'
import throttle from 'throttle-debounce/throttle'
import ScrollEvents from './scroll'
Expand All @@ -19,6 +19,8 @@ class WindowEvents {
}

this.options = opts ? { ...defaultOptions, ...opts } : defaultOptions

const publisher = new Covenant()
this.on = publisher.on
this.once = publisher.once
this.off = publisher.off
Expand Down
4 changes: 3 additions & 1 deletion test/resize-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ describe('Resize events', () => {

winEvents.on('resize.scrollHeightChange', callback)

document.body.scrollHeight = 2500
window.resizeTo(720, 480)
window.resizeTo(320, 480)
document.body.scrollHeight = 2500

jest.runAllTimers()
expect(callback).toHaveBeenCalledWith({
orientation: 'portrait',
Expand Down
4 changes: 2 additions & 2 deletions test/scroll-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ describe('Scroll events', () => {
window.scrollTo(0, 200)
window.scrollTo(0, 311)

jest.advanceTimersByTime(99)
jest.advanceTimersByTime(90)
expect(callback).toHaveBeenCalledTimes(1)

jest.advanceTimersByTime(2)
jest.advanceTimersByTime(12)
expect(callback).toHaveBeenCalledTimes(2)
})
})
Expand Down
29 changes: 27 additions & 2 deletions test/windowevents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('WindowEvents', () => {
})

describe('#off', () => {
it('removes a previously added event listener', () => {
it('removes a previously added event listener by token', () => {
const winEvents = new WindowEvents()
const callback = jest.fn()
const callback2 = jest.fn()
Expand All @@ -58,8 +58,30 @@ describe('WindowEvents', () => {
expect(callback2).toHaveBeenCalledWith({ visible: true })
})

it('removes a previously added event listener by function reference', () => {
const winEvents = new WindowEvents()
const callback = jest.fn()
const callback2 = jest.fn()

winEvents.on('visibilityChange', callback)
winEvents.on('visibilityChange', callback2)

document.hidden = true
window.dispatchEvent(new Event('visibilitychange'))

expect(callback).toHaveBeenCalledWith({ visible: false })

winEvents.off('visibilityChange', callback2)

document.hidden = false
window.dispatchEvent(new Event('visibilitychange'))

expect(callback).toHaveBeenCalledWith({ visible: true })
expect(callback2).not.toHaveBeenCalledWith({ visible: true })
})

describe('when a listener is not specified', () => {
it('removes all previously added event listener', () => {
it('removes all previously added event listeners', () => {
const winEvents = new WindowEvents()
const callback = jest.fn()
const callback2 = jest.fn()
Expand All @@ -79,6 +101,9 @@ describe('WindowEvents', () => {

expect(callback).not.toHaveBeenCalledWith({ visible: true })
expect(callback2).not.toHaveBeenCalledWith({ visible: true })

expect(callback).toBeCalledTimes(1)
expect(callback2).toBeCalledTimes(1)
})
})
})
Expand Down
125 changes: 69 additions & 56 deletions windowevents.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion windowevents.min.js

Large diffs are not rendered by default.

0 comments on commit 50ae21f

Please sign in to comment.