UNPKG

2.02 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.getRelativeTimeout = exports.getDeadlineTimeoutString = exports.minDeadline = void 0;
20function minDeadline(...deadlineList) {
21 let minValue = Infinity;
22 for (const deadline of deadlineList) {
23 const deadlineMsecs = deadline instanceof Date ? deadline.getTime() : deadline;
24 if (deadlineMsecs < minValue) {
25 minValue = deadlineMsecs;
26 }
27 }
28 return minValue;
29}
30exports.minDeadline = minDeadline;
31const units = [
32 ['m', 1],
33 ['S', 1000],
34 ['M', 60 * 1000],
35 ['H', 60 * 60 * 1000],
36];
37function getDeadlineTimeoutString(deadline) {
38 const now = new Date().getTime();
39 if (deadline instanceof Date) {
40 deadline = deadline.getTime();
41 }
42 const timeoutMs = Math.max(deadline - now, 0);
43 for (const [unit, factor] of units) {
44 const amount = timeoutMs / factor;
45 if (amount < 1e8) {
46 return String(Math.ceil(amount)) + unit;
47 }
48 }
49 throw new Error('Deadline is too far in the future');
50}
51exports.getDeadlineTimeoutString = getDeadlineTimeoutString;
52function getRelativeTimeout(deadline) {
53 const deadlineMs = deadline instanceof Date ? deadline.getTime() : deadline;
54 const now = new Date().getTime();
55 return deadlineMs - now;
56}
57exports.getRelativeTimeout = getRelativeTimeout;
58//# sourceMappingURL=deadline.js.map
\No newline at end of file