Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A How to create a delayed loading indicator when working with ngrx/store?

This heavily relies on this question which deals with another matter. In order to delay the loader, there must be a clear difference between the start of loaded from the caller's perspective and w...

posted 4y ago by Alexei‭  ·  edited 4y ago by Alexei‭

Answer
#2: Post edited by user avatar Alexei‭ · 2020-11-03T17:23:53Z (about 4 years ago)
fixed code alignment
#1: Initial revision by user avatar Alexei‭ · 2020-11-03T17:23:10Z (about 4 years ago)
This heavily relies on [this question](https://stackoverflow.com/questions/45419480/creating-a-loading-indicator-delay-with-ngrx-effects) which deals with another matter.

In order to delay the loader, there must be a clear difference between the start of loaded from the caller's perspective and when the loader is actually displayed (i.e. `isLoading = true`).

The following code does the trick:

### The reducer 

	export const loadingReducerInner = createReducer(
		initialLoadingState,
		on(LoadingSetVisibleAction, (state) => {
				return {
					isLoading: true,
					loaderMessage: state.loaderMessage
				};
		}),
		on(LoadingEndedAction, _ => {
				return {
					isLoading: false,
					loaderMessage: ""
				};
		})
	);

	export function loadingReducer(state: LoadingState, action: Action): LoadingState {
		return loadingReducerInner(state, action);
	}

### The effect

There are two possible things that might happen:

- the load is fast enough (<500ms in this case) and LoadingSetVisibleAction is not called because a LoadingEndedAction will be dispatched
- the load is slower and the loader is set to true before LoadingEndedAction is dispatched

	@Injectable()
	export class LoadingEffects {

		constructor(private actions$: Actions) { }

		startDelayTimer$ = createEffect(() => this.actions$.pipe(
				ofType(LoadingStartedAction),
				switchMap(_ => {
					const ret$ = timer(500).pipe(
						takeUntil( this.actions$.pipe(ofType(LoadingEndedAction))),
						mapTo(LoadingSetVisibleAction())
					);
					return ret$;
				})
			));
	}

Of course, the caller must ensure that LoadingStartedAction is always paired with a LoadingEndedAction dispatch both on positive and negative (an exception is raised during the AJAX call) flows.