-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.js
36 lines (30 loc) · 1016 Bytes
/
cache.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
import { app, db } from './firebase'
import { collection, getDocs, doc, setDoc } from 'firebase/firestore'
export class Cache {
static instance = null;
static eventTimer = null;
static events = []
static getInstance() {
if(this.instance == null) {
this.instance = new Cache();
}
return this.instance;
}
async retrieveEvents(refreshEvents) {
this.events = []
const querySnapshot = await getDocs(collection(db, 'events'));
querySnapshot.forEach((doc) => {
this.events.push(doc.data());
});
refreshEvents(this.events);
return this.events;
}
getAllEvents(refreshEvents) {
if (this.eventTimer == null || Math.abs((new Date().getTime() - this.eventTimer.getTime()) / 1000) > 30) {
this.retrieveEvents(refreshEvents);
this.eventTimer = new Date();
} else {
console.log("not enough time has passed to refresh");
}
}
}