@cleverbrush/scheduler 1.0.0-beta.6 → 1.0.0

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.
@@ -1,358 +0,0 @@
1
- import { Schedule } from './types.js';
2
-
3
- const MS_IN_DAY = 1000 * 60 * 60 * 24;
4
- const MS_IN_WEEK = MS_IN_DAY * 7;
5
-
6
- const getDayOfWeek = (date: Date) => {
7
- const res = date.getUTCDay();
8
- if (res === 0) return 7;
9
- return res;
10
- };
11
-
12
- const getNumberOfDaysInMonth = (date: Date) => {
13
- return new Date(
14
- Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 0, 0, 0, 0, 0)
15
- ).getDate();
16
- };
17
-
18
- export class ScheduleCalculator {
19
- #schedule: Schedule;
20
- #currentDate = new Date();
21
- #hour = 9;
22
- #minute = 0;
23
- #maxRepeat = -1;
24
- #repeatCount = 0;
25
-
26
- #hasNext = false;
27
- #next: Date | undefined;
28
-
29
- constructor(schedule: Schedule) {
30
- if (!schedule) throw new Error('schedule is required');
31
- this.#schedule = { ...schedule };
32
-
33
- if (typeof schedule.startsOn !== 'undefined') {
34
- this.#currentDate = schedule.startsOn;
35
- } else {
36
- this.#schedule.startsOn = new Date();
37
- this.#currentDate = this.#schedule.startsOn;
38
- }
39
-
40
- if (schedule.every !== 'minute') {
41
- if (typeof schedule.hour === 'number') {
42
- this.#hour = schedule.hour;
43
- }
44
-
45
- if (typeof schedule.minute === 'number') {
46
- this.#minute = schedule.minute;
47
- }
48
-
49
- if (
50
- schedule.every === 'day' &&
51
- new Date(
52
- Date.UTC(
53
- this.#currentDate.getUTCFullYear(),
54
- this.#currentDate.getUTCMonth(),
55
- this.#currentDate.getUTCDate(),
56
- this.#hour,
57
- this.#minute,
58
- 0,
59
- 0
60
- )
61
- ).getTime() < this.#currentDate.getTime()
62
- ) {
63
- const date = new Date(
64
- Date.UTC(
65
- this.#currentDate.getUTCFullYear(),
66
- this.#currentDate.getUTCMonth(),
67
- this.#currentDate.getUTCDate() + 1,
68
- this.#hour,
69
- this.#minute,
70
- 0,
71
- 0
72
- )
73
- );
74
- this.#currentDate = date;
75
- }
76
- }
77
-
78
- if (typeof schedule.maxOccurences === 'number') {
79
- this.#maxRepeat = schedule.maxOccurences;
80
- }
81
-
82
- const next = this.#getNext();
83
-
84
- if (typeof next !== 'undefined') {
85
- this.#next = next;
86
- this.#hasNext = true;
87
- }
88
-
89
- let leftToSkip =
90
- typeof this.#schedule.skipFirst === 'number'
91
- ? this.#schedule.skipFirst
92
- : 0;
93
-
94
- while (leftToSkip-- > 0 && this.#hasNext) {
95
- this.next();
96
- }
97
- }
98
-
99
- #getNext(): Date | undefined {
100
- let candidate: Date | null = null;
101
- let dayOfWeek: number;
102
-
103
- switch (this.#schedule.every) {
104
- case 'minute':
105
- candidate =
106
- this.#repeatCount === 0
107
- ? this.#schedule.startsOn
108
- : new Date(
109
- Date.UTC(
110
- this.#currentDate.getUTCFullYear(),
111
- this.#currentDate.getUTCMonth(),
112
- this.#currentDate.getUTCDate(),
113
- this.#currentDate.getUTCHours(),
114
- this.#currentDate.getUTCMinutes() +
115
- this.#schedule.interval,
116
- this.#currentDate.getUTCSeconds(),
117
- this.#currentDate.getUTCMilliseconds()
118
- )
119
- );
120
- break;
121
- case 'day':
122
- {
123
- const date = new Date(
124
- this.#currentDate.getTime() +
125
- MS_IN_DAY *
126
- (this.#repeatCount === 0
127
- ? 0
128
- : this.#schedule.interval - 1)
129
- );
130
-
131
- candidate = new Date(
132
- Date.UTC(
133
- date.getUTCFullYear(),
134
- date.getUTCMonth(),
135
- date.getUTCDate(),
136
- this.#hour,
137
- this.#minute,
138
- 0,
139
- 0
140
- )
141
- );
142
- }
143
- break;
144
- case 'week':
145
- {
146
- let date = this.#currentDate;
147
-
148
- do {
149
- let found = false;
150
- dayOfWeek = getDayOfWeek(date);
151
- if (Number.isNaN(dayOfWeek)) return;
152
- for (let i = 0; i < 7 - dayOfWeek + 1; i++) {
153
- date = new Date(
154
- date.getTime() + (i == 0 ? 0 : MS_IN_DAY)
155
- );
156
- if (
157
- this.#schedule.endsOn &&
158
- date > this.#schedule.endsOn
159
- ) {
160
- return;
161
- }
162
- if (
163
- this.#schedule.dayOfWeek.includes(
164
- getDayOfWeek(date)
165
- )
166
- ) {
167
- const dateWithTime = new Date(
168
- Date.UTC(
169
- date.getUTCFullYear(),
170
- date.getUTCMonth(),
171
- date.getUTCDate(),
172
- this.#hour,
173
- this.#minute,
174
- 0,
175
- 0
176
- )
177
- );
178
- if (dateWithTime > this.#schedule.startsOn) {
179
- candidate = dateWithTime;
180
- found = true;
181
- break;
182
- }
183
- }
184
- }
185
-
186
- if (found) break;
187
-
188
- date = new Date(
189
- date.getTime() +
190
- MS_IN_DAY +
191
- (this.#repeatCount == 0
192
- ? 0
193
- : (this.#schedule.interval - 1) *
194
- MS_IN_WEEK)
195
- );
196
- } while (
197
- (this.#schedule.endsOn &&
198
- date <= this.#schedule.endsOn) ||
199
- !this.#schedule.endsOn
200
- );
201
- }
202
- break;
203
- case 'month':
204
- {
205
- let dateTime;
206
- const cDate = this.#currentDate;
207
- let iteration = 0;
208
- do {
209
- const date =
210
- this.#schedule.day === 'last'
211
- ? getNumberOfDaysInMonth(
212
- new Date(
213
- Date.UTC(
214
- cDate.getUTCFullYear(),
215
- cDate.getUTCMonth() +
216
- iteration *
217
- (this.#repeatCount === 0
218
- ? 1
219
- : this.#schedule
220
- .interval),
221
- 1,
222
- 0,
223
- 0,
224
- 0,
225
- 0
226
- )
227
- )
228
- )
229
- : (this.#schedule.day as number);
230
- dateTime = new Date(
231
- Date.UTC(
232
- cDate.getUTCFullYear(),
233
- cDate.getUTCMonth() +
234
- iteration *
235
- (this.#repeatCount === 0
236
- ? 1
237
- : this.#schedule.interval),
238
- date,
239
- this.#hour,
240
- this.#minute,
241
- 0,
242
- 0
243
- )
244
- );
245
- iteration++;
246
- } while (
247
- dateTime < this.#schedule.startsOn ||
248
- dateTime <= this.#currentDate
249
- );
250
- candidate = dateTime;
251
- }
252
- break;
253
- case 'year':
254
- {
255
- let dateTime;
256
- const cDate = this.#currentDate;
257
- let iteration = 0;
258
- do {
259
- const date =
260
- this.#schedule.day === 'last'
261
- ? getNumberOfDaysInMonth(
262
- new Date(
263
- Date.UTC(
264
- cDate.getUTCFullYear() +
265
- iteration *
266
- (this.#repeatCount === 0
267
- ? 1
268
- : this.#schedule
269
- .interval),
270
- this.#schedule.month - 1,
271
- 1,
272
- 0,
273
- 0,
274
- 0,
275
- 0
276
- )
277
- )
278
- )
279
- : (this.#schedule.day as number);
280
- dateTime = new Date(
281
- Date.UTC(
282
- cDate.getUTCFullYear() +
283
- iteration *
284
- (this.#repeatCount === 0
285
- ? 1
286
- : this.#schedule.interval),
287
- this.#schedule.month - 1,
288
- date,
289
- this.#hour,
290
- this.#minute,
291
- 0,
292
- 0
293
- )
294
- );
295
- iteration++;
296
- } while (
297
- dateTime < this.#schedule.startsOn ||
298
- dateTime <= this.#currentDate
299
- );
300
- candidate = dateTime;
301
- }
302
- break;
303
- default:
304
- throw new Error('unknown schedule type');
305
- }
306
-
307
- if (!candidate) return;
308
-
309
- if (
310
- typeof this.#schedule.endsOn !== 'undefined' &&
311
- candidate > this.#schedule.endsOn
312
- ) {
313
- return;
314
- }
315
-
316
- return candidate;
317
- }
318
-
319
- public hasNext(span?: number): boolean {
320
- if (!this.#hasNext) {
321
- return false;
322
- }
323
-
324
- if (typeof span !== 'number') return this.#hasNext;
325
-
326
- return this.#next.getTime() - new Date().getTime() <= span;
327
- }
328
-
329
- public next(): {
330
- date: Date;
331
- index: number;
332
- } {
333
- if (!this.#hasNext) throw new Error('schedule is over');
334
-
335
- const result = this.#next as Date;
336
-
337
- this.#currentDate = new Date(
338
- result.getTime() +
339
- (['day', 'week'].includes(this.#schedule.every) ? MS_IN_DAY : 0)
340
- );
341
-
342
- this.#repeatCount++;
343
- const next = this.#getNext();
344
-
345
- this.#next = next as Date;
346
- this.#hasNext = typeof next !== 'undefined';
347
-
348
- if (this.#maxRepeat > 0 && this.#repeatCount >= this.#maxRepeat) {
349
- this.#next = undefined;
350
- this.#hasNext = false;
351
- }
352
-
353
- return {
354
- date: result,
355
- index: this.#repeatCount
356
- };
357
- }
358
- }