1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createSchedule = void 0;
|
7 |
|
8 | var _momentTimezone = _interopRequireDefault(require("moment-timezone"));
|
9 |
|
10 | var _next = _interopRequireDefault(require("./next"));
|
11 |
|
12 | var _parse = _interopRequireDefault(require("./parse"));
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 | const MAX_DELAY = Math.pow(2, 31) - 1;
|
17 |
|
18 | class Job {
|
19 | constructor(schedule, fn) {
|
20 | let scheduledDate;
|
21 |
|
22 | const wrapper = () => {
|
23 | const now = Date.now();
|
24 |
|
25 | if (scheduledDate > now) {
|
26 | this._timeout = setTimeout(wrapper, scheduledDate - now);
|
27 | return;
|
28 | }
|
29 |
|
30 | this._isRunning = true;
|
31 | let result;
|
32 |
|
33 | try {
|
34 | result = fn();
|
35 | } catch (_) {}
|
36 |
|
37 | let then;
|
38 |
|
39 | if (result != null && typeof (then = result.then) === 'function') {
|
40 | then.call(result, scheduleNext, scheduleNext);
|
41 | } else {
|
42 | scheduleNext();
|
43 | }
|
44 | };
|
45 |
|
46 | const scheduleNext = () => {
|
47 | this._isRunning = false;
|
48 |
|
49 | if (this._isEnabled) {
|
50 | const now = schedule._createDate();
|
51 |
|
52 | scheduledDate = +(0, _next.default)(schedule._schedule, now);
|
53 | const delay = scheduledDate - now;
|
54 | this._timeout = delay < MAX_DELAY ? setTimeout(wrapper, delay) : setTimeout(scheduleNext, MAX_DELAY);
|
55 | }
|
56 | };
|
57 |
|
58 | this._isEnabled = false;
|
59 | this._isRunning = false;
|
60 | this._scheduleNext = scheduleNext;
|
61 | this._timeout = undefined;
|
62 | }
|
63 |
|
64 | start() {
|
65 | this.stop();
|
66 | this._isEnabled = true;
|
67 |
|
68 | if (!this._isRunning) {
|
69 | this._scheduleNext();
|
70 | }
|
71 | }
|
72 |
|
73 | stop() {
|
74 | this._isEnabled = false;
|
75 | clearTimeout(this._timeout);
|
76 | }
|
77 |
|
78 | }
|
79 |
|
80 | class Schedule {
|
81 | constructor(pattern, zone = 'utc') {
|
82 | this._schedule = (0, _parse.default)(pattern);
|
83 | this._createDate = zone.toLowerCase() === 'utc' ? _momentTimezone.default.utc : zone === 'local' ? _momentTimezone.default : () => _momentTimezone.default.tz(zone);
|
84 | }
|
85 |
|
86 | createJob(fn) {
|
87 | return new Job(this, fn);
|
88 | }
|
89 |
|
90 | next(n) {
|
91 | const dates = new Array(n);
|
92 | const schedule = this._schedule;
|
93 |
|
94 | let date = this._createDate();
|
95 |
|
96 | for (let i = 0; i < n; ++i) {
|
97 | dates[i] = (date = (0, _next.default)(schedule, date)).toDate();
|
98 | }
|
99 |
|
100 | return dates;
|
101 | }
|
102 |
|
103 | startJob(fn) {
|
104 | const job = this.createJob(fn);
|
105 | job.start();
|
106 | return job.stop.bind(job);
|
107 | }
|
108 |
|
109 | }
|
110 |
|
111 | const createSchedule = (...args) => new Schedule(...args);
|
112 |
|
113 | exports.createSchedule = createSchedule;
|
114 |
|
\ | No newline at end of file |