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.

How to create a delayed loading indicator when working with ngrx/store?

+1
−0

I am working on an Angular application using ngrx and I have a loader state + reducer that is used to display a loader. However, very short AJAX calls cause a flicker and I need to delay showing the loaded to avoid being shown for very fast responses (e.g. < 300ms).

My state is very simple:

export interface LoadingState {
    isLoading: boolean;
    loaderMessage: string;
}

export const initialLoadingState: LoadingState = {
    isLoading: false,
    loaderMessage: ""
};
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+0
−0

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 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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »