patching changes

This commit is contained in:
lotte
2018-09-11 16:05:21 +02:00
parent 14fd58dd21
commit acb5f5197d
6 changed files with 102 additions and 12 deletions

View File

@@ -1,10 +1,11 @@
import {
ObjectCacheAction, ObjectCacheActionTypes, AddToObjectCacheAction,
RemoveFromObjectCacheAction, ResetObjectCacheTimestampsAction
RemoveFromObjectCacheAction, ResetObjectCacheTimestampsAction, PatchObjectCacheAction
} from './object-cache.actions';
import { hasValue } from '../../shared/empty.util';
import { hasValue, isNotEmpty } from '../../shared/empty.util';
import { CacheEntry } from './cache-entry';
import { ResourceType } from '../shared/resource-type';
import { Operation } from 'fast-json-patch';
export enum DirtyType {
Created = 'Created',
@@ -36,6 +37,7 @@ export class ObjectCacheEntry implements CacheEntry {
timeAdded: number;
msToLive: number;
requestHref: string;
operations: Operation[];
}
/**
@@ -76,6 +78,9 @@ export function objectCacheReducer(state = initialState, action: ObjectCacheActi
return resetObjectCacheTimestamps(state, action as ResetObjectCacheTimestampsAction)
}
case ObjectCacheActionTypes.PATCH: {
return patchObjectCache(state, action as PatchObjectCacheAction);
}
default: {
return state;
}
@@ -98,7 +103,8 @@ function addToObjectCache(state: ObjectCacheState, action: AddToObjectCacheActio
data: action.payload.objectToCache,
timeAdded: action.payload.timeAdded,
msToLive: action.payload.msToLive,
requestHref: action.payload.requestHref
requestHref: action.payload.requestHref,
operations: []
}
});
}
@@ -143,3 +149,23 @@ function resetObjectCacheTimestamps(state: ObjectCacheState, action: ResetObject
});
return newState;
}
/**
* Add the list of patch operations to a cached object
*
* @param state
* the current state
* @param action
* a PatchObjectCacheAction
* @return ObjectCacheState
* the new state, with the new operations added to the state of the specified ObjectCacheEntry
*/
function patchObjectCache(state: ObjectCacheState, action: PatchObjectCacheAction): ObjectCacheState {
const uuid = action.payload.uuid;
const operations = action.payload.operations;
const newState = Object.assign({}, state);
if (hasValue(newState[uuid])) {
newState[uuid].operations = state[uuid].operations.concat(operations);
}
return newState;
}