Skip to content

Commit 41a3e02

Browse files
authored
Merge pull request #25 from opentripplanner/dev
New release
2 parents 7d03c5b + 7049707 commit 41a3e02

File tree

9 files changed

+134
-13
lines changed

9 files changed

+134
-13
lines changed

Diff for: __tests__/actions/__snapshots__/form.js.snap

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`actions > api > form should debounce numerous requests to plan a trip 1`] = `
4+
Array [
5+
Array [
6+
Object {
7+
"type": "FORM_CHANGED",
8+
},
9+
],
10+
Array [
11+
Object {
12+
"type": "FORM_CHANGED",
13+
},
14+
],
15+
Array [
16+
Object {
17+
"type": "FORM_CHANGED",
18+
},
19+
],
20+
Array [
21+
Object {
22+
"type": "FORM_CHANGED",
23+
},
24+
],
25+
Array [
26+
Object {
27+
"type": "FORM_CHANGED",
28+
},
29+
],
30+
Array [
31+
Object {
32+
"type": "FORM_CHANGED",
33+
},
34+
],
35+
Array [
36+
Object {
37+
"type": "FORM_CHANGED",
38+
},
39+
],
40+
Array [
41+
Object {
42+
"type": "FORM_CHANGED",
43+
},
44+
],
45+
Array [
46+
Object {
47+
"type": "FORM_CHANGED",
48+
},
49+
],
50+
Array [
51+
Object {
52+
"type": "FORM_CHANGED",
53+
},
54+
],
55+
Array [
56+
[Function],
57+
],
58+
]
59+
`;

Diff for: __tests__/actions/api.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
import nock from 'nock'
44

5-
import {planTrip} from '../../lib/actions/api'
5+
import {timeoutPromise} from '../test-utils'
66

7-
function timeoutPromise (ms) {
8-
return new Promise((resolve, reject) => {
9-
setTimeout(resolve, ms)
10-
})
11-
}
7+
import {planTrip} from '../../lib/actions/api'
128

139
describe('actions > api', () => {
1410
describe('> planTrip', () => {

Diff for: __tests__/actions/form.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* globals describe, expect, it, jest */
2+
3+
import {timeoutPromise} from '../test-utils'
4+
5+
import {formChanged} from '../../lib/actions/form'
6+
7+
describe('actions > api > form', () => {
8+
it('should debounce numerous requests to plan a trip', async () => {
9+
const defaultState = {
10+
otp: {
11+
config: {
12+
api: {
13+
host: 'http://mock-host.com',
14+
path: '/api',
15+
port: 80
16+
},
17+
autoPlan: true,
18+
debouncePlanTimeMs: 500
19+
},
20+
currentQuery: {
21+
from: { lat: 12, lon: 34 },
22+
to: { lat: 34, lon: 12 }
23+
},
24+
searches: []
25+
}
26+
}
27+
28+
const formChangedAction = formChanged()
29+
30+
const mockDispatch = jest.fn()
31+
for (var i = 0; i < 10; i++) {
32+
formChangedAction(mockDispatch, () => defaultState)
33+
}
34+
35+
// wait for request to complete
36+
await timeoutPromise(1000)
37+
38+
expect(mockDispatch.mock.calls).toMatchSnapshot()
39+
})
40+
})

Diff for: __tests__/test-utils/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function timeoutPromise (ms) {
2+
return new Promise((resolve, reject) => {
3+
setTimeout(resolve, ms)
4+
})
5+
}

Diff for: lib/actions/api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export function planTrip (customOtpQueryBuilder) {
2828
const queryBuilderFn = customOtpQueryBuilder || otpState.config.customOtpQueryBuilder || constructPlanQuery
2929
const url = queryBuilderFn(otpState.config.api, otpState.currentQuery)
3030
// setURLSearch(url)
31-
let response, json
31+
let json
3232
try {
33-
response = await fetch(url)
33+
const response = await fetch(url)
3434
if (response.status >= 400) {
3535
const error = new Error('Received error from server')
3636
error.response = response

Diff for: lib/actions/form.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import debounce from 'lodash.debounce'
12
import { createAction } from 'redux-actions'
23

34
import { planTrip } from './api'
@@ -37,12 +38,25 @@ export function setTime (payload) {
3738
}
3839
}
3940

41+
let debouncedPlanTrip // store as variable here, so it can be reused.
42+
let lastDebouncePlanTimeMs
43+
4044
export function formChanged () {
4145
return function (dispatch, getState) {
4246
dispatch(changingForm())
43-
// TODO: make auto-plan trip a configurable
44-
if (queryIsValid(getState().otp)) {
45-
dispatch(planTrip())
47+
const otpState = getState().otp
48+
const {autoPlan, debouncePlanTimeMs} = otpState.config
49+
50+
// check if a trip plan should be made
51+
if (autoPlan && queryIsValid(otpState)) {
52+
// trip plan should be made
53+
54+
// check if debouncing function needs to be (re)created
55+
if (!debouncedPlanTrip || lastDebouncePlanTimeMs !== debouncePlanTimeMs) {
56+
debouncedPlanTrip = debounce(() => dispatch(planTrip()), debouncePlanTimeMs)
57+
lastDebouncePlanTimeMs = debouncePlanTimeMs
58+
}
59+
debouncedPlanTrip()
4660
}
4761
}
4862
}

Diff for: lib/reducers/create-otp-reducer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ function createOtpReducer (config, initialQuery) {
2222
defaultQuery.mode = defaultQuery.mode || config.modes[0] || 'TRANSIT,WALK'
2323
// populate query by merging any provided query params w/ the default params
2424
const currentQuery = Object.assign(defaultQuery, initialQuery)
25-
console.log(currentQuery)
2625
const initialState = {
27-
config,
26+
config: Object.assign({
27+
autoPlan: true,
28+
debouncePlanTimeMs: 0
29+
}, config),
2830
currentQuery,
2931
searches: [],
3032
activeSearch: 0

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"immutability-helper": "^2.1.1",
3737
"isomorphic-fetch": "^2.2.1",
3838
"leaflet": "^1.0.3",
39+
"lodash.debounce": "^4.0.8",
3940
"moment": "^2.17.1",
4041
"object-path": "^0.11.3",
4142
"qs": "^6.3.0",

Diff for: yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -4306,6 +4306,10 @@ lodash.assign@^4.0.3, lodash.assign@^4.0.6:
43064306
version "4.2.0"
43074307
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"
43084308

4309+
lodash.debounce@^4.0.8:
4310+
version "4.0.8"
4311+
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
4312+
43094313
lodash.isarguments@^3.0.0:
43104314
version "3.1.0"
43114315
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"

0 commit comments

Comments
 (0)