mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-11 20:13:07 +00:00
implementation of server synchronization
This commit is contained in:
92
src/app/core/cache/server-sync-buffer.reducer.ts
vendored
Normal file
92
src/app/core/cache/server-sync-buffer.reducer.ts
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
import { RestRequestMethod } from '../data/request.models';
|
||||
import { hasNoValue, hasValue } from '../../shared/empty.util';
|
||||
import {
|
||||
AddToSSBAction,
|
||||
EmptySSBAction,
|
||||
ServerSyncBufferAction,
|
||||
ServerSyncBufferActionTypes
|
||||
} from './server-sync-buffer.actions';
|
||||
|
||||
/**
|
||||
* An entry in the ServerSyncBufferState
|
||||
* href: unique href of an ObjectCacheEntry
|
||||
* method: RestRequestMethod type
|
||||
*/
|
||||
export class ServerSyncBufferEntry {
|
||||
href: string;
|
||||
method: RestRequestMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ServerSyncBuffer State
|
||||
*
|
||||
* Consists list of ServerSyncBufferState
|
||||
*/
|
||||
export interface ServerSyncBufferState {
|
||||
buffer: ServerSyncBufferEntry[];
|
||||
}
|
||||
|
||||
// Object.create(null) ensures the object has no default js properties (e.g. `__proto__`)
|
||||
const initialState: ServerSyncBufferState = { buffer: [] };
|
||||
|
||||
/**
|
||||
* The ServerSyncBuffer Reducer
|
||||
*
|
||||
* @param state
|
||||
* the current state
|
||||
* @param action
|
||||
* the action to perform on the state
|
||||
* @return ServerSyncBufferState
|
||||
* the new state
|
||||
*/
|
||||
export function serverSyncBufferReducer(state = initialState, action: ServerSyncBufferAction): ServerSyncBufferState {
|
||||
switch (action.type) {
|
||||
|
||||
case ServerSyncBufferActionTypes.ADD: {
|
||||
return addToServerSyncQueue(state, action as AddToSSBAction)
|
||||
}
|
||||
|
||||
case ServerSyncBufferActionTypes.EMPTY: {
|
||||
return emptyServerSyncQueue(state, action as EmptySSBAction);
|
||||
}
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new entry to the buffer with a specified method
|
||||
*
|
||||
* @param state
|
||||
* the current state
|
||||
* @param action
|
||||
* an AddToSSBAction
|
||||
* @return ServerSyncBufferState
|
||||
* the new state, with a new entry added to the buffer
|
||||
*/
|
||||
function addToServerSyncQueue(state: ServerSyncBufferState, action: AddToSSBAction): ServerSyncBufferState {
|
||||
const actionEntry = action.payload as ServerSyncBufferEntry;
|
||||
if (hasNoValue(state.buffer.find((entry) => entry.href === actionEntry.href && entry.method === actionEntry.method))) {
|
||||
return Object.assign({}, state, { buffer: state.buffer.concat(actionEntry) });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all ServerSyncBuffers entry from the buffer with a specified method
|
||||
* If no method is specified, empty the whole buffer
|
||||
*
|
||||
* @param state
|
||||
* the current state
|
||||
* @param action
|
||||
* an AddToSSBAction
|
||||
* @return ServerSyncBufferState
|
||||
* the new state, with a new entry added to the buffer
|
||||
*/
|
||||
function emptyServerSyncQueue(state: ServerSyncBufferState, action: EmptySSBAction): ServerSyncBufferState {
|
||||
let newBuffer = [];
|
||||
if (hasValue(action.payload)) {
|
||||
newBuffer = state.buffer.filter((entry) => entry.method !== action.payload);
|
||||
}
|
||||
return Object.assign({}, state, { buffer: newBuffer });
|
||||
}
|
Reference in New Issue
Block a user