Skip to content
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

Add tests and small syntactic improvements #2

Open
wants to merge 8 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*]
indent_size = 2
indent_style = space
end_of_line = lf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
.vscode/
*~
.#*
coverage
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ req('foo').then(…)
req('foo').then(…)
```

## SEE ALSO
Copy link
Owner

Choose a reason for hiding this comment

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

I like the tests, but as a rule I use tap for my tests, not jest. Ordinarily I'm not that picky, but as I'll have to maintain this going forward, I would like to see them in tap format before landing.

## Tests

* [inflight](https://npmjs.com/package/inflight) - For the callback based function on which this is based.
Run `npm test`

## STILL NEEDS
## SEE ALSO

Tests!
* [inflight](https://npmjs.com/package/inflight) - For the callback based function on which this is based.
36 changes: 17 additions & 19 deletions inflight.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
'use strict'
module.exports = inflight

let Bluebird
Copy link
Owner

@iarna iarna Aug 19, 2019

Choose a reason for hiding this comment

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

Why remove this? Why not let people use bluebird if they have bluebird? Bluebird is more than a ponyfill, it also adds additional really handy APIs. Also it's often faster than native Promises.

try {
Bluebird = require('bluebird')
} catch (_) {
Bluebird = Promise
const active = {}

function cleanup() {
delete active[unique]
}

function _inflight (unique, doFly) {
if (!active[unique]) {
active[unique] = (new Promise(function (resolve) {
return resolve(doFly())
}))
active[unique].then(cleanup, cleanup)
function cleanup() { delete active[unique] }
Copy link
Owner

Choose a reason for hiding this comment

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

The other copy of cleanup() should also be removed, but really I'd prefer that you just remove this commit and 1eb2b41

}
return active[unique]
}

const active = {}
inflight.active = active
function inflight (unique, doFly) {
return Bluebird.all([unique, doFly]).then(function (args) {
return Promise.all([unique, doFly]).then(function (args) {
const unique = args[0]
const doFly = args[1]
if (Array.isArray(unique)) {
return Bluebird.all(unique).then(function (uniqueArr) {
return Promise.all(unique).then(function (uniqueArr) {
return _inflight(uniqueArr.join(''), doFly)
})
} else {
return _inflight(unique, doFly)
}
})

function _inflight (unique, doFly) {
if (!active[unique]) {
active[unique] = (new Bluebird(function (resolve) {
return resolve(doFly())
}))
active[unique].then(cleanup, cleanup)
function cleanup() { delete active[unique] }
}
return active[unique]
}
}
79 changes: 79 additions & 0 deletions inflight.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const inflight = require('./inflight')

describe('inflight()', () => {
it('calls a single doFly()', async () => {
const doFly = jest.fn()
await inflight('unique-key', doFly)
expect(doFly).toHaveBeenCalled()
})

it('calls multiple doFly()', async () => {
const req = jest.fn((key) => {
return inflight(key, () => {
return new Promise(resolve => {
setTimeout(resolve(key), 50)
})
})
})

const result = await Promise.all([
req('hello'),
req('hello'),
])
expect(result).toEqual(['hello', 'hello'])
})

it('calls doFly() only once if it is in progress', async () => {
let calls = 0
const req = jest.fn((key) => {
return inflight(key, () => {
return new Promise(resolve => {
calls++
setTimeout(resolve(42), 10000)
})
})
})

const result = await Promise.all([
req('hello'),
req('hello'),
])
expect(result).toEqual([42, 42])
expect(calls).toEqual(1)
})

it('calls doFly() again after it is settled', async () => {
let calls = 0
const req = jest.fn((key) => {
return inflight(key, () => {
return new Promise(resolve => {
calls++
setTimeout(resolve(calls + 100), 10000)
})
})
})

const firstResult = await req('hello')
expect(firstResult).toEqual(101)
const secondResult = await req('hello')
expect(secondResult).toEqual(102)
})

it('can handle `unique` that is a promise', async () => {
const doFly = jest.fn()
await inflight(Promise.resolve('unique-key'), doFly)
expect(doFly).toHaveBeenCalled()
})

it('can handle `unique` that is an array', async () => {
const doFly = jest.fn()
await inflight(Promise.resolve(['unique', 'key']), doFly)
expect(doFly).toHaveBeenCalled()
})

it('can handle `unique` that is an array of promises', async () => {
const doFly = jest.fn()
await inflight([Promise.resolve('unique'), Promise.resolve('key')], doFly)
expect(doFly).toHaveBeenCalled()
})
})
Loading