Skip to content

Commit cada937

Browse files
authored
fix: return value from pager
1 parent 671293e commit cada937

File tree

4 files changed

+74
-22
lines changed

4 files changed

+74
-22
lines changed

src/browser/browser.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ const createAdapted = adapter => ({
2020

2121
return content
2222
},
23-
page: async (data, delegate) => {
23+
page: delegate => async data => {
24+
let result = null
25+
2426
await adapter(async adapted => {
25-
await pager(adapted.context(), data)(delegate)
27+
result = await pager(adapted.context())(delegate)(data)
2628
})
29+
30+
return result
2731
},
2832
})
2933

src/browser/browser.test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ describe('browser', () => {
1616
})
1717

1818
it('should throw when adapter is not object', () => {
19-
expect(() => browser()(false)).toThrow('browser adapter must be a function.')
19+
expect(() => browser()(false)).toThrow(
20+
'browser adapter must be a function.'
21+
)
2022
})
2123

2224
it('should return content from get', async () => {
@@ -45,11 +47,27 @@ describe('browser', () => {
4547

4648
let result = null
4749
await use(async ({ page }) => {
48-
await page('http://www.google.com', (context, data) => {
49-
result = { context, data }
50-
})
50+
await page(accumulated => {
51+
result = accumulated
52+
return {
53+
complete: true,
54+
}
55+
})()
5156
})
5257

5358
expect(result.context).toBeInstanceOf(Page)
5459
})
60+
61+
it('should return expected value from page', async () => {
62+
const { use } = browser()(puppeteer)
63+
64+
let result = null
65+
await use(async ({ page }) => {
66+
result = await page(accumulated => {
67+
return Object.assign({}, accumulated, { complete: true })
68+
})('foo')
69+
})
70+
71+
expect(result).toEqual('foo')
72+
})
5573
})

src/browser/pager.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import isNil from 'inspected/schema/is-nil'
22

3-
const pager = (context, data) => async delegate => {
3+
const pager = context => delegate => async value => {
44
if (isNil(delegate)) {
55
throw new Error('page delegate must be specified.')
66
}
77

8-
let currentContext = context
9-
let currentData = data
8+
let accumulated = { complete: false, context, value }
9+
while (accumulated.complete === false) {
10+
accumulated = await delegate(accumulated)
1011

11-
let result = { complete: false }
12-
while (result.complete === false) {
13-
result = await delegate(currentContext, currentData)
14-
15-
if (!result) {
12+
if (!accumulated) {
1613
break
1714
}
15+
}
1816

19-
currentContext = result.context
20-
currentData = result.data
17+
if (!accumulated) {
18+
return null
2119
}
20+
21+
return accumulated.value ? accumulated.value : null
2222
}
2323

2424
export default pager

src/browser/pager.test.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,54 @@ import pager from './pager'
22

33
describe('pager', () => {
44
it('should throw if delegate is undefined', async () => {
5-
await expect(pager()()).rejects.toHaveProperty(
5+
await expect(pager()()()).rejects.toHaveProperty(
66
'message',
77
'page delegate must be specified.'
88
)
99
})
1010

1111
it('should throw if delegate is null', async () => {
12-
await expect(pager()(null)).rejects.toHaveProperty(
12+
await expect(pager()()(null)).rejects.toHaveProperty(
1313
'message',
1414
'page delegate must be specified.'
1515
)
1616
})
1717

1818
it('should pass context and data to delegate', async () => {
1919
let result = null
20-
await pager({ foo: 'bar' }, 'baz')(
21-
async (context, data) => (result = { context, data })
22-
)
23-
expect(result).toEqual({ context: { foo: 'bar' }, data: 'baz' })
20+
await pager({ foo: 'bar' })(async accumulated => {
21+
result = accumulated
22+
return { complete: true }
23+
})('baz')
24+
25+
expect(result).toEqual({
26+
context: { foo: 'bar' },
27+
value: 'baz',
28+
complete: false,
29+
})
30+
})
31+
32+
it('should return assigned value', async () => {
33+
const result = await pager()(async () => {
34+
return {
35+
complete: true,
36+
value: 'foo',
37+
}
38+
})()
39+
40+
expect(result).toEqual('foo')
41+
})
42+
43+
it('should accumulate context and value', async () => {
44+
const result = await pager(10)(async accumulated => {
45+
const complete = accumulated.value > 50
46+
return {
47+
complete,
48+
context: accumulated.context - 1,
49+
value: accumulated.value + accumulated.context,
50+
}
51+
})(1)
52+
53+
expect(result).toEqual(55)
2454
})
2555
})

0 commit comments

Comments
 (0)