fixed SSB commit delay

This commit is contained in:
lotte
2018-10-31 11:59:09 +01:00
parent b48ba97d86
commit 80b11515c4
3 changed files with 28 additions and 7 deletions

View File

@@ -11,7 +11,7 @@ import { ObjectCacheState } from '../cache/object-cache.reducer';
describe('AuthResponseParsingService', () => { describe('AuthResponseParsingService', () => {
let service: AuthResponseParsingService; let service: AuthResponseParsingService;
const EnvConfig = { cache: { msToLive: 1000 } } as GlobalConfig; const EnvConfig = { cache: { msToLive: 1000 } } as any;
const store = new MockStore<ObjectCacheState>({}); const store = new MockStore<ObjectCacheState>({});
const objectCacheService = new ObjectCacheService(store as any); const objectCacheService = new ObjectCacheService(store as any);

View File

@@ -8,6 +8,7 @@ import {
RemoveFromObjectCacheAction, RemoveFromObjectCacheAction,
ResetObjectCacheTimestampsAction ResetObjectCacheTimestampsAction
} from './object-cache.actions'; } from './object-cache.actions';
import { Operation } from 'fast-json-patch';
class NullAction extends RemoveFromObjectCacheAction { class NullAction extends RemoveFromObjectCacheAction {
type = null; type = null;
@@ -21,6 +22,7 @@ class NullAction extends RemoveFromObjectCacheAction {
describe('objectCacheReducer', () => { describe('objectCacheReducer', () => {
const selfLink1 = 'https://localhost:8080/api/core/items/1698f1d3-be98-4c51-9fd8-6bfedcbd59b7'; const selfLink1 = 'https://localhost:8080/api/core/items/1698f1d3-be98-4c51-9fd8-6bfedcbd59b7';
const selfLink2 = 'https://localhost:8080/api/core/items/28b04544-1766-4e82-9728-c4e93544ecd3'; const selfLink2 = 'https://localhost:8080/api/core/items/28b04544-1766-4e82-9728-c4e93544ecd3';
const newName = 'new different name';
const testState = { const testState = {
[selfLink1]: { [selfLink1]: {
data: { data: {
@@ -140,15 +142,31 @@ describe('objectCacheReducer', () => {
}); });
it('should perform the ADD_PATCH action without affecting the previous state', () => { it('should perform the ADD_PATCH action without affecting the previous state', () => {
const action = new AddPatchObjectCacheAction(selfLink1, [{ op: 'replace', path: '/name', value: 'random string' }]); const action = new AddPatchObjectCacheAction(selfLink1, [{
op: 'replace',
path: '/name',
value: 'random string'
}]);
// testState has already been frozen above // testState has already been frozen above
objectCacheReducer(testState, action); objectCacheReducer(testState, action);
}); });
it('should perform the APPLY_PATCH action without affecting the previous state', () => { it('should when the ADD_PATCH action dispatched', () => {
const patch = [{ op: 'add', path: '/name', value: newName } as Operation];
const action = new AddPatchObjectCacheAction(selfLink1, patch);
const newState = objectCacheReducer(testState, action);
expect(newState[selfLink1].patches.map((p) => p.operations)).toContain(patch);
});
it('should when the APPLY_PATCH action dispatched', () => {
const patch = [{ op: 'add', path: '/name', value: newName } as Operation];
const addPatchAction = new AddPatchObjectCacheAction(selfLink1, patch);
const stateWithPatch = objectCacheReducer(testState, addPatchAction);
const action = new ApplyPatchObjectCacheAction(selfLink1); const action = new ApplyPatchObjectCacheAction(selfLink1);
// testState has already been frozen above const newState = objectCacheReducer(stateWithPatch, action);
objectCacheReducer(testState, action); expect(newState[selfLink1].patches).toEqual([]);
expect((newState[selfLink1].data as any).name).toEqual(newName);
}); });
}); });

View File

@@ -1,4 +1,4 @@
import { delay, exhaustMap, first, map, switchMap } from 'rxjs/operators'; import { delay, exhaustMap, first, map, switchMap, tap } from 'rxjs/operators';
import { Inject, Injectable } from '@angular/core'; import { Inject, Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects'; import { Actions, Effect, ofType } from '@ngrx/effects';
import { import {
@@ -38,7 +38,9 @@ export class ServerSyncBufferEffects {
exhaustMap((action: AddToSSBAction) => { exhaustMap((action: AddToSSBAction) => {
const autoSyncConfig = this.EnvConfig.cache.autoSync; const autoSyncConfig = this.EnvConfig.cache.autoSync;
const timeoutInSeconds = autoSyncConfig.timePerMethod[action.payload.method] || autoSyncConfig.defaultTime; const timeoutInSeconds = autoSyncConfig.timePerMethod[action.payload.method] || autoSyncConfig.defaultTime;
return observableOf(new CommitSSBAction(action.payload.method)).pipe(delay(timeoutInSeconds * 1000)) return observableOf(new CommitSSBAction(action.payload.method)).pipe(
delay(timeoutInSeconds * 1000),
)
}) })
); );
@@ -54,6 +56,7 @@ export class ServerSyncBufferEffects {
switchMap((action: CommitSSBAction) => { switchMap((action: CommitSSBAction) => {
return this.store.pipe( return this.store.pipe(
select(serverSyncBufferSelector()), select(serverSyncBufferSelector()),
first(), /* necessary, otherwise delay will not have any effect after the first run */
switchMap((bufferState: ServerSyncBufferState) => { switchMap((bufferState: ServerSyncBufferState) => {
const actions: Array<Observable<Action>> = bufferState.buffer const actions: Array<Observable<Action>> = bufferState.buffer
.filter((entry: ServerSyncBufferEntry) => { .filter((entry: ServerSyncBufferEntry) => {