@ehmpathy/uni-time 1.4.2 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/wrappers/waitFor.d.ts +23 -0
- package/dist/wrappers/waitFor.js +63 -0
- package/dist/wrappers/waitFor.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -22,4 +22,5 @@ __exportStar(require("./checks/isUniDateTime"), exports);
|
|
|
22
22
|
__exportStar(require("./casts/toMillisecondsSinceEpoch"), exports);
|
|
23
23
|
__exportStar(require("./manipulate/addDuration"), exports);
|
|
24
24
|
__exportStar(require("./manipulate/subDuration"), exports);
|
|
25
|
+
__exportStar(require("./wrappers/waitFor"), exports);
|
|
25
26
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC;AACrC,uDAAqC;AACrC,gDAA8B;AAC9B,qDAAmC;AACnC,yDAAuC;AACvC,mEAAiD;AACjD,2DAAyC;AACzC,2DAAyC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC;AACrC,uDAAqC;AACrC,gDAA8B;AAC9B,qDAAmC;AACnC,yDAAuC;AACvC,mEAAiD;AACjD,2DAAyC;AACzC,2DAAyC;AACzC,qDAAmC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HelpfulError } from '@ehmpathy/error-fns';
|
|
2
|
+
import { NotUndefined } from 'type-fns';
|
|
3
|
+
import { UniDuration } from '../domain/UniDuration';
|
|
4
|
+
export declare class WaitForTimedOutError extends HelpfulError {
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* tactic
|
|
8
|
+
* .what: wait for an output
|
|
9
|
+
* .why:
|
|
10
|
+
* - enables waiting until some desired result is ready
|
|
11
|
+
* - ensures best practices and a pit of success contract
|
|
12
|
+
* - uses readable names for maintainability
|
|
13
|
+
*/
|
|
14
|
+
export declare const waitFor: <O>(extractor: () => Promise<O>, options?: {
|
|
15
|
+
/**
|
|
16
|
+
* the interval at which to check for completion
|
|
17
|
+
*/
|
|
18
|
+
interval?: UniDuration;
|
|
19
|
+
/**
|
|
20
|
+
* the maximum duration to wait for, until timeout
|
|
21
|
+
*/
|
|
22
|
+
timeout?: UniDuration;
|
|
23
|
+
}) => Promise<NotUndefined<O>>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.waitFor = exports.WaitForTimedOutError = void 0;
|
|
13
|
+
const error_fns_1 = require("@ehmpathy/error-fns");
|
|
14
|
+
const type_fns_1 = require("type-fns");
|
|
15
|
+
const UniDuration_1 = require("../domain/UniDuration");
|
|
16
|
+
const sleep_1 = require("../utils/sleep");
|
|
17
|
+
const DEFAULT_TIMEOUT = { seconds: 60 };
|
|
18
|
+
const DEFAULT_INTERVAL = { seconds: 10 };
|
|
19
|
+
class WaitForTimedOutError extends error_fns_1.HelpfulError {
|
|
20
|
+
}
|
|
21
|
+
exports.WaitForTimedOutError = WaitForTimedOutError;
|
|
22
|
+
/**
|
|
23
|
+
* tactic
|
|
24
|
+
* .what: wait for an output
|
|
25
|
+
* .why:
|
|
26
|
+
* - enables waiting until some desired result is ready
|
|
27
|
+
* - ensures best practices and a pit of success contract
|
|
28
|
+
* - uses readable names for maintainability
|
|
29
|
+
*/
|
|
30
|
+
const waitFor = (
|
|
31
|
+
/**
|
|
32
|
+
* the procedure which extracts some output that we are waiting for
|
|
33
|
+
*
|
|
34
|
+
* note
|
|
35
|
+
* - waitFor waits until the extractor returns a value other than `void` or `undefined`
|
|
36
|
+
*/
|
|
37
|
+
extractor,
|
|
38
|
+
/**
|
|
39
|
+
* options to control the wait
|
|
40
|
+
*/
|
|
41
|
+
options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
var _a, _b;
|
|
43
|
+
// define the timeout and interval
|
|
44
|
+
const timeout = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
|
|
45
|
+
const interval = (_b = options === null || options === void 0 ? void 0 : options.interval) !== null && _b !== void 0 ? _b : DEFAULT_INTERVAL;
|
|
46
|
+
// track the timestamps of interest
|
|
47
|
+
const startAtMSE = new Date().getTime();
|
|
48
|
+
const timeoutAtMSE = startAtMSE + (0, UniDuration_1.toMilliseconds)(timeout);
|
|
49
|
+
// check each interval
|
|
50
|
+
while (new Date().getTime() < timeoutAtMSE) {
|
|
51
|
+
const output = yield extractor();
|
|
52
|
+
if ((0, type_fns_1.isNotUndefined)(output))
|
|
53
|
+
return output;
|
|
54
|
+
yield (0, sleep_1.sleep)(interval);
|
|
55
|
+
}
|
|
56
|
+
// throw an error if reached here, since it implies a timeout
|
|
57
|
+
throw new WaitForTimedOutError('no output returned from waitFor.extractor', {
|
|
58
|
+
interval,
|
|
59
|
+
timeout,
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
exports.waitFor = waitFor;
|
|
63
|
+
//# sourceMappingURL=waitFor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waitFor.js","sourceRoot":"","sources":["../../src/wrappers/waitFor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAAmD;AACnD,uCAAwD;AAExD,uDAAoE;AACpE,0CAAuC;AAEvC,MAAM,eAAe,GAAgB,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AACrD,MAAM,gBAAgB,GAAgB,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AAEtD,MAAa,oBAAqB,SAAQ,wBAAY;CAAG;AAAzD,oDAAyD;AAEzD;;;;;;;GAOG;AACI,MAAM,OAAO,GAAG;AACrB;;;;;GAKG;AACH,SAA2B;AAE3B;;GAEG;AACH,OAUC,EACyB,EAAE;;IAC5B,kCAAkC;IAClC,MAAM,OAAO,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,eAAe,CAAC;IACpD,MAAM,QAAQ,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAI,gBAAgB,CAAC;IAEvD,mCAAmC;IACnC,MAAM,UAAU,GAAW,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;IAChD,MAAM,YAAY,GAAW,UAAU,GAAG,IAAA,4BAAc,EAAC,OAAO,CAAC,CAAC;IAElE,sBAAsB;IACtB,OAAO,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,YAAY,EAAE,CAAC;QAC3C,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAC;QACjC,IAAI,IAAA,yBAAc,EAAC,MAAM,CAAC;YAAE,OAAO,MAAM,CAAC;QAC1C,MAAM,IAAA,aAAK,EAAC,QAAQ,CAAC,CAAC;IACxB,CAAC;IAED,6DAA6D;IAC7D,MAAM,IAAI,oBAAoB,CAAC,2CAA2C,EAAE;QAC1E,QAAQ;QACR,OAAO;KACR,CAAC,CAAC;AACL,CAAC,CAAA,CAAC;AA5CW,QAAA,OAAO,WA4ClB"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@ehmpathy/uni-time",
|
|
3
3
|
"author": "ehmpathy",
|
|
4
4
|
"description": "a glossary of universally intuitive time, date, and duration domain literals",
|
|
5
|
-
"version": "1.
|
|
5
|
+
"version": "1.5.0",
|
|
6
6
|
"repository": "ehmpathy/uni-time",
|
|
7
7
|
"homepage": "https://github.com/ehmpathy/uni-time",
|
|
8
8
|
"keywords": [
|