summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2018-10-11 02:41:21 -0700
committerNick Van Doorn <vandoorn.nick@gmail.com>2018-10-11 02:41:21 -0700
commite41b307ce23ac2674447a3c2b4e37a0ff3194617 (patch)
treeb66fa7d306dd2b98b1a297378e35c469cc40da16
parent5eda7a0d40116170a3dafc6e37b770ca70050106 (diff)
Improve comments
-rw-r--r--projects/3-realtime-stocks/src/stock-data.js26
1 files changed, 21 insertions, 5 deletions
diff --git a/projects/3-realtime-stocks/src/stock-data.js b/projects/3-realtime-stocks/src/stock-data.js
index c70e2b4..8c97604 100644
--- a/projects/3-realtime-stocks/src/stock-data.js
+++ b/projects/3-realtime-stocks/src/stock-data.js
@@ -1,23 +1,39 @@
export const generateValue = () => {
+ // randomly generate either 1 or -1
const plusOrMinus = Math.round(Math.random()) ? 1 : -1
+ // start at 1 and add [-0.5, 0.5]
return 1 + plusOrMinus * Math.random() * 0.5
}
export const generateStartingPoints = (n, pollPeriod) => {
+ // For some reason JS doesn't have a proper range
+ // function, so we have to make a new Array full of
+ // "empty items" (wtf js?). This means we have to
+ // fill the array before we can use functional operators/loops
+ // on it. JS is weird.
return new Array(n).fill(1).map((k, i) => {
return {
value: generateValue(),
- datetime: Date.now() - (n - i) * 1000
+ // first entries should be oldest,
+ // so take (n - i) as the scaling
+ // factor on pollPeriod
+ datetime: Date.now() - (n - i) * pollPeriod
}
})
}
const N_STARTING_POINTS = 50
+// Note that the current implementation of this class
+// could be static or not a class at all.
+// Generally, data providers receive some type of dependency injection
+// through the constructor, so I left it like this.
export class StockDataProvider {
- constructor() {}
+ constructor(secretApiKey) {
+ // Note used, for dependency injection demo only
+ this.secretApiKey = secretApiKey
+ }
subscribeToStockData(pollFrequency, callback) {
- // generate 100 random points
- // 15 minutes apart
- let points = generateStartingPoints(N_STARTING_POINTS)
+ // generate 50 random points
+ let points = generateStartingPoints(N_STARTING_POINTS, pollFrequency)
const pushCallback = () => {
points.push({ value: generateValue(), datetime: Date.now() })
points.shift()