implementation of server synchronization

This commit is contained in:
lotte
2018-09-14 09:26:30 +02:00
parent acb5f5197d
commit 17ad62c172
11 changed files with 378 additions and 48 deletions

View File

@@ -1,11 +1,15 @@
import {
ObjectCacheAction, ObjectCacheActionTypes, AddToObjectCacheAction,
RemoveFromObjectCacheAction, ResetObjectCacheTimestampsAction, PatchObjectCacheAction
ObjectCacheAction,
ObjectCacheActionTypes,
AddToObjectCacheAction,
RemoveFromObjectCacheAction,
ResetObjectCacheTimestampsAction,
AddPatchObjectCacheAction, ApplyPatchObjectCacheAction
} from './object-cache.actions';
import { hasValue, isNotEmpty } from '../../shared/empty.util';
import { CacheEntry } from './cache-entry';
import { ResourceType } from '../shared/resource-type';
import { Operation } from 'fast-json-patch';
import { applyPatch, Operation } from 'fast-json-patch';
export enum DirtyType {
Created = 'Created',
@@ -13,7 +17,11 @@ export enum DirtyType {
Deleted = 'Deleted'
}
/**
export interface Patch {
uuid?: string;
operations: Operation[];
}
/**conca
* An interface to represent objects that can be cached
*
* A cacheable object should have a self link
@@ -37,7 +45,8 @@ export class ObjectCacheEntry implements CacheEntry {
timeAdded: number;
msToLive: number;
requestHref: string;
operations: Operation[];
patches: Patch[] = [];
isDirty: boolean;
}
/**
@@ -78,9 +87,14 @@ export function objectCacheReducer(state = initialState, action: ObjectCacheActi
return resetObjectCacheTimestamps(state, action as ResetObjectCacheTimestampsAction)
}
case ObjectCacheActionTypes.PATCH: {
return patchObjectCache(state, action as PatchObjectCacheAction);
case ObjectCacheActionTypes.ADD_PATCH: {
return addPatchObjectCache(state, action as AddPatchObjectCacheAction);
}
case ObjectCacheActionTypes.APPLY_PATCH: {
return applyPatchObjectCache(state, action as ApplyPatchObjectCacheAction);
}
default: {
return state;
}
@@ -104,7 +118,7 @@ function addToObjectCache(state: ObjectCacheState, action: AddToObjectCacheActio
timeAdded: action.payload.timeAdded,
msToLive: action.payload.msToLive,
requestHref: action.payload.requestHref,
operations: []
isDirty: false
}
});
}
@@ -156,16 +170,41 @@ function resetObjectCacheTimestamps(state: ObjectCacheState, action: ResetObject
* @param state
* the current state
* @param action
* a PatchObjectCacheAction
* an AddPatchObjectCacheAction
* @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;
function addPatchObjectCache(state: ObjectCacheState, action: AddPatchObjectCacheAction): ObjectCacheState {
const uuid = action.payload.href;
const operations = action.payload.operations;
const newState = Object.assign({}, state);
if (hasValue(newState[uuid])) {
newState[uuid].operations = state[uuid].operations.concat(operations);
const patches = newState[uuid].patches;
newState[uuid].patches = [...patches, {operations} as Patch];
newState[uuid].isDirty = true;
}
return newState;
}
/**
* Apply the list of patch operations to a cached object
*
* @param state
* the current state
* @param action
* an ApplyPatchObjectCacheAction
* @return ObjectCacheState
* the new state, with the new operations applied to the state of the specified ObjectCacheEntry
*/
function applyPatchObjectCache(state: ObjectCacheState, action: ApplyPatchObjectCacheAction): ObjectCacheState {
const uuid = action.payload;
const newState = Object.assign({}, state);
if (hasValue(newState[uuid])) {
// flatten two dimensional array
const flatPatch: Operation[] = [].concat(...newState[uuid].patches);
const newData = applyPatch( newState[uuid].data, flatPatch);
newState[uuid].data = newData.newDocument;
newState[uuid].patches = [];
}
return newState;
}