@stonyx/cron 0.2.1-beta.4 → 0.2.1-beta.41

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.
package/pnpm-lock.yaml DELETED
@@ -1,370 +0,0 @@
1
- lockfileVersion: '9.0'
2
-
3
- settings:
4
- autoInstallPeers: true
5
- excludeLinksFromLockfile: false
6
-
7
- importers:
8
-
9
- .:
10
- dependencies:
11
- stonyx:
12
- specifier: 0.2.3-beta.2
13
- version: 0.2.3-beta.2
14
- devDependencies:
15
- '@stonyx/utils':
16
- specifier: 0.2.3-beta.3
17
- version: 0.2.3-beta.3
18
- qunit:
19
- specifier: ^2.24.1
20
- version: 2.25.0
21
- sinon:
22
- specifier: ^21.0.0
23
- version: 21.0.1
24
-
25
- packages:
26
-
27
- '@sinonjs/commons@3.0.1':
28
- resolution: {integrity: sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==}
29
-
30
- '@sinonjs/fake-timers@15.1.0':
31
- resolution: {integrity: sha512-cqfapCxwTGsrR80FEgOoPsTonoefMBY7dnUEbQ+GRcved0jvkJLzvX6F4WtN+HBqbPX/SiFsIRUp+IrCW/2I2w==}
32
-
33
- '@sinonjs/samsam@8.0.3':
34
- resolution: {integrity: sha512-hw6HbX+GyVZzmaYNh82Ecj1vdGZrqVIn/keDTg63IgAwiQPO+xCz99uG6Woqgb4tM0mUiFENKZ4cqd7IX94AXQ==}
35
-
36
- '@stonyx/utils@0.2.3-beta.3':
37
- resolution: {integrity: sha512-CLmz/DPgNWU7IXJYQXzmAdph65iapp6mHnvKZQULrd6IdKPGb6KlVMVL29yuyXg0fOapkkoPV4Xf6PmksSE8bw==}
38
-
39
- call-bind-apply-helpers@1.0.2:
40
- resolution: {integrity: sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==}
41
- engines: {node: '>= 0.4'}
42
-
43
- call-bound@1.0.4:
44
- resolution: {integrity: sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==}
45
- engines: {node: '>= 0.4'}
46
-
47
- chalk@5.6.2:
48
- resolution: {integrity: sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==}
49
- engines: {node: ^12.17.0 || ^14.13 || >=16.0.0}
50
-
51
- commander@7.2.0:
52
- resolution: {integrity: sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==}
53
- engines: {node: '>= 10'}
54
-
55
- diff@8.0.3:
56
- resolution: {integrity: sha512-qejHi7bcSD4hQAZE0tNAawRK1ZtafHDmMTMkrrIGgSLl7hTnQHmKCeB45xAcbfTqK2zowkM3j3bHt/4b/ARbYQ==}
57
- engines: {node: '>=0.3.1'}
58
-
59
- dunder-proto@1.0.1:
60
- resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==}
61
- engines: {node: '>= 0.4'}
62
-
63
- es-define-property@1.0.1:
64
- resolution: {integrity: sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==}
65
- engines: {node: '>= 0.4'}
66
-
67
- es-errors@1.3.0:
68
- resolution: {integrity: sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==}
69
- engines: {node: '>= 0.4'}
70
-
71
- es-object-atoms@1.1.1:
72
- resolution: {integrity: sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==}
73
- engines: {node: '>= 0.4'}
74
-
75
- fs@0.0.1-security:
76
- resolution: {integrity: sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==}
77
-
78
- function-bind@1.1.2:
79
- resolution: {integrity: sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==}
80
-
81
- get-intrinsic@1.3.0:
82
- resolution: {integrity: sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==}
83
- engines: {node: '>= 0.4'}
84
-
85
- get-proto@1.0.1:
86
- resolution: {integrity: sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==}
87
- engines: {node: '>= 0.4'}
88
-
89
- globalyzer@0.1.0:
90
- resolution: {integrity: sha512-40oNTM9UfG6aBmuKxk/giHn5nQ8RVz/SS4Ir6zgzOv9/qC3kKZ9v4etGTcJbEl/NyVQH7FGU7d+X1egr57Md2Q==}
91
-
92
- globrex@0.1.2:
93
- resolution: {integrity: sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==}
94
-
95
- gopd@1.2.0:
96
- resolution: {integrity: sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==}
97
- engines: {node: '>= 0.4'}
98
-
99
- has-flag@4.0.0:
100
- resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==}
101
- engines: {node: '>=8'}
102
-
103
- has-symbols@1.1.0:
104
- resolution: {integrity: sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==}
105
- engines: {node: '>= 0.4'}
106
-
107
- hasown@2.0.2:
108
- resolution: {integrity: sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==}
109
- engines: {node: '>= 0.4'}
110
-
111
- inherits@2.0.3:
112
- resolution: {integrity: sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==}
113
-
114
- math-intrinsics@1.1.0:
115
- resolution: {integrity: sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==}
116
- engines: {node: '>= 0.4'}
117
-
118
- node-chronicle@0.2.0:
119
- resolution: {integrity: sha512-se9NsW67UZqDFbK/+Rbml9KdCVTwzaadIgL4NDBcDpLMhOf1qerRxXicE+/dBsoyhDqWCwAFbBjR3iytF3gepA==}
120
-
121
- node-watch@0.7.3:
122
- resolution: {integrity: sha512-3l4E8uMPY1HdMMryPRUAl+oIHtXtyiTlIiESNSVSNxcPfzAFzeTbXFQkZfAwBbo0B1qMSG8nUABx+Gd+YrbKrQ==}
123
- engines: {node: '>=6'}
124
-
125
- object-inspect@1.13.4:
126
- resolution: {integrity: sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==}
127
- engines: {node: '>= 0.4'}
128
-
129
- path@0.12.7:
130
- resolution: {integrity: sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q==}
131
-
132
- process@0.11.10:
133
- resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==}
134
- engines: {node: '>= 0.6.0'}
135
-
136
- punycode@1.4.1:
137
- resolution: {integrity: sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ==}
138
-
139
- qs@6.14.1:
140
- resolution: {integrity: sha512-4EK3+xJl8Ts67nLYNwqw/dsFVnCf+qR7RgXSK9jEEm9unao3njwMDdmsdvoKBKHzxd7tCYz5e5M+SnMjdtXGQQ==}
141
- engines: {node: '>=0.6'}
142
-
143
- qunit@2.25.0:
144
- resolution: {integrity: sha512-MONPKgjavgTqArCwZOEz8nEMbA19zNXIp5ZOW9rPYj5cbgQp0fiI36c9dPTSzTRRzx+KcfB5eggYB/ENqxi0+w==}
145
- engines: {node: '>=10'}
146
- hasBin: true
147
-
148
- side-channel-list@1.0.0:
149
- resolution: {integrity: sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==}
150
- engines: {node: '>= 0.4'}
151
-
152
- side-channel-map@1.0.1:
153
- resolution: {integrity: sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==}
154
- engines: {node: '>= 0.4'}
155
-
156
- side-channel-weakmap@1.0.2:
157
- resolution: {integrity: sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==}
158
- engines: {node: '>= 0.4'}
159
-
160
- side-channel@1.1.0:
161
- resolution: {integrity: sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==}
162
- engines: {node: '>= 0.4'}
163
-
164
- sinon@21.0.1:
165
- resolution: {integrity: sha512-Z0NVCW45W8Mg5oC/27/+fCqIHFnW8kpkFOq0j9XJIev4Ld0mKmERaZv5DMLAb9fGCevjKwaEeIQz5+MBXfZcDw==}
166
-
167
- stonyx@0.2.3-beta.2:
168
- resolution: {integrity: sha512-w2XmOMmqxbPqs6/ph+GlxHYMG3+BRo/FAnXA8EPBDy+ooh6i8SwjdjUcoqREMeV6zYUCrm63ztUjQ2TCKnW2vw==}
169
- hasBin: true
170
-
171
- supports-color@7.2.0:
172
- resolution: {integrity: sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==}
173
- engines: {node: '>=8'}
174
-
175
- tiny-glob@0.2.9:
176
- resolution: {integrity: sha512-g/55ssRPUjShh+xkfx9UPDXqhckHEsHr4Vd9zX55oSdGZc/MD0m3sferOkwWtp98bv+kcVfEHtRJgBVJzelrzg==}
177
-
178
- type-detect@4.0.8:
179
- resolution: {integrity: sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==}
180
- engines: {node: '>=4'}
181
-
182
- type-detect@4.1.0:
183
- resolution: {integrity: sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==}
184
- engines: {node: '>=4'}
185
-
186
- url@0.11.4:
187
- resolution: {integrity: sha512-oCwdVC7mTuWiPyjLUz/COz5TLk6wgp0RCsN+wHZ2Ekneac9w8uuV0njcbbie2ME+Vs+d6duwmYuR3HgQXs1fOg==}
188
- engines: {node: '>= 0.4'}
189
-
190
- util@0.10.4:
191
- resolution: {integrity: sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==}
192
-
193
- snapshots:
194
-
195
- '@sinonjs/commons@3.0.1':
196
- dependencies:
197
- type-detect: 4.0.8
198
-
199
- '@sinonjs/fake-timers@15.1.0':
200
- dependencies:
201
- '@sinonjs/commons': 3.0.1
202
-
203
- '@sinonjs/samsam@8.0.3':
204
- dependencies:
205
- '@sinonjs/commons': 3.0.1
206
- type-detect: 4.1.0
207
-
208
- '@stonyx/utils@0.2.3-beta.3': {}
209
-
210
- call-bind-apply-helpers@1.0.2:
211
- dependencies:
212
- es-errors: 1.3.0
213
- function-bind: 1.1.2
214
-
215
- call-bound@1.0.4:
216
- dependencies:
217
- call-bind-apply-helpers: 1.0.2
218
- get-intrinsic: 1.3.0
219
-
220
- chalk@5.6.2: {}
221
-
222
- commander@7.2.0: {}
223
-
224
- diff@8.0.3: {}
225
-
226
- dunder-proto@1.0.1:
227
- dependencies:
228
- call-bind-apply-helpers: 1.0.2
229
- es-errors: 1.3.0
230
- gopd: 1.2.0
231
-
232
- es-define-property@1.0.1: {}
233
-
234
- es-errors@1.3.0: {}
235
-
236
- es-object-atoms@1.1.1:
237
- dependencies:
238
- es-errors: 1.3.0
239
-
240
- fs@0.0.1-security: {}
241
-
242
- function-bind@1.1.2: {}
243
-
244
- get-intrinsic@1.3.0:
245
- dependencies:
246
- call-bind-apply-helpers: 1.0.2
247
- es-define-property: 1.0.1
248
- es-errors: 1.3.0
249
- es-object-atoms: 1.1.1
250
- function-bind: 1.1.2
251
- get-proto: 1.0.1
252
- gopd: 1.2.0
253
- has-symbols: 1.1.0
254
- hasown: 2.0.2
255
- math-intrinsics: 1.1.0
256
-
257
- get-proto@1.0.1:
258
- dependencies:
259
- dunder-proto: 1.0.1
260
- es-object-atoms: 1.1.1
261
-
262
- globalyzer@0.1.0: {}
263
-
264
- globrex@0.1.2: {}
265
-
266
- gopd@1.2.0: {}
267
-
268
- has-flag@4.0.0: {}
269
-
270
- has-symbols@1.1.0: {}
271
-
272
- hasown@2.0.2:
273
- dependencies:
274
- function-bind: 1.1.2
275
-
276
- inherits@2.0.3: {}
277
-
278
- math-intrinsics@1.1.0: {}
279
-
280
- node-chronicle@0.2.0:
281
- dependencies:
282
- chalk: 5.6.2
283
- fs: 0.0.1-security
284
- path: 0.12.7
285
- url: 0.11.4
286
-
287
- node-watch@0.7.3: {}
288
-
289
- object-inspect@1.13.4: {}
290
-
291
- path@0.12.7:
292
- dependencies:
293
- process: 0.11.10
294
- util: 0.10.4
295
-
296
- process@0.11.10: {}
297
-
298
- punycode@1.4.1: {}
299
-
300
- qs@6.14.1:
301
- dependencies:
302
- side-channel: 1.1.0
303
-
304
- qunit@2.25.0:
305
- dependencies:
306
- commander: 7.2.0
307
- node-watch: 0.7.3
308
- tiny-glob: 0.2.9
309
-
310
- side-channel-list@1.0.0:
311
- dependencies:
312
- es-errors: 1.3.0
313
- object-inspect: 1.13.4
314
-
315
- side-channel-map@1.0.1:
316
- dependencies:
317
- call-bound: 1.0.4
318
- es-errors: 1.3.0
319
- get-intrinsic: 1.3.0
320
- object-inspect: 1.13.4
321
-
322
- side-channel-weakmap@1.0.2:
323
- dependencies:
324
- call-bound: 1.0.4
325
- es-errors: 1.3.0
326
- get-intrinsic: 1.3.0
327
- object-inspect: 1.13.4
328
- side-channel-map: 1.0.1
329
-
330
- side-channel@1.1.0:
331
- dependencies:
332
- es-errors: 1.3.0
333
- object-inspect: 1.13.4
334
- side-channel-list: 1.0.0
335
- side-channel-map: 1.0.1
336
- side-channel-weakmap: 1.0.2
337
-
338
- sinon@21.0.1:
339
- dependencies:
340
- '@sinonjs/commons': 3.0.1
341
- '@sinonjs/fake-timers': 15.1.0
342
- '@sinonjs/samsam': 8.0.3
343
- diff: 8.0.3
344
- supports-color: 7.2.0
345
-
346
- stonyx@0.2.3-beta.2:
347
- dependencies:
348
- node-chronicle: 0.2.0
349
-
350
- supports-color@7.2.0:
351
- dependencies:
352
- has-flag: 4.0.0
353
-
354
- tiny-glob@0.2.9:
355
- dependencies:
356
- globalyzer: 0.1.0
357
- globrex: 0.1.2
358
-
359
- type-detect@4.0.8: {}
360
-
361
- type-detect@4.1.0: {}
362
-
363
- url@0.11.4:
364
- dependencies:
365
- punycode: 1.4.1
366
- qs: 6.14.1
367
-
368
- util@0.10.4:
369
- dependencies:
370
- inherits: 2.0.3
package/src/main.js DELETED
@@ -1,112 +0,0 @@
1
- /*
2
- * Copyright 2025 Stone Costa
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the 'License');
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import config from 'stonyx/config';
18
- import log from 'stonyx/log';
19
- import { getTimestamp } from "@stonyx/utils/date";
20
- import MinHeap from '@stonyx/cron/min-heap';
21
-
22
- export default class Cron {
23
- jobs = {};
24
- heap = new MinHeap();
25
- timer = null;
26
-
27
- constructor() {
28
- if (Cron.instance) return Cron.instance;
29
- Cron.instance = this;
30
- }
31
-
32
- scheduleNextRun() {
33
- clearTimeout(this.timer);
34
-
35
- const { heap } = this;
36
-
37
- if (heap.isEmpty()) return;
38
-
39
- const nextJob = heap.peek();
40
- const delay = Math.max(0, nextJob.nextTrigger - getTimestamp()) * 1000;
41
-
42
- this.timer = setTimeout(() => this.runDueJobs(), delay);
43
- }
44
-
45
- async runDueJobs() {
46
- const now = getTimestamp();
47
- const { heap } = this;
48
-
49
- while (!heap.isEmpty() && heap.peek().nextTrigger <= now) {
50
- const job = heap.pop();
51
-
52
- if (config.debug) this.log('job has been triggered', job.key);
53
-
54
- try {
55
- await job.callback();
56
- } catch (err) {
57
- log.error(`Cron job "${job.key}" failed:`, err);
58
- }
59
-
60
- this.setNextTrigger(job);
61
- heap.push(job);
62
- }
63
-
64
- this.scheduleNextRun();
65
- }
66
-
67
- register(key, callback, interval, runOnInit=false) {
68
- const job = { callback, interval, key };
69
- this.jobs[key] = job;
70
- this.setNextTrigger(job);
71
- this.heap.push(job);
72
-
73
- if (config.debug) {
74
- this.log(`job has been registered with interval: ${interval}`, key);
75
- }
76
-
77
- if (runOnInit) {
78
- try {
79
- callback();
80
- } catch (err) {
81
- log.error(`Cron job "${key}" failed on init:`, err);
82
- }
83
- }
84
-
85
- this.scheduleNextRun();
86
- }
87
-
88
- unregister(key) {
89
- const { heap, jobs } = this;
90
- const job = jobs[key];
91
-
92
- if (!job) return;
93
-
94
- delete jobs[key];
95
- heap.remove(job);
96
-
97
- if (config.debug) this.log('job has been unregistered', key);
98
-
99
- this.scheduleNextRun();
100
- }
101
-
102
- setNextTrigger(job) {
103
- job.nextTrigger = getTimestamp() + parseInt(job.interval, 10);
104
- }
105
-
106
- log(text, key = null) {
107
- if (!config.cron?.log) return;
108
-
109
- const tag = key ? `Cron::${key}` : `Cron`;
110
- log.cron(`${tag} - ${text}:`);
111
- }
112
- }
package/src/min-heap.js DELETED
@@ -1,73 +0,0 @@
1
- export default class MinHeap {
2
- constructor() {
3
- this.items = [];
4
- }
5
-
6
- push(job) {
7
- this.items.push(job);
8
- this.bubbleUp();
9
- }
10
-
11
- pop() {
12
- if (this.items.length === 1) return this.items.pop();
13
- const top = this.items[0];
14
- this.items[0] = this.items.pop();
15
- this.bubbleDown();
16
- return top;
17
- }
18
-
19
- peek() {
20
- return this.items[0];
21
- }
22
-
23
- bubbleUp() {
24
- let idx = this.items.length - 1;
25
- while (idx > 0) {
26
- const parentIdx = Math.floor((idx - 1) / 2);
27
- if (this.items[idx].nextTrigger >= this.items[parentIdx].nextTrigger) break;
28
- [this.items[idx], this.items[parentIdx]] = [this.items[parentIdx], this.items[idx]];
29
- idx = parentIdx;
30
- }
31
- }
32
-
33
- bubbleDown() {
34
- let idx = 0;
35
- const length = this.items.length;
36
-
37
- while (true) {
38
- let leftIdx = 2 * idx + 1;
39
- let rightIdx = 2 * idx + 2;
40
- let swapIdx = null;
41
-
42
- if (leftIdx < length && this.items[leftIdx].nextTrigger < this.items[idx].nextTrigger) {
43
- swapIdx = leftIdx;
44
- }
45
- if (
46
- rightIdx < length &&
47
- this.items[rightIdx].nextTrigger < (
48
- swapIdx === null ? this.items[idx].nextTrigger : this.items[leftIdx].nextTrigger
49
- )
50
- ) {
51
- swapIdx = rightIdx;
52
- }
53
- if (swapIdx === null) break;
54
- [this.items[idx], this.items[swapIdx]] = [this.items[swapIdx], this.items[idx]];
55
- idx = swapIdx;
56
- }
57
- }
58
-
59
- remove(job) {
60
- const idx = this.items.indexOf(job);
61
- if (idx === -1) return;
62
- const end = this.items.pop();
63
- if (idx < this.items.length) {
64
- this.items[idx] = end;
65
- this.bubbleUp();
66
- this.bubbleDown();
67
- }
68
- }
69
-
70
- isEmpty() {
71
- return this.items.length === 0;
72
- }
73
- }