-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.js
More file actions
220 lines (215 loc) · 7.7 KB
/
Sequence.js
File metadata and controls
220 lines (215 loc) · 7.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright 2021-2025 Nernar (github.com/nernar)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
LIBRARY({
name: "Sequence",
version: 1,
api: "AdaptedScript",
dependencies: ["Retention:1"],
shared: true
});
IMPORT("Retention:1");
var Sequence = /** @class */ (function () {
/**
* Processes thread besides [[sync()]] interface
* context, that was used to indicate process.
* Calls [[process()]] for every element requested
* by [[next()]] besides [[execute()]] passed value.
* @param obj merges with prototype
*/
function Sequence(obj) {
if (obj !== undefined) {
for (var element in obj) {
this[element] = obj[element];
}
}
this.id = "sequence" + (Sequence.uid++);
}
Sequence.prototype.getThread = function () {
return this.thread !== undefined ? this.thread : null;
};
Sequence.prototype.getSynchronizeTime = function () {
return this.between !== undefined ? this.between : 5;
};
Sequence.prototype.setSynchronizeTime = function (ms) {
this.between = ms - 0;
};
Sequence.prototype.getPriority = function () {
return this.priority !== undefined ? this.priority : 8;
};
Sequence.prototype.setPriority = function (priority) {
var _a;
this.priority = priority - 0;
(_a = this.getThread()) === null || _a === void 0 ? void 0 : _a.setPriority(this.getPriority());
};
Sequence.prototype.setFixedCount = function (count) {
this.count = count - 0;
};
Sequence.prototype.getFixedCount = function () {
return this.count !== undefined ? this.count : 1;
};
Sequence.prototype.setReportingEnabled = function (enabled) {
this.reporting = !!enabled;
};
Sequence.prototype.isReportingEnabled = function () {
return this.reporting !== undefined ? this.reporting : true;
};
/**
* Sync recursive action, that awaits when
* process is completed, interrupted or cancelled.
* @param active process startup milliseconds
*/
Sequence.prototype.sync = function (active) {
var _this = this;
handle(function () {
if (_this.completed == null) {
if (_this.updated) {
_this.update && _this.update(_this.count === undefined ?
_this.index : _this.index / _this.count * 100, _this.index);
delete _this.updated;
}
_this.tick && _this.tick(_this.index, Date.now() - active, active);
_this.sync && _this.sync(active);
return;
}
if (_this.completed) {
_this.complete && _this.complete(Date.now() - active, active);
}
delete _this.completed;
delete _this.index;
}, this.getSynchronizeTime());
};
/**
* Action that launches main process and sync.
* @param value data to process
*/
Sequence.prototype.execute = function (value) {
var _this = this;
if (this.thread != null) {
MCSystem.throwException("Sequence: Sequence (id=" + this.id + ") is already running");
}
this.index = 0;
this.thread = {};
handle(function () {
var active = Date.now(), next;
_this.create && _this.create.call(_this, value, active);
_this.thread = handleThread(function () {
try {
if (_this.uncount != null) {
_this.count = _this.uncount(value);
_this.require();
}
while ((next = _this.next(value, _this.index)) !== undefined) {
if (_this.isInterrupted())
java.lang.Thread.sleep(1);
_this.index = _this.process(next, value, _this.index);
_this.require();
}
_this.completed = true;
}
catch (e) {
_this.completed = false;
handle(function () { return _this.cancel && _this.cancel(e); });
}
delete _this.thread;
if (_this.uncount != null) {
delete _this.count;
}
delete _this.updated;
}, _this.getPriority());
_this.sync && _this.sync(active);
});
};
/**
* Must be called inside [[process()]] or [[next()]]
* if you want to force update process indexes.
* Recommended to use if [[uncount()]] wouldn't
* help in dynamical reupdate or just update progress.
* @param index currently progress
* @param count maximum value
*/
Sequence.prototype.require = function (index, count) {
if (index !== undefined) {
this.index = index;
}
if (count !== undefined) {
this.count = count;
}
this.updated = true;
};
Sequence.prototype.shrink = function (addition) {
if (addition !== undefined) {
if (this.count === undefined) {
this.count = 0;
}
this.count += addition;
}
this.updated = true;
};
/**
* Calls for every item inside [[process]], passed
* value will be used into it. That action created
* to communicate executing object with process,
* split it to processable parts.
* @param value passed on execute
* @param index was returned by [[process()]]
* @returns value or element to [[process()]]
* @async Wouldn't access interface thread.
*/
Sequence.prototype.next = function (value, index) {
if (index >= this.getFixedCount()) {
return;
}
return ++index;
};
/**
* Main sequence process in thread;
* handles object and returns index.
* @param element next result to handle
* @param value elements resolver
* @param index indicates progress
* @returns index to sync
* @throws must be overwritten in usage
* @async Wouldn't access interface thread.
*/
Sequence.prototype.process = function (element, value, index) {
MCSystem.throwException("Sequence: Sequence.process must be implemented");
};
Sequence.prototype.cancel = function (error) {
if (error && error.message != "java.lang.InterruptedException: null") {
if (this.isReportingEnabled())
reportError(error);
}
};
Sequence.prototype.interrupt = function () {
var _a;
if (!this.isInterrupted()) {
(_a = this.getThread()) === null || _a === void 0 ? void 0 : _a.interrupt();
}
};
Sequence.prototype.isInterrupted = function () {
var _a;
return (_a = this.getThread()) === null || _a === void 0 ? void 0 : _a.isInterrupted();
};
Sequence.prototype.assureYield = function (thread) {
if (this.getThread() == null) {
return false;
}
while (this.getThread() != null) {
java.lang.Thread.yield();
}
return true;
};
Sequence.uid = 0;
return Sequence;
}());
EXPORT("Sequence", Sequence);