mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 10:34:15 +00:00
Refactored Data Services
This commit is contained in:
58
src/app/core/cache/object-cache.reducer.ts
vendored
Normal file
58
src/app/core/cache/object-cache.reducer.ts
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
import { ObjectCacheAction, ObjectCacheActionTypes, AddToObjectCacheAction, RemoveFromObjectCacheAction } from "./object-cache.actions";
|
||||
import { hasValue } from "../../shared/empty.util";
|
||||
|
||||
export interface CacheableObject {
|
||||
uuid: string;
|
||||
}
|
||||
|
||||
export interface ObjectCacheEntry {
|
||||
data: CacheableObject;
|
||||
timeAdded: number;
|
||||
msToLive: number;
|
||||
}
|
||||
|
||||
export interface ObjectCacheState {
|
||||
[uuid: string]: ObjectCacheEntry
|
||||
}
|
||||
|
||||
// Object.create(null) ensures the object has no default js properties (e.g. `__proto__`)
|
||||
const initialState: ObjectCacheState = Object.create(null);
|
||||
|
||||
export const objectCacheReducer = (state = initialState, action: ObjectCacheAction): ObjectCacheState => {
|
||||
switch (action.type) {
|
||||
|
||||
case ObjectCacheActionTypes.ADD: {
|
||||
return addToObjectCache(state, <AddToObjectCacheAction>action);
|
||||
}
|
||||
|
||||
case ObjectCacheActionTypes.REMOVE: {
|
||||
return removeFromObjectCache(state, <RemoveFromObjectCacheAction>action)
|
||||
}
|
||||
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function addToObjectCache(state: ObjectCacheState, action: AddToObjectCacheAction): ObjectCacheState {
|
||||
return Object.assign({}, state, {
|
||||
[action.payload.objectToCache.uuid]: {
|
||||
data: action.payload.objectToCache,
|
||||
timeAdded: new Date().getTime(),
|
||||
msToLive: action.payload.msToLive
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function removeFromObjectCache(state: ObjectCacheState, action: RemoveFromObjectCacheAction): ObjectCacheState {
|
||||
if (hasValue(state[action.payload])) {
|
||||
let newObjectCache = Object.assign({}, state);
|
||||
delete newObjectCache[action.payload];
|
||||
|
||||
return newObjectCache;
|
||||
}
|
||||
else {
|
||||
return state;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user