taskbuffer/ts/taskbuffer.classes.taskdebounced.ts

25 lines
773 B
TypeScript
Raw Normal View History

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);
}
});
this.taskFunction = optionsArg.taskFunction;
2020-07-12 10:57:15 +00:00
this._observableIntake.observable.pipe(plugins.smartrx.rxjs.ops.debounceTime(optionsArg.debounceTimeInMillis)).subscribe((x) => {
2020-07-12 02:40:45 +00:00
this.taskFunction(x);
});
}
}