For instance, I have the below pseudo code:
public action1():
return stuff.pipe(delay(15000));
public action2():
return stuff.pipe(delay(15000));
public action3():
return stuff.pipe(delay(15000));
The delay is identical for each return and will not change. But instead of having it as a static value, I would like to make it into a constant that can be easily referenced and modified if needed. Something like the below pseudo code:
Const delay = pipe(delay(15000))
public action1():
return stuff.delay;
public action2():
return stuff.delay;
public action3():
return stuff.delay;
I have tried to directly reference the delay by using const but I am getting errors when attempting this. I am using the RJXS Delay operator. The primary question is, Is there a way to convert this operator into a constant that can be used in multiple areas?
You can define a generic function which will accept observer and and bind delay with it and return.
You can do something like bellow
const getAfterDelay = (observer) => observer.pipe(delay(15000));
public action1():
return getAfterDelay(stuff);
public action2():
return getAfterDelay(stuff);
public action3():
return getAfterDelay(stuff);
declare module 'rxjs/internal/Observable' {
interface Observable<T> {
delay(): Observable<T>;
}
}
Observable.prototype.delay = function(): Observable<any> {
return this.pipe(delay(15000));
};
This allows for the syntax that you're looking for: stuff.delay();.
Observable Delay Extension Method in Typescript for StackBlitz example.