-
Notifications
You must be signed in to change notification settings - Fork 6
/
split.js
66 lines (53 loc) · 1.63 KB
/
split.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
66
import firebase from "firebase/app"; // Firebase core
import "firebase/firestore";
import "firebase/performance";
import { firebaseConfigSplit as firebaseConfig } from "./shared/firebase-config";
import { renderPage, logPerformance } from "./shared/helpers";
// STEPS
// 1) Import Firebase components
// 2) Subscribe to Firestore
// 3) Render data
firebase.initializeApp(firebaseConfig);
firebase.performance(); // Use Firebase Performance - 1 line
let firstLoad = false;
// Subscribe to stock data from Firestore and render page on updates.
subscribeToFirestore((stockData) => renderPage({
title: "Importing Firestore and Perf Only",
tableData: stockData
}));
/**
* FUNCTIONS
*/
// Begin `onSnapshot` subscription to "current" collection in Firestore.
function subscribeToFirestore(renderFn) {
firebase
.firestore()
.collection(`current`)
.onSnapshot(snap => {
if (!firstLoad) {
// Measure time between navigation start and now (first data loaded)
performance && performance.measure('initialDataLoadTime');
// Log to console for internal development
logPerformance();
firstLoad = true;
}
const stocks = formatSDKStocks(snap);
renderFn(stocks);
});
}
// Format stock data in Firestore format (returned from `onSnapshot()`)
function formatSDKStocks(snap) {
const stocks = [];
snap.forEach(docSnap => {
if (!docSnap.data()) return;
const symbol = docSnap.id;
const value = docSnap.data().closeValue;
stocks.push({
symbol,
value,
delta: docSnap.data().delta,
timestamp: docSnap.data().timestamp
});
});
return stocks;
}