Browse Source

Score caching

pull/8/head
Tim Glasgow 2 years ago
parent
commit
bec4b55cfd
  1. 4
      squarenotsquare/src/realm/DbAPI.js
  2. 32
      squarenotsquare/src/realm/repos/ScoreRepo.js

4
squarenotsquare/src/realm/DbAPI.js

@ -31,7 +31,7 @@ class DatabaseAPI {
}
getHighScores() {
return this.scoreRepo.getHighScores();
return this.scoreRepo.getCachedScores();
}
async initDB() {
@ -39,6 +39,8 @@ class DatabaseAPI {
this.systemRepo = repos.systemRepo;
this.scoreRepo = repos.scoreRepo;
this.userRepo = repos.userRepo;
this.scoreRepo.loadCache();
}
}

32
squarenotsquare/src/realm/repos/ScoreRepo.js

@ -7,11 +7,31 @@ export default class ScoreRepo {
constructor(db) {
this.realmDB = db;
this.scoreCache = [];
}
updateScoreCache = (newScore) => {
this.scoreCache.push(newScore);
this.scoreCache.sort((a, b) => {
a.value < b.value
});
if (this.scoreCache.length > 99) {
this.scoreCache = this.scoreCache.slice(99, this.scoreCache.length - 1);
}
}
createScore = (user, score) => {
let scoreID = uuidv4();
let newScore = {
id: scoreID,
user: user,
value: score
};
this.updateScoreCache(newScore);
this.realmDB.write(() => {
this.realmDB.create(
ScoreEntity.name,
@ -23,7 +43,7 @@ export default class ScoreRepo {
true,
);
});
};
}
getHighScores = () => {
let score = this.realmDB.objects(ScoreEntity.name).sorted('value', true);
@ -36,5 +56,13 @@ export default class ScoreRepo {
highScores.push({user: row.user, value: row.value});
});
return highScores;
};
}
loadCache = () => {
this.scoreCache = this.getHighScores();
}
getCachedScores = () => {
return this.scoreCache;
}
}
Loading…
Cancel
Save