Skip to content

Latest commit

 

History

History
40 lines (28 loc) · 2.16 KB

entity-effects.md

File metadata and controls

40 lines (28 loc) · 2.16 KB

Entity Effects

Work in Progress

Effects are a way to trigger side effects with actions.

A one common, desirable side effect is an asynchronous HTTP call to the remote server to fetch or save entity data.

You implement one or more effects with the help of the @ngrx/effects package.

Actions dispatched to the ngrx store can be detected and processed by your effect method. After processing, whether synchronously or asynchronously, your method can dispatch new action(s) to the store

The ngrx-data library implements an effect named persist$ in its EntityEffects class.

The persist$ method filters for certain EntityAction.op values. These values turn into HTTP GET, PUT, POST, and DELETE requests with entity data. When the server responds (whether favorably or with an error), the persist$ method dispatches new EntityActions to the store with the appropriate response data.

Cancellation

You can try to cancel a save by dispatching a CANCEL_PERSIST EntityAction with the correlation id of the persistence action that you want to cancel.

The EntityCache.cancel$ watches for this action and is piped into the EntityCache.persist$, where it can try to cancel an entity collection query or save operation or at least prevent the server response from updating the cache.

It's not obvious that this is ever a great idea for a save operation. You cannot tell the server to cancel this way and cannot know if the server did or did not save. Nor can you count on processing the cancel request before the client receives the server response and applies the changes on the server or to the cache.

If you cancel before the server results arrive, the EntityEffect will not try to update the cached collection with late arriving server results. The effect will issue a CANCELED_PERSIST action instead. The EntityCollection reducer ignores this action but you can listen for it among the store actions and thus know that the cancellation took effect on the client.

More to come on the subject of effects