@push.rocks/taskbuffer 3.0.11 → 3.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,20 +4,24 @@ import { CycleCounter } from './taskbuffer.classes.cyclecounter.js';
4
4
 
5
5
  import { logger } from './taskbuffer.logging.js';
6
6
 
7
- export interface ITaskFunction {
8
- (x?: any): PromiseLike<any>;
7
+ export interface ITaskFunction<T = undefined> {
8
+ (x?: any, setupValue?: T): PromiseLike<any>;
9
9
  }
10
10
 
11
- export type TPreOrAfterTaskFunction = () => Task;
11
+ export interface ITaskSetupFunction<T = undefined> {
12
+ (): Promise<T>;
13
+ }
14
+
15
+ export type TPreOrAfterTaskFunction = () => Task<any>;
12
16
 
13
- export class Task {
17
+ export class Task<T = undefined> {
14
18
  // STATIC
15
- public static extractTask(preOrAfterTaskArg: Task | TPreOrAfterTaskFunction): Task {
19
+ public static extractTask<T = undefined>(preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction): Task<T> {
16
20
  switch (true) {
17
21
  case !preOrAfterTaskArg:
18
22
  return null;
19
23
  case preOrAfterTaskArg instanceof Task:
20
- return preOrAfterTaskArg as Task;
24
+ return preOrAfterTaskArg as Task<T>;
21
25
  case typeof preOrAfterTaskArg === 'function':
22
26
  const taskFunction = preOrAfterTaskArg as TPreOrAfterTaskFunction;
23
27
  return taskFunction();
@@ -32,7 +36,7 @@ export class Task {
32
36
  return done.promise;
33
37
  };
34
38
 
35
- public static isTask = (taskArg: Task): boolean => {
39
+ public static isTask = (taskArg: Task<any>): boolean => {
36
40
  if (taskArg instanceof Task && typeof taskArg.taskFunction === 'function') {
37
41
  return true;
38
42
  } else {
@@ -40,10 +44,10 @@ export class Task {
40
44
  }
41
45
  };
42
46
 
43
- public static isTaskTouched = (
44
- taskArg: Task | TPreOrAfterTaskFunction,
45
- touchedTasksArray: Task[]
46
- ): boolean => {
47
+ public static isTaskTouched<T = undefined> (
48
+ taskArg: Task<T> | TPreOrAfterTaskFunction,
49
+ touchedTasksArray: Task<T>[]
50
+ ): boolean {
47
51
  const taskToCheck = Task.extractTask(taskArg);
48
52
  let result = false;
49
53
  for (const keyArg in touchedTasksArray) {
@@ -54,42 +58,39 @@ export class Task {
54
58
  return result;
55
59
  };
56
60
 
57
- public static runTask = async (
58
- taskArg: Task | TPreOrAfterTaskFunction,
59
- optionsArg: { x?: any; touchedTasksArray?: Task[] }
61
+ public static runTask = async <T>(
62
+ taskArg: Task<T> | TPreOrAfterTaskFunction,
63
+ optionsArg: { x?: any; touchedTasksArray?: Task<T>[] }
60
64
  ) => {
61
- // extracts the task in case it is specified as a return value of a function
62
65
  const taskToRun = Task.extractTask(taskArg);
63
66
  const done = plugins.smartpromise.defer();
64
67
 
65
- // pay respect to execDelay
68
+ if (!taskToRun.setupValue && taskToRun.taskSetup) {
69
+ taskToRun.setupValue = await taskToRun.taskSetup();
70
+ }
71
+
66
72
  if (taskToRun.execDelay) {
67
73
  await plugins.smartdelay.delayFor(taskToRun.execDelay);
68
74
  }
69
75
 
70
- // set running params
71
76
  taskToRun.running = true;
72
77
 
73
78
  done.promise.then(async () => {
74
79
  taskToRun.running = false;
75
80
  });
76
81
 
77
- // handle options
78
82
  const options = {
79
83
  ...{ x: undefined, touchedTasksArray: [] },
80
84
  ...optionsArg,
81
85
  };
82
86
  const x = options.x;
83
- const touchedTasksArray: Task[] = options.touchedTasksArray;
87
+ const touchedTasksArray: Task<T>[] = options.touchedTasksArray;
84
88
 
85
89
  touchedTasksArray.push(taskToRun);
86
90
 
87
- // run the task cascade
88
91
  const localDeferred = plugins.smartpromise.defer();
89
92
  localDeferred.promise
90
93
  .then(() => {
91
- // lets run any preTask
92
-
93
94
  if (taskToRun.preTask && !Task.isTaskTouched(taskToRun.preTask, touchedTasksArray)) {
94
95
  return Task.runTask(taskToRun.preTask, { x, touchedTasksArray });
95
96
  } else {
@@ -99,9 +100,8 @@ export class Task {
99
100
  }
100
101
  })
101
102
  .then(async (x) => {
102
- // lets run the main task
103
103
  try {
104
- return await taskToRun.taskFunction(x);
104
+ return await taskToRun.taskFunction(x, taskToRun.setupValue);
105
105
  } catch (e) {
106
106
  console.log(e);
107
107
  }
@@ -126,15 +126,9 @@ export class Task {
126
126
  };
127
127
 
128
128
  // INSTANCE
129
- // mandatory properties
130
129
  public name: string;
131
- /**
132
- * the version of the task
133
- * should follow semver
134
- * might be important for DistributedCoordinator
135
- */
136
130
  public version: string;
137
- public taskFunction: ITaskFunction;
131
+ public taskFunction: ITaskFunction<T>;
138
132
  public buffered: boolean;
139
133
  public cronJob: plugins.smarttime.CronJob;
140
134
 
@@ -142,11 +136,9 @@ export class Task {
142
136
  public execDelay: number;
143
137
  public timeout: number;
144
138
 
145
- // tasks to run before and after
146
- public preTask: Task | TPreOrAfterTaskFunction;
147
- public afterTask: Task | TPreOrAfterTaskFunction;
139
+ public preTask: Task<T> | TPreOrAfterTaskFunction;
140
+ public afterTask: Task<T> | TPreOrAfterTaskFunction;
148
141
 
149
- // initialize by default
150
142
  public running: boolean = false;
151
143
  public bufferRunner = new BufferRunner(this);
152
144
  public cycleCounter = new CycleCounter(this);
@@ -154,36 +146,18 @@ export class Task {
154
146
  public idle: boolean = true;
155
147
  private _state: string = 'ready';
156
148
 
149
+ public taskSetup: ITaskSetupFunction<T>;
150
+ public setupValue: T;
151
+
157
152
  constructor(optionsArg: {
158
- /**
159
- * the task function to run, must return promise
160
- */
161
- taskFunction: ITaskFunction;
162
- /**
163
- * any other task to run before
164
- */
165
- preTask?: Task | TPreOrAfterTaskFunction;
166
- /**
167
- * any other task to run after
168
- */
169
- afterTask?: Task | TPreOrAfterTaskFunction;
170
- /**
171
- * wether this task should run buffered
172
- */
153
+ taskFunction: ITaskFunction<T>;
154
+ preTask?: Task<T> | TPreOrAfterTaskFunction;
155
+ afterTask?: Task<T> | TPreOrAfterTaskFunction;
173
156
  buffered?: boolean;
174
- /**
175
- * the maximum buffer
176
- */
177
157
  bufferMax?: number;
178
- /**
179
- * the execution delay, before the task is executed
180
- * only makes sense when running in buffered mode
181
- */
182
158
  execDelay?: number;
183
- /**
184
- * the name of the task
185
- */
186
159
  name?: string;
160
+ taskSetup?: ITaskSetupFunction<T>;
187
161
  }) {
188
162
  this.taskFunction = optionsArg.taskFunction;
189
163
  this.preTask = optionsArg.preTask;
@@ -193,11 +167,9 @@ export class Task {
193
167
  this.bufferMax = optionsArg.bufferMax;
194
168
  this.execDelay = optionsArg.execDelay;
195
169
  this.name = optionsArg.name;
170
+ this.taskSetup = optionsArg.taskSetup;
196
171
  }
197
172
 
198
- /**
199
- * trigger the task. Will trigger buffered if this.buffered is true
200
- */
201
173
  public trigger(x?: any): Promise<any> {
202
174
  if (this.buffered) {
203
175
  return this.triggerBuffered(x);
@@ -206,18 +178,10 @@ export class Task {
206
178
  }
207
179
  }
208
180
 
209
- /**
210
- * trigger task unbuffered.
211
- * will actually run the task, not considering any buffered limits.
212
- */
213
181
  public triggerUnBuffered(x?: any): Promise<any> {
214
- return Task.runTask(this, { x: x });
182
+ return Task.runTask<T>(this, { x: x });
215
183
  }
216
184
 
217
- /**
218
- * trigger task buffered.
219
- * note: .trigger() also calls this function
220
- */
221
185
  public triggerBuffered(x?: any): Promise<any> {
222
186
  return this.bufferRunner.trigger(x);
223
187
  }
@@ -1,6 +1,6 @@
1
1
  import * as plugins from './taskbuffer.plugins.js';
2
2
 
3
- import { Task, ITaskFunction } from './taskbuffer.classes.task.js';
3
+ import { Task, type ITaskFunction } from './taskbuffer.classes.task.js';
4
4
 
5
5
  export class TaskDebounced<T = unknown> extends Task {
6
6
  private _debouncedTaskFunction: ITaskFunction;
@@ -1,6 +1,6 @@
1
1
  import * as plugins from './taskbuffer.plugins.js';
2
2
 
3
- import { Task, ITaskFunction } from './taskbuffer.classes.task.js';
3
+ import { Task, type ITaskFunction } from './taskbuffer.classes.task.js';
4
4
 
5
5
  /**
6
6
  * TaskOnce is run exactly once, no matter how often it is triggered