MCPcopy Create free account
hub / github.com/ngrx/platform / EntityCacheEffects

Class EntityCacheEffects

modules/data/src/effects/entity-cache-effects.ts:43–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41
42@Injectable()
43export class EntityCacheEffects {
44 // See https://github.com/ReactiveX/rxjs/blob/master/doc/marble-testing.md
45 /** Delay for error and skip observables. Must be multiple of 10 for marble testing. */
46 private responseDelay = 10;
47
48 constructor(
49 private actions: Actions,
50 private dataService: EntityCacheDataService,
51 private entityActionFactory: EntityActionFactory,
52 private logger: Logger,
53 /**
54 * Injecting an optional Scheduler that will be undefined
55 * in normal application usage, but its injected here so that you can mock out
56 * during testing using the RxJS TestScheduler for simulating passages of time.
57 */
58 @Optional()
59 @Inject(ENTITY_EFFECTS_SCHEDULER)
60 private scheduler: SchedulerLike
61 ) {}
62
63 /**
64 * Observable of SAVE_ENTITIES_CANCEL actions with non-null correlation ids
65 */
66 saveEntitiesCancel$: Observable<SaveEntitiesCancel> = createEffect(
67 () =>
68 this.actions.pipe(
69 ofType(EntityCacheAction.SAVE_ENTITIES_CANCEL),
70 filter((a: SaveEntitiesCancel) => a.payload.correlationId != null)
71 ),
72 { dispatch: false }
73 );
74
75 // Concurrent persistence requests considered unsafe.
76 // `mergeMap` allows for concurrent requests which may return in any order
77 saveEntities$: Observable<Action> = createEffect(() =>
78 this.actions.pipe(
79 ofType(EntityCacheAction.SAVE_ENTITIES),
80 mergeMap((action: SaveEntities) => this.saveEntities(action))
81 )
82 );
83
84 /**
85 * Perform the requested SaveEntities actions and return a scalar Observable<Action>
86 * that the effect should dispatch to the store after the server responds.
87 * @param action The SaveEntities action
88 */
89 saveEntities(action: SaveEntities): Observable<Action> {
90 const error = action.payload.error;
91 if (error) {
92 return this.handleSaveEntitiesError$(action)(error);
93 }
94 try {
95 const changeSet = excludeEmptyChangeSetItems(action.payload.changeSet);
96 const { correlationId, mergeStrategy, tag, url } = action.payload;
97 const options = { correlationId, mergeStrategy, tag };
98
99 if (changeSet.changes.length === 0) {
100 // nothing to save

Callers

nothing calls this directly

Calls 4

saveEntitiesMethod · 0.95
createEffectFunction · 0.90
ofTypeFunction · 0.90
filterFunction · 0.50

Tested by

no test coverage detected