taskbuffer/ts/taskbuffer.classes.taskdebounced.ts

28 lines
799 B
TypeScript
Raw Permalink Normal View History

2022-03-25 11:14:49 +00:00
import * as plugins from './taskbuffer.plugins.js';
2020-07-12 02:40:45 +00:00
2022-03-25 11:14:49 +00:00
import { Task, ITaskFunction } from './taskbuffer.classes.task.js';
2020-07-12 02:40:45 +00:00
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
}