-
Notifications
You must be signed in to change notification settings - Fork 0
/
places-to-coords.js
66 lines (55 loc) · 1.61 KB
/
places-to-coords.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// @ts-check
require('dotenv').config();
// I'm using a DIY composable event emitter. Kind of like an Observable.
const { Emitter, throttleWith } = require('./emitter');
// Only allow this many Google Maps API requests at a time.
const CONCURRENT_REQUESTS = 1;
const GOOGLE_MAPS_API_KEY = process.env.GOOGLE_MAPS_API_KEY;
const googleMapsClient = require('@google/maps')
.createClient({
key: GOOGLE_MAPS_API_KEY,
timeout: 1e3
});
const geocode = (address, callback) => {
googleMapsClient.geocode({
address
}, callback);
}
const geocodeResponse$ = Emitter();
const geocodeRequest$ = Emitter();
geocodeRequest$
.compose(throttleWith(geocodeResponse$, CONCURRENT_REQUESTS))
.addCallback((err, address) => {
geocode(address, (err, data) => {
const out = null;
try {
const location = data.json.results[0].geometry.location;
const out = {
address,
location
};
geocodeResponse$.callCallbacks(err, out);
} catch (err) {
geocodeResponse$.callCallbacks(err, null);
}
})
});
const fromArray = (places, callback = (err, data) => {}) => {
console.log('INPUT\n', require('util').inspect(places, { colors: true }))
const out = [];
return new Promise((resolve, reject) => {
geocodeResponse$
.addCallback((err, data) => {
callback(err, data);
if (err) return reject(err);
out.push(data);
if (out.length === places.length) {
resolve(out);
}
});
places.forEach(place => {
geocodeRequest$.callCallbacks(null, place)
});
})
};
module.exports = fromArray;