added basic comments cache store model

This commit is contained in:
noumantahir 2022-01-28 23:33:48 +05:00
parent 63bdd98b90
commit c6f69c64e2
4 changed files with 52 additions and 5 deletions

View File

@ -73,7 +73,8 @@ const UpvoteContainer = (props) => {
if ( if (
lastCacheUpdate && lastCacheUpdate &&
lastCacheUpdate.postPath === postPath && lastCacheUpdate.postPath === postPath &&
content.post_fetched_at < lastCacheUpdate.updatedAt content.post_fetched_at < lastCacheUpdate.updatedAt &&
lastCacheUpdate.type === 'vote'
) { ) {
_handleCachedVote(); _handleCachedVote();
} }

View File

@ -1,6 +1,7 @@
import { import {
UPDATE_VOTE_CACHE, UPDATE_VOTE_CACHE,
PURGE_EXPIRED_CACHE PURGE_EXPIRED_CACHE,
UPDATE_COMMENT_CACHE
} from '../constants/constants'; } from '../constants/constants';
import { Vote } from '../reducers/cacheReducer'; import { Vote } from '../reducers/cacheReducer';
@ -12,6 +13,14 @@ import { Vote } from '../reducers/cacheReducer';
}, },
type: UPDATE_VOTE_CACHE type: UPDATE_VOTE_CACHE
}) })
export const updateCommentCache = (commentPath:string, comment:Comment) => ({
payload:{
commentPath,
comment
},
type: UPDATE_COMMENT_CACHE
})
export const purgeExpiredCache = () => ({ export const purgeExpiredCache = () => ({
type: PURGE_EXPIRED_CACHE type: PURGE_EXPIRED_CACHE

View File

@ -107,3 +107,4 @@ export const TEMP_BENEFICIARIES_ID = 'temp-beneficiaries';
//CACHE //CACHE
export const PURGE_EXPIRED_CACHE = 'PURGE_EXPIRED_CACHE'; export const PURGE_EXPIRED_CACHE = 'PURGE_EXPIRED_CACHE';
export const UPDATE_VOTE_CACHE = 'UPDATE_VOTE_CACHE'; export const UPDATE_VOTE_CACHE = 'UPDATE_VOTE_CACHE';
export const UPDATE_COMMENT_CACHE = 'UPDATE_COMMENT_CACHE';

View File

@ -1,4 +1,4 @@
import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE } from "../constants/constants"; import { PURGE_EXPIRED_CACHE, UPDATE_VOTE_CACHE, UPDATE_COMMENT_CACHE } from "../constants/constants";
export interface Vote { export interface Vote {
amount:number; amount:number;
@ -8,16 +8,29 @@ export interface Vote {
expiresAt:number; expiresAt:number;
} }
export interface Comment {
author:string,
permlink:string,
parentAuthor:string,
parentPermlink:string,
content:string,
createdAt:number,
expiresAt:number,
}
interface State { interface State {
votes:Map<string, Vote> votes:Map<string, Vote>
comments:Map<string, Comment>
lastUpdate:{ lastUpdate:{
postPath:string, postPath:string,
updatedAt:number, updatedAt:number,
type:'vote'|'comment',
} }
} }
const initialState:State = { const initialState:State = {
votes:new Map(), votes:new Map(),
comments:new Map(),
lastUpdate:null, lastUpdate:null,
}; };
@ -33,19 +46,42 @@ const initialState:State = {
...state, //spread operator in requried here, otherwise persist do not register change ...state, //spread operator in requried here, otherwise persist do not register change
lastUpdate:{ lastUpdate:{
postPath:payload.postPath, postPath:payload.postPath,
updatedAt: new Date().getTime() updatedAt: new Date().getTime(),
type:'vote',
}
};
case UPDATE_COMMENT_CACHE:
if(!state.comments){
state.comments = new Map<string, Comment>();
}
state.comments.set(payload.commentPath, payload.comment);
return {
...state, //spread operator in requried here, otherwise persist do not register change
lastUpdate:{
postPath:payload.commentPath,
updatedAt: new Date().getTime(),
type:'comment'
} }
}; };
case PURGE_EXPIRED_CACHE: case PURGE_EXPIRED_CACHE:
const currentTime = new Date().getTime(); const currentTime = new Date().getTime();
if(state.votes && state.votes.entries){ if(state.votes && state.votes.size){
Array.from(state.votes).forEach((entry)=>{ Array.from(state.votes).forEach((entry)=>{
if(entry[1].expiresAt < currentTime){ if(entry[1].expiresAt < currentTime){
state.votes.delete(entry[0]); state.votes.delete(entry[0]);
} }
}) })
} }
if(state.comments && state.comments.size){
Array.from(state.comments).forEach((entry)=>{
if(entry[1].expiresAt < currentTime){
state.comments.delete(entry[0]);
}
})
}
return { return {
...state ...state