@beecode/msh-util 1.2.1 → 2.0.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/array-util.d.ts +23 -0
- package/dist/array-util.d.ts.map +1 -0
- package/dist/array-util.js +27 -0
- package/{src/class-factory-pattern.ts → dist/class-factory-pattern.d.ts} +5 -9
- package/dist/class-factory-pattern.d.ts.map +1 -0
- package/dist/class-factory-pattern.js +26 -0
- package/{src/express/error-handler.ts → dist/express/error-handler.d.ts} +2 -10
- package/dist/express/error-handler.d.ts.map +1 -0
- package/dist/express/error-handler.js +26 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/joi-util.d.ts +47 -0
- package/dist/joi-util.d.ts.map +1 -0
- package/dist/joi-util.js +62 -0
- package/{src/memoize-factory.ts → dist/memoize-factory.d.ts} +3 -14
- package/dist/memoize-factory.d.ts.map +1 -0
- package/dist/memoize-factory.js +24 -0
- package/dist/object-util.d.ts +72 -0
- package/dist/object-util.d.ts.map +1 -0
- package/dist/object-util.js +115 -0
- package/dist/regex-util.d.ts +12 -0
- package/dist/regex-util.d.ts.map +1 -0
- package/dist/regex-util.js +12 -0
- package/dist/single-threshold-promise.d.ts +31 -0
- package/dist/single-threshold-promise.d.ts.map +1 -0
- package/dist/single-threshold-promise.js +46 -0
- package/dist/singleton/async.d.ts +50 -0
- package/dist/singleton/async.d.ts.map +1 -0
- package/dist/singleton/async.js +75 -0
- package/{src/singleton/pattern.ts → dist/singleton/pattern.d.ts} +3 -13
- package/dist/singleton/pattern.d.ts.map +1 -0
- package/dist/singleton/pattern.js +41 -0
- package/dist/string-util.d.ts +10 -0
- package/dist/string-util.d.ts.map +1 -0
- package/dist/string-util.js +18 -0
- package/dist/time-util.d.ts +74 -0
- package/dist/time-util.d.ts.map +1 -0
- package/dist/time-util.js +90 -0
- package/dist/time-zone.d.ts +467 -0
- package/dist/time-zone.d.ts.map +1 -0
- package/dist/time-zone.js +468 -0
- package/{src/timeout.ts → dist/timeout.d.ts} +2 -3
- package/dist/timeout.d.ts.map +1 -0
- package/dist/timeout.js +17 -0
- package/dist/type-util.d.ts +50 -0
- package/dist/type-util.d.ts.map +1 -0
- package/dist/type-util.js +54 -0
- package/lib/array-util.d.ts.map +1 -1
- package/lib/array-util.js +30 -28
- package/lib/class-factory-pattern.d.ts.map +1 -1
- package/lib/class-factory-pattern.js +17 -8
- package/lib/express/error-handler.d.ts.map +1 -1
- package/lib/express/error-handler.js +15 -11
- package/lib/index.d.ts +16 -13
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +107 -29
- package/lib/joi-util.d.ts.map +1 -1
- package/lib/joi-util.js +66 -22
- package/lib/memoize-factory.d.ts +1 -1
- package/lib/memoize-factory.d.ts.map +1 -1
- package/lib/memoize-factory.js +19 -13
- package/lib/object-util.d.ts.map +1 -1
- package/lib/object-util.js +110 -55
- package/lib/package.json +1 -0
- package/lib/regex-util.js +15 -13
- package/lib/single-threshold-promise.d.ts +1 -1
- package/lib/single-threshold-promise.d.ts.map +1 -1
- package/lib/single-threshold-promise.js +74 -28
- package/lib/singleton/async.d.ts +1 -1
- package/lib/singleton/async.d.ts.map +1 -1
- package/lib/singleton/async.js +105 -45
- package/lib/singleton/pattern.d.ts +1 -1
- package/lib/singleton/pattern.d.ts.map +1 -1
- package/lib/singleton/pattern.js +13 -12
- package/lib/string-util.js +21 -19
- package/lib/time-util.js +69 -39
- package/lib/time-zone.d.ts +467 -0
- package/lib/time-zone.d.ts.map +1 -0
- package/lib/time-zone.js +473 -0
- package/lib/timeout.js +9 -6
- package/lib/type-util.js +57 -55
- package/lib/types/global.d.js +5 -0
- package/lib/types/types.d.js +3 -0
- package/package.json +188 -134
- package/lib/array-util.js.map +0 -1
- package/lib/class-factory-pattern.js.map +0 -1
- package/lib/express/error-handler.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/joi-util.js.map +0 -1
- package/lib/memoize-factory.js.map +0 -1
- package/lib/object-util.js.map +0 -1
- package/lib/regex-util.js.map +0 -1
- package/lib/single-threshold-promise.js.map +0 -1
- package/lib/singleton/async.js.map +0 -1
- package/lib/singleton/pattern.js.map +0 -1
- package/lib/string-util.js.map +0 -1
- package/lib/time-util.js.map +0 -1
- package/lib/timeout.js.map +0 -1
- package/lib/type-util.js.map +0 -1
- package/lib/types/any-function/index.d.ts +0 -2
- package/lib/types/any-function/index.d.ts.map +0 -1
- package/lib/types/any-function/index.js +0 -3
- package/lib/types/any-function/index.js.map +0 -1
- package/lib/types/any-function/no-params.d.ts +0 -2
- package/lib/types/any-function/no-params.d.ts.map +0 -1
- package/lib/types/any-function/no-params.js +0 -3
- package/lib/types/any-function/no-params.js.map +0 -1
- package/lib/types/any-function/promise-no-params.d.ts +0 -2
- package/lib/types/any-function/promise-no-params.d.ts.map +0 -1
- package/lib/types/any-function/promise-no-params.js +0 -3
- package/lib/types/any-function/promise-no-params.js.map +0 -1
- package/lib/types/any-function/promise.d.ts +0 -2
- package/lib/types/any-function/promise.d.ts.map +0 -1
- package/lib/types/any-function/promise.js +0 -3
- package/lib/types/any-function/promise.js.map +0 -1
- package/src/array-util.test.ts +0 -50
- package/src/array-util.ts +0 -26
- package/src/class-factory-pattern.test.ts +0 -39
- package/src/express/error-handler.test.ts +0 -44
- package/src/index.ts +0 -25
- package/src/joi-util.test.ts +0 -192
- package/src/joi-util.ts +0 -65
- package/src/memoize-factory.test.ts +0 -40
- package/src/object-util.test.ts +0 -360
- package/src/object-util.ts +0 -127
- package/src/regex-util.test.ts +0 -25
- package/src/regex-util.ts +0 -11
- package/src/single-threshold-promise.test.ts +0 -91
- package/src/single-threshold-promise.ts +0 -56
- package/src/singleton/async.test.ts +0 -122
- package/src/singleton/async.ts +0 -90
- package/src/singleton/pattern.test.ts +0 -16
- package/src/string-util.test.ts +0 -18
- package/src/string-util.ts +0 -18
- package/src/time-util.test.ts +0 -89
- package/src/time-util.ts +0 -98
- package/src/timeout.test.ts +0 -65
- package/src/type-util.test.ts +0 -20
- package/src/type-util.ts +0 -54
- package/src/types/any-function/index.ts +0 -1
- package/src/types/any-function/no-params.ts +0 -1
- package/src/types/any-function/promise-no-params.ts +0 -1
- package/src/types/any-function/promise.ts +0 -1
- package/src/types/types.d.ts +0 -2
package/lib/object-util.js
CHANGED
|
@@ -1,21 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
6
|
exports.ObjectUtil = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
|
|
8
|
+
var _util = _interopRequireDefault(require("util"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
12
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
13
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
14
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
15
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
16
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
17
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
18
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
19
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
20
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
21
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
var ObjectUtil = exports.ObjectUtil = /*#__PURE__*/function () {
|
|
24
|
+
function ObjectUtil() {
|
|
25
|
+
_classCallCheck(this, ObjectUtil);
|
|
26
|
+
}
|
|
27
|
+
return _createClass(ObjectUtil, [{
|
|
28
|
+
key: "deepClone",
|
|
29
|
+
value:
|
|
10
30
|
/**
|
|
11
31
|
* Deep clone object. Returned object will have no references to the object passed through params
|
|
12
32
|
* @template T
|
|
13
33
|
* @param {T} objectToClone
|
|
14
34
|
* @return {T}
|
|
15
35
|
*/
|
|
16
|
-
deepClone(objectToClone) {
|
|
17
|
-
|
|
36
|
+
function deepClone(objectToClone) {
|
|
37
|
+
return (0, _lodash["default"])(objectToClone);
|
|
18
38
|
}
|
|
39
|
+
|
|
19
40
|
/**
|
|
20
41
|
* Pick only properties from the property list. It is only allowed to pick properties from the first level
|
|
21
42
|
* @template T
|
|
@@ -24,14 +45,17 @@ class ObjectUtil {
|
|
|
24
45
|
* @param {L[]} keyList
|
|
25
46
|
* @return {Pick<T, L>}
|
|
26
47
|
*/
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
48
|
+
}, {
|
|
49
|
+
key: "pickByList",
|
|
50
|
+
value: function pickByList(obj, keyList) {
|
|
51
|
+
return keyList.reduce(function (pickedObj, key) {
|
|
52
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
53
|
+
pickedObj[key] = obj[key];
|
|
54
|
+
}
|
|
55
|
+
return pickedObj;
|
|
56
|
+
}, {}); // eslint-disable-line @typescript-eslint/prefer-reduce-type-parameter
|
|
34
57
|
}
|
|
58
|
+
|
|
35
59
|
/**
|
|
36
60
|
* Pick objects properties using keys from the second object.
|
|
37
61
|
* @template T
|
|
@@ -40,10 +64,13 @@ class ObjectUtil {
|
|
|
40
64
|
* @param {Partial<T>} objWithPickKeys
|
|
41
65
|
* @return {Pick<T, L>}
|
|
42
66
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
67
|
+
}, {
|
|
68
|
+
key: "pickByObjectKeys",
|
|
69
|
+
value: function pickByObjectKeys(obj, objWithPickKeys) {
|
|
70
|
+
var keys = Object.keys(objWithPickKeys);
|
|
71
|
+
return this.pickByList(obj, keys);
|
|
46
72
|
}
|
|
73
|
+
|
|
47
74
|
/**
|
|
48
75
|
* This function will do stringify deeper that JSON.stringify.
|
|
49
76
|
* @param {any} entity - entity thant needs to be stringify
|
|
@@ -62,34 +89,59 @@ class ObjectUtil {
|
|
|
62
89
|
* // }`
|
|
63
90
|
* console.log(new ObjectUtil().deepStringify({ b: 1, a: 2 }, {isSorted:true, compact: true})) // { a: 2, b: 1 }
|
|
64
91
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
92
|
+
}, {
|
|
93
|
+
key: "deepStringify",
|
|
94
|
+
value: function deepStringify(
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
+
entity, options) {
|
|
97
|
+
var _ref = options !== null && options !== void 0 ? options : {},
|
|
98
|
+
_ref$isSorted = _ref.isSorted,
|
|
99
|
+
isSorted = _ref$isSorted === void 0 ? false : _ref$isSorted,
|
|
100
|
+
_ref$isPrettyPrinted = _ref.isPrettyPrinted,
|
|
101
|
+
isPrettyPrinted = _ref$isPrettyPrinted === void 0 ? false : _ref$isPrettyPrinted,
|
|
102
|
+
_ref$prettyPrintCompa = _ref.prettyPrintCompactLevel,
|
|
103
|
+
prettyPrintCompactLevel = _ref$prettyPrintCompa === void 0 ? 0 : _ref$prettyPrintCompa;
|
|
104
|
+
var compact = this._deepStringifyCompact({
|
|
105
|
+
isPrettyPrinted: isPrettyPrinted,
|
|
106
|
+
prettyPrintCompactLevel: prettyPrintCompactLevel
|
|
107
|
+
});
|
|
108
|
+
return _util["default"].inspect(entity, {
|
|
109
|
+
breakLength: Infinity,
|
|
110
|
+
compact: compact,
|
|
111
|
+
depth: Infinity,
|
|
112
|
+
maxArrayLength: Infinity,
|
|
113
|
+
maxStringLength: Infinity,
|
|
114
|
+
sorted: isSorted
|
|
115
|
+
});
|
|
76
116
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
117
|
+
}, {
|
|
118
|
+
key: "_deepStringifyCompact",
|
|
119
|
+
value: function _deepStringifyCompact(params) {
|
|
120
|
+
var isPrettyPrinted = params.isPrettyPrinted,
|
|
121
|
+
prettyPrintCompactLevel = params.prettyPrintCompactLevel;
|
|
122
|
+
if (!isPrettyPrinted) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
return prettyPrintCompactLevel;
|
|
83
126
|
}
|
|
127
|
+
|
|
84
128
|
/**
|
|
85
129
|
* We are converting objects to string (or null or undefined) and comparing if the result is equal
|
|
86
130
|
* @param a
|
|
87
131
|
* @param b
|
|
88
132
|
* @return {boolean}
|
|
89
133
|
*/
|
|
90
|
-
|
|
91
|
-
|
|
134
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
135
|
+
}, {
|
|
136
|
+
key: "deepEqual",
|
|
137
|
+
value: function deepEqual(a, b) {
|
|
138
|
+
return this.deepStringify(a, {
|
|
139
|
+
isSorted: true
|
|
140
|
+
}) === this.deepStringify(b, {
|
|
141
|
+
isSorted: true
|
|
142
|
+
});
|
|
92
143
|
}
|
|
144
|
+
|
|
93
145
|
/**
|
|
94
146
|
* This function is going to convert any null to undefined in the object that is passed to it.
|
|
95
147
|
* @template T
|
|
@@ -98,21 +150,24 @@ class ObjectUtil {
|
|
|
98
150
|
* @example
|
|
99
151
|
* console.log(new ObjectUtil().deepNullToUndefined({ a: null, b: { c: null } })) // { a: undefined, b: { c: undefined } }
|
|
100
152
|
*/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
153
|
+
}, {
|
|
154
|
+
key: "deepNullToUndefined",
|
|
155
|
+
value: function deepNullToUndefined(objectWithNulls) {
|
|
156
|
+
var _this = this;
|
|
157
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
158
|
+
return Object.entries(objectWithNulls).reduce(function (acc, cur) {
|
|
159
|
+
var _cur = _slicedToArray(cur, 2),
|
|
160
|
+
key = _cur[0],
|
|
161
|
+
value = _cur[1];
|
|
162
|
+
if (value === null) {
|
|
163
|
+
acc[key] = undefined;
|
|
164
|
+
} else if (_typeof(value) === 'object' && !(value instanceof Date)) {
|
|
165
|
+
acc[key] = _this.deepNullToUndefined(value);
|
|
166
|
+
} else {
|
|
167
|
+
acc[key] = value;
|
|
168
|
+
}
|
|
169
|
+
return acc;
|
|
170
|
+
}, {});
|
|
115
171
|
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
//# sourceMappingURL=object-util.js.map
|
|
172
|
+
}]);
|
|
173
|
+
}();
|
package/lib/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
package/lib/regex-util.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.regexUtil = void 0;
|
|
4
|
-
exports.regexUtil = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
//# sourceMappingURL=regex-util.js.map
|
|
7
|
+
var regexUtil = exports.regexUtil = {
|
|
8
|
+
/**
|
|
9
|
+
* This is a UUID regex expression. This is usually used in express router to constrict the values passed as a path parameter (if you are using UUID as your identifier).
|
|
10
|
+
* @return {string}
|
|
11
|
+
* @example
|
|
12
|
+
* const { uuid } = regexUtil
|
|
13
|
+
* router.route(`/users/:userId(${uuid})`).get(getUsersById)
|
|
14
|
+
* //...
|
|
15
|
+
*/
|
|
16
|
+
uuid: "\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b"
|
|
17
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export type AnyFunctionPromiseNoParams<T> = () => Promise<T>;
|
|
2
2
|
/**
|
|
3
3
|
* SingleThresholdPromise returns a single promise, and subsequent calls made before the promise resolves will return the same promise.
|
|
4
4
|
* @example
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"single-threshold-promise.d.ts","sourceRoot":"","sources":["../src/single-threshold-promise.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"single-threshold-promise.d.ts","sourceRoot":"","sources":["../src/single-threshold-promise.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,0BAA0B,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAA;AAE5D;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,sBAAsB,CAAC,CAAC;IACpC,SAAS,CAAC,MAAM,EAAE;QAEjB,QAAQ,CAAC,EAAE;YAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;SAAE,EAAE,CAAA;KAC7F,CAAK;IAEN,SAAS,CAAC,UAAU,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAA;gBAEvC,SAAS,EAAE,0BAA0B,CAAC,CAAC,CAAC;IAIpD,SAAS,CAAC,eAAe,IAAI,IAAI;IAO3B,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;CAkB3B"}
|
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.SingleThresholdPromise = void 0;
|
|
7
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
8
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
9
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
11
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
14
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
4
17
|
/**
|
|
5
18
|
* SingleThresholdPromise returns a single promise, and subsequent calls made before the promise resolves will return the same promise.
|
|
6
19
|
* @example
|
|
@@ -18,32 +31,65 @@ exports.SingleThresholdPromise = void 0;
|
|
|
18
31
|
* }
|
|
19
32
|
* }
|
|
20
33
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if ('promises' in this._cache) {
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
this._cache.promises.push({ reject, resolve });
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
this._cache.promises = [];
|
|
39
|
-
const result = await this._factoryFn().catch((err) => {
|
|
40
|
-
this._rejectPromises();
|
|
41
|
-
throw err;
|
|
34
|
+
var SingleThresholdPromise = exports.SingleThresholdPromise = /*#__PURE__*/function () {
|
|
35
|
+
function SingleThresholdPromise(factoryFn) {
|
|
36
|
+
_classCallCheck(this, SingleThresholdPromise);
|
|
37
|
+
_defineProperty(this, "_cache", {});
|
|
38
|
+
this._factoryFn = factoryFn;
|
|
39
|
+
}
|
|
40
|
+
return _createClass(SingleThresholdPromise, [{
|
|
41
|
+
key: "_rejectPromises",
|
|
42
|
+
value: function _rejectPromises() {
|
|
43
|
+
if (this._cache.promises) {
|
|
44
|
+
this._cache.promises.forEach(function (promise) {
|
|
45
|
+
return promise.reject(new Error('Cache was cleaned'));
|
|
42
46
|
});
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return result;
|
|
47
|
+
}
|
|
48
|
+
delete this._cache.promises;
|
|
46
49
|
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
}, {
|
|
51
|
+
key: "promise",
|
|
52
|
+
value: function () {
|
|
53
|
+
var _promise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
54
|
+
var _this = this;
|
|
55
|
+
var result;
|
|
56
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
57
|
+
while (1) switch (_context.prev = _context.next) {
|
|
58
|
+
case 0:
|
|
59
|
+
if (!('promises' in this._cache)) {
|
|
60
|
+
_context.next = 2;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
return _context.abrupt("return", new Promise(function (resolve, reject) {
|
|
64
|
+
_this._cache.promises.push({
|
|
65
|
+
reject: reject,
|
|
66
|
+
resolve: resolve
|
|
67
|
+
});
|
|
68
|
+
}));
|
|
69
|
+
case 2:
|
|
70
|
+
this._cache.promises = [];
|
|
71
|
+
_context.next = 5;
|
|
72
|
+
return this._factoryFn()["catch"](function (err) {
|
|
73
|
+
_this._rejectPromises();
|
|
74
|
+
throw err;
|
|
75
|
+
});
|
|
76
|
+
case 5:
|
|
77
|
+
result = _context.sent;
|
|
78
|
+
this._cache.promises.forEach(function (promise) {
|
|
79
|
+
return promise.resolve(result);
|
|
80
|
+
});
|
|
81
|
+
delete this._cache.promises;
|
|
82
|
+
return _context.abrupt("return", result);
|
|
83
|
+
case 9:
|
|
84
|
+
case "end":
|
|
85
|
+
return _context.stop();
|
|
86
|
+
}
|
|
87
|
+
}, _callee, this);
|
|
88
|
+
}));
|
|
89
|
+
function promise() {
|
|
90
|
+
return _promise.apply(this, arguments);
|
|
91
|
+
}
|
|
92
|
+
return promise;
|
|
93
|
+
}()
|
|
94
|
+
}]);
|
|
95
|
+
}();
|
package/lib/singleton/async.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export type AnyFunctionPromiseNoParams<T> = () => Promise<T>;
|
|
2
2
|
/**
|
|
3
3
|
* This is a singleton wrapper that is used to wrap around async function. We have additional functionality to clear the cache
|
|
4
4
|
* and reject any subscriptions to initial promise. And we can also check if there is anything i cache
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/singleton/async.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/singleton/async.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,0BAA0B,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAA;AAE5D;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAc,CAAC,CAAC;IAC5B,SAAS,CAAC,MAAM,EAAE;QACjB,SAAS,CAAC,EAAE,CAAC,CAAA;QAEb,QAAQ,CAAC,EAAE;YAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;SAAE,EAAE,CAAA;KAC7F,CAAK;IAEN,SAAS,CAAC,QAAQ,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAA;gBAErC,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC;IAIlD;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB,SAAS,CAAC,eAAe,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,KAAK,CAAA;KAAE,GAAG,IAAI;IASzD;;;;OAIG;IACG,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;IAuB3B;;;;OAIG;IACH,MAAM,IAAI,CAAC,GAAG,SAAS;CAOvB"}
|
package/lib/singleton/async.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
3
6
|
exports.SingletonAsync = void 0;
|
|
7
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
8
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
9
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
11
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
14
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
4
17
|
/**
|
|
5
18
|
* This is a singleton wrapper that is used to wrap around async function. We have additional functionality to clear the cache
|
|
6
19
|
* and reject any subscriptions to initial promise. And we can also check if there is anything i cache
|
|
@@ -19,60 +32,107 @@ exports.SingletonAsync = void 0;
|
|
|
19
32
|
* // cache value after we call promise
|
|
20
33
|
* console.log(configSingleton().cache()) // { env: 'prod' }
|
|
21
34
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
var SingletonAsync = exports.SingletonAsync = /*#__PURE__*/function () {
|
|
36
|
+
function SingletonAsync(factory) {
|
|
37
|
+
_classCallCheck(this, SingletonAsync);
|
|
38
|
+
_defineProperty(this, "_cache", {});
|
|
39
|
+
this._factory = factory;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Empty cached value and reject any subscribed promise that is waiting for the initial promise to be resolved.
|
|
44
|
+
*/
|
|
45
|
+
return _createClass(SingletonAsync, [{
|
|
46
|
+
key: "cleanCache",
|
|
47
|
+
value: function cleanCache() {
|
|
48
|
+
delete this._cache.singleton;
|
|
49
|
+
this._rejectPromises({
|
|
50
|
+
error: new Error('Cache was cleaned')
|
|
51
|
+
});
|
|
26
52
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this._cache.promises.forEach((promise) => promise.reject(error));
|
|
38
|
-
}
|
|
39
|
-
delete this._cache.promises;
|
|
53
|
+
}, {
|
|
54
|
+
key: "_rejectPromises",
|
|
55
|
+
value: function _rejectPromises(params) {
|
|
56
|
+
var error = params.error;
|
|
57
|
+
if (this._cache.promises) {
|
|
58
|
+
this._cache.promises.forEach(function (promise) {
|
|
59
|
+
return promise.reject(error);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
delete this._cache.promises;
|
|
40
63
|
}
|
|
64
|
+
|
|
41
65
|
/**
|
|
42
66
|
* Return singleton value in a promise. If there is no cached value then try to get it from factory.
|
|
43
67
|
* @template T
|
|
44
68
|
* @returns {Promise<T>}
|
|
45
69
|
*/
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
}, {
|
|
71
|
+
key: "promise",
|
|
72
|
+
value: (function () {
|
|
73
|
+
var _promise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
74
|
+
var _this = this;
|
|
75
|
+
var result;
|
|
76
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
77
|
+
while (1) switch (_context.prev = _context.next) {
|
|
78
|
+
case 0:
|
|
79
|
+
if (!('singleton' in this._cache)) {
|
|
80
|
+
_context.next = 2;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
return _context.abrupt("return", this._cache.singleton);
|
|
84
|
+
case 2:
|
|
85
|
+
if (!('promises' in this._cache)) {
|
|
86
|
+
_context.next = 4;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
return _context.abrupt("return", new Promise(function (resolve, reject) {
|
|
90
|
+
_this._cache.promises.push({
|
|
91
|
+
reject: reject,
|
|
92
|
+
resolve: resolve
|
|
93
|
+
});
|
|
94
|
+
}));
|
|
95
|
+
case 4:
|
|
96
|
+
this._cache.promises = [];
|
|
97
|
+
_context.next = 7;
|
|
98
|
+
return this._factory()["catch"](function (error) {
|
|
99
|
+
_this._rejectPromises({
|
|
100
|
+
error: error
|
|
101
|
+
});
|
|
102
|
+
throw error;
|
|
103
|
+
});
|
|
104
|
+
case 7:
|
|
105
|
+
result = _context.sent;
|
|
106
|
+
this._cache.singleton = result;
|
|
107
|
+
this._cache.promises.forEach(function (promise) {
|
|
108
|
+
return promise.resolve(result);
|
|
109
|
+
});
|
|
110
|
+
delete this._cache.promises;
|
|
111
|
+
return _context.abrupt("return", result);
|
|
112
|
+
case 12:
|
|
113
|
+
case "end":
|
|
114
|
+
return _context.stop();
|
|
115
|
+
}
|
|
116
|
+
}, _callee, this);
|
|
117
|
+
}));
|
|
118
|
+
function promise() {
|
|
119
|
+
return _promise.apply(this, arguments);
|
|
120
|
+
}
|
|
121
|
+
return promise;
|
|
122
|
+
}()
|
|
65
123
|
/**
|
|
66
124
|
* Return cached value, if there is no value cached return undefined.
|
|
67
125
|
* @template T
|
|
68
126
|
* @returns {T | undefined}
|
|
69
127
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
128
|
+
)
|
|
129
|
+
}, {
|
|
130
|
+
key: "cached",
|
|
131
|
+
value: function cached() {
|
|
132
|
+
if ('singleton' in this._cache) {
|
|
133
|
+
return this._cache.singleton;
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
75
136
|
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
//# sourceMappingURL=async.js.map
|
|
137
|
+
}]);
|
|
138
|
+
}();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export type AnyFunctionNoParams<T> = () => T;
|
|
2
2
|
/**
|
|
3
3
|
* Singleton patter wrapper function
|
|
4
4
|
* @param {AnyFunctionNoParams<R>} factoryFn Factory function that is used to generate value that is going to be cached and return by
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pattern.d.ts","sourceRoot":"","sources":["../../src/singleton/pattern.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"pattern.d.ts","sourceRoot":"","sources":["../../src/singleton/pattern.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,MAAM,CAAC,CAAA;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,gBAAgB,iBAAkB,oBAAoB,CAAC,CAAC,KAAG,oBAAoB,CAAC,CAU5F,CAAA"}
|