molstar
Version:
A comprehensive macromolecular library.
50 lines • 2.17 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __awaiter, __generator } from "tslib";
import { now } from '../../mol-util/now';
function chunkedSubtask(ctx, initialChunk, state, f, update) {
return __awaiter(this, void 0, void 0, function () {
var chunkSize, globalProcessed, globalTime, start, lastSize, currentTime, delta;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
chunkSize = Math.max(initialChunk, 0);
globalProcessed = 0, globalTime = 0;
if (ctx.isSynchronous) {
f(Number.MAX_SAFE_INTEGER, state);
return [2 /*return*/, state];
}
start = now();
lastSize = 0, currentTime = 0;
_a.label = 1;
case 1:
if (!((lastSize = f(chunkSize, state)) > 0)) return [3 /*break*/, 4];
globalProcessed += lastSize;
delta = now() - start;
currentTime += delta;
globalTime += delta;
if (!ctx.shouldUpdate) return [3 /*break*/, 3];
return [4 /*yield*/, update(ctx, state, globalProcessed)];
case 2:
_a.sent();
chunkSize = Math.round(currentTime * globalProcessed / globalTime) + 1;
start = now();
currentTime = 0;
_a.label = 3;
case 3: return [3 /*break*/, 1];
case 4:
if (!ctx.shouldUpdate) return [3 /*break*/, 6];
return [4 /*yield*/, update(ctx, state, globalProcessed)];
case 5:
_a.sent();
_a.label = 6;
case 6: return [2 /*return*/, state];
}
});
});
}
export { chunkedSubtask };
//# sourceMappingURL=chunked.js.map