-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
160 lines (121 loc) · 3.67 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.OLSKTasks = global.OLSKTasks || {})));
}(this, (function(exports) {
'use strict';
const mod = {
OLSKTasksInputDataIsDateObject (inputData) {
if (!(inputData instanceof Date)) {
return false;
}
if (Number.isNaN(inputData.getTime())) {
return false;
}
return true;
},
OLSKTasksInputDataIsTaskObject (inputData) {
if (typeof inputData !== 'object' || inputData === null) {
return false;
}
if (typeof inputData.OLSKTaskFireTimeInterval !== 'number') {
return false;
}
if (typeof inputData.OLSKTaskShouldBePerformed !== 'function') {
return false;
}
if (typeof inputData.OLSKTaskCallback !== 'function') {
return false;
}
if (inputData.OLSKTaskFiresImmediately !== undefined) {
if (typeof inputData.OLSKTaskFiresImmediately !== 'boolean') {
return false;
}
}
if (inputData.OLSKTaskFireDates !== undefined) {
if (!Array.isArray(inputData.OLSKTaskFireDates)) {
return false;
}
if (!inputData.OLSKTaskFireDates.filter(mod.OLSKTasksInputDataIsDateObject).length) {
return false;
}
}
if (inputData.OLSKTaskFireLimit !== undefined) {
if (typeof inputData.OLSKTaskFireLimit !== 'number') {
return false;
}
if (inputData.OLSKTaskFireLimit < 0) {
return false;
}
}
if (inputData._OLSKTaskFireCount !== undefined) {
if (typeof inputData._OLSKTaskFireCount !== 'number') {
return false;
}
if (inputData._OLSKTaskFireCount < 0) {
return false;
}
}
if (inputData.OLSKTaskStartedAt !== undefined) {
if (!mod.OLSKTasksInputDataIsDateObject(inputData.OLSKTaskStartedAt)) {
return false;
}
}
if (inputData.OLSKTaskStoppedAt !== undefined) {
if (!mod.OLSKTasksInputDataIsDateObject(inputData.OLSKTaskStoppedAt)) {
return false;
}
}
if (inputData.OLSKTaskName !== undefined) {
if (typeof inputData.OLSKTaskName !== 'string') {
return false;
}
}
return true;
},
OLSKTasksTimeoutForTaskObject (inputData, callbackInput) {
if (!mod.OLSKTasksInputDataIsTaskObject(inputData)) {
throw new Error('OLSKErrorInputNotValid');
}
inputData._OLSKTaskFireCount = 0;
inputData.OLSKTaskStartedAt = new Date();
var taskParentCallback = function() {
if (!inputData.OLSKTaskShouldBePerformed()) {
return;
}
inputData._OLSKTaskFireCount++;
inputData.OLSKTaskCallback(callbackInput);
};
if (inputData.OLSKTaskFiresImmediately === true) {
taskParentCallback();
}
if (inputData.OLSKTaskFireDates) {
inputData._OLSKTaskFireDates = inputData.OLSKTaskFireDates.slice(0);
var timeOutCallback = function() {
if (!inputData._OLSKTaskFireDates.length) {
inputData.OLSKTaskStoppedAt = new Date();
return;
}
inputData._OLSKTaskTimerID = setTimeout(function() {
taskParentCallback();
timeOutCallback();
}, inputData._OLSKTaskFireDates.shift() - new Date());
return inputData._OLSKTaskTimerID;
};
return timeOutCallback();
}
inputData._OLSKTaskTimerID = setInterval(function() {
if (inputData._OLSKTaskFireCount >= inputData.OLSKTaskFireLimit) {
inputData.OLSKTaskStoppedAt = new Date();
return clearInterval(inputData._OLSKTaskTimerID);
}
taskParentCallback();
}, inputData.OLSKTaskFireTimeInterval * 1000);
return inputData._OLSKTaskTimerID;
},
};
Object.assign(exports, mod);
Object.defineProperty(exports, '__esModule', {
value: true
});
})));