Skip to content

Instantly share code, notes, and snippets.

@jinhduong
Last active July 12, 2017 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinhduong/53dc823525ded39b72c246faf988ecfb to your computer and use it in GitHub Desktop.
Save jinhduong/53dc823525ded39b72c246faf988ecfb to your computer and use it in GitHub Desktop.
Loading-indicator Decorators in Angular 2/4
export function LoadingIndicator(): MethodDecorator {
return function (target: Object, propertyKey: string | symbol, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
// Inject your show loading function HERE
const subs = originalMethod.apply(this, args);
// If it's using do operator
if (subs.subscribe) {
subs.subscribe(() => {
// Stop your show loading function HERE
});
} else {
// If it's using subscribe
const _interval = setInterval(() => {
if (subs.isStopped) {
// Or stop your show loading function HERE
clearInterval(_interval);
}
}, 50);
};
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment