2020-07-12 02:40:45 +00:00
|
|
|
import * as plugins from './taskbuffer.plugins';
|
|
|
|
|
|
|
|
import { Task, ITaskFunction } from './taskbuffer.classes.task';
|
|
|
|
|
|
|
|
export class TaskDebounced<T = unknown> extends Task {
|
|
|
|
private _debouncedTaskFunction: ITaskFunction;
|
|
|
|
private _observableIntake = new plugins.smartrx.ObservableIntake<T>();
|
|
|
|
|
|
|
|
constructor(optionsArg: {
|
|
|
|
name: string;
|
|
|
|
taskFunction: ITaskFunction;
|
2020-07-12 10:57:15 +00:00
|
|
|
debounceTimeInMillis: number;
|
2020-07-12 02:40:45 +00:00
|
|
|
}) {
|
|
|
|
super({
|
|
|
|
name: optionsArg.name,
|
|
|
|
taskFunction: async (x: T) => {
|
|
|
|
this._observableIntake.push(x);
|
2020-09-02 13:04:52 +00:00
|
|
|
},
|
2020-07-12 02:40:45 +00:00
|
|
|
});
|
|
|
|
this.taskFunction = optionsArg.taskFunction;
|
2020-09-02 13:04:52 +00:00
|
|
|
this._observableIntake.observable
|
|
|
|
.pipe(plugins.smartrx.rxjs.ops.debounceTime(optionsArg.debounceTimeInMillis))
|
|
|
|
.subscribe((x) => {
|
|
|
|
this.taskFunction(x);
|
|
|
|
});
|
2020-07-12 02:40:45 +00:00
|
|
|
}
|
2020-09-02 13:04:52 +00:00
|
|
|
}
|